From 02e201227eb3eecdd413cf5c28fdee2b0df2177e Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Tue, 27 Feb 2018 15:36:40 +0100
Subject: [PATCH] [REFACTORING] ListTemplate + basket

---
 .../basket/controllers/BasketController.php   | 164 +++++++++---------
 .../controllers/ListTemplateController.php    |  88 +++++-----
 2 files changed, 125 insertions(+), 127 deletions(-)

diff --git a/src/app/basket/controllers/BasketController.php b/src/app/basket/controllers/BasketController.php
index 94ee3932fc5..5393f3a0596 100644
--- a/src/app/basket/controllers/BasketController.php
+++ b/src/app/basket/controllers/BasketController.php
@@ -4,11 +4,11 @@
  * Copyright Maarch since 2008 under licence GPLv3.
  * See LICENCE.txt file at the root folder for more details.
  * This file is part of Maarch software.
+ *
  */
 
 /**
  * @brief Basket Controller
- *
  * @author dev@maarch.org
  */
 
@@ -81,11 +81,11 @@ class BasketController
         BasketModel::create($data);
         HistoryController::add([
             'tableName' => 'baskets',
-            'recordId' => $data['id'],
+            'recordId'  => $data['id'],
             'eventType' => 'ADD',
-            'info' => _BASKET_CREATION." : {$data['id']}",
-            'moduleId' => 'basket',
-            'eventId' => 'basketCreation',
+            'info'      => _BASKET_CREATION . " : {$data['id']}",
+            'moduleId'  => 'basket',
+            'eventId'   => 'basketCreation',
         ]);
 
         return $response->withJson(['basket' => $data['id']]);
@@ -123,11 +123,11 @@ class BasketController
         BasketModel::update($data);
         HistoryController::add([
             'tableName' => 'baskets',
-            'recordId' => $aArgs['id'],
+            'recordId'  => $aArgs['id'],
             'eventType' => 'UP',
-            'info' => _BASKET_MODIFICATION." : {$aArgs['id']}",
-            'moduleId' => 'basket',
-            'eventId' => 'basketModification',
+            'info'      => _BASKET_MODIFICATION . " : {$aArgs['id']}",
+            'moduleId'  => 'basket',
+            'eventId'   => 'basketModification',
         ]);
 
         return $response->withJson(['success' => 'success']);
@@ -147,11 +147,11 @@ class BasketController
         BasketModel::delete(['id' => $aArgs['id']]);
         HistoryController::add([
             'tableName' => 'baskets',
-            'recordId' => $aArgs['id'],
+            'recordId'  => $aArgs['id'],
             'eventType' => 'DEL',
-            'info' => _BASKET_SUPPRESSION." : {$aArgs['id']}",
-            'moduleId' => 'basket',
-            'eventId' => 'basketSuppression',
+            'info'      => _BASKET_SUPPRESSION . " : {$aArgs['id']}",
+            'moduleId'  => 'basket',
+            'eventId'   => 'basketSuppression',
         ]);
 
         return $response->withJson(['baskets' => BasketModel::get()]);
@@ -164,10 +164,10 @@ class BasketController
         }
 
         $baskets = BasketModel::get([
-            'select' => ['basket_id', 'basket_name', 'basket_desc', 'basket_order'],
-            'where' => ['is_visible = ?'],
-            'data' => ['Y'],
-            'orderBy' => ['basket_order'],
+            'select'    => ['basket_id', 'basket_name', 'basket_desc', 'basket_order'],
+            'where'     => ['is_visible = ?'],
+            'data'      => ['Y'],
+            'orderBy'   => ['basket_order']
         ]);
 
         return $response->withJson(['baskets' => $baskets]);
@@ -194,18 +194,18 @@ class BasketController
 
         HistoryController::add([
             'tableName' => 'baskets',
-            'recordId' => $aArgs['id'],
+            'recordId'  => $aArgs['id'],
             'eventType' => 'UP',
-            'info' => _BASKETS_SORT_MODIFICATION,
-            'moduleId' => 'basket',
-            'eventId' => 'basketModification',
+            'info'      => _BASKETS_SORT_MODIFICATION,
+            'moduleId'  => 'basket',
+            'eventId'   => 'basketModification',
         ]);
 
         $baskets = BasketModel::get([
-            'select' => ['basket_id', 'basket_name', 'basket_desc', 'basket_order'],
-            'where' => ['is_visible = ?'],
-            'data' => ['Y'],
-            'orderBy' => ['basket_order'],
+            'select'    => ['basket_id', 'basket_name', 'basket_desc', 'basket_order'],
+            'where'     => ['is_visible = ?'],
+            'data'      => ['Y'],
+            'orderBy'   => ['basket_order']
         ]);
 
         return $response->withJson(['baskets' => $baskets]);
@@ -228,9 +228,9 @@ class BasketController
         foreach ($groups as $key => $group) {
             $actionsForGroup = $allActions;
             $actions = BasketModel::getActionsForGroupById([
-                'id' => $aArgs['id'],
-                'groupId' => $group['group_id'],
-                'select' => ['id_action', 'where_clause', 'used_in_basketlist', 'used_in_action_page', 'default_action_list'],
+                'id'        => $aArgs['id'],
+                'groupId'   => $group['group_id'],
+                'select'    => ['id_action', 'where_clause', 'used_in_basketlist', 'used_in_action_page', 'default_action_list']
             ]);
             $actionIds = [];
             foreach ($actions as $action) {
@@ -240,14 +240,14 @@ class BasketController
             $redirects = [];
             if (!empty($actionIds)) {
                 $statuses = BasketModel::getGroupActionStatus([
-                    'select' => ['status_id', 'action_id'],
-                    'where' => ['basket_id = ?', 'group_id = ?', 'action_id in (?)'],
-                    'data' => [$aArgs['id'], $group['group_id'], $actionIds],
+                    'select'    => ['status_id', 'action_id'],
+                    'where'     => ['basket_id = ?', 'group_id = ?', 'action_id in (?)'],
+                    'data'      => [$aArgs['id'], $group['group_id'], $actionIds]
                 ]);
                 $redirects = BasketModel::getGroupActionRedirect([
-                    'select' => ['entity_id', 'action_id', 'keyword', 'redirect_mode'],
-                    'where' => ['basket_id = ?', 'group_id = ?', 'action_id in (?)'],
-                    'data' => [$aArgs['id'], $group['group_id'], $actionIds],
+                    'select'    => ['entity_id', 'action_id', 'keyword', 'redirect_mode'],
+                    'where'     => ['basket_id = ?', 'group_id = ?', 'action_id in (?)'],
+                    'data'      => [$aArgs['id'], $group['group_id'], $actionIds]
                 ]);
             }
             foreach ($actions as $actionKey => $action) {
@@ -311,7 +311,7 @@ class BasketController
         if (!$check) {
             return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
         }
-        $data['groupActions'] = self::checkGroupActions(['groupActions' => $data['groupActions']]);
+        $data['groupActions'] = BasketController::checkGroupActions(['groupActions' => $data['groupActions']]);
         if (!empty($data['groupActions']['errors'])) {
             return $response->withStatus(400)->withJson(['errors' => $data['groupActions']['errors']]);
         }
@@ -324,34 +324,34 @@ class BasketController
         foreach ($data['groupActions'] as $groupAction) {
             if ($groupAction['checked']) {
                 BasketModel::createGroupAction([
-                    'id' => $aArgs['id'],
-                    'groupId' => $data['group_id'],
-                    'actionId' => $groupAction['id'],
-                    'whereClause' => $groupAction['where_clause'],
-                    'usedInBasketlist' => $groupAction['used_in_basketlist'],
-                    'usedInActionPage' => $groupAction['used_in_action_page'],
-                    'defaultActionList' => $groupAction['default_action_list'],
+                    'id'                => $aArgs['id'],
+                    'groupId'           => $data['group_id'],
+                    'actionId'          => $groupAction['id'],
+                    'whereClause'       => $groupAction['where_clause'],
+                    'usedInBasketlist'  => $groupAction['used_in_basketlist'],
+                    'usedInActionPage'  => $groupAction['used_in_action_page'],
+                    'defaultActionList' => $groupAction['default_action_list']
                 ]);
 
                 if (!empty($groupAction['statuses'])) {
                     foreach ($groupAction['statuses'] as $status) {
                         BasketModel::createGroupActionStatus([
-                            'id' => $aArgs['id'],
-                            'groupId' => $data['group_id'],
-                            'actionId' => $groupAction['id'],
-                            'statusId' => $status,
+                            'id'        => $aArgs['id'],
+                            'groupId'   => $data['group_id'],
+                            'actionId'  => $groupAction['id'],
+                            'statusId'  => $status
                         ]);
                     }
                 }
                 if (!empty($groupAction['redirects'])) {
                     foreach ($groupAction['redirects'] as $redirect) {
                         BasketModel::createGroupActionRedirect([
-                            'id' => $aArgs['id'],
-                            'groupId' => $data['group_id'],
-                            'actionId' => $groupAction['id'],
-                            'entityId' => $redirect['entity_id'],
-                            'keyword' => $redirect['keyword'],
-                            'redirectMode' => $redirect['redirect_mode'],
+                            'id'            => $aArgs['id'],
+                            'groupId'       => $data['group_id'],
+                            'actionId'      => $groupAction['id'],
+                            'entityId'      => $redirect['entity_id'],
+                            'keyword'       => $redirect['keyword'],
+                            'redirectMode'  => $redirect['redirect_mode']
                         ]);
                     }
                 }
@@ -359,11 +359,11 @@ class BasketController
         }
         HistoryController::add([
             'tableName' => 'baskets',
-            'recordId' => $aArgs['id'],
+            'recordId'  => $aArgs['id'],
             'eventType' => 'UP',
-            'info' => _BASKET_GROUP_CREATION." : {$aArgs['id']}",
-            'moduleId' => 'basket',
-            'eventId' => 'basketModification',
+            'info'      => _BASKET_GROUP_CREATION . " : {$aArgs['id']}",
+            'moduleId'  => 'basket',
+            'eventId'   => 'basketModification',
         ]);
 
         return $response->withJson(['success' => 'success']);
@@ -387,7 +387,7 @@ class BasketController
         if (!$check) {
             return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
         }
-        $data['groupActions'] = self::checkGroupActions(['groupActions' => $data['groupActions']]);
+        $data['groupActions'] = BasketController::checkGroupActions(['groupActions' => $data['groupActions']]);
         if (!empty($data['groupActions']['errors'])) {
             return $response->withStatus(400)->withJson(['errors' => $data['groupActions']['errors']]);
         }
@@ -402,34 +402,34 @@ class BasketController
         foreach ($data['groupActions'] as $groupAction) {
             if ($groupAction['checked']) {
                 BasketModel::createGroupAction([
-                    'id' => $aArgs['id'],
-                    'groupId' => $aArgs['groupId'],
-                    'actionId' => $groupAction['id'],
-                    'whereClause' => $groupAction['where_clause'],
-                    'usedInBasketlist' => $groupAction['used_in_basketlist'],
-                    'usedInActionPage' => $groupAction['used_in_action_page'],
-                    'defaultActionList' => $groupAction['default_action_list'],
+                    'id'                => $aArgs['id'],
+                    'groupId'           => $aArgs['groupId'],
+                    'actionId'          => $groupAction['id'],
+                    'whereClause'       => $groupAction['where_clause'],
+                    'usedInBasketlist'  => $groupAction['used_in_basketlist'],
+                    'usedInActionPage'  => $groupAction['used_in_action_page'],
+                    'defaultActionList' => $groupAction['default_action_list']
                 ]);
 
                 if (!empty($groupAction['statuses'])) {
                     foreach ($groupAction['statuses'] as $status) {
                         BasketModel::createGroupActionStatus([
-                            'id' => $aArgs['id'],
-                            'groupId' => $aArgs['groupId'],
-                            'actionId' => $groupAction['id'],
-                            'statusId' => $status,
+                            'id'        => $aArgs['id'],
+                            'groupId'   => $aArgs['groupId'],
+                            'actionId'  => $groupAction['id'],
+                            'statusId'  => $status
                         ]);
                     }
                 }
                 if (!empty($groupAction['redirects'])) {
                     foreach ($groupAction['redirects'] as $redirect) {
                         BasketModel::createGroupActionRedirect([
-                            'id' => $aArgs['id'],
-                            'groupId' => $aArgs['groupId'],
-                            'actionId' => $groupAction['id'],
-                            'entityId' => $redirect['entity_id'],
-                            'keyword' => $redirect['keyword'],
-                            'redirectMode' => $redirect['redirect_mode'],
+                            'id'            => $aArgs['id'],
+                            'groupId'       => $aArgs['groupId'],
+                            'actionId'      => $groupAction['id'],
+                            'entityId'      => $redirect['entity_id'],
+                            'keyword'       => $redirect['keyword'],
+                            'redirectMode'  => $redirect['redirect_mode']
                         ]);
                     }
                 }
@@ -437,11 +437,11 @@ class BasketController
         }
         HistoryController::add([
             'tableName' => 'baskets',
-            'recordId' => $aArgs['id'],
+            'recordId'  => $aArgs['id'],
             'eventType' => 'UP',
-            'info' => _BASKET_GROUP_MODIFICATION." : {$aArgs['id']}",
-            'moduleId' => 'basket',
-            'eventId' => 'basketModification',
+            'info'      => _BASKET_GROUP_MODIFICATION . " : {$aArgs['id']}",
+            'moduleId'  => 'basket',
+            'eventId'   => 'basketModification',
         ]);
 
         return $response->withJson(['success' => 'success']);
@@ -461,11 +461,11 @@ class BasketController
         BasketModel::deleteGroup(['id' => $aArgs['id'], 'groupId' => $aArgs['groupId']]);
         HistoryController::add([
             'tableName' => 'baskets',
-            'recordId' => $aArgs['id'],
+            'recordId'  => $aArgs['id'],
             'eventType' => 'UP',
-            'info' => _BASKET_GROUP_SUPPRESSION." : {$aArgs['id']}",
-            'moduleId' => 'basket',
-            'eventId' => 'basketModification',
+            'info'      => _BASKET_GROUP_SUPPRESSION . " : {$aArgs['id']}",
+            'moduleId'  => 'basket',
+            'eventId'   => 'basketModification',
         ]);
 
         return $response->withJson(['success' => 'success']);
diff --git a/src/app/entity/controllers/ListTemplateController.php b/src/app/entity/controllers/ListTemplateController.php
index 23fea3bc268..c11024f000e 100644
--- a/src/app/entity/controllers/ListTemplateController.php
+++ b/src/app/entity/controllers/ListTemplateController.php
@@ -4,11 +4,11 @@
  * Copyright Maarch since 2008 under licence GPLv3.
  * See LICENCE.txt file at the root folder for more details.
  * This file is part of Maarch software.
+ *
  */
 
 /**
  * @brief List Template Controller
- *
  * @author dev@maarch.org
  */
 
@@ -28,7 +28,7 @@ class ListTemplateController
 {
     public function get(Request $request, Response $response)
     {
-        $rawListTemplates = ListTemplateModel::get(['select' => ['id', 'object_id', 'object_type', 'title', 'description'], 'where' => ['(object_type <> ?) AND (object_id LIKE ?)'], 'data' => ['entity_id', 'VISA_CIRCUIT_%']]);
+        $rawListTemplates = ListTemplateModel::get(['select' => ['id', 'object_id', 'object_type', 'title', 'description']]);
 
         $listTemplates = [];
         $tmpTemplates = [];
@@ -47,18 +47,16 @@ class ListTemplateController
         $listTemplates = ListTemplateModel::getById(['id' => $aArgs['id']]);
         if (empty($listTemplates)) {
             return $response->withStatus(400)->withJson(['errors' => 'List template not found']);
-        } else {
-            $listTemplatesObj = array(
-                'object_id' => $listTemplates[0]['object_id'],
-                'object_type' => $listTemplates[0]['object_type'],
-                'object_type' => $listTemplates[0]['object_type'],
-                'title' => $listTemplates[0]['title'],
-                'description' => $listTemplates[0]['description'],
-                'diffusionList' => [$listTemplates],
-            );
         }
+        $listTemplate = [
+            'object_id'     => $listTemplates[0]['object_id'],
+            'object_type'   => $listTemplates[0]['object_type'],
+            'title'         => $listTemplates[0]['title'],
+            'description'   => $listTemplates[0]['description'],
+            'diffusionList' => [$listTemplates],
+        ];
 
-        return $response->withJson(['listTemplate' => $listTemplatesObj]);
+        return $response->withJson(['listTemplate' => $listTemplate]);
     }
 
     public function create(Request $request, Response $response)
@@ -90,34 +88,34 @@ class ListTemplateController
                 }
             }
         } else {
-            $data['object_id'] = $data['object_type'].'_'.DatabaseModel::uniqueId();
+            $data['object_id'] = $data['object_type'] . '_' . DatabaseModel::uniqueId();
         }
 
-        $checkItems = self::checkItems(['items' => $data['items']]);
+        $checkItems = ListTemplateController::checkItems(['items' => $data['items']]);
         if (!empty($checkItems['errors'])) {
             return $response->withStatus(400)->withJson(['errors' => $checkItems['errors']]);
         }
 
         foreach ($data['items'] as $item) {
             ListTemplateModel::create([
-                'object_id' => $data['object_id'],
-                'object_type' => $data['object_type'],
-                'title' => $data['title'],
-                'description' => $data['description'],
-                'sequence' => $item['sequence'],
-                'item_id' => $item['item_id'],
-                'item_type' => $item['item_type'],
-                'item_mode' => $item['item_mode'],
+                'object_id'     => $data['object_id'],
+                'object_type'   => $data['object_type'],
+                'title'         => $data['title'],
+                'description'   => $data['description'],
+                'sequence'      => $item['sequence'],
+                'item_id'       => $item['item_id'],
+                'item_type'     => $item['item_type'],
+                'item_mode'     => $item['item_mode'],
             ]);
         }
 
         HistoryController::add([
             'tableName' => 'listmodels',
-            'recordId' => $data['object_id'],
+            'recordId'  => $data['object_id'],
             'eventType' => 'ADD',
-            'info' => _LIST_TEMPLATE_CREATION." : {$data['title']} {$data['description']}",
-            'moduleId' => 'listTemplate',
-            'eventId' => 'listTemplateCreation',
+            'info'      => _LIST_TEMPLATE_CREATION . " : {$data['title']} {$data['description']}",
+            'moduleId'  => 'listTemplate',
+            'eventId'   => 'listTemplateCreation',
         ]);
 
         return $response->withJson(['success' => 'success']);
@@ -150,35 +148,35 @@ class ListTemplateController
             }
         }
 
-        $checkItems = self::checkItems(['items' => $data['items']]);
+        $checkItems = ListTemplateController::checkItems(['items' => $data['items']]);
         if (!empty($checkItems['errors'])) {
             return $response->withStatus(400)->withJson(['errors' => $checkItems['errors']]);
         }
 
         ListTemplateModel::delete([
             'where' => ['object_id = ?', 'object_type = ?'],
-            'data' => [$listTemplates[0]['object_id'], $listTemplates[0]['object_type']],
+            'data'  => [$listTemplates[0]['object_id'], $listTemplates[0]['object_type']]
         ]);
         foreach ($data['items'] as $item) {
             ListTemplateModel::create([
-                'object_id' => $listTemplates[0]['object_id'],
-                'object_type' => $listTemplates[0]['object_type'],
-                'title' => $data['title'],
-                'description' => $data['description'],
-                'sequence' => $item['sequence'],
-                'item_id' => $item['item_id'],
-                'item_type' => $item['item_type'],
-                'item_mode' => $item['item_mode'],
+                'object_id'     => $listTemplates[0]['object_id'],
+                'object_type'   => $listTemplates[0]['object_type'],
+                'title'         => $data['title'],
+                'description'   => $data['description'],
+                'sequence'      => $item['sequence'],
+                'item_id'       => $item['item_id'],
+                'item_type'     => $item['item_type'],
+                'item_mode'     => $item['item_mode'],
             ]);
         }
 
         HistoryController::add([
             'tableName' => 'listmodels',
-            'recordId' => $listTemplates[0]['object_id'],
+            'recordId'  => $listTemplates[0]['object_id'],
             'eventType' => 'UP',
-            'info' => _LIST_TEMPLATE_MODIFICATION." : {$data['title']} {$data['description']}",
-            'moduleId' => 'listTemplate',
-            'eventId' => 'listTemplateModification',
+            'info'      => _LIST_TEMPLATE_MODIFICATION . " : {$data['title']} {$data['description']}",
+            'moduleId'  => 'listTemplate',
+            'eventId'   => 'listTemplateModification',
         ]);
 
         return $response->withJson(['success' => 'success']);
@@ -206,15 +204,15 @@ class ListTemplateController
 
         ListTemplateModel::delete([
             'where' => ['object_id = ?', 'object_type = ?'],
-            'data' => [$listTemplates[0]['object_id'], $listTemplates[0]['object_type']],
+            'data'  => [$listTemplates[0]['object_id'], $listTemplates[0]['object_type']]
         ]);
         HistoryController::add([
             'tableName' => 'listmodels',
-            'recordId' => $listTemplates[0]['object_id'],
+            'recordId'  => $listTemplates[0]['object_id'],
             'eventType' => 'DEL',
-            'info' => _LIST_TEMPLATE_SUPPRESSION." : {$listTemplates[0]['object_id']} {$listTemplates[0]['object_type']}",
-            'moduleId' => 'listTemplate',
-            'eventId' => 'listTemplateSuppression',
+            'info'      => _LIST_TEMPLATE_SUPPRESSION . " : {$listTemplates[0]['object_id']} {$listTemplates[0]['object_type']}",
+            'moduleId'  => 'listTemplate',
+            'eventId'   => 'listTemplateSuppression',
         ]);
 
         return $response->withJson(['success' => 'success']);
-- 
GitLab