From ee6d66e94a17fdc3f7d61813c1aba40bc82aab5d Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Tue, 30 Jan 2018 17:51:30 +0100
Subject: [PATCH] FIX #51 history action admin

---
 .../action-administration.component.html      |  3 +-
 .../action/controllers/ActionController.php   | 40 ++++++++++----
 src/app/action/models/ActionModelAbstract.php | 54 +++++++++----------
 3 files changed, 58 insertions(+), 39 deletions(-)

diff --git a/apps/maarch_entreprise/Views/action-administration.component.html b/apps/maarch_entreprise/Views/action-administration.component.html
index ce0c72ca713..af79b1f5615 100755
--- a/apps/maarch_entreprise/Views/action-administration.component.html
+++ b/apps/maarch_entreprise/Views/action-administration.component.html
@@ -33,8 +33,7 @@
                 </div>
                 <div class="form-group">
                     <mat-form-field>
-                        <mat-select id="action_page" name="action_page" title="{{lang.actionPage}}" placeholder="{{lang.actionPage}}" [(ngModel)]="action.action_page"
-                            required>
+                        <mat-select id="action_page" name="action_page" title="{{lang.actionPage}}" placeholder="{{lang.actionPage}}" [(ngModel)]="action.action_page" >
                             <mat-option *ngFor="let action of actionPagesList.actionsPageList" [value]="action.name">
                                 {{action.label}}
                             </mat-option>
diff --git a/src/app/action/controllers/ActionController.php b/src/app/action/controllers/ActionController.php
index e0a676d2849..8c30ba66d0f 100644
--- a/src/app/action/controllers/ActionController.php
+++ b/src/app/action/controllers/ActionController.php
@@ -11,10 +11,10 @@
 
 namespace Action\controllers;
 
+use History\controllers\HistoryController;
 use Respect\Validation\Validator;
 use Action\models\ActionModel;
 use Status\models\StatusModel;
-use Core\Models\LangModel;
 use Core\Models\ServiceModel;
 use Slim\Http\Request;
 use Slim\Http\Response;
@@ -111,8 +111,6 @@ class ActionController
         $return = ActionModel::create($aArgs);
 
         if ($return) {
-            $id = $aArgs['id'];
-
             $obj = max(ActionModel::get());
         } else {
             return $response
@@ -120,10 +118,17 @@ class ActionController
                 ->withJson(['errors' => _NOT_CREATE]);
         }
 
+        HistoryController::add([
+            'tableName' => 'actions',
+            'recordId'  => $obj['id'],
+            'eventType' => 'ADD',
+            'eventId'   => 'actionadd',
+            'info'      => _ACTION. ' "' . $obj['label_action'] .'" ' ._ADDED
+        ]);
+
         return $response->withJson(
             [
-            'success'   =>  _ACTION. ' <b>' . $obj['id'] .'</b> ' ._ADDED,
-            'action'      => $obj
+            'action'  => $obj
             ]
         );
     }
@@ -159,9 +164,16 @@ class ActionController
                 ->withJson(['errors' => _NOT_UPDATE]);
         }
 
+        HistoryController::add([
+            'tableName' => 'actions',
+            'recordId'  => $obj['id'],
+            'eventType' => 'UP',
+            'eventId'   => 'actionup',
+            'info'      => _ACTION. ' "' . $obj['label_action'] .'" ' ._UPDATED
+        ]);
+
         return $response->withJson(
             [
-            'success' => _ACTION. ' <b>' . $id .'</b> ' ._UPDATED,
             'action'  => $obj
             ]
         );
@@ -174,18 +186,26 @@ class ActionController
         }
 
         if (isset($aArgs['id'])) {
-            $id = $aArgs['id'];
+            $id  = $aArgs['id'];
+            $obj = ActionModel::getById(['id' => $id]);
             ActionModel::delete(['id' => $id]);
         } else {
             return $response
                 ->withStatus(500)
                 ->withJson(['errors' => _NOT_DELETE]);
         }
-        
+
+        HistoryController::add([
+            'tableName' => 'actions',
+            'recordId'  => $id,
+            'eventType' => 'DEL',
+            'eventId'   => 'actiondel',
+            'info'      => _ACTION. ' "' . $obj['label_action'] .'" ' ._DELETED
+        ]);
+
         return $response->withJson(
             [
-            'success'   => _ACTION. ' <b>' . $id .'</b> ' ._DELETED,
-            'action'      => ActionModel::get()
+            'action' => ActionModel::get()
             ]
         );
     }
diff --git a/src/app/action/models/ActionModelAbstract.php b/src/app/action/models/ActionModelAbstract.php
index f19dceaeb25..ee8e0ed8b00 100644
--- a/src/app/action/models/ActionModelAbstract.php
+++ b/src/app/action/models/ActionModelAbstract.php
@@ -22,8 +22,8 @@ class ActionModelAbstract
         ValidatorModel::arrayType($aArgs, ['select']);
 
         $actions = DatabaseModel::select([
-            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
-            'table'     => ['actions']
+            'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
+            'table'  => ['actions']
         ]);
 
         return $actions;
@@ -36,10 +36,10 @@ class ActionModelAbstract
 
         $aReturn = DatabaseModel::select(
             [
-            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
-            'table'     => ['actions'],
-            'where'     => ['id = ?'],
-            'data'      => [$aArgs['id']]
+            'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
+            'table'  => ['actions'],
+            'where'  => ['id = ?'],
+            'data'   => [$aArgs['id']]
             ]
         );
 
@@ -48,12 +48,12 @@ class ActionModelAbstract
         }
 
         $aReturn = $aReturn[0];
-        $aReturn['actionCategories']=DatabaseModel::select(
+        $aReturn['actionCategories'] = DatabaseModel::select(
             [
-            'select'    => ['category_id'],
-            'table'     => ['actions_categories'],
-            'where'     => ['action_id = ?'],
-            'data'      => [$aArgs['id']]
+            'select' => ['category_id'],
+            'table'  => ['actions_categories'],
+            'where'  => ['action_id = ?'],
+            'data'   => [$aArgs['id']]
             ]
         );
        
@@ -106,12 +106,12 @@ class ActionModelAbstract
 
         $aDelete = DatabaseModel::delete(
             ['table' => 'actions_categories',
-            'where' => ['action_id = ?'],
-            'data'  => [$aArgs['id']]
+            'where'  => ['action_id = ?'],
+            'data'   => [$aArgs['id']]
             ]
         );
 
-        $tab['action_id']=$aArgs['id'];
+        $tab['action_id'] = $aArgs['id'];
 
         for ($i=0;$i<count($aArgs['actionCategories']);$i++) {
             $tab['category_id']=$aArgs['actionCategories'][$i];
@@ -218,11 +218,11 @@ class ActionModelAbstract
                     $desc =  'no description';
                 }
                 $tabActions_pages['actionsPageList'][] = array(
-                    'id'          => (string) $actionPage->ID,
-                    'label'       => $label,
-                    'name'        => (string) $actionPage->NAME,
-                    'desc'        => $desc,
-                    'origin'      => ucfirst($origin),
+                    'id'     => (string) $actionPage->ID,
+                    'label'  => $label,
+                    'name'   => (string) $actionPage->NAME,
+                    'desc'   => $desc,
+                    'origin' => ucfirst($origin),
                 );
             }
         }
@@ -259,10 +259,10 @@ class ActionModelAbstract
         ValidatorModel::intVal($aArgs, ['id']);
 
         $action = DatabaseModel::select([
-            'select'    => ['action_page'],
-            'table'     => ['actions'],
-            'where'     => ['id = ? AND enabled = ?'],
-            'data'      => [$aArgs['id'], 'Y']
+            'select' => ['action_page'],
+            'table'  => ['actions'],
+            'where'  => ['id = ? AND enabled = ?'],
+            'data'   => [$aArgs['id'], 'Y']
         ]);
 
         if (empty($action[0])) {
@@ -278,10 +278,10 @@ class ActionModelAbstract
         ValidatorModel::stringType($aArgs, ['groupId', 'basketId']);
 
         $action = DatabaseModel::select([
-            'select'    => ['id_action'],
-            'table'     => ['actions_groupbaskets'],
-            'where'     => ['group_id = ?', 'basket_id = ?', 'default_action_list = ?'],
-            'data'      => [$aArgs['groupId'], $aArgs['basketId'], 'Y']
+            'select' => ['id_action'],
+            'table'  => ['actions_groupbaskets'],
+            'where'  => ['group_id = ?', 'basket_id = ?', 'default_action_list = ?'],
+            'data'   => [$aArgs['groupId'], $aArgs['basketId'], 'Y']
         ]);
 
         if (empty($action[0])) {
-- 
GitLab