From c101db84e5bd6ae889b5cf828761600490b1a9e9 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Wed, 12 Jul 2017 14:11:19 +0200
Subject: [PATCH] FEAT #5730 add Validator + fix select/insert/delete call

---
 .../app/action-administration.component.js    |  14 +--
 .../app/action-administration.component.ts    |  16 +--
 .../app/actions-administration.component.js   |  29 ++---
 .../app/actions-administration.component.ts   |  33 +++---
 core/Controllers/ActionsController.php        |  51 ++++-----
 core/Models/ActionsModelAbstract.php          | 108 +++++++++++-------
 6 files changed, 126 insertions(+), 125 deletions(-)

diff --git a/apps/maarch_entreprise/js/angular/app/action-administration.component.js b/apps/maarch_entreprise/js/angular/app/action-administration.component.js
index 5412e5adf23..f36ded8271c 100644
--- a/apps/maarch_entreprise/js/angular/app/action-administration.component.js
+++ b/apps/maarch_entreprise/js/angular/app/action-administration.component.js
@@ -92,10 +92,9 @@ var ActionAdministrationComponent = (function () {
                 .map(function (res) { return res.json(); })
                 .subscribe(function (data) {
                 _this.router.navigate(['/administration/actions']);
-                //TO_DO_NOTIF
-            }, function (errors) {
-                console.log(errors);
-                //TO_DO_NOTIF_ERRORS
+                successNotification(data.success);
+            }, function (err) {
+                errorNotification(JSON.parse(err._body).errors);
             });
         }
         else if (this.mode == 'update') {
@@ -103,10 +102,9 @@ var ActionAdministrationComponent = (function () {
                 .map(function (res) { return res.json(); })
                 .subscribe(function (data) {
                 _this.router.navigate(['/administration/actions']);
-                //TO_DO_NOTIF
-            }, function (errors) {
-                console.log(errors);
-                //TO_DO_NOTIF_ERRORS
+                successNotification(data.success);
+            }, function (err) {
+                errorNotification(JSON.parse(err._body).errors);
             });
         }
     };
diff --git a/apps/maarch_entreprise/js/angular/app/action-administration.component.ts b/apps/maarch_entreprise/js/angular/app/action-administration.component.ts
index bd111042990..f354ce1b8e4 100644
--- a/apps/maarch_entreprise/js/angular/app/action-administration.component.ts
+++ b/apps/maarch_entreprise/js/angular/app/action-administration.component.ts
@@ -4,6 +4,8 @@ import 'rxjs/add/operator/map';
 import { Router, ActivatedRoute } from '@angular/router';
 
 declare function $j(selector: any) : any;
+declare function successNotification(message: string) : void;
+declare function errorNotification(message: string) : void;
 
 declare var angularGlobals : any;
 
@@ -103,22 +105,20 @@ export class ActionAdministrationComponent implements OnInit {
             .map(res => res.json())
             .subscribe((data) => {
                 this.router.navigate(['/administration/actions']);
-                //TO_DO_NOTIF
+                successNotification(data.success);
 
-            },(errors) => {
-                console.log(errors);
-                //TO_DO_NOTIF_ERRORS
+            },(err) => {
+                errorNotification(JSON.parse(err._body).errors);
             });
         }else if (this.mode == 'update') {
             this.http.put(this.coreUrl + 'rest/actions/' + this.action.id, this.action)
             .map(res => res.json())
             .subscribe((data) => {
                 this.router.navigate(['/administration/actions']);
-                //TO_DO_NOTIF
+                successNotification(data.success);
 
-            },(errors) => {
-                console.log(errors);
-                //TO_DO_NOTIF_ERRORS
+            },(err) => {
+                errorNotification(JSON.parse(err._body).errors);
             });
         }
 
diff --git a/apps/maarch_entreprise/js/angular/app/actions-administration.component.js b/apps/maarch_entreprise/js/angular/app/actions-administration.component.js
index 72805326169..959cb20ff8c 100644
--- a/apps/maarch_entreprise/js/angular/app/actions-administration.component.js
+++ b/apps/maarch_entreprise/js/angular/app/actions-administration.component.js
@@ -35,6 +35,7 @@ var ActionsAdministrationComponent = (function () {
         this.http.get(this.coreUrl + 'rest/administration/actions')
             .map(function (res) { return res.json(); })
             .subscribe(function (data) {
+            console.log('toto');
             _this.actions = data['actions'];
             _this.titles = data['titles'];
             _this.lang = data['lang'];
@@ -84,27 +85,17 @@ var ActionsAdministrationComponent = (function () {
             this.http.delete(this.coreUrl + 'rest/actions/' + id)
                 .map(function (res) { return res.json(); })
                 .subscribe(function (data) {
-                if (data.errors) {
-                    _this.resultInfo = data.errors;
-                    $j('#resultInfo').removeClass().addClass('alert alert-danger alert-dismissible');
-                    $j("#resultInfo").fadeTo(3000, 500).slideUp(500, function () {
-                        $j("#resultInfo").slideUp(500);
-                    });
-                }
-                else {
-                    var list = _this.actions;
-                    for (var i = 0; i < list.length; i++) {
-                        if (list[i].id == id) {
-                            list.splice(i, 1);
-                        }
+                var list = _this.actions;
+                for (var i = 0; i < list.length; i++) {
+                    if (list[i].id == id) {
+                        list.splice(i, 1);
                     }
-                    _this.table.row($j("#" + id)).remove().draw();
-                    _this.resultInfo = _this.lang.delete_action;
-                    $j('#resultInfo').removeClass().addClass('alert alert-success alert-dismissible');
-                    $j("#resultInfo").fadeTo(3000, 500).slideUp(500, function () {
-                        $j("#resultInfo").slideUp(500);
-                    });
                 }
+                _this.table.row($j("#" + id)).remove().draw();
+                _this.resultInfo = _this.lang.delete_action;
+                successNotification(data.success);
+            }, function (err) {
+                errorNotification(JSON.parse(err._body).errors);
             });
         }
     };
diff --git a/apps/maarch_entreprise/js/angular/app/actions-administration.component.ts b/apps/maarch_entreprise/js/angular/app/actions-administration.component.ts
index 96454236d93..0600373db9e 100644
--- a/apps/maarch_entreprise/js/angular/app/actions-administration.component.ts
+++ b/apps/maarch_entreprise/js/angular/app/actions-administration.component.ts
@@ -3,6 +3,8 @@ import { Http } from '@angular/http';
 import 'rxjs/add/operator/map';
 
 declare function $j(selector: any) : any;
+declare function successNotification(message: string) : void;
+declare function errorNotification(message: string) : void;
 
 declare var angularGlobals : any;
 
@@ -43,6 +45,7 @@ export class ActionsAdministrationComponent implements OnInit {
         this.http.get(this.coreUrl + 'rest/administration/actions')
             .map(res => res.json())
             .subscribe((data) => {
+                console.log('toto');
                 this.actions = data['actions'];
                 this.titles = data['titles'];
                 this.lang= data['lang'];
@@ -94,28 +97,18 @@ export class ActionsAdministrationComponent implements OnInit {
             this.http.delete(this.coreUrl + 'rest/actions/' + id)
                 .map(res => res.json())
                 .subscribe((data) => {
-                    if (data.errors) {
-                        this.resultInfo = data.errors;
-                        $j('#resultInfo').removeClass().addClass('alert alert-danger alert-dismissible');
-                        $j("#resultInfo").fadeTo(3000, 500).slideUp(500, function(){
-                            $j("#resultInfo").slideUp(500);
-                        });
-                    }
-                    else
-                    {
-                        var list = this.actions;
-                        for(var i = 0; i<list.length;i++){
-                            if(list[i].id==id){
-                                list.splice(i,1);
-                            }
+                    var list = this.actions;
+                    for(var i = 0; i<list.length;i++){
+                        if(list[i].id==id){
+                            list.splice(i,1);
                         }
-                        this.table.row($j("#"+id)).remove().draw();
-                        this.resultInfo = this.lang.delete_action;
-                        $j('#resultInfo').removeClass().addClass('alert alert-success alert-dismissible');
-                        $j("#resultInfo").fadeTo(3000, 500).slideUp(500, function(){
-                            $j("#resultInfo").slideUp(500);
-                        });
                     }
+                    this.table.row($j("#"+id)).remove().draw();
+                    this.resultInfo = this.lang.delete_action;
+                    successNotification(data.success);
+                    
+                }, (err) => {
+                    errorNotification(JSON.parse(err._body).errors);
                 });
         }
     }
diff --git a/core/Controllers/ActionsController.php b/core/Controllers/ActionsController.php
index d8512d918c5..aa445834beb 100644
--- a/core/Controllers/ActionsController.php
+++ b/core/Controllers/ActionsController.php
@@ -1,15 +1,11 @@
 <?php
-
 /**
 * 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 Status Controller
-* @author dev@maarch.org
+* @brief   ActionsController
+* @author  dev <dev@maarch.org>
 * @ingroup core
 */
 
@@ -24,7 +20,8 @@ use Core\Models\LangModel;
 
 class ActionsController
 {
-    public function getForAdministration(RequestInterface $request, ResponseInterface $response){
+    public function getForAdministration(RequestInterface $request, ResponseInterface $response)
+    {
         
         $obj['lang'] = LangModel::getActionsForAdministrationLang();
         $obj ['actions']= ActionsModel::getList();
@@ -132,11 +129,12 @@ class ActionsController
                 ->withJson(['errors' => _NOT_CREATE]);
         }
 
-        $datas = [
-            $obj,
-        ];
-
-        return  $response->withJson($datas);
+        return $response->withJson(
+            [
+            'success'   =>  _ACTION. ' <b>' . $obj['id'] .'</b> ' ._ADDED,
+            'action'      => $obj
+            ]
+        );
     }
 
     public function update(RequestInterface $request, ResponseInterface $response, $aArgs)
@@ -160,20 +158,19 @@ class ActionsController
 
         if ($return) {
             $id = $aArgs['id'];
-            $obj = ActionsModel::getById([
-                'id' => $id
-            ]);
+            $obj = ActionsModel::getById(['id' => $id]);
         } else {
             return $response
                 ->withStatus(500)
                 ->withJson(['errors' => _NOT_UPDATE]);
         }
 
-        $datas = [
-            $obj,
-        ];
-
-        return $response->withJson($datas);
+        return $response->withJson(
+            [
+            'success'   => _ACTION. ' <b>' . $id .'</b> ' ._UPDATED,
+            'action'      => $obj
+            ]
+        );
     }
 
 
@@ -181,20 +178,20 @@ class ActionsController
     {
         if (isset($aArgs['id'])) {
             $id = $aArgs['id'];
-            $obj = ActionsModel::delete([
-                'id' => $id
-            ]);
+            $obj = ActionsModel::delete(['id' => $id]);
         } else {
             return $response
                 ->withStatus(500)
                 ->withJson(['errors' => _NOT_DELETE]);
         }
         
-        $datas = [
-            $obj,
-        ];
 
-        return $response->withJson($datas);
+        return $response->withJson(
+            [
+            'success'   => _ACTION. ' <b>' . $id .'</b> ' ._DELETED,
+            'action'      => $obj
+            ]
+        );
     }
 
     protected function control($aArgs, $mode)
diff --git a/core/Models/ActionsModelAbstract.php b/core/Models/ActionsModelAbstract.php
index b2f51b6efb6..1ad912cfc65 100644
--- a/core/Models/ActionsModelAbstract.php
+++ b/core/Models/ActionsModelAbstract.php
@@ -1,96 +1,108 @@
 <?php
-
 /**
 * 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 Status Model
-* @author dev@maarch.org
+* @brief   ActionsModelAbstract
+* @author  dev <dev@maarch.org>
 * @ingroup core
 */
 
 namespace Core\Models;
 
-require_once 'apps/maarch_entreprise/services/Table.php';
-
-class ActionsModelAbstract extends \Apps_Table_Service
+class ActionsModelAbstract
 {
     public static function getList()
     {
-        $aReturn = static::select([
+        $aReturn = DatabaseModel::select(
+            [
             'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
             'table'     => ['actions'],
-        ]);
+            ]
+        );
 
         return $aReturn;
     }
 
     public static function getById(array $aArgs = [])
     {
-        static::checkRequired($aArgs, ['id']);
+        ValidatorModel::notEmpty($aArgs, ['id']);
+        ValidatorModel::intVal($aArgs, ['id']);
 
-        $aReturn = static::select([
+        $aReturn = DatabaseModel::select(
+            [
             'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
             'table'     => ['actions'],
             'where'     => ['id = ?'],
             'data'      => [$aArgs['id']]
-        ]);
+            ]
+        );
 
         if (empty($aReturn[0])) {
             return [];
         }
 
-        $aReturn=$aReturn[0];
-        $aReturn['actionCategories']=static::select([
-            'select'    => 'category_id',
+        $aReturn = $aReturn[0];
+        $aReturn['actionCategories']=DatabaseModel::select(
+            [
+            'select'    => ['category_id'],
             'table'     => ['actions_categories'],
             'where'     => ['action_id = ?'],
             'data'      => [$aArgs['id']]
-        ]);
+            ]
+        );
        
         return $aReturn;
     }
 
     public static function create(array $aArgs = [])
     {
-        $actioncategories=$aArgs['actionCategories'];
+        $actioncategories = $aArgs['actionCategories'];
         unset($aArgs['actionCategories']);
-        $aReturn = static::insertInto($aArgs, 'actions');
-        $tab['action_id']=max(ActionsModel::getList())['id'];
+        $aReturn = DatabaseModel::insert(
+            [
+            'table'         => 'actions',
+            'columnsValues' => $aArgs
+            ]
+        );
+
+        $tab['action_id'] = max(ActionsModel::getList())['id'];
 
         for ($i=0;$i<count($actioncategories);$i++) {
 
             $tab['category_id'] = $actioncategories[$i];
-            $aInsert = static::insertInto($tab, 'actions_categories');
+            $aInsert = DatabaseModel::insert(
+                [
+                'table'         => 'actions_categories',
+                'columnsValues' => $tab
+                ]
+            );    
         }
-
         return $aReturn;
     }
 
     public static function update(array $aArgs = [])
     {
-        static::checkRequired($aArgs, ['id']);
+        ValidatorModel::notEmpty($aArgs, ['id']);
+        ValidatorModel::intVal($aArgs, ['id']);
         
-        $aReturn = parent::update(
+        $aReturn = DatabaseModel::update(
             ['table'     => 'actions',
             'set'       => [
-                'keyword' => $aArgs['keyword'],          
-                'label_action' => $aArgs['label_action'],
-                'id_status' => $aArgs['id_status'],
-                'action_page' => $aArgs['action_page'],
-                'history' => $aArgs['history'],
-                'is_folder_action' => $aArgs['is_folder_action'],
-                'history' => $aArgs['history']
+                'keyword'           => $aArgs['keyword'],          
+                'label_action'      => $aArgs['label_action'],
+                'id_status'         => $aArgs['id_status'],
+                'action_page'       => $aArgs['action_page'],
+                'history'           => $aArgs['history'],
+                'is_folder_action'  => $aArgs['is_folder_action'],
+                'history'           => $aArgs['history']
             ],
             'where'     => ['id = ?'],
             'data'      => [$aArgs['id']]]
         );
 
-        $aDelete = static::deleteFrom(
+        $aDelete = DatabaseModel::delete(
             ['table' => 'actions_categories',
             'where' => ['action_id = ?'],
             'data'  => [$aArgs['id']]
@@ -102,31 +114,41 @@ class ActionsModelAbstract extends \Apps_Table_Service
         for ($i=0;$i<count($aArgs['actionCategories']);$i++) {
 
             $tab['category_id']=$aArgs['actionCategories'][$i];
-            $aInsert = static::insertInto($tab, 'actions_categories');
+            $aInsert = DatabaseModel::insert(
+                [
+                'table'         => 'actions_categories',
+                'columnsValues' => $tab
+                ]
+            );    
         }
 
- 
         return $aReturn;
     }
 
     public static function delete(array $aArgs = [])
     {
-        static::checkRequired($aArgs, ['id']);
+        ValidatorModel::notEmpty($aArgs, ['id']);
+        ValidatorModel::intVal($aArgs, ['id']);
 
-        $aReturn = static::deleteFrom([
+        $aReturn = DatabaseModel::delete(
+            [
                 'table' => 'actions',
                 'where' => ['id = ?'],
                 'data'  => [$aArgs['id']]
-            ]);
-        $aDelete = static::deleteFrom([
+            ]
+        );
+        $aDelete = DatabaseModel::delete(
+            [
                 'table' => 'actions_categories',
                 'where' => ['action_id = ?'],
                 'data'  => [$aArgs['id']]
-            ]);
+            ]
+        );
         return $aReturn;
     }
 
-    public static function getLettersBoxCategories(){
+    public static function getLettersBoxCategories()
+    {
         if (file_exists('custom/' .$_SESSION['custom_override_id']. '/apps/maarch_entreprise/xml/config.xml')) {
             $path = 'custom/' .$_SESSION['custom_override_id']. '/apps/maarch_entreprise/xml/config.xml';
         } else {
@@ -138,9 +160,9 @@ class ActionsModelAbstract extends \Apps_Table_Service
         $categories= $xmlfile->COLLECTION->categories;
         if (count($categories) > 0) {
             foreach ($categories->category as $category) {
-               $categoriesTmp = ['id' => (string)$category->id, 'label'=> constant((string)$category->label)];
+                $categoriesTmp = ['id' => (string)$category->id, 'label'=> constant((string)$category->label)];
 
-                if($category->id == (string)$categories->default_category){
+                if ($category->id == (string)$categories->default_category) {
                     $categoriesTmp['default_category']=true;
 
                 } else {
-- 
GitLab