Skip to content
Snippets Groups Projects
Verified Commit e1559330 authored by Damien's avatar Damien
Browse files

FEAT #11296 TIME 2:10 Get resources from folders

parent b2dc782c
No related branches found
No related tags found
No related merge requests found
...@@ -14,11 +14,15 @@ ...@@ -14,11 +14,15 @@
namespace Folder\controllers; namespace Folder\controllers;
use Attachment\models\AttachmentModel;
use Entity\models\EntityModel; use Entity\models\EntityModel;
use Folder\models\EntityFolderModel; use Folder\models\EntityFolderModel;
use Folder\models\FolderModel; use Folder\models\FolderModel;
use Folder\models\ResourceFolderModel; use Folder\models\ResourceFolderModel;
use Group\models\ServiceModel;
use History\controllers\HistoryController; use History\controllers\HistoryController;
use Resource\controllers\ResourceListController;
use Resource\models\ResourceListModel;
use Respect\Validation\Validator; use Respect\Validation\Validator;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
...@@ -62,28 +66,100 @@ class FolderController ...@@ -62,28 +66,100 @@ class FolderController
return $response->withJson(['folders' => $tree]); return $response->withJson(['folders' => $tree]);
} }
public function getById(Request $request, Response $response, array $aArgs) public function getById(Request $request, Response $response, array $args)
{ {
if (!Validator::numeric()->notEmpty()->validate($aArgs['id'])) { if (!Validator::numeric()->notEmpty()->validate($args['id'])) {
return $response->withStatus(400)->withJson(['errors' => 'Query id is empty or not an integer']); return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
} }
$folder = FolderController::getScopeFolders(['login' => $GLOBALS['userId'], 'folderId' => $aArgs['id']]); $folder = FolderController::getScopeFolders(['login' => $GLOBALS['userId'], 'folderId' => $args['id']]);
if (empty($folder[0])) { if (empty($folder[0])) {
return $response->withStatus(400)->withJson(['errors' => 'Folder not found or out of your perimeter']); return $response->withStatus(400)->withJson(['errors' => 'Folder not found or out of your perimeter']);
} }
$folder[0]['sharing']['entities'] = []; $folder = $folder[0];
if ($folder[0]['public']) {
$entitiesFolder = EntityFolderModel::getByFolderId(['folder_id' => $aArgs['id']]); $folder['sharing']['entities'] = [];
if ($folder['public']) {
$entitiesFolder = EntityFolderModel::getByFolderId(['folder_id' => $args['id']]);
foreach ($entitiesFolder as $value) { foreach ($entitiesFolder as $value) {
$folder[0]['sharing']['entities'][] = ['entity_id' => $value['entity_id'], 'edition' => $value['edition']]; $folder['sharing']['entities'][] = ['entity_id' => $value['entity_id'], 'edition' => $value['edition']];
} }
} }
//TODO Get resources $foldersResources = ResourceFolderModel::get(['select' => ['res_id'], 'where' => ['folder_id = ?'], 'data' => [$args['id']]]);
$foldersResources = array_column($foldersResources, 'res_id');
$queryParams = $request->getQueryParams();
$queryParams['offset'] = (empty($queryParams['offset']) || !is_numeric($queryParams['offset']) ? 0 : (int)$queryParams['offset']);
$queryParams['limit'] = (empty($queryParams['limit']) || !is_numeric($queryParams['limit']) ? 10 : (int)$queryParams['limit']);
$allQueryData = ResourceListController::getResourcesListQueryData(['data' => $queryParams]);
if (!empty($allQueryData['order'])) {
$data['order'] = $allQueryData['order'];
}
$rawResources = ResourceListModel::getOnView([
'select' => ['res_id'],
'table' => $allQueryData['table'],
'leftJoin' => $allQueryData['leftJoin'],
'where' => array_merge(['res_id in (?)'], $allQueryData['where']),
'data' => array_merge([$foldersResources], $allQueryData['queryData']),
'orderBy' => empty($data['order']) ? ['creation_date'] : [$data['order']]
]);
$resIds = ResourceListController::getIdsWithOffsetAndLimit(['resources' => $rawResources, 'offset' => $queryParams['offset'], 'limit' => $queryParams['limit']]);
$formattedResources = [];
if (!empty($resIds)) {
$excludeAttachmentTypes = ['converted_pdf', 'print_folder'];
if (!ServiceModel::hasService(['id' => 'view_documents_with_notes', 'userId' => $GLOBALS['userId'], 'location' => 'attachments', 'type' => 'use'])) {
$excludeAttachmentTypes[] = 'document_with_notes';
}
$attachments = AttachmentModel::getOnView([
'select' => ['COUNT(res_id)', 'res_id_master'],
'where' => ['res_id_master in (?)', 'status not in (?)', 'attachment_type not in (?)', '((status = ? AND typist = ?) OR status != ?)'],
'data' => [$resIds, ['DEL', 'OBS'], $excludeAttachmentTypes, 'TMP', $GLOBALS['userId'], 'TMP'],
'groupBy' => ['res_id_master']
]);
$select = [
'res_letterbox.res_id', 'res_letterbox.subject', 'res_letterbox.barcode', 'mlb_coll_ext.alt_identifier',
'status.label_status AS "status.label_status"', 'status.img_filename AS "status.img_filename"'
];
$tableFunction = ['status', 'mlb_coll_ext'];
$leftJoinFunction = ['res_letterbox.status = status.id', 'res_letterbox.res_id = mlb_coll_ext.res_id'];
$order = 'CASE res_letterbox.res_id ';
foreach ($resIds as $key => $resId) {
$order .= "WHEN {$resId} THEN {$key} ";
}
$order .= 'END';
$resources = ResourceListModel::getOnResource([
'select' => $select,
'table' => $tableFunction,
'leftJoin' => $leftJoinFunction,
'where' => ['res_letterbox.res_id in (?)'],
'data' => [$resIds],
'orderBy' => [$order]
]);
$formattedResources = ResourceListController::getFormattedResources([
'resources' => $resources,
'userId' => $GLOBALS['id'],
'attachments' => $attachments,
'checkLocked' => false
]);
}
$folder['resources'] = $formattedResources;
$folder['countResources'] = count($rawResources);
//TODO Get default action
return $response->withJson(['folder' => $folder[0]]); return $response->withJson(['folder' => $folder]);
} }
public function create(Request $request, Response $response) public function create(Request $request, Response $response)
...@@ -296,7 +372,7 @@ class FolderController ...@@ -296,7 +372,7 @@ class FolderController
return $response->withStatus(200); return $response->withStatus(200);
} }
public function folderDeletion($aArgs = []) public static function folderDeletion(array $aArgs = [])
{ {
$folder = FolderController::getScopeFolders(['login' => $GLOBALS['userId'], 'folderId' => $aArgs['folderId'], 'edition' => true]); $folder = FolderController::getScopeFolders(['login' => $GLOBALS['userId'], 'folderId' => $aArgs['folderId'], 'edition' => true]);
if (empty($folder[0])) { if (empty($folder[0])) {
...@@ -322,7 +398,7 @@ class FolderController ...@@ -322,7 +398,7 @@ class FolderController
// login (string) : Login of user connected // login (string) : Login of user connected
// folderId (integer) : Check specific folder // folderId (integer) : Check specific folder
// edition (boolean) : whether user can edit or not // edition (boolean) : whether user can edit or not
private static function getScopeFolders($aArgs = []) private static function getScopeFolders(array $aArgs)
{ {
$login = $aArgs['login']; $login = $aArgs['login'];
$userEntities = EntityModel::getEntitiesByUserId([ $userEntities = EntityModel::getEntitiesByUserId([
......
...@@ -46,9 +46,7 @@ class ResourceListController ...@@ -46,9 +46,7 @@ class ResourceListController
{ {
public function get(Request $request, Response $response, array $aArgs) public function get(Request $request, Response $response, array $aArgs)
{ {
$currentUser = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]); $errors = ResourceListController::listControl(['groupId' => $aArgs['groupId'], 'userId' => $aArgs['userId'], 'basketId' => $aArgs['basketId'], 'currentUserId' => $GLOBALS['id']]);
$errors = ResourceListController::listControl(['groupId' => $aArgs['groupId'], 'userId' => $aArgs['userId'], 'basketId' => $aArgs['basketId'], 'currentUserId' => $currentUser['id']]);
if (!empty($errors['errors'])) { if (!empty($errors['errors'])) {
return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]); return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
} }
...@@ -76,16 +74,8 @@ class ResourceListController ...@@ -76,16 +74,8 @@ class ResourceListController
]); ]);
$count = count($rawResources); $count = count($rawResources);
$resIds = []; $resIds = ResourceListController::getIdsWithOffsetAndLimit(['resources' => $rawResources, 'offset' => $data['offset'], 'limit' => $data['limit']]);
if (!empty($rawResources[$data['offset']])) {
$start = $data['offset'];
$i = 0;
while ($i < $data['limit'] && !empty($rawResources[$start])) {
$resIds[] = $rawResources[$start]['res_id'];
++$start;
++$i;
}
}
$allResources = []; $allResources = [];
foreach ($rawResources as $resource) { foreach ($rawResources as $resource) {
$allResources[] = $resource['res_id']; $allResources[] = $resource['res_id'];
...@@ -151,80 +141,13 @@ class ResourceListController ...@@ -151,80 +141,13 @@ class ResourceListController
'orderBy' => [$order] 'orderBy' => [$order]
]); ]);
foreach ($resources as $key => $resource) { $formattedResources = ResourceListController::getFormattedResources([
$formattedResources[$key]['res_id'] = $resource['res_id']; 'resources' => $resources,
$formattedResources[$key]['alt_identifier'] = $resource['alt_identifier']; 'userId' => $GLOBALS['id'],
$formattedResources[$key]['barcode'] = $resource['barcode']; 'attachments' => $attachments,
$formattedResources[$key]['subject'] = $resource['subject']; 'checkLocked' => true,
$formattedResources[$key]['confidentiality'] = $resource['confidentiality']; 'listDisplay' => $listDisplay
$formattedResources[$key]['statusLabel'] = $resource['status.label_status']; ]);
$formattedResources[$key]['statusImage'] = $resource['status.img_filename'];
$formattedResources[$key]['priorityColor'] = $resource['priorities.color'];
$formattedResources[$key]['closing_date'] = $resource['closing_date'];
$formattedResources[$key]['countAttachments'] = 0;
foreach ($attachments as $attachment) {
if ($attachment['res_id_master'] == $resource['res_id']) {
$formattedResources[$key]['countAttachments'] = $attachment['count'];
break;
}
}
$formattedResources[$key]['countNotes'] = NoteModel::countByResId(['resId' => $resource['res_id'], 'login' => $GLOBALS['userId']]);
$isLocked = true;
if (empty($resource['locker_user_id'] || empty($resource['locker_time']))) {
$isLocked = false;
} elseif ($resource['locker_user_id'] == $currentUser['id']) {
$isLocked = false;
} elseif (strtotime($resource['locker_time']) < time()) {
$isLocked = false;
}
if ($isLocked) {
$formattedResources[$key]['locker'] = UserModel::getLabelledUserById(['id' => $resource['locker_user_id']]);
}
$formattedResources[$key]['isLocked'] = $isLocked;
$display = [];
foreach ($listDisplay as $value) {
$value = (array)$value;
if ($value['value'] == 'getPriority') {
$value['displayValue'] = $resource['priorities.label'];
$display[] = $value;
} elseif ($value['value'] == 'getCategory') {
$value['displayValue'] = $resource['category_id'];
$display[] = $value;
} elseif ($value['value'] == 'getDoctype') {
$value['displayValue'] = $resource['doctypes.description'];
$display[] = $value;
} elseif ($value['value'] == 'getAssignee') {
$value['displayValue'] = ResourceListController::getAssignee(['resId' => $resource['res_id']]);
$display[] = $value;
} elseif ($value['value'] == 'getSenders') {
$value['displayValue'] = ResourceListController::getSenders(['resId' => $resource['res_id']]);
$display[] = $value;
} elseif ($value['value'] == 'getRecipients') {
$value['displayValue'] = ResourceListController::getRecipients(['resId' => $resource['res_id']]);
$display[] = $value;
} elseif ($value['value'] == 'getVisaWorkflow') {
$value['displayValue'] = ResourceListController::getVisaWorkflow(['resId' => $resource['res_id']]);
$display[] = $value;
} elseif ($value['value'] == 'getSignatories') {
$value['displayValue'] = ResourceListController::getSignatories(['resId' => $resource['res_id']]);
$display[] = $value;
} elseif ($value['value'] == 'getParallelOpinionsNumber') {
$value['displayValue'] = ResourceListController::getParallelOpinionsNumber(['resId' => $resource['res_id']]);
$display[] = $value;
} elseif ($value['value'] == 'getCreationAndProcessLimitDates') {
$value['displayValue'] = ['creationDate' => $resource['creation_date'], 'processLimitDate' => $resource['process_limit_date']];
$display[] = $value;
} elseif ($value['value'] == 'getModificationDate') {
$value['displayValue'] = $resource['modification_date'];
$display[] = $value;
} elseif ($value['value'] == 'getOpinionLimitDate') {
$value['displayValue'] = $resource['opinion_limit_date'];
$display[] = $value;
}
}
$formattedResources[$key]['display'] = $display;
}
$defaultAction['component'] = $groupBasket[0]['list_event']; $defaultAction['component'] = $groupBasket[0]['list_event'];
} }
...@@ -460,14 +383,16 @@ class ResourceListController ...@@ -460,14 +383,16 @@ class ResourceListController
public static function getResourcesListQueryData(array $args) public static function getResourcesListQueryData(array $args)
{ {
ValidatorModel::notEmpty($args, ['basketClause', 'login']);
ValidatorModel::stringType($args, ['basketClause', 'login']); ValidatorModel::stringType($args, ['basketClause', 'login']);
ValidatorModel::arrayType($args, ['data']); ValidatorModel::arrayType($args, ['data']);
$table = []; $table = [];
$leftJoin = []; $leftJoin = [];
$whereClause = PreparedClauseController::getPreparedClause(['clause' => $args['basketClause'], 'login' => $args['login']]); $where = [];
$where = [$whereClause]; if (!empty($args['basketClause'])) {
$whereClause = PreparedClauseController::getPreparedClause(['clause' => $args['basketClause'], 'login' => $args['login']]);
$where = [$whereClause];
}
$queryData = []; $queryData = [];
$order = null; $order = null;
...@@ -1052,4 +977,122 @@ class ResourceListController ...@@ -1052,4 +977,122 @@ class ResourceListController
return $notes[0]['count']; return $notes[0]['count'];
} }
public static function getIdsWithOffsetAndLimit(array $args)
{
ValidatorModel::notEmpty($args, ['resources']);
ValidatorModel::arrayType($args, ['resources']);
ValidatorModel::intVal($args, ['offset', 'limit']);
$ids = [];
if (!empty($args['resources'][$args['offset']])) {
$start = $args['offset'];
$i = 0;
while ($i < $args['limit'] && !empty($args['resources'][$start])) {
$ids[] = $args['resources'][$start]['res_id'];
++$start;
++$i;
}
}
return $ids;
}
public static function getFormattedResources(array $args)
{
ValidatorModel::notEmpty($args, ['resources', 'userId']);
ValidatorModel::arrayType($args, ['resources', 'attachments', 'listDisplay']);
ValidatorModel::intVal($args, ['userId']);
ValidatorModel::boolType($args, ['checkLocked']);
$formattedResources = [];
$resources = $args['resources'];
$attachments = $args['attachments'];
$currentUser = UserModel::getById(['id' => $args['userId'], 'select' => ['user_id']]);
foreach ($resources as $key => $resource) {
$formattedResources[$key]['res_id'] = $resource['res_id'];
$formattedResources[$key]['alt_identifier'] = $resource['alt_identifier'];
$formattedResources[$key]['barcode'] = $resource['barcode'];
$formattedResources[$key]['subject'] = $resource['subject'];
$formattedResources[$key]['confidentiality'] = $resource['confidentiality'];
$formattedResources[$key]['statusLabel'] = $resource['status.label_status'];
$formattedResources[$key]['statusImage'] = $resource['status.img_filename'];
$formattedResources[$key]['priorityColor'] = $resource['priorities.color'];
$formattedResources[$key]['closing_date'] = $resource['closing_date'];
$formattedResources[$key]['countAttachments'] = 0;
foreach ($attachments as $attachment) {
if ($attachment['res_id_master'] == $resource['res_id']) {
$formattedResources[$key]['countAttachments'] = $attachment['count'];
break;
}
}
$formattedResources[$key]['countNotes'] = NoteModel::countByResId(['resId' => $resource['res_id'], 'login' => $currentUser['user_id']]);
if (!empty($args['checkLocked'])) {
$isLocked = true;
if (empty($resource['locker_user_id'] || empty($resource['locker_time']))) {
$isLocked = false;
} elseif ($resource['locker_user_id'] == $args['userId']) {
$isLocked = false;
} elseif (strtotime($resource['locker_time']) < time()) {
$isLocked = false;
}
if ($isLocked) {
$formattedResources[$key]['locker'] = UserModel::getLabelledUserById(['id' => $resource['locker_user_id']]);
}
$formattedResources[$key]['isLocked'] = $isLocked;
}
if (isset($args['listDisplay'])) {
$display = [];
foreach ($args['listDisplay'] as $value) {
$value = (array)$value;
if ($value['value'] == 'getPriority') {
$value['displayValue'] = $resource['priorities.label'];
$display[] = $value;
} elseif ($value['value'] == 'getCategory') {
$value['displayValue'] = $resource['category_id'];
$display[] = $value;
} elseif ($value['value'] == 'getDoctype') {
$value['displayValue'] = $resource['doctypes.description'];
$display[] = $value;
} elseif ($value['value'] == 'getAssignee') {
$value['displayValue'] = ResourceListController::getAssignee(['resId' => $resource['res_id']]);
$display[] = $value;
} elseif ($value['value'] == 'getSenders') {
$value['displayValue'] = ResourceListController::getSenders(['resId' => $resource['res_id']]);
$display[] = $value;
} elseif ($value['value'] == 'getRecipients') {
$value['displayValue'] = ResourceListController::getRecipients(['resId' => $resource['res_id']]);
$display[] = $value;
} elseif ($value['value'] == 'getVisaWorkflow') {
$value['displayValue'] = ResourceListController::getVisaWorkflow(['resId' => $resource['res_id']]);
$display[] = $value;
} elseif ($value['value'] == 'getSignatories') {
$value['displayValue'] = ResourceListController::getSignatories(['resId' => $resource['res_id']]);
$display[] = $value;
} elseif ($value['value'] == 'getParallelOpinionsNumber') {
$value['displayValue'] = ResourceListController::getParallelOpinionsNumber(['resId' => $resource['res_id']]);
$display[] = $value;
} elseif ($value['value'] == 'getCreationAndProcessLimitDates') {
$value['displayValue'] = ['creationDate' => $resource['creation_date'], 'processLimitDate' => $resource['process_limit_date']];
$display[] = $value;
} elseif ($value['value'] == 'getModificationDate') {
$value['displayValue'] = $resource['modification_date'];
$display[] = $value;
} elseif ($value['value'] == 'getOpinionLimitDate') {
$value['displayValue'] = $resource['opinion_limit_date'];
$display[] = $value;
}
}
$formattedResources[$key]['display'] = $display;
}
}
return $formattedResources;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment