From 6079a6f6b20fd977c004837e671cfb87dfe741e6 Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Fri, 8 Mar 2019 12:32:03 +0100 Subject: [PATCH] FEAT #9105 Actions methods return + notes refactoring --- rest/index.php | 4 +-- .../AcknowledgementReceiptTrait.php | 2 +- src/app/note/controllers/NoteController.php | 30 +++++++------------ .../controllers/ResourceListController.php | 14 ++++++--- .../app/notes/note-editor.component.ts | 2 +- 5 files changed, 25 insertions(+), 27 deletions(-) diff --git a/rest/index.php b/rest/index.php index 4aaa230fdc2..76dd3b77b92 100755 --- a/rest/index.php +++ b/rest/index.php @@ -214,8 +214,8 @@ $app->get('/listTemplates/types/{typeId}/roles', \Entity\controllers\ListTemplat $app->put('/listTemplates/types/{typeId}/roles', \Entity\controllers\ListTemplateController::class . ':updateTypeRoles'); //Notes -$app->get('/res/{resId}/notes/templates', \Note\controllers\NoteController::class . ':getTemplateListByResId'); -$app->get('/notes/templates', \Note\controllers\NoteController::class . ':getAllTemplateList'); +$app->get('/notes/templates/resources/{resId}', \Note\controllers\NoteController::class . ':getTemplatesByResId'); +$app->get('/notes/templates', \Note\controllers\NoteController::class . ':getTemplates'); $app->get('/res/{resId}/notes', \Note\controllers\NoteController::class . ':getByResId'); $app->post('/res/{resId}/notes', \Note\controllers\NoteController::class . ':create'); diff --git a/src/app/action/controllers/AcknowledgementReceiptTrait.php b/src/app/action/controllers/AcknowledgementReceiptTrait.php index 2919314fcd5..5184d822615 100644 --- a/src/app/action/controllers/AcknowledgementReceiptTrait.php +++ b/src/app/action/controllers/AcknowledgementReceiptTrait.php @@ -179,6 +179,6 @@ trait AcknowledgementReceiptTrait } } - return ['ids' => $ids, 'errors' => $errors]; + return ['data' => $ids, 'errors' => $errors]; } } diff --git a/src/app/note/controllers/NoteController.php b/src/app/note/controllers/NoteController.php index cd3daee09b7..bd1ef0c01bd 100755 --- a/src/app/note/controllers/NoteController.php +++ b/src/app/note/controllers/NoteController.php @@ -126,38 +126,30 @@ class NoteController return ['encodedDocument' => base64_encode($fileContent)]; } - public static function getTemplateListByResId(Request $request, Response $response, array $aArgs) + public static function getTemplatesByResId(Request $request, Response $response, array $aArgs) { - $check = Validator::intVal()->notEmpty()->validate($aArgs['resId']); - if (!$check) { - return $response->withStatus(400)->withJson(['errors' => 'resId is empty or not an integer']); + if (!Validator::intVal()->notEmpty()->validate($aArgs['resId'])) { + return $response->withStatus(400)->withJson(['errors' => 'Route resId is not an integer']); } - - if (!empty($aArgs['resId']) && !ResController::hasRightByResId(['resId' => $aArgs['resId'], 'userId' => $GLOBALS['userId']])) { + if (!ResController::hasRightByResId(['resId' => $aArgs['resId'], 'userId' => $GLOBALS['userId']])) { return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']); } - //get all templates note - $tmpAllNotes = TemplateModel::getByTarget(['template_target' => 'notes', 'select' => ['template_id', 'template_label', 'template_content']]); - - //get entity resource - $resEntity = ResModel::getById(['resId' => $aArgs['resId'], 'select' => ['destination']]); + $resource = ResModel::getById(['resId' => $aArgs['resId'], 'select' => ['destination']]); if (!empty($resEntity['destination'])) { - //get retricted templates note - $aReturn = TemplateModel::getWithAssociation(['select' => ['DISTINCT(templates.template_id), template_label', 'template_content'], 'where' => ['template_target = ?', 'value_field = ?', 'templates.template_id = templates_association.template_id'], 'data' => ['notes', $resEntity['destination']], 'orderBy' => ['template_label']]); + $templates = TemplateModel::getWithAssociation(['select' => ['DISTINCT(templates.template_id), template_label', 'template_content'], 'where' => ['template_target = ?', 'value_field = ?', 'templates.template_id = templates_association.template_id'], 'data' => ['notes', $resource['destination']], 'orderBy' => ['template_label']]); } else { - $aReturn = TemplateModel::getByTarget(['template_target' => 'notes', 'select' => ['template_label', 'template_content']]); + $templates = TemplateModel::getByTarget(['template_target' => 'notes', 'select' => ['template_label', 'template_content']]); } - return $response->withJson($aReturn); + return $response->withJson(['templates' => $templates]); } - public static function getAllTemplateList(Request $request, Response $response) + public static function getTemplates(Request $request, Response $response) { - //get all templates note - $aReturn = TemplateModel::getByTarget(['template_target' => 'notes', 'select' => ['template_label', 'template_content']]); + $templates = TemplateModel::getByTarget(['template_target' => 'notes', 'select' => ['template_label', 'template_content']]); - return $response->withJson($aReturn); + return $response->withJson(['templates' => $templates]); } } diff --git a/src/app/resource/controllers/ResourceListController.php b/src/app/resource/controllers/ResourceListController.php index 01fb0513719..a9ead58b6cb 100644 --- a/src/app/resource/controllers/ResourceListController.php +++ b/src/app/resource/controllers/ResourceListController.php @@ -642,12 +642,18 @@ class ResourceListController foreach ($resourcesForAction as $resId) { if (!empty($method)) { $methodResponse = ActionMethodController::$method(['resId' => $resId, 'data' => $body['data']]); + if (!empty($methodResponse['errors'])) { - return $response->withStatus(500)->withJson(['errors' => $methodResponse['errors']]); + if (empty($methodResponses['errors'])) { + $methodResponses['errors'] = []; + } + $methodResponses['errors'] = array_merge($methodResponses['errors'], $methodResponse['errors']); } - if ($methodResponse !== true) { - //TODO array_merge keys avec errors - $methodResponses = array_merge($methodResponses, $methodResponse); + if (!empty($methodResponse['data'])) { + if (empty($methodResponses['data'])) { + $methodResponses['data'] = []; + } + $methodResponses['data'] = array_merge($methodResponses['data'], $methodResponse['data']); } } } diff --git a/src/frontend/app/notes/note-editor.component.ts b/src/frontend/app/notes/note-editor.component.ts index 5ce5be6c10b..62101ba37a2 100644 --- a/src/frontend/app/notes/note-editor.component.ts +++ b/src/frontend/app/notes/note-editor.component.ts @@ -50,7 +50,7 @@ export class NoteEditorComponent implements AfterViewInit { getTemplatesNote() { if (this.templatesNote.length == 0) { if (this.resIds.length == 1) { - this.http.get("../../rest/res/" + this.resIds[0] + "/notes/templates") + this.http.get("../../rest/notes/templates/resources/" + this.resIds[0]) .subscribe((data: any) => { this.templatesNote = data['templates']; }); -- GitLab