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

FEAT #9105 setAction

parent 806eafc5
No related branches found
No related tags found
No related merge requests found
......@@ -267,6 +267,7 @@ $app->put('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/loc
$app->get('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/filters', \Resource\controllers\ResourceListController::class . ':getFilters');
$app->put('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/exports', \Resource\controllers\ExportController::class . ':updateExport');
$app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/summarySheets', \Resource\controllers\SummarySheetController::class . ':createList');
$app->put('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}', \Resource\controllers\ResourceListController::class . ':setAction');
$app->get('/resourcesList/exportTemplate', \Resource\controllers\ExportController::class . ':getExportTemplates');
//SignatureBook
......
......@@ -101,6 +101,7 @@ class ActionController
return $response->withStatus(400)->withJson(['errors' => 'Data actionPageId does not exist']);
}
unset($body['actionPageId']);
$id = ActionModel::create($body);
if (!empty($body['actionCategories'])) {
ActionModel::createCategories(['id' => $id, 'categories' => $body['actionCategories']]);
......@@ -186,39 +187,6 @@ class ActionController
return $response->withJson(['actions' => ActionModel::get()]);
}
public static function terminateAction(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['id', 'resId']);
ValidatorModel::intVal($aArgs, ['id', 'resId']);
$set = ['locker_user_id' => null, 'locker_time' => null];
$action = ActionModel::getById(['id' => $aArgs['id'], 'select' => ['label_action', 'id_status', 'history']]);
if (!empty($action['id_status']) && $action['id_status'] != '_NOSTATUS_') {
$set['status'] = $action['id_status'];
}
ResModel::update([
'set' => $set,
'where' => ['res_id = ?'],
'data' => [$aArgs['resId']]
]);
if ($action['history'] == 'Y') {
HistoryController::add([
'tableName' => 'actions',
'recordId' => $aArgs['resId'],
'eventType' => 'ACTION#' . $aArgs['id'],
'eventId' => $aArgs['id'],
'info' => $action['label_action']
]);
//TODO M2M
}
return true;
}
protected function control($aArgs, $mode)
{
$errors = [];
......
<?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 ActionController
* @author dev <dev@maarch.org>
* @ingroup core
*/
namespace Action\controllers;
use History\controllers\HistoryController;
use Resource\models\ResModel;
use Action\models\ActionModel;
use SrcCore\models\ValidatorModel;
class ActionMethodController
{
const COMPONENTS_ACTIONS = [
'confirmAction' => null
];
public static function terminateAction(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['id', 'resId']);
ValidatorModel::intVal($aArgs, ['id', 'resId']);
$set = ['locker_user_id' => null, 'locker_time' => null, 'modification_date' => 'CURRENT_TIMESTAMP'];
$action = ActionModel::getById(['id' => $aArgs['id'], 'select' => ['label_action', 'id_status', 'history']]);
if (!empty($action['id_status']) && $action['id_status'] != '_NOSTATUS_') {
$set['status'] = $action['id_status'];
}
ResModel::update([
'set' => $set,
'where' => ['res_id = ?'],
'data' => [$aArgs['resId']]
]);
if ($action['history'] == 'Y') {
HistoryController::add([
'tableName' => 'actions',
'recordId' => $aArgs['resId'],
'eventType' => 'ACTION#' . $aArgs['id'],
'eventId' => $aArgs['id'],
'info' => $action['label_action']
]);
//TODO M2M
}
return true;
}
}
......@@ -493,51 +493,6 @@ class ResController
return $response->withJson(['success' => 'success']);
}
public function lock(Request $request, Response $response, array $aArgs)
{
$body = $request->getParsedBody();
if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
return $response->withStatus(400)->withJson(['errors' => 'Data resources is empty or not an array']);
}
$currentUser = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]);
$lockers = [];
foreach ($body['resources'] as $resource) {
if (!Validator::intVal()->notEmpty()->validate($resource) || !ResController::hasRightByResId(['resId' => $resource, 'userId' => $GLOBALS['userId']])) {
return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
}
}
$resources = ResModel::get(['select' => ['locker_user_id', 'locker_time'], 'where' => ['res_id in (?)'], 'data' => $body['resources']]);
foreach ($resources as $resource) {
$lock = true;
if (empty($resource['locker_user_id'] || empty($resource['locker_time']))) {
$lock = false;
} elseif ($resource['locker_user_id'] == $currentUser['id']) {
$lock = false;
} elseif (strtotime($resource['locker_time']) < time()) {
$lock = false;
}
if ($lock) {
$lockers[] = UserModel::getLabelledUserById(['id' => $resource['locker_user_id']]);
}
}
if (!empty($lockers)) {
return $response->withStatus(403)->withJson(['lockBy' => $lockers]);
}
ResModel::update([
'set' => ['locker_user_id' => $currentUser['id'], 'locker_time' => 'CURRENT_TIMESTAMP + interval \'1\' MINUTE'],
'where' => ['res_id in (?)'],
'data' => [$body['resources']]
]);
return $response->withStatus(204);
}
public function getNotesCountForCurrentUserById(Request $request, Response $response, array $aArgs)
{
return $response->withJson(NoteModel::countByResId(['resId' => $aArgs['resId'], 'login' => $GLOBALS['userId']]));
......
......@@ -14,6 +14,7 @@
namespace Resource\controllers;
use Action\controllers\ActionMethodController;
use Action\models\ActionModel;
use Attachment\models\AttachmentModel;
use Basket\models\ActionGroupBasketModel;
......@@ -550,18 +551,84 @@ class ResourceListController
}
if (!empty($actions)) {
$actions = ActionModel::get(['select' => ['label_action', 'component'], 'where' => ['id in (?)'], 'data' => [$actions], 'orderBy' => ['label_action']]);
$actions = ActionModel::get(['select' => ['id', 'label_action', 'component'], 'where' => ['id in (?)'], 'data' => [$actions], 'orderBy' => ['label_action']]);
}
return $response->withJson(['actions' => $actions]);
}
public function setAction(Request $request, Response $response, array $aArgs)
{
$body = $request->getParsedBody();
if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
return $response->withStatus(400)->withJson(['errors' => 'Data resources is empty or not an array']);
}
$body['resources'] = array_slice($body['resources'], 0, 500);
$currentUser = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]);
$errors = ResourceListController::listControl(['groupId' => $aArgs['groupId'], 'userId' => $aArgs['userId'], 'basketId' => $aArgs['basketId'], 'currentUserId' => $currentUser['id']]);
if (!empty($errors['errors'])) {
return $response->withStatus($errors['code'])->withJson(['errors' => $errors['errors']]);
}
$basket = BasketModel::getById(['id' => $aArgs['basketId'], 'select' => ['basket_clause', 'basket_id']]);
$group = GroupModel::getById(['id' => $aArgs['groupId'], 'select' => ['group_id']]);
$actionGroupBasket = ActionGroupBasketModel::get([
'select' => [1],
'where' => ['basket_id = ?', 'group_id = ?', 'id_action = ?'],
'data' => [$basket['basket_id'], $group['group_id'], $aArgs['actionId']]
]);
if (empty($actionGroupBasket)) {
return $response->withStatus(400)->withJson(['errors' => 'Action is not linked to this group basket']);
}
//TODO Check locked ???
$user = UserModel::getById(['id' => $aArgs['userId'], 'select' => ['user_id']]);
$whereClause = PreparedClauseController::getPreparedClause(['clause' => $basket['basket_clause'], 'login' => $user['user_id']]);
$resources = ResModel::getOnView([
'select' => ['res_id', 'locker_user_id', 'locker_time'],
'where' => [$whereClause, 'res_view_letterbox.res_id in (?)'],
'data' => [$body['resources']]
]);
$resourcesInBasket = [];
foreach ($resources as $resource) {
$resourcesInBasket[] = $resource['res_id'];
}
foreach ($body['resources'] as $resId) {
if (!in_array($resId, $resourcesInBasket)) {
return $response->withStatus(403)->withJson(['errors' => 'Resources out of perimeter']);
}
}
$action = ActionModel::getById(['id' => $aArgs['actionId'], 'select' => ['component']]);
if (empty($action['component'])) {
return $response->withStatus(400)->withJson(['errors' => 'Action component does not exist']);
}
if (!array_key_exists($action['component'], ActionMethodController::COMPONENTS_ACTIONS)) {
return $response->withStatus(400)->withJson(['errors' => 'Action method does not exist']);
}
foreach ($body['resources'] as $resId) {
$method = ActionMethodController::COMPONENTS_ACTIONS[$action['component']];
if (!empty($method)) {
ActionMethodController::$method(['id' => $aArgs['actionId'], 'resId' => $resId]);
}
ActionMethodController::terminateAction(['id' => $aArgs['actionId'], 'resId' => $resId]);
}
return $response->withStatus(204);
}
public function lock(Request $request, Response $response, array $aArgs)
{
$body = $request->getParsedBody();
if (!Validator::arrayType()->notEmpty()->validate($body['resources'])) {
return $response->withStatus(400)->withJson(['errors' => 'Data resources is empty or not an array']);
}
$body['resources'] = array_slice($body['resources'], 0, 500);
$currentUser = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]);
$errors = ResourceListController::listControl(['groupId' => $aArgs['groupId'], 'userId' => $aArgs['userId'], 'basketId' => $aArgs['basketId'], 'currentUserId' => $currentUser['id']]);
......
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