From 59918f7f13ce925c8d24ae731111224cca468fb9 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Wed, 6 Nov 2019 11:19:10 +0100
Subject: [PATCH] FEAT #11698 TIME 0:45 Get models by user

---
 src/app/user/controllers/UserController.php | 40 ++++++++++++++++++---
 1 file changed, 36 insertions(+), 4 deletions(-)

diff --git a/src/app/user/controllers/UserController.php b/src/app/user/controllers/UserController.php
index 5d34249c1c7..8f4d7b1f334 100755
--- a/src/app/user/controllers/UserController.php
+++ b/src/app/user/controllers/UserController.php
@@ -1332,24 +1332,56 @@ class UserController
 
     public function getTemplates(Request $request, Response $response)
     {
+        $queryParams = $request->getQueryParams();
+
         $entities = UserModel::getEntitiesByLogin(['login' => $GLOBALS['userId']]);
         $entities = array_column($entities, 'entity_id');
         if (empty($entities)) {
             $entities = [0];
         }
 
+        $where = ['(templates_association.value_field in (?) OR templates_association.template_id IS NULL)'];
+        $data = [$entities];
+        if (!empty($queryParams['type'])) {
+            $where[] = 'templates.template_type = ?';
+            $data[] = strtoupper($queryParams['type']);
+        }
+        if (!empty($queryParams['target'])) {
+            $where[] = 'templates.template_target = ?';
+            $data[] = $queryParams['target'];
+        }
         $templates = TemplateModel::getWithAssociation([
-            'select'    => ['DISTINCT(templates.template_id)', 'templates.template_label', 'templates.template_file_name'],
-            'where'     => ['templates.template_type = ?', '(templates_association.value_field in (?) OR templates_association.template_id IS NULL)'],
-            'data'      => ['OFFICE', $entities],
+            'select'    => ['DISTINCT(templates.template_id)', 'templates.template_label', 'templates.template_file_name', 'templates.template_path', 'templates.template_target', 'templates.template_attachment_type'],
+            'where'     => $where,
+            'data'      => $data,
             'orderBy'   => ['templates.template_label']
         ]);
 
+        $docserver = DocserverModel::getCurrentDocserver(['typeId' => 'TEMPLATES', 'collId' => 'templates', 'select' => ['path_template']]);
         foreach ($templates as $key => $template) {
             $explodeFile = explode('.', $template['template_file_name']);
             $ext = $explodeFile[count($explodeFile) - 1];
+            $exists = is_file($docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $template['template_path']) . $template['template_file_name']);
+
+            $templates[$key] = [
+                'id'                => $template['template_id'],
+                'label'             => $template['template_label'],
+                'extension'         => $ext,
+                'exists'            => $exists,
+                'target'            => $template['template_target'],
+                'attachmentType'    => $template['template_attachment_type']
+            ];
+        }
 
-            $templates[$key] = ['id' => $template['template_id'], 'label' => $template['template_label'], 'extension' => $ext];
+        if (!empty($queryParams['target']) && $queryParams['target'] == 'attachments') {
+            $templatesTmp = [];
+            foreach ($templates as $key => $template) {
+                if (empty($templatesTmp[$template['attachmentType']])) {
+                    $templatesTmp[$template['attachmentType']] = [];
+                }
+                $templatesTmp[$template['attachmentType']][] = $template;
+            }
+            $templates = $templatesTmp;
         }
 
         return $response->withJson(['templates' => $templates]);
-- 
GitLab