From f10f7f3d8d1fd9d0b53330fa1a678addff3dd7c5 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Tue, 6 Feb 2018 14:51:34 +0100
Subject: [PATCH] FEAT #56 Entities administration begining

---
 apps/maarch_entreprise/actions/sendFileWS.php |   2 +-
 composer.json                                 |   1 +
 core/Models/LangModelAbstract.php             | 119 ------------------
 modules/visa/Controllers/VisaController.php   |   2 +-
 rest/index.php                                |   6 +-
 src/app/basket/models/BasketModelAbstract.php |  15 +++
 .../entity/controllers/EntityController.php   |  54 +++++++-
 src/app/entity/models/EntityModelAbstract.php |  89 ++++++++++---
 src/app/entity/models/UserEntityModel.php     |  19 +++
 .../entity/models/UserEntityModelAbstract.php |  66 ++++++++++
 .../controllers}/ListinstanceController.php   |  11 +-
 .../models}/ListinstanceModel.php             |   3 +-
 .../models}/ListinstanceModelAbstract.php     |  21 +++-
 .../resource/controllers/ResController.php    |   2 +-
 src/app/resource/models/ResModelAbstract.php  |   9 +-
 15 files changed, 262 insertions(+), 157 deletions(-)
 create mode 100644 src/app/entity/models/UserEntityModel.php
 create mode 100644 src/app/entity/models/UserEntityModelAbstract.php
 rename {core/Controllers => src/app/listinstance/controllers}/ListinstanceController.php (62%)
 rename {core/Models => src/app/listinstance/models}/ListinstanceModel.php (86%)
 rename {core/Models => src/app/listinstance/models}/ListinstanceModelAbstract.php (81%)

diff --git a/apps/maarch_entreprise/actions/sendFileWS.php b/apps/maarch_entreprise/actions/sendFileWS.php
index 3700c6b9d25..e10b955bd04 100644
--- a/apps/maarch_entreprise/actions/sendFileWS.php
+++ b/apps/maarch_entreprise/actions/sendFileWS.php
@@ -23,7 +23,7 @@ function manage_send($aId)
         $response = \Core\Models\CurlModel::exec(['curlCallId' => 'sendFile', 'bodyData' => $bodyParams]);
 
         if (!empty($response['publikId'])) {
-            \Resource\models\ResModel::update(['resId' => $document['res_id'], 'set' => ['custom_t1' => $response['publikId']]]);
+            \Resource\models\ResModel::update(['set' => ['custom_t1' => $response['publikId']], 'where' => ['res_id = ?'], 'data' => [$document['res_id']]]);
         }
         $result .= $resId . '#';
     }
diff --git a/composer.json b/composer.json
index 23fbb3bac07..30dc307bb30 100755
--- a/composer.json
+++ b/composer.json
@@ -7,6 +7,7 @@
             "Contact\\"       : "src/app/contact/",
             "Entity\\"        : "src/app/entity/",
             "History\\"       : "src/app/history/",
+            "Listinstance\\"  : "src/app/listinstance/",
             "Parameter\\"     : "src/app/parameter/",
             "Priority\\"      : "src/app/priority/",
             "Report\\"        : "src/app/report/",
diff --git a/core/Models/LangModelAbstract.php b/core/Models/LangModelAbstract.php
index 6b7ae12bca4..b3df095b2ac 100755
--- a/core/Models/LangModelAbstract.php
+++ b/core/Models/LangModelAbstract.php
@@ -17,45 +17,6 @@ namespace Core\Models;
 
 class LangModelAbstract
 {
-    public static function getParametersLang()
-    {
-        $aLang = [
-                'admin'                 => _ADMIN,
-                'parameter'             =>  _PARAMETER,
-                'parameters'            =>  _PARAMETERS,
-                'identifier'            =>  _PARAMETER_IDENTIFIER,
-                'description'           =>  _DESCRIPTION,
-                'value'                 =>  _VALUE,
-                'type'                  =>  _TYPE,
-                'string'                =>  _STRING,
-                'integer'               =>  _INTEGER,
-                'date'                  =>  _DATE,
-                'validate'              =>  _VALIDATE,
-                'cancel'                =>  _CANCEL,
-                'modify'                =>  _MODIFY,
-                'delete'                =>  _DELETE,
-                'page'                  =>  _PAGE,
-                'outOf'                 =>  _OUT_OF,
-                'search'                =>  _SEARCH,
-                'recordsPerPage'        =>  _RECORDS_PER_PAGE,
-                'display'               =>  _DISPLAY,
-                'noRecord'              =>  _NO_RECORD,
-                'noResult'              =>  _NO_RESULTS,
-                'available'             =>  _AVAILABLE,
-                'filteredFrom'          =>  _FILTERED_FROM,
-                'records'               =>  _RECORDS,
-                'record'                =>  _RECORD,
-                'first'                 =>  _FIRST,
-                'last'                  =>  _LAST,
-                'next'                  =>  _NEXT,
-                'previous'              =>  _PREVIOUS,
-                'paramCreatedSuccess'   =>  _PARAM_CREATED_SUCCESS,
-                'paramUpdatedSuccess'   =>  _PARAM_UPDATED_SUCCESS,
-                'deleteConfirm'         =>  _DELETE_CONFIRM,
-                'controlTechnicalParams'=>  _CONTROL_TECHNICAL_PARAMS
-        ];
-        return $aLang;
-    }
 
     public static function getSignatureBookLang()
     {
@@ -90,85 +51,5 @@ class LangModelAbstract
 
         return $aLang;
     }
-    public static function getReportsLang()
-    {
-        
-        $aLang = [
-            'folder'               => _FOLDER,
-            'entities'             => _ENTITIES,
-            'mappli'               => _MAARCH_APPLICATION,
-            'group'                => _GROUP,
-            'selectGroup'          => _SELECT_GROUP,
-            'validate'             => _VALIDATE,
-            'cancel'               => _CANCEL
-
-            ];
-
-        return $aLang;
-    }
-
-    public static function getUsersAdministrationLang()
-    {
-        $aLang = [
-            'back'                  => _BASK_BACK,
-            'addUser'               => _ADD_USER,
-            'lastname'              => _LASTNAME,
-            'firstname'             => _FIRSTNAME,
-            'identifier'            => _ID,
-            'status'                => _STATUS,
-            'mail'                  => _MAIL,
-            'edit'                  => _MODIFY,
-            'suspend'               => _SUSPEND,
-            'authorize'             => _AUTHORIZE,
-            'delete'                => _DELETE,
-            'users'                 => _USERS,
-            'admin'                 => _ADMIN,
-            'noResult'              => _NO_RESULTS,
-            'noRecord'              => _NO_RECORD,
-            'previous'              => _PREVIOUS_PAGE,
-            'next'                  => _NEXT_PAGE,
-            'record'                => _RECORD,
-            'search'                => _SEARCH,
-            'deleteMsg'             => _REALLY_DELETE,
-            'suspendMsg'            => _REALLY_SUSPEND,
-            'authorizeMsg'          => _REALLY_AUTHORIZE,
-            'checkListDiffMsg'      => _PLEASE_CHECK_LISTDIFF,
-            'user'                  => _USER,
-            'userModification'      => _ADMIN_USER_MODIFICATION,
-            'reinitPassword'        => _REINITIALIZE_PASSWORD,
-            'manageBaskets'         => _MANAGE_BASKETS,
-            'manageAbsences'        => _MANAGE_ABSENCES,
-            'manageSignatures'      => _MANAGE_SIGNATURES,
-            'primaryEntity'         => _PRIMARY_ENTITY,
-            'secondaryEntity'       => _SECONDARY_ENTITY,
-            'userId'                => _ID,
-            'initials'              => _INITIALS,
-            'phoneNumber'           => _PHONE_NUMBER,
-            'email'                 => _EMAIL,
-            'fingerprint'           => _DIGITAL_FINGERPRINT,
-            'saveModification'      => _SAVE_MODIFICATION,
-            'emailSignatures'       => _EMAIL_SIGNATURES,
-            'sbSignatures'          => _SB_SIGNATURES,
-            'newSignature'          => _DEFINE_NEW_SIGNATURE,
-            'signatureLabel'        => _SIGNATURE_LABEL,
-            'updateSignature'       => _UPDATE_SIGNATURE,
-            'deleteSignature'       => _DELETE_SIGNATURE,
-            'clickOn'               => _CLICK_ON,
-            'toSignature'           => _TO_ADD_SIGNATURE,
-            'toUpdateSignature'     => _TO_UPDATE_SIGNATURE,
-            'validate'              => _VALIDATE,
-            'cancel'                => _CANCEL,
-            'to'                    => _TO,
-            'activateAbs'           => _ACTIVATE_ABSENCE,
-            'deactivateAbs'         => _DEACTIVATE_ABSENCE,
-            'basketToRedirect'      => _CHOOSE_BASKET_TO_REDIRECT,
-            'autoLogout'            => _AUTO_LOGOUT_AFTER_BASKETS_REDIRECTIONS,
-            'abs'                   => _ABS,
-            'active'                => _ACTIVE,
-            'inactive'              => _INACTIVE,
-        ];
-
-        return $aLang;
-    }
 
 }
diff --git a/modules/visa/Controllers/VisaController.php b/modules/visa/Controllers/VisaController.php
index 745a8c6a1a9..fa00e5c7a64 100755
--- a/modules/visa/Controllers/VisaController.php
+++ b/modules/visa/Controllers/VisaController.php
@@ -18,11 +18,11 @@ use Basket\models\BasketModel;
 use Action\models\ActionModel;
 use Core\Models\ContactModel;
 use Core\Models\LinkModel;
-use Core\Models\ListinstanceModel;
 use Core\Models\UserModel;
 use Core\Models\LangModel;
 use Core\Models\DocserverModel;
 use Core\Models\ServiceModel;
+use Listinstance\models\ListinstanceModel;
 use Notes\Models\NoteModel;
 use Psr\Http\Message\RequestInterface;
 use Psr\Http\Message\ResponseInterface;
diff --git a/rest/index.php b/rest/index.php
index 48a4508d433..2d7d70a0ff5 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -207,6 +207,10 @@ $app->put('/currentUser/emailSignature/{id}', \Core\Controllers\UserController::
 $app->delete('/currentUser/emailSignature/{id}', \Core\Controllers\UserController::class . ':deleteCurrentUserEmailSignature');
 $app->put('/currentUser/groups/{groupId}/baskets/{basketId}', \Core\Controllers\UserController::class . ':updateBasketPreference');
 
+//Entities
+$app->get('/entities', \Entity\controllers\EntityController::class . ':get');
+$app->put('/entities/{id}/reassign/{newEntityId}', \Entity\controllers\EntityController::class . ':reassignEntity');
+
 //Parameters
 $app->get('/parameters', \Parameter\controllers\ParameterController::class . ':get');
 $app->post('/parameters', \Parameter\controllers\ParameterController::class . ':create');
@@ -249,7 +253,7 @@ $app->get('/reports/groups/{groupId}', \Report\controllers\ReportController::cla
 $app->put('/reports/groups/{groupId}', \Report\controllers\ReportController::class . ':updateForGroupId');
 
 //Listinstance
-$app->get('/listinstance/{id}', \Core\Controllers\ListinstanceController::class . ':getById');
+$app->get('/listinstance/{id}', \Listinstance\controllers\ListinstanceController::class . ':getById');
 
 //Contacts
 $app->post('/contacts', \Contact\controllers\ContactController::class . ':create');
diff --git a/src/app/basket/models/BasketModelAbstract.php b/src/app/basket/models/BasketModelAbstract.php
index 2671faa8c1f..6da6a6b9a55 100644
--- a/src/app/basket/models/BasketModelAbstract.php
+++ b/src/app/basket/models/BasketModelAbstract.php
@@ -250,6 +250,21 @@ class BasketModelAbstract
         return true;
     }
 
+    public static function updateGroupActionRedirect(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['set', 'where', 'data']);
+        ValidatorModel::arrayType($aArgs, ['set', 'where', 'data']);
+
+        DatabaseModel::delete([
+            'table' => 'groupbasket_redirect',
+            'set'   => $aArgs['set'],
+            'where' => $aArgs['where'],
+            'data'  => $aArgs['data']
+        ]);
+
+        return true;
+    }
+
     public static function deleteGroup(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['id', 'groupId']);
diff --git a/src/app/entity/controllers/EntityController.php b/src/app/entity/controllers/EntityController.php
index 450fbfb4cee..6ccf1bd9e9e 100644
--- a/src/app/entity/controllers/EntityController.php
+++ b/src/app/entity/controllers/EntityController.php
@@ -14,19 +14,69 @@
 
 namespace Entity\controllers;
 
+use Basket\models\BasketModel;
 use Core\Models\ServiceModel;
-use Respect\Validation\Validator;
+use Entity\models\EntityModel;
+use Entity\models\UserEntityModel;
+use Resource\models\ResModel;
 use Slim\Http\Request;
 use Slim\Http\Response;
 
 class EntityController
 {
     public function get(Request $request, Response $response)
+    {
+        $entities = EntityModel::getAllowedEntitiesByUserId(['userId' => $GLOBALS['userId']]);
+        foreach ($entities as $key => $entity) {
+            $entities[$key]['users'] = EntityModel::getUsersById(['id' => $entity['entity_id'], 'select' => ['users.user_id', 'users.firstname', 'users.lastname']]);
+        }
+
+        return $response->withJson(['entities' => $entities]);
+    }
+
+    public function reassignEntity(Request $request, Response $response, array $aArgs)
     {
         if (!ServiceModel::hasService(['id' => 'manage_entities', 'userId' => $GLOBALS['userId'], 'location' => 'entities', 'type' => 'admin'])) {
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
         }
 
-        return $response->withJson(['baskets' => BasketModel::get()]);
+        ResModel::update(['set' => ['destination' => $aArgs['newEntityId']], 'where' => ['destination = ?', 'status != ?'], 'data' => [$aArgs['id'], 'DEL']]);
+
+        $users = UserEntityModel::get(['select' => ['user_id', 'entity_id', 'primary_entity'], 'where' => ['entity_id = ? OR entity_id = ?'], 'data' => [$aArgs['id'], $aArgs['newEntityId']]]);
+        $tmpUsers = [];
+        $doubleUsers = [];
+        foreach ($users as $user) {
+            if (in_array($user['user_id'], $tmpUsers)) {
+                $doubleUsers[] = $user['user_id'];
+            }
+            $tmpUsers[] = $user['user_id'];
+        }
+        foreach ($users as $user) {
+            if (in_array($user['user_id'], $doubleUsers)) {
+                if ($user['entity_id'] == $aArgs['id'] && $user['primary_entity'] == 'N') {
+                    UserEntityModel::delete(['where' => ['user_id = ?', 'entity_id = ?'], 'data' => [$user['user_id'], $aArgs['id']]]);
+                } elseif ($user['entity_id'] == $aArgs['id'] && $user['primary_entity'] == 'Y') {
+                    UserEntityModel::delete(['where' => ['user_id = ?', 'entity_id = ?'], 'data' => [$user['user_id'], $aArgs['newEntityId']]]);
+                }
+            }
+        }
+
+        UserEntityModel::update(['set' => ['entity_id = ?'], 'where' => ['entity_id = ?'], 'data' => [$aArgs['newEntityId'], $aArgs['id']]]);
+
+
+        $entities = EntityModel::get(['select' => ['entity_id', 'parent_entity_id'], 'where' => ['parent_entity_id = ?'], 'data' => [$aArgs['id']]]);
+        foreach ($entities as $entity) {
+            if ($entity['entity_id'] = $aArgs['newEntityId']) {
+                $entityToReplace = EntityModel::getById(['entityId' => $aArgs['id'], 'select' => ['parent_entity_id']]);
+                EntityModel::update(['set' => ['parent_entity_id' => $entityToReplace['parent_entity_id']], 'where' => ['entity_id = ?'], 'data' => [$aArgs['newEntityId']]]);
+            } else {
+                EntityModel::update(['set' => ['parent_entity_id' => $aArgs['newEntityId']], 'where' => ['entity_id = ?'], 'data' => [$entity['entity_id']]]);
+            }
+        }
+
+        BasketModel::updateGroupActionRedirect(['set' => ['entity_id' => $aArgs['newEntityId']], 'where' => ['entity_id = ?'], 'data' => [$aArgs['id']]]);
+        // TODO listinstance
+
+        return $response->withJson(['success' => 'success']);
     }
 }
diff --git a/src/app/entity/models/EntityModelAbstract.php b/src/app/entity/models/EntityModelAbstract.php
index 49f5cf6c2bb..d468ffc8237 100644
--- a/src/app/entity/models/EntityModelAbstract.php
+++ b/src/app/entity/models/EntityModelAbstract.php
@@ -38,28 +38,31 @@ class EntityModelAbstract
     public static function getById(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['entityId']);
+        ValidatorModel::stringType($aArgs, ['entityId']);
 
-        if (is_array($aArgs['entityId'])) {
-            $where = ['entity_id in (?)'];
-        } else {
-            ValidatorModel::stringType($aArgs, ['entityId']);
-            $where = ['entity_id = ?'];
-        }
-
-        $aEntities = DatabaseModel::select([
+        $aEntity = DatabaseModel::select([
             'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
             'table'     => ['entities'],
-            'where'     => $where,
+            'where'     => ['entity_id = ?'],
             'data'      => [$aArgs['entityId']]
         ]);
 
-        if (empty($aEntities[0])) {
-            return [];
-        } elseif (is_array($aArgs['entityId'])) {
-            return $aEntities;
-        } else {
-            return $aEntities[0];
-        }
+        return $aEntity[0];
+    }
+
+    public static function update(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['set', 'where', 'data']);
+        ValidatorModel::arrayType($aArgs, ['set', 'where', 'data']);
+
+        DatabaseModel::delete([
+            'table' => 'entities',
+            'set'   => $aArgs['set'],
+            'where' => $aArgs['where'],
+            'data'  => $aArgs['data']
+        ]);
+
+        return true;
     }
 
     public static function getByEmail(array $aArgs = [])
@@ -129,7 +132,7 @@ class EntityModelAbstract
         return array_unique($entities);
     }
 
-    public static function getAvailableEntitiesForAdministratorByUserId(array $aArgs = [])
+    public static function getAvailableEntitiesForAdministratorByUserId(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['userId', 'administratorUserId']);
         ValidatorModel::stringType($aArgs, ['userId', 'administratorUserId']);
@@ -172,4 +175,56 @@ class EntityModelAbstract
         return $allEntities;
     }
 
+    public static function getAllowedEntitiesByUserId(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['userId']);
+        ValidatorModel::stringType($aArgs, ['userId']);
+
+        if ($aArgs['userId'] == 'superadmin') {
+            $rawEntitiesAllowed = EntityModel::get(['select' => ['entity_id'], 'where' => ['enabled = ?'], 'data' => ['Y'], 'orderBy' => ['entity_label']]);
+            $entitiesAllowed = [];
+            foreach ($rawEntitiesAllowed as $value) {
+                $entitiesAllowed[] = $value['entity_id'];
+            }
+        } else {
+            $entitiesAllowed = EntityModel::getAllEntitiesByUserId(['userId' => $aArgs['userId']]);
+        }
+
+        $allEntities = EntityModel::get(['select' => ['entity_id', 'entity_label', 'parent_entity_id'], 'where' => ['enabled = ?'], 'data' => ['Y'], 'orderBy' => ['entity_label']]);
+
+        foreach ($allEntities as $key => $value) {
+            $allEntities[$key]['id'] = $value['entity_id'];
+            if (empty($value['parent_entity_id'])) {
+                $allEntities[$key]['parent'] = '#';
+                $allEntities[$key]['icon'] = "fa fa-building";
+            } else {
+                $allEntities[$key]['parent'] = $value['parent_entity_id'];
+                $allEntities[$key]['icon'] = "fa fa-sitemap";
+            }
+            if (in_array($value['entity_id'], $entitiesAllowed)) {
+                $allEntities[$key]['allowed'] = true;
+            } else {
+                $allEntities[$key]['allowed'] = false;
+            }
+            $allEntities[$key]['text'] = $value['entity_label'];
+        }
+
+        return $allEntities;
+    }
+
+    public static function getUsersById(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['id']);
+        ValidatorModel::stringType($aArgs, ['id']);
+        ValidatorModel::arrayType($aArgs, ['select']);
+
+        $aUsers = DatabaseModel::select([
+            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
+            'table'     => ['users_entities, users'],
+            'where'     => ['users_entities.entity_id = ?', 'users_entities.user_id = users.user_id'],
+            'data'      => [$aArgs['id']]
+        ]);
+
+        return $aUsers;
+    }
 }
diff --git a/src/app/entity/models/UserEntityModel.php b/src/app/entity/models/UserEntityModel.php
new file mode 100644
index 00000000000..587339d9c97
--- /dev/null
+++ b/src/app/entity/models/UserEntityModel.php
@@ -0,0 +1,19 @@
+<?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 User Entity Model
+* @author dev@maarch.org
+*/
+
+namespace Entity\models;
+
+class UserEntityModel extends UserEntityModelAbstract
+{
+}
diff --git a/src/app/entity/models/UserEntityModelAbstract.php b/src/app/entity/models/UserEntityModelAbstract.php
new file mode 100644
index 00000000000..c46356753f2
--- /dev/null
+++ b/src/app/entity/models/UserEntityModelAbstract.php
@@ -0,0 +1,66 @@
+<?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 User Entity Model Abstract
+* @author dev@maarch.org
+*/
+
+namespace Entity\models;
+
+use Core\Models\DatabaseModel;
+use Core\Models\UserModel;
+use Core\Models\ValidatorModel;
+
+class UserEntityModelAbstract
+{
+    public static function get(array $aArgs = [])
+    {
+        ValidatorModel::notEmpty($aArgs, ['select', 'where', 'data']);
+        ValidatorModel::arrayType($aArgs, ['select', 'where', 'data']);
+
+        $users = DatabaseModel::select([
+            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
+            'table'     => 'users_entities',
+            'where'     => $aArgs['where'],
+            'data'      => $aArgs['data']
+        ]);
+
+        return $users;
+    }
+
+    public static function update(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['set', 'where', 'data']);
+        ValidatorModel::arrayType($aArgs, ['set', 'where', 'data']);
+
+        DatabaseModel::delete([
+            'table' => 'users_entities',
+            'set'   => $aArgs['set'],
+            'where' => $aArgs['where'],
+            'data'  => $aArgs['data']
+        ]);
+
+        return true;
+    }
+
+    public static function delete(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['where', 'data']);
+        ValidatorModel::arrayType($aArgs, ['where', 'data']);
+
+        DatabaseModel::delete([
+            'table' => 'users_entities',
+            'where' => $aArgs['where'],
+            'data'  => $aArgs['data']
+        ]);
+
+        return true;
+    }
+}
diff --git a/core/Controllers/ListinstanceController.php b/src/app/listinstance/controllers/ListinstanceController.php
similarity index 62%
rename from core/Controllers/ListinstanceController.php
rename to src/app/listinstance/controllers/ListinstanceController.php
index 360148e52de..59a5d15c281 100644
--- a/core/Controllers/ListinstanceController.php
+++ b/src/app/listinstance/controllers/ListinstanceController.php
@@ -10,19 +10,18 @@
 /**
  * @brief Listinstance Controller
  * @author dev@maarch.org
- * @ingroup core
  */
 
-namespace Core\Controllers;
+namespace Listinstance\controllers;
 
-use Core\Models\ListinstanceModel;
-use Psr\Http\Message\RequestInterface;
-use Psr\Http\Message\ResponseInterface;
+use Listinstance\models\ListinstanceModel;
+use Slim\Http\Request;
+use Slim\Http\Response;
 
 
 class ListinstanceController
 {
-    public function getById(RequestInterface $request, ResponseInterface $response, $aArgs)
+    public function getById(Request $request, Response $response, array $aArgs)
     {
         $listinstance = ListinstanceModel::getById(['id' => $aArgs['id']]);
 
diff --git a/core/Models/ListinstanceModel.php b/src/app/listinstance/models/ListinstanceModel.php
similarity index 86%
rename from core/Models/ListinstanceModel.php
rename to src/app/listinstance/models/ListinstanceModel.php
index 76bc8c07481..ac71883fea9 100644
--- a/core/Models/ListinstanceModel.php
+++ b/src/app/listinstance/models/ListinstanceModel.php
@@ -10,10 +10,9 @@
 /**
  * @brief Listinstance Model
  * @author dev@maarch.org
- * @ingroup listinstance
  */
 
-namespace Core\Models;
+namespace Listinstance\models;
 
 class ListinstanceModel extends ListinstanceModelAbstract
 {
diff --git a/core/Models/ListinstanceModelAbstract.php b/src/app/listinstance/models/ListinstanceModelAbstract.php
similarity index 81%
rename from core/Models/ListinstanceModelAbstract.php
rename to src/app/listinstance/models/ListinstanceModelAbstract.php
index 3da3f7eef61..8d5a9f23020 100644
--- a/core/Models/ListinstanceModelAbstract.php
+++ b/src/app/listinstance/models/ListinstanceModelAbstract.php
@@ -10,10 +10,12 @@
 /**
  * @brief Listinstance Model Abstract
  * @author dev@maarch.org
- * @ingroup listinstance
  */
 
-namespace Core\Models;
+namespace Listinstance\models;
+
+use Core\Models\DatabaseModel;
+use Core\Models\ValidatorModel;
 
 class ListinstanceModelAbstract
 {
@@ -37,6 +39,21 @@ class ListinstanceModelAbstract
         return $aListinstance[0];
     }
 
+    public static function update(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['set', 'where', 'data']);
+        ValidatorModel::arrayType($aArgs, ['set', 'where', 'data']);
+
+        DatabaseModel::delete([
+            'table' => 'listinstance',
+            'set'   => $aArgs['set'],
+            'where' => $aArgs['where'],
+            'data'  => $aArgs['data']
+        ]);
+
+        return true;
+    }
+
     public static function setSignatory(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['resId', 'signatory', 'userId']);
diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php
index eeba115584f..0272a314cb6 100755
--- a/src/app/resource/controllers/ResController.php
+++ b/src/app/resource/controllers/ResController.php
@@ -141,7 +141,7 @@ class ResController
             return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
         }
 
-        ResModel::update(['resId' => $document['res_id'], 'set' => ['status' => $data['status']]]);
+        ResModel::update(['set' => ['status' => $data['status']], 'where' => ['res_id = ?'], 'data' => [$document['res_id']]]);
 
         HistoryController::add([
             'tableName' => 'res_letterbox',
diff --git a/src/app/resource/models/ResModelAbstract.php b/src/app/resource/models/ResModelAbstract.php
index 476e4759fa0..ab760e43dac 100644
--- a/src/app/resource/models/ResModelAbstract.php
+++ b/src/app/resource/models/ResModelAbstract.php
@@ -102,15 +102,14 @@ class ResModelAbstract
 
     public static function update(array $aArgs)
     {
-        ValidatorModel::notEmpty($aArgs, ['resId', 'set']);
-        ValidatorModel::intVal($aArgs, ['resId']);
-        ValidatorModel::arrayType($aArgs, ['set']);
+        ValidatorModel::notEmpty($aArgs, ['set', 'where', 'data']);
+        ValidatorModel::arrayType($aArgs, ['set', 'where', 'data']);
 
         DatabaseModel::update([
             'table' => 'res_letterbox',
             'set'   => $aArgs['set'],
-            'where' => ['res_id = ?'],
-            'data'  => [$aArgs['resId']]
+            'where' => $aArgs['where'],
+            'data'  => $aArgs['data']
         ]);
 
         return true;
-- 
GitLab