From ab8b9c248e10d2ea2f4ac3bcb8c6c3fea44f2b30 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Wed, 20 Dec 2017 17:50:08 +0100
Subject: [PATCH] [REFACTORING] Docserver + docserverType + convert.sql

---
 .../angular/app/signature-book.component.js   |  14 +-
 core/Controllers/DocserverController.php      | 718 +++---------------
 core/Controllers/DocserverTypeController.php  | 125 +--
 core/Controllers/StoreController.php          |  13 +-
 core/Controllers/UserController.php           |  20 +-
 core/Models/DocserverModelAbstract.php        |  52 +-
 core/Models/DocserverTypeModelAbstract.php    |  37 +-
 .../Controllers/ReconciliationController.php  |   6 +-
 .../Controllers/ProcessConvertController.php  |   4 +-
 modules/convert/convert.sql                   |   4 +-
 modules/visa/Controllers/VisaController.php   |   2 +-
 rest/index.php                                |  12 +-
 12 files changed, 215 insertions(+), 792 deletions(-)

diff --git a/apps/maarch_entreprise/js/angular/app/signature-book.component.js b/apps/maarch_entreprise/js/angular/app/signature-book.component.js
index d1935e010be..6126fd0809d 100755
--- a/apps/maarch_entreprise/js/angular/app/signature-book.component.js
+++ b/apps/maarch_entreprise/js/angular/app/signature-book.component.js
@@ -67,7 +67,8 @@ var SignatureBookComponent = (function () {
         window['angularSignatureBookComponent'] = {
             componentAfterAttach: function (value) { return _this.processAfterAttach(value); },
             componentAfterAction: function () { return _this.processAfterAction(); },
-            componentAfterNotes: function () { return _this.processAfterNotes(); }
+            componentAfterNotes: function () { return _this.processAfterNotes(); },
+            componentAfterLinks: function () { return _this.processAfterLinks(); }
         };
     }
     SignatureBookComponent.prototype.prepareSignatureBook = function () {
@@ -154,6 +155,10 @@ var SignatureBookComponent = (function () {
         var _this = this;
         this.zone.run(function () { return _this.refreshNotes(); });
     };
+    SignatureBookComponent.prototype.processAfterLinks = function () {
+        var _this = this;
+        this.zone.run(function () { return _this.refreshLinks(); });
+    };
     SignatureBookComponent.prototype.processAfterAction = function () {
         var _this = this;
         var idToGo = -1;
@@ -352,6 +357,13 @@ var SignatureBookComponent = (function () {
             _this.signatureBook.nbNotes = data;
         });
     };
+    SignatureBookComponent.prototype.refreshLinks = function () {
+        var _this = this;
+        this.http.get(this.coreUrl + 'rest/links/resId/' + this.resId)
+            .subscribe(function (data) {
+            _this.signatureBook.nbLinks = data.length;
+        });
+    };
     SignatureBookComponent.prototype.signFile = function (attachment, signature) {
         var _this = this;
         if (!this.loadingSign && this.signatureBook.canSign) {
diff --git a/core/Controllers/DocserverController.php b/core/Controllers/DocserverController.php
index 6fb6e2031e6..827e5619b9b 100755
--- a/core/Controllers/DocserverController.php
+++ b/core/Controllers/DocserverController.php
@@ -15,7 +15,6 @@
 
 namespace Core\Controllers;
 
-use Core\Models\CoreConfigModel;
 use Psr\Http\Message\RequestInterface;
 use Psr\Http\Message\ResponseInterface;
 use Respect\Validation\Validator;
@@ -23,628 +22,135 @@ use Core\Models\DocserverModel;
 
 class DocserverController
 {
-    public function getList(RequestInterface $request, ResponseInterface $response)
+    public function get(RequestInterface $request, ResponseInterface $response)
     {
-        $obj = DocserverModel::getList();
-        
-        $datas = [
-            $obj,
-        ];
-        
-        return $response->withJson($datas);
+        return $response->withJson(['docservers' => DocserverModel::get()]);
     }
 
     public function getById(RequestInterface $request, ResponseInterface $response, $aArgs)
     {
-        if (isset($aArgs['docserver_id'])) {
-            $id = $aArgs['docserver_id'];
-            $obj = DocserverModel::getById([
-                'docserver_id' => $id
-            ]);
-        } else {
-            return $response
-                ->withStatus(500)
-                ->withJson(['errors' => _DOCSERVER_ID . ' ' . _IS_EMPTY]);
-        }
-        
-        $datas = [
-            $obj,
-        ];
-
-        return $response->withJson($datas);
-    }
-
-    public function create(RequestInterface $request, ResponseInterface $response, $aArgs)
-    {
-        $errors = [];
-
-        $errors = $this->control($request, 'create');
-
-        if (!empty($errors)) {
-            return $response
-                ->withStatus(500)
-                ->withJson(['errors' => $errors]);
-        }
-
-        $aArgs = $request->getQueryParams();
-
-        $return = DocserverModel::create($aArgs);
-
-        if ($return) {
-            $id = $aArgs['id'];
-            $obj = DocserverModel::getById([
-                'id' => $id
-            ]);
-        } else {
-            return $response
-                ->withStatus(500)
-                ->withJson(['errors' => _NOT_CREATE]);
-        }
-
-        $datas = [
-            $obj,
-        ];
-
-        return $response->withJson($datas);
-    }
-
-    public function update(RequestInterface $request, ResponseInterface $response, $aArgs)
-    {
-        $errors = [];
+        $docserver = DocserverModel::getById(['id' => $aArgs['id']]);
 
-        $errors = $this->control($request, 'update');
-
-        if (!empty($errors)) {
-            return $response
-                ->withStatus(500)
-                ->withJson(['errors' => $errors]);
-        }
-
-        $aArgs = $request->getQueryParams();
-
-        $return = DocserverModel::update($aArgs);
-
-        if ($return) {
-            $id = $aArgs['id'];
-            $obj = DocserverModel::getById([
-                'id' => $id
-            ]);
-        } else {
-            return $response
-                ->withStatus(500)
-                ->withJson(['errors' => _NOT_UPDATE]);
+        if(empty($docserver)){
+            return $response->withStatus(400)->withJson(['errors' => 'Docserver not found']);
         }
 
-        $datas = [
-            $obj,
-        ];
-
-        return $response->withJson($datas);
+        return $response->withJson($docserver);
     }
 
     public function delete(RequestInterface $request, ResponseInterface $response, $aArgs)
     {
-        if (isset($aArgs['id'])) {
-            $id = $aArgs['id'];
-            $obj = DocserverModel::delete([
-                'id' => $id
-            ]);
-        } else {
-            return $response
-                ->withStatus(500)
-                ->withJson(['errors' => _NOT_DELETE]);
-        }
-        
-        $datas = [
-            $obj,
-        ];
-
-        return $response->withJson($datas);
-    }
-
-    protected function control($request, $mode)
-    {
-        $errors = [];
-
-        if ($mode == 'update') {
-            $obj = DocserverModel::getById([
-                'id' => $request->getParam('id')
-            ]);
-            if (empty($obj)) {
-                array_push(
-                    $errors,
-                    _ID . ' ' . $request->getParam('id') . ' ' . _NOT_EXISTS
-                );
-            }
-        }
-
-        if (!Validator::notEmpty()->validate($request->getParam('id'))) {
-            array_push($errors, _ID . ' ' . _IS_EMPTY);
-        } elseif ($mode == 'create') {
-            $obj = DocserverModel::getById([
-                'id' => $request->getParam('id')
-            ]);
-            if (!empty($obj)) {
-                array_push(
-                    $errors,
-                    _ID . ' ' . $obj[0]['id'] . ' ' . _ALREADY_EXISTS
-                );
-            }
-        }
-
-        if (!Validator::regex('/^[\w.-]*$/')->validate($request->getParam('id'))) {
-            array_push($errors, _ID . ' ' . _NOT . ' ' . _VALID);
-        }
-
-        if (!Validator::notEmpty()
-                ->validate($request->getParam('label_status'))) {
-            array_push($errors, _LABEL_STATUS . ' ' . _IS_EMPTY);
-        }
-
-        if (Validator::notEmpty()
-                ->validate($request->getParam('is_system')) &&
-            !Validator::contains('Y')
-                ->validate($request->getParam('is_system')) &&
-            !Validator::contains('N')
-                ->validate($request->getParam('is_system'))
-        ) {
-            array_push($errors, _IS_SYSTEM . ' ' . _NOT . ' ' . _VALID);
-        }
-
-        if (Validator::notEmpty()
-                ->validate($request->getParam('is_folder_status')) &&
-            !Validator::contains('Y')
-                ->validate($request->getParam('is_folder_status')) &&
-            !Validator::contains('N')
-                ->validate($request->getParam('is_folder_status'))
-        ) {
-            array_push($errors, _IS_FOLDER_STATUS . ' ' . _NOT . ' ' . _VALID);
-        }
-
-        if (Validator::notEmpty()
-                ->validate($request->getParam('img_filename')) &&
-            (!Validator::regex('/^[\w-.]+$/')
-                ->validate($request->getParam('img_filename')) ||
-            !Validator::length(null, 255)
-                ->validate($request->getParam('img_filename')))
-        ) {
-            array_push($errors, _IMG_FILENAME . ' ' . _NOT . ' ' . _VALID);
-        }
-
-        if (Validator::notEmpty()
-                ->validate($request->getParam('maarch_module')) &&
-            !Validator::length(null, 255)
-                ->validate($request->getParam('maarch_module'))
-        ) {
-            array_push($errors, _MAARCH_MODULE . ' ' . _NOT . ' ' . _VALID);
-        }
-
-        if (Validator::notEmpty()
-                ->validate($request->getParam('can_be_searched')) &&
-            !Validator::contains('Y')
-                ->validate($request->getParam('can_be_searched')) &&
-            !Validator::contains('N')
-                ->validate($request->getParam('can_be_searched'))
-        ) {
-            array_push($errors, _CAN_BE_SEARCHED . ' ' . _NOT . ' ' . _VALID);
-        }
-
-        if (Validator::notEmpty()
-                ->validate($request->getParam('can_be_modified')) &&
-            !Validator::contains('Y')
-                ->validate($request->getParam('can_be_modified')) &&
-            !Validator::contains('N')
-                ->validate($request->getParam('can_be_modified'))
-        ) {
-            array_push($errors, _CAN_BE_MODIFIED . ' ' . _NOT . ' ' . _VALID);
-        }
-
-        return $errors;
-    }
-
-    /**
-     * Get docservers to insert a new doc.
-     * Can return null if no corresponding object.
-     * @param  $coll_id  string Collection identifier
-     * @return docservers
-     */
-    public function getDocserverToInsert($aArgs)
-    {
-        if (isset($aArgs['collId'])) {
-            $collId = $aArgs['collId'];
-            $obj = DocserverModel::getDocserverToInsert([
-                'collId' => $collId
-            ]);
-        } else {
-            return ['errors' => 'collId ' . _EMPTY];
-        }
-
-        $datas = $obj;
-
-        return $datas;
-    }
+        //TODO Droit de suppression
+        $docserver = DocserverModel::getById(['id' => $aArgs['id']]);
 
-    /**
-    * Checks the size of the docserver plus a new file to see
-    * if there is enough disk space
-    *
-    * @param  $docserver docservers object
-    * @param  $filesize integer File size
-    * @return integer New docserver size or 0 if not enough disk space available
-    */
-    public function checkSize($aArgs)
-    {
-        $newDsSize = $aArgs['docserver']['actual_size_number'] + $aArgs['filesize'];
-
-        if (empty($aArgs['docserver']['actual_size_number'])) {
-            $datas = [
-                    'errors' => 'actual_size_number' . _EMPTY,
-            ];
-
-            return $datas;
+        if(empty($docserver)){
+            return $response->withStatus(400)->withJson(['errors' => 'Docserver does not exist']);
         }
 
-        if (empty($aArgs['filesize'])) {
-            $datas = [
-                    'errors' => 'filesize' . _EMPTY,
-            ];
+        DocserverModel::delete(['id' => $aArgs['id']]);
 
-            return $datas;
-        }
-        
-        if ($aArgs['docserver']['size_limit_number'] > 0 &&
-            $newDsSize >= $aArgs['docserver']['size_limit_number']
-        ) {
-            $datas = [
-                'newDsSize' => 0,
-            ];
-        } else {
-            $datas = [
-                'newDsSize' => $newDsSize,
-            ];
-        }
-
-        return $datas;
+        return $response->withJson(['docservers' => DocserverModel::get()]);
     }
 
-    /**
-    * Sets the size of the docserver
-    * @param $docserver docservers object
-    * @param $newSize integer New size of the docserver
-    */
-    public function setSize($aArgs)
-    {
-        if (empty($aArgs['docserver_id'])) {
-            return ['errors' => 'docserver_id ' . _EMPTY];
-        }
-
-        if (empty($aArgs['actual_size_number'])) {
-            return ['errors' => 'actual_size_number ' . _EMPTY];
-        }
-
-        //$obj = DocserverModel::setSize($aArgs);
-        $return = DocserverModel::update($aArgs);
-        
-        $datas = [
-            'setSize' => true,
-        ];
 
-        return $datas;
-    }
-
-
-    /**
-    * Calculates the next file name in the docserver
-    * @param $pathOnDocserver docservers path
-    * @return array Contains 3 items :
-    * subdirectory path and new filename and error
-    */
-    public function getNextFileNameInDocserver($aArgs)
-    {
-        if (empty($aArgs['pathOnDocserver'])) {
-            $datas = [
-                    'errors' => 'pathOnDocserver ' . _EMPTY,
-            ];
-
-            return $datas;
-        }
-
-        if (!is_dir($aArgs['pathOnDocserver'])) {
-            $datas = [
-                    'errors' => 'pathOnDocserver ' . _NOT_EXISTS,
-            ];
-
-            return $datas;
-        }
-
-        $pathOnDocserver = $aArgs['pathOnDocserver'];
-
-        $dsTools = new \Core\Controllers\DocserverToolsController();
-
-        umask(0022);
-        //Scans the docserver path
-        $fileTab = scandir($pathOnDocserver);
-        //Removes . and .. lines
-        array_shift($fileTab);
-        array_shift($fileTab);
-
-        if (file_exists($pathOnDocserver . DIRECTORY_SEPARATOR . 'package_information')) {
-            unset($fileTab[array_search('package_information', $fileTab)]);
-        }
-        
-        if (is_dir($pathOnDocserver . DIRECTORY_SEPARATOR . 'BATCH')) {
-            unset($fileTab[array_search('BATCH', $fileTab)]);
-        }
-
-        $nbFiles = count($fileTab);
-        //Docserver is empty
-        if ($nbFiles == 0) {
-            //Creates the directory
-            if (!mkdir($pathOnDocserver . '0001', 0770)) {
-                $datas = [
-                    'errors' => 'Pb to create directory on the docserver:'
-                        . $pathOnDocserver,
-                ];
-
-                return $datas;
-            } else {
-                $dsTools->setRights(
-                    ['path' => $pathOnDocserver . '0001' . DIRECTORY_SEPARATOR]
-                );
-                $destinationDir = $pathOnDocserver . '0001'
-                                . DIRECTORY_SEPARATOR;
-                $fileDestinationName = '0001';
-                $fileDestinationName = $fileDestinationName . '_' . mt_rand();
-                $datas = [
-                    'destinationDir' => $destinationDir,
-                    'fileDestinationName' => $fileDestinationName,
-                ];
-
-                return $datas;
-            }
-        } else {
-            //Gets next usable subdirectory in the docserver
-            $destinationDir = $pathOnDocserver
-                . str_pad(
-                    count($fileTab),
-                    4,
-                    '0',
-                    STR_PAD_LEFT
-                )
-                . DIRECTORY_SEPARATOR;
-            $fileTabBis = scandir(
-                $pathOnDocserver
-                . strval(str_pad(count($fileTab), 4, '0', STR_PAD_LEFT))
-            );
-            //Removes . and .. lines
-            array_shift($fileTabBis);
-            array_shift($fileTabBis);
-            $nbFilesBis = count($fileTabBis);
-            //If number of files => 1000 then creates a new subdirectory
-            if ($nbFilesBis >= 1000) {
-                $newDir = ($nbFiles) + 1;
-                if (!mkdir(
-                    $pathOnDocserver
-                    . str_pad($newDir, 4, '0', STR_PAD_LEFT),
-                    0770
-                )
-                ) {
-                    $datas = [
-                        'errors' => 'Pb to create directory on the docserver:'
-                        . $pathOnDocserver
-                        . str_pad($newDir, 4, '0', STR_PAD_LEFT),
-                    ];
-
-                    return $datas;
-                } else {
-                    $dsTools->setRights(
-                        [
-                            'path' => $pathOnDocserver
-                                . str_pad($newDir, 4, '0', STR_PAD_LEFT)
-                                . DIRECTORY_SEPARATOR
-                        ]
-                    );
-                    $destinationDir = $pathOnDocserver
-                        . str_pad($newDir, 4, '0', STR_PAD_LEFT)
-                        . DIRECTORY_SEPARATOR;
-                    $fileDestinationName = '0001';
-                    $fileDestinationName = $fileDestinationName . '_' . mt_rand();
-                    $datas = [
-                        'destinationDir' => $destinationDir,
-                        'fileDestinationName' => $fileDestinationName,
-                    ];
-
-                    return $datas;
-                }
-            } else {
-                //Docserver contains less than 1000 files
-                $newFileName = $nbFilesBis + 1;
-                $greater = $newFileName;
-                for ($n = 0; $n < count($fileTabBis); $n++) {
-                    $currentFileName = array();
-                    $currentFileName = explode('.', $fileTabBis[$n]);
-                    if ((int) $greater <= (int) $currentFileName[0]) {
-                        if ((int) $greater == (int) $currentFileName[0]) {
-                            $greater ++;
-                        } else {
-                            //$greater < current
-                            $greater = (int) $currentFileName[0] + 1;
-                        }
-                    }
-                }
-                $fileDestinationName = str_pad($greater, 4, '0', STR_PAD_LEFT);
-                $fileDestinationName = $fileDestinationName . '_' . mt_rand();
-                $datas = [
-                    'destinationDir' => $destinationDir,
-                    'fileDestinationName' => $fileDestinationName,
-                ];
-
-                return $datas;
-            }
-        }
-    }
-
-    /**
-     * Store a new doc in a docserver.
-     * @param   $collId collection resource
-     * @param   $fileInfos infos of the doc to store, contains :
-     *          tmpDir : path to tmp directory
-     *          size : size of the doc
-     *          format : format of the doc
-     *          tmpFileName : file name of the doc in Maarch tmp directory
-     * @return  array of docserver data for res_x else return error
-     */
-    public function storeResourceOnDocserver($aArgs)
-    {
-        if (empty($aArgs['collId'])) {
-            return ['errors' => 'collId ' . _EMPTY];
-        }
-
-        if (empty($aArgs['fileInfos'])) {
-            return ['errors' => 'fileInfos ' . _EMPTY];
-        }
-
-        if (empty($aArgs['fileInfos']['tmpDir'])) {
-            return ['errors' => 'fileInfos.tmpDir ' . _EMPTY];
-        }
-
-        if (empty($aArgs['fileInfos']['size'])) {
-            return ['errors' => 'fileInfos.size ' . _EMPTY];
-        }
-
-        if (empty($aArgs['fileInfos']['format'])) {
-            return ['errors' => 'fileInfos.format ' . _EMPTY];
-        }
-
-        if (empty($aArgs['fileInfos']['tmpFileName'])) {
-            return ['errors' => 'fileInfos.tmpFileName ' . _EMPTY];
-        }
-
-        if (!is_dir($aArgs['fileInfos']['tmpDir'])) {
-            return ['errors' => 'fileInfos.tmpDir ' . _NOT_EXISTS];
-        }
-
-        if (!file_exists($aArgs['fileInfos']['tmpDir']
-            . $aArgs['fileInfos']['tmpFileName'])) {
-            return ['errors' => 'fileInfos.tmpDir fileInfos.tmpFileName' . _NOT_EXISTS];
-        }
-
-        $collId = $aArgs['collId'];
-        $fileInfos = $aArgs['fileInfos'];
-        $size = $aArgs['fileInfos']['size'];
-        $tmpDir = $aArgs['fileInfos']['tmpDir'];
-
-        $dsTools = new \Core\Controllers\DocserverToolsController();
-
-        $docserver = $this->getDocserverToInsert(['collId' => $collId]);
-        $docserver = $docserver[0];
-
-        $tmpSourceCopy = '';
-        
-        if (empty($docserver)) {
-            return [
-                'errors' => _DOCSERVER_ERROR . ' : '
-                    . _NO_AVAILABLE_DOCSERVER . ' .  ' . _MORE_INFOS
-            ];
-        }
-
-        $newSize = $this->checkSize(
-            [
-                'docserver' => $docserver,
-                'filesize' => $size,
-            ]
-        );
-
-        if ($newSize['newDsSize'] == 0) {
-            return [
-                'errors' => _DOCSERVER_ERROR . ' : '
-                . _NOT_ENOUGH_DISK_SPACE . ' .  ' . _MORE_INFOS
-            ];
-        }
-
-        if ($tmpDir == '') {
-            $tmp = CoreConfigModel::getTmpPath();
-        } else {
-            $tmp = $tmpDir;
-        }
-
-        $d = dir($tmp);
-        $pathTmp = $d->path;
-        while ($entry = $d->read()) {
-            if ($entry == $fileInfos['tmpFileName']) {
-                $tmpSourceCopy = $pathTmp . $entry;
-                $theFile = $entry;
-                break;
-            }
-        }
-        $d->close();
-
-        $pathOnDocserver = array();
-        $pathOnDocserver = $dsTools->createPathOnDocServer(
-            ['path' => $docserver['path_template']]
-        );
-
-        $docinfo = $this->getNextFileNameInDocserver(
-            ['pathOnDocserver' => $pathOnDocserver['createPathOnDocServer']['destinationDir']]
-        );
-
-        if ($docinfo['errors'] <> '') {
-            return ['errors' => _FILE_SEND_ERROR];
-        }
-
-        $docserverTypeControler = new \Core\Models\DocserverTypeModel();
-        $docserverTypeObject = $docserverTypeControler->getById(
-            ['docserver_type_id' => $docserver['docserver_type_id']]
-        );
-
-        $docserverTypeObject = $docserverTypeObject[0];
-
-        $pathInfoTmpSrc = pathinfo($tmpSourceCopy);
-
-        $docinfo['fileDestinationName'] .= '.'
-            . strtolower($pathInfoTmpSrc['extension']);
-
-        $copyResult = $dsTools->copyOnDocserver(
-            [
-                'sourceFilePath'             => $tmpSourceCopy,
-                'destinationDir'             => $docinfo['destinationDir'],
-                'fileDestinationName'        => $docinfo['fileDestinationName'],
-                'docserverSourceFingerprint' => $docserverTypeObject['fingerprint_mode'],
-            ]
-        );
-
-        if (isset($copyResult['errors']) && $copyResult['errors'] <> '') {
-            return ['errors' => $copyResult['errors']];
-        }
-
-        $destinationDir = $copyResult['copyOnDocserver']['destinationDir'];
-        $fileDestinationName = $copyResult['copyOnDocserver']['fileDestinationName'];
-        
-        $destinationDir = substr(
-            $destinationDir,
-            strlen($docserver['path_template'])
-        ) . DIRECTORY_SEPARATOR;
-        
-        $destinationDir = str_replace(
-            DIRECTORY_SEPARATOR,
-            '#',
-            $destinationDir
-        );
-
-        $this->setSize(
-            [
-                'docserver_id' => $docserver['docserver_id'],
-                'actual_size_number' => $newSize['newDsSize']
-            ]
-        );
-
-        $datas = [
-            'path_template' => $docserver['path_template'],
-            'destination_dir' => $destinationDir,
-            'docserver_id' => $docserver['docserver_id'],
-            'file_destination_name' => $fileDestinationName,
-        ];
-
-        return $datas;
-    }
+    // Modèle pour le create et update
+//    protected function control($request, $mode)
+//    {
+//        $errors = [];
+//
+//        if ($mode == 'update') {
+//            $obj = DocserverModel::getById([
+//                'id' => $request->getParam('id')
+//            ]);
+//            if (empty($obj)) {
+//                array_push(
+//                    $errors,
+//                    _ID . ' ' . $request->getParam('id') . ' ' . _NOT_EXISTS
+//                );
+//            }
+//        }
+//
+//        if (!Validator::notEmpty()->validate($request->getParam('id'))) {
+//            array_push($errors, _ID . ' ' . _IS_EMPTY);
+//        } elseif ($mode == 'create') {
+//            $obj = DocserverModel::getById([
+//                'id' => $request->getParam('id')
+//            ]);
+//            if (!empty($obj)) {
+//                array_push(
+//                    $errors,
+//                    _ID . ' ' . $obj[0]['id'] . ' ' . _ALREADY_EXISTS
+//                );
+//            }
+//        }
+//
+//        if (!Validator::regex('/^[\w.-]*$/')->validate($request->getParam('id'))) {
+//            array_push($errors, _ID . ' ' . _NOT . ' ' . _VALID);
+//        }
+//
+//        if (!Validator::notEmpty()
+//                ->validate($request->getParam('label_status'))) {
+//            array_push($errors, _LABEL_STATUS . ' ' . _IS_EMPTY);
+//        }
+//
+//        if (Validator::notEmpty()
+//                ->validate($request->getParam('is_system')) &&
+//            !Validator::contains('Y')
+//                ->validate($request->getParam('is_system')) &&
+//            !Validator::contains('N')
+//                ->validate($request->getParam('is_system'))
+//        ) {
+//            array_push($errors, _IS_SYSTEM . ' ' . _NOT . ' ' . _VALID);
+//        }
+//
+//        if (Validator::notEmpty()
+//                ->validate($request->getParam('is_folder_status')) &&
+//            !Validator::contains('Y')
+//                ->validate($request->getParam('is_folder_status')) &&
+//            !Validator::contains('N')
+//                ->validate($request->getParam('is_folder_status'))
+//        ) {
+//            array_push($errors, _IS_FOLDER_STATUS . ' ' . _NOT . ' ' . _VALID);
+//        }
+//
+//        if (Validator::notEmpty()
+//                ->validate($request->getParam('img_filename')) &&
+//            (!Validator::regex('/^[\w-.]+$/')
+//                ->validate($request->getParam('img_filename')) ||
+//            !Validator::length(null, 255)
+//                ->validate($request->getParam('img_filename')))
+//        ) {
+//            array_push($errors, _IMG_FILENAME . ' ' . _NOT . ' ' . _VALID);
+//        }
+//
+//        if (Validator::notEmpty()
+//                ->validate($request->getParam('maarch_module')) &&
+//            !Validator::length(null, 255)
+//                ->validate($request->getParam('maarch_module'))
+//        ) {
+//            array_push($errors, _MAARCH_MODULE . ' ' . _NOT . ' ' . _VALID);
+//        }
+//
+//        if (Validator::notEmpty()
+//                ->validate($request->getParam('can_be_searched')) &&
+//            !Validator::contains('Y')
+//                ->validate($request->getParam('can_be_searched')) &&
+//            !Validator::contains('N')
+//                ->validate($request->getParam('can_be_searched'))
+//        ) {
+//            array_push($errors, _CAN_BE_SEARCHED . ' ' . _NOT . ' ' . _VALID);
+//        }
+//
+//        if (Validator::notEmpty()
+//                ->validate($request->getParam('can_be_modified')) &&
+//            !Validator::contains('Y')
+//                ->validate($request->getParam('can_be_modified')) &&
+//            !Validator::contains('N')
+//                ->validate($request->getParam('can_be_modified'))
+//        ) {
+//            array_push($errors, _CAN_BE_MODIFIED . ' ' . _NOT . ' ' . _VALID);
+//        }
+//
+//        return $errors;
+//    }
 }
diff --git a/core/Controllers/DocserverTypeController.php b/core/Controllers/DocserverTypeController.php
index 623c50104aa..dcfe92939bb 100755
--- a/core/Controllers/DocserverTypeController.php
+++ b/core/Controllers/DocserverTypeController.php
@@ -22,133 +22,34 @@ use Core\Models\DocserverTypeModel;
 
 class DocserverTypeController
 {
-    public function getList(RequestInterface $request, ResponseInterface $response)
+    public function get(RequestInterface $request, ResponseInterface $response)
     {
-        $obj = DocserverTypeModel::getList();
-        
-        $datas = [
-            [
-                'DocserverType' => $obj,
-            ]
-        ];
-        
-        return $response->withJson($datas);
+        return $response->withJson(['docserverTypes' => DocserverTypeModel::get()]);
     }
 
     public function getById(RequestInterface $request, ResponseInterface $response, $aArgs)
     {
-        if (isset($aArgs['docserver_type_id'])) {
-            $id = $aArgs['docserver_type_id'];
-            $obj = DocserverTypeModel::getById([
-                'docserver_type_id' => $id
-            ]);
-        } else {
-            return $response
-                ->withStatus(500)
-                ->withJson(['errors' => _DOCSERVER_TYPE_ID . ' ' . _IS_EMPTY]);
-        }
-        
-        $datas = [
-            [
-                'DocserverType' => $obj,
-            ]
-        ];
-
-        return $response->withJson($datas);
-    }
-
-    public function create(RequestInterface $request, ResponseInterface $response, $aArgs)
-    {
-        $errors = [];
-
-        $errors = $this->control($request, 'create');
+        $docserverType = DocserverTypeModel::getById(['id' => $aArgs['id']]);
 
-        if (!empty($errors)) {
-            return $response
-                ->withStatus(500)
-                ->withJson(['errors' => $errors]);
+        if(empty($docserverType)){
+            return $response->withStatus(400)->withJson(['errors' => 'Docserver Type not found']);
         }
 
-        $aArgs = $request->getQueryParams();
-
-        $return = DocserverTypeModel::create($aArgs);
-
-        if ($return) {
-            $id = $aArgs['docserver_type_id'];
-            $obj = DocserverTypeModel::getById([
-                'docserver_type_id' => $id
-            ]);
-        } else {
-            return $response
-                ->withStatus(500)
-                ->withJson(['errors' => _NOT_CREATE]);
-        }
-
-        $datas = [
-            [
-                'DocserverType' => $obj,
-            ]
-        ];
-
-        return $response->withJson($datas);
+        return $response->withJson($docserverType);
     }
 
-    public function update(RequestInterface $request, ResponseInterface $response, $aArgs)
+    public function delete(RequestInterface $request, ResponseInterface $response, $aArgs)
     {
-        $errors = [];
+        //TODO Droit de suppression
+        $docserverType = DocserverTypeModel::getById(['id' => $aArgs['id']]);
 
-        $errors = $this->control($request, 'update');
-
-        if (!empty($errors)) {
-            return $response
-                ->withStatus(500)
-                ->withJson(['errors' => $errors]);
+        if(empty($docserverType)){
+            return $response->withStatus(400)->withJson(['errors' => 'Docserver Type does not exist']);
         }
 
-        $aArgs = $request->getQueryParams();
-
-        $return = DocserverTypeModel::update($aArgs);
-
-        if ($return) {
-            $id = $aArgs['docserver_type_id'];
-            $obj = DocserverTypeModel::getById([
-                'docserver_type_id' => $id
-            ]);
-        } else {
-            return $response
-                ->withStatus(500)
-                ->withJson(['errors' => _NOT_UPDATE]);
-        }
+        DocserverTypeModel::delete(['id' => $aArgs['id']]);
 
-        $datas = [
-            [
-                'DocserverType' => $obj,
-            ]
-        ];
-
-        return $response->withJson($datas);
-    }
-
-    public function delete(RequestInterface $request, ResponseInterface $response, $aArgs)
-    {
-        if (isset($aArgs['docserver_type_id'])) {
-            $id = $aArgs['docserver_type_id'];
-            $obj = DocserverTypeModel::delete([
-                'docserver_type_id' => $id
-            ]);
-        } else {
-            return $response
-                ->withStatus(500)
-                ->withJson(['errors' => _NOT_DELETE]);
-        }
-        
-        $datas = [
-            [
-                'DocserverType' => $obj,
-            ]
-        ];
-
-        return $response->withJson($datas);
+        return $response->withJson(['docserverTypes' => DocserverTypeModel::get()]);
     }
 
     protected function control($request, $mode)
diff --git a/core/Controllers/StoreController.php b/core/Controllers/StoreController.php
index d477366702e..a6303b1b131 100644
--- a/core/Controllers/StoreController.php
+++ b/core/Controllers/StoreController.php
@@ -90,15 +90,11 @@ class StoreController
     {
         ValidatorModel::notEmpty($aArgs, ['collId', 'fileInfos']);
         ValidatorModel::arrayType($aArgs, ['fileInfos']);
-        ValidatorModel::stringType($aArgs, ['collId']);
+        ValidatorModel::stringType($aArgs, ['collId', 'docserverTypeId']);
         ValidatorModel::notEmpty($aArgs['fileInfos'], ['tmpDir', 'size', 'format', 'tmpFileName']);
         ValidatorModel::stringType($aArgs['fileInfos'], ['tmpDir', 'format', 'tmpFileName']);
         ValidatorModel::intVal($aArgs['fileInfos'], ['size']);
 
-        if (empty($aArgs['docserverTypeId'])) {
-            $aArgs['docserverTypeId'] = 'DOC';
-        }
-
         if (!is_dir($aArgs['fileInfos']['tmpDir'])) {
             return ['errors' => '[storeRessourceOnDocserver] FileInfos.tmpDir does not exist'];
         }
@@ -106,9 +102,8 @@ class StoreController
             return ['errors' => '[storeRessourceOnDocserver] FileInfos.tmpFileName does not exist'];
         }
 
-        $docserver = DocserverModel::getDocserverToInsert(
-            ['collId' => $aArgs['collId'], 'typeId' => $aArgs['docserverTypeId']]
-        )[0];
+        $aArgs['docserverTypeId'] = empty($aArgs['docserverTypeId']) ? 'DOC' : $aArgs['docserverTypeId'];
+        $docserver = DocserverModel::getDocserverToInsert(['collId' => $aArgs['collId'], 'typeId' => $aArgs['docserverTypeId']]);
         if (empty($docserver)) {
             return ['errors' => '[storeRessourceOnDocserver] No available Docserver'];
         }
@@ -125,7 +120,7 @@ class StoreController
         $pathInfoOnTmp = pathinfo($aArgs['fileInfos']['tmpDir'] . $aArgs['fileInfos']['tmpFileName']);
         $docinfo['fileDestinationName'] .= '.' . strtolower($pathInfoOnTmp['extension']);
 
-        $docserverTypeObject = DocserverTypeModel::getById(['docserver_type_id' => $docserver['docserver_type_id']])[0];
+        $docserverTypeObject = DocserverTypeModel::getById(['id' => $docserver['docserver_type_id']]);
         $copyResult = StoreController::copyOnDocServer([
             'sourceFilePath'             => $aArgs['fileInfos']['tmpDir'] . $aArgs['fileInfos']['tmpFileName'],
             'destinationDir'             => $docinfo['destinationDir'],
diff --git a/core/Controllers/UserController.php b/core/Controllers/UserController.php
index fe0cc41b1c5..525e81360c6 100755
--- a/core/Controllers/UserController.php
+++ b/core/Controllers/UserController.php
@@ -28,8 +28,6 @@ use Psr\Http\Message\RequestInterface;
 use Psr\Http\Message\ResponseInterface;
 use Respect\Validation\Validator;
 
-include_once 'core/class/docservers_controler.php';
-
 
 class UserController
 {
@@ -386,16 +384,16 @@ class UserController
 
         file_put_contents(CoreConfigModel::getTmpPath() . $tmpName, $file);
 
-        $docservers_controler = new \docservers_controler();
-        $storeInfos = $docservers_controler->storeResourceOnDocserver(
-            'templates',
-            [
-                'tmpDir'      => CoreConfigModel::getTmpPath(),
-                'size'        => $data['size'],
-                'format'      => $ext,
-                'tmpFileName' => $tmpName
+        $storeInfos = StoreController::storeResourceOnDocServer([
+            'collId'            => 'templates',
+            'docserverTypeId'   => 'TEMPLATES',
+            'fileInfos'         => [
+                'tmpDir'        => CoreConfigModel::getTmpPath(),
+                'size'          => $data['size'],
+                'format'        => $ext,
+                'tmpFileName'   => $tmpName,
             ]
-        );
+        ]);
 
         if (!file_exists($storeInfos['path_template']. str_replace('#', '/', $storeInfos['destination_dir']) .$storeInfos['file_destination_name'])) {
             return $response->withStatus(500)->withJson(['errors' => $storeInfos['error'] .' templates']);
diff --git a/core/Models/DocserverModelAbstract.php b/core/Models/DocserverModelAbstract.php
index d57ae343c4e..7acd1ba5b3c 100755
--- a/core/Models/DocserverModelAbstract.php
+++ b/core/Models/DocserverModelAbstract.php
@@ -17,29 +17,35 @@ namespace Core\Models;
 
 class DocserverModelAbstract
 {
-    public static function getList()
+    public static function get(array $aArgs = [])
     {
-        $aReturn = DatabaseModel::select([
+        ValidatorModel::arrayType($aArgs, ['select']);
+
+        $aDocservers = DatabaseModel::select([
             'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
             'table'     => ['docservers'],
         ]);
 
-        return $aReturn;
+        return $aDocservers;
     }
 
     public static function getById(array $aArgs = [])
     {
-        ValidatorModel::notEmpty($aArgs, ['docserver_id']);
-        ValidatorModel::stringType($aArgs, ['docserver_id']);
+        ValidatorModel::notEmpty($aArgs, ['id']);
+        ValidatorModel::stringType($aArgs, ['id']);
 
-        $aReturn = DatabaseModel::select([
+        $aDocserver = DatabaseModel::select([
             'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
             'table'     => ['docservers'],
             'where'     => ['docserver_id = ?'],
-            'data'      => [$aArgs['docserver_id']]
+            'data'      => [$aArgs['id']]
         ]);
 
-        return $aReturn;
+        if (empty($aDocserver[0])) {
+            return [];
+        }
+
+        return $aDocserver[0];
     }
 
     public static function getByTypeId(array $aArgs = [])
@@ -109,15 +115,15 @@ class DocserverModelAbstract
         return true;
     }
 
-    public static function delete(array $aArgs = [])
+    public static function delete(array $aArgs)
     {
-        ValidatorModel::notEmpty($aArgs, ['docserver_id']);
-        ValidatorModel::stringType($aArgs, ['docserver_id']);
+        ValidatorModel::notEmpty($aArgs, ['id']);
+        ValidatorModel::stringType($aArgs, ['id']);
 
         DatabaseModel::delete([
             'table'     => 'docservers',
             'where'     => ['docserver_id = ?'],
-            'data'      => [$aArgs['docserver_id']]
+            'data'      => [$aArgs['id']]
         ]);
 
         return true;
@@ -127,11 +133,10 @@ class DocserverModelAbstract
     /**
      * Get docservers to insert a new doc.
      * Can return null if no corresponding object.
-     * @param  $collId  string Collection identifier
-     * @param  string $typeId [description]
-     * @return docservers 
+     * @param  $aArgs array
+     * @return array
      */
-    public static function getDocserverToInsert(array $aArgs = [])
+    public static function getDocserverToInsert(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['collId']);
         ValidatorModel::stringType($aArgs, ['collId']);
@@ -140,18 +145,19 @@ class DocserverModelAbstract
             $aArgs['typeId'] = 'DOC';
         }
 
-        $aReturn = DatabaseModel::select([
+        $aDocserver = DatabaseModel::select([
             'select'    => ['*'],
             'table'     => ['docservers'],
-            'where'     => [
-                "is_readonly = 'N' and enabled = 'Y' and " . 
-                "coll_id = ? and docserver_type_id = ?"
-                        ],
-            'data'      => [$aArgs['collId'], $aArgs['typeId']],
+            'where'     => ['is_readonly = ?', 'enabled = ?', 'coll_id = ?', 'docserver_type_id = ?'],
+            'data'      => ['N', 'Y', $aArgs['collId'], $aArgs['typeId']],
             'order_by'  => ['priority_number'],
             'limit'     => 1,
         ]);
 
-        return $aReturn;
+        if (empty($aDocserver[0])) {
+            return [];
+        }
+
+        return $aDocserver[0];
     }
 }
diff --git a/core/Models/DocserverTypeModelAbstract.php b/core/Models/DocserverTypeModelAbstract.php
index 2ef070edb0c..d16e99acee7 100755
--- a/core/Models/DocserverTypeModelAbstract.php
+++ b/core/Models/DocserverTypeModelAbstract.php
@@ -17,30 +17,35 @@ namespace Core\Models;
 
 class DocserverTypeModelAbstract
 {
-    public static function getList()
+    public static function get(array $aArgs = [])
     {
-        $aReturn = DatabaseModel::select([
+        ValidatorModel::arrayType($aArgs, ['select']);
+
+        $aDocserverTypes = DatabaseModel::select([
             'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
             'table'     => ['docserver_types'],
         ]);
 
-        return $aReturn;
+        return $aDocserverTypes;
     }
 
-    public static function getById(array $aArgs = [])
+    public static function getById(array $aArgs)
     {
-        ValidatorModel::notEmpty($aArgs, ['docserver_type_id']);
-        ValidatorModel::stringType($aArgs, ['docserver_type_id']);
-
+        ValidatorModel::notEmpty($aArgs, ['id']);
+        ValidatorModel::stringType($aArgs, ['id']);
 
-        $aReturn = DatabaseModel::select([
+        $aDocserverType = DatabaseModel::select([
             'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
             'table'     => ['docserver_types'],
             'where'     => ['docserver_type_id = ?'],
-            'data'      => [$aArgs['docserver_type_id']]
+            'data'      => [$aArgs['id']]
         ]);
 
-        return $aReturn;
+        if (empty($aDocserverType[0])) {
+            return [];
+        }
+
+        return $aDocserverType[0];
     }
 
     public static function create(array $aArgs = [])
@@ -71,15 +76,15 @@ class DocserverTypeModelAbstract
         return true;
     }
 
-    public static function delete(array $aArgs = [])
+    public static function delete(array $aArgs)
     {
-        ValidatorModel::notEmpty($aArgs, ['docserver_type_id']);
-        ValidatorModel::stringType($aArgs, ['docserver_type_id']);
+        ValidatorModel::notEmpty($aArgs, ['id']);
+        ValidatorModel::stringType($aArgs, ['id']);
 
         DatabaseModel::delete([
-                'table' => 'docserver_types',
-                'where' => ['docserver_type_id = ?'],
-                'data'  => [$aArgs['docserver_type_id']]
+            'table' => 'docserver_types',
+            'where' => ['docserver_type_id = ?'],
+            'data'  => [$aArgs['id']]
         ]);
 
         return true;
diff --git a/modules/attachments/Controllers/ReconciliationController.php b/modules/attachments/Controllers/ReconciliationController.php
index 42e0259eb46..81505ed0e4c 100755
--- a/modules/attachments/Controllers/ReconciliationController.php
+++ b/modules/attachments/Controllers/ReconciliationController.php
@@ -281,16 +281,16 @@ class ReconciliationController{
         $filetmp .= $filename;
 
         $docserver = DocserverModel::getById([
-            'docserver_id' => $docserverId
+            'id' => $docserverId
         ]);
         $docserverType = DocserverTypeModel::getById([
-            'docserver_type_id' => $docserver[0]['docserver_type_id']
+            'id' => $docserver['docserver_type_id']
         ]);
 
         $fingerprint = DocserverToolsController::doFingerprint(
             [
                 'path'            => $filetmp,
-                'fingerprintMode' => $docserverType[0]['fingerprint_mode'],
+                'fingerprintMode' => $docserverType['fingerprint_mode'],
             ]
         );
 
diff --git a/modules/convert/Controllers/ProcessConvertController.php b/modules/convert/Controllers/ProcessConvertController.php
index 844a0020cee..74eba8b0090 100644
--- a/modules/convert/Controllers/ProcessConvertController.php
+++ b/modules/convert/Controllers/ProcessConvertController.php
@@ -184,7 +184,7 @@ class ProcessConvertController
             return $returnArray;
         }
 
-        $targetDs = DocserverModel::getById(['docserver_id' => $storeResult['docserver_id']]);
+        $targetDs = DocserverModel::getById(['id' => $storeResult['docserver_id']]);
 
         // LogsController::info(['message'=>'avant update', 'code'=>19, ]);
         //update the \Database
@@ -194,7 +194,7 @@ class ProcessConvertController
                 'resTable'   => $resTable, 
                 'adrTable'   => $adrTable, 
                 'resId'      => $resId,
-                'docserver'  => $targetDs[0],
+                'docserver'  => $targetDs,
                 'path'       => $storeResult['destination_dir'],
                 'fileName'   => $storeResult['file_destination_name']
             ]
diff --git a/modules/convert/convert.sql b/modules/convert/convert.sql
index 6a6663fa4c2..a0a088bf3aa 100644
--- a/modules/convert/convert.sql
+++ b/modules/convert/convert.sql
@@ -96,7 +96,7 @@ WITH (OIDS=FALSE);
 -- ************************************************************************* --
 
 -- docservers
-UPDATE docservers set docserver_type_id = 'DOC';
+UPDATE docservers set docserver_type_id = 'DOC' where docserver_type_id <> 'TEMPLATES' and docserver_type_id <> 'TNL';
 
 
 DELETE FROM docserver_types where docserver_type_id = 'CONVERT';
@@ -136,7 +136,7 @@ update docservers set docserver_id = 'TNL_MLB', priority_number = 12 where docse
 
 DELETE FROM docserver_types where docserver_type_id = 'FULLTEXT';
 INSERT INTO docserver_types (docserver_type_id, docserver_type_label, enabled, is_container, container_max_number, is_compressed, compression_mode, is_meta, meta_template, is_logged, log_template, is_signed, fingerprint_mode) 
-VALUES ('FULLTEXT', 'Fulltext', 'Y', 'N', 0, 'N', 'NONE', 'N', 'NONE', 'N', 'NONE', 'Y', 'NONE');
+VALUES ('FULLTEXT', 'FULLTEXT', 'Y', 'N', 0, 'N', 'NONE', 'N', 'NONE', 'N', 'NONE', 'Y', 'NONE');
 
 DELETE FROM docservers where docserver_id = 'FULLTEXT_MLB';
 INSERT INTO docservers (docserver_id, docserver_type_id, device_label, is_readonly, enabled, size_limit_number, actual_size_number, path_template, ext_docserver_info, chain_before, chain_after, creation_date, closing_date, coll_id, priority_number, docserver_location_id, adr_priority_number) 
diff --git a/modules/visa/Controllers/VisaController.php b/modules/visa/Controllers/VisaController.php
index d300a727040..8e5dbe5a708 100755
--- a/modules/visa/Controllers/VisaController.php
+++ b/modules/visa/Controllers/VisaController.php
@@ -23,8 +23,8 @@ use Core\Models\UserModel;
 use Core\Models\LangModel;
 use Core\Models\DocserverModel;
 use Core\Models\ServiceModel;
-use Notes\Models\NoteModel;
 use Baskets\Models\BasketsModel;
+use Notes\Models\NoteModel;
 use Psr\Http\Message\RequestInterface;
 use Psr\Http\Message\ResponseInterface;
 use Visa\Models\VisaModel;
diff --git a/rest/index.php b/rest/index.php
index cecadbdd4f9..9921d8d09ba 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -145,13 +145,13 @@ $app->post('/status', \Core\Controllers\StatusController::class . ':create');
 $app->put('/status/{identifier}', \Core\Controllers\StatusController::class . ':update');
 $app->delete('/status/{identifier}', \Core\Controllers\StatusController::class . ':delete');
 
-//docserver
-$app->get('/docserver', \Core\Controllers\DocserverController::class . ':getList');
-$app->get('/docserver/{id}', \Core\Controllers\DocserverController::class . ':getById');
+//Docservers
+$app->get('/docservers', \Core\Controllers\DocserverController::class . ':get');
+$app->get('/docservers/{id}', \Core\Controllers\DocserverController::class . ':getById');
 
-//docserverType
-$app->get('/docserverType', \core\Controllers\DocserverTypeController::class . ':getList');
-$app->get('/docserverType/{id}', \core\Controllers\DocserverTypeController::class . ':getById');
+//DocserverTypes
+$app->get('/docserverTypes', \core\Controllers\DocserverTypeController::class . ':get');
+$app->get('/docserverTypes/{id}', \core\Controllers\DocserverTypeController::class . ':getById');
 
 //ListModels
 $app->get('/listModels/itemId/{itemId}/itemMode/{itemMode}/objectType/{objectType}', \Entities\Controllers\ListModelsController::class . ':getListModelsDiffListDestByUserId');
-- 
GitLab