Newer
Older
<?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 Resource Controller
* @author dev@maarch.org
*/
use SrcCore\models\ValidatorModel;
use SrcCore\controllers\PreparedClauseController;
use Docserver\models\ResDocserverModel;
//*****************************************************************************************
//LOG ONLY LOG FOR DEBUG
// $file = fopen('storeResourceLogs.log', a);
// fwrite($file, '[' . date('Y-m-d H:i:s') . '] new request' . PHP_EOL);
// foreach ($data as $key => $value) {
// if ($key <> 'encodedFile') {
// fwrite($file, '[' . date('Y-m-d H:i:s') . '] ' . $key . ' : ' . $value . PHP_EOL);
// }
// }
// fclose($file);
// ob_flush();
// ob_start();
// print_r($data);
// file_put_contents("storeResourceLogs.log", ob_get_flush());
//END LOG FOR DEBUG ONLY
//*****************************************************************************************
public function create(Request $request, Response $response)
if (!ServiceModel::hasService(['id' => 'index_mlb', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'menu'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$check = Validator::notEmpty()->validate($data['encodedFile']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['fileFormat']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['status']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['collId']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['table']);
$check = $check && Validator::arrayType()->notEmpty()->validate($data['data']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
$resId = StoreController::storeResource($data);
if (empty($resId) || !empty($resId['errors'])) {
return $response->withStatus(500)->withJson(['errors' => '[ResController create] ' . $resId['errors']]);
return $response->withJson(['resId' => $resId]);
public function createExt(Request $request, Response $response)
if (!ServiceModel::hasService(['id' => 'index_mlb', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'menu'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$data = $request->getParams();
$check = Validator::intVal()->notEmpty()->validate($data['resId']);
$check = $check && Validator::arrayType()->notEmpty()->validate($data['data']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
$document = ResModel::getById(['resId' => $data['resId'], 'select' => ['1']]);
if (empty($document)) {
return $response->withStatus(404)->withJson(['errors' => 'Document does not exist']);
}
$documentExt = ResModel::getExtById(['resId' => $data['resId'], 'select' => ['1']]);
if (!empty($documentExt)) {
return $response->withStatus(400)->withJson(['errors' => 'Document already exists in mlb_coll_ext']);
}
$formatedData = StoreController::prepareExtStorage(['resId' => $data['resId'], 'data' => $data['data']]);
$check = Validator::stringType()->notEmpty()->validate($formatedData['category_id']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
return $response->withJson(['status' => true]);
public function updateStatus(Request $request, Response $response)
{
$data = $request->getParams();
if (empty($data['status'])) {
$data['status'] = 'COU';
}
if (empty(StatusModel::getById(['id' => $data['status']]))) {
return $response->withStatus(400)->withJson(['errors' => _STATUS_NOT_FOUND]);
if (empty($data['historyMessage'])) {
$data['historyMessage'] = _UPDATE_STATUS;
}
Pegane Nestor
committed
$check = Validator::arrayType()->notEmpty()->validate($data['chrono']) || Validator::arrayType()->notEmpty()->validate($data['resId']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['status']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['historyMessage']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
Pegane Nestor
committed
$identifiers = !empty($data['chrono'])? $data['chrono']: $data['resId'] ;
foreach($identifiers as $id){
if (!empty($data['chrono'])) {
Pegane Nestor
committed
$document = ResModel::getResIdByAltIdentifier(['altIdentifier' => $id]);
Pegane Nestor
committed
$document = ResModel::getById(['resId' => $id, 'select' => ['res_id']]);
}
if (empty($document)) {
return $response->withStatus(400)->withJson(['errors' => _DOCUMENT_NOT_FOUND]);
}
if (!ResController::hasRightByResId(['resId' => $document['res_id'], 'userId' => $GLOBALS['userId']])) {
return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
}
Pegane Nestor
committed
ResModel::update(['set' => ['status' => $data['status']], 'where' => ['res_id = ?'], 'data' => [$document['res_id']]]);
Pegane Nestor
committed
HistoryController::add([
'tableName' => 'res_letterbox',
'recordId' => $document['res_id'],
'eventType' => 'UP',
'info' => $data['historyMessage'],
'moduleId' => 'apps',
'eventId' => 'resup',
]);
Pegane Nestor
committed
}
return $response->withJson(['success' => 'success']);
Pegane Nestor
committed
//EXTERNAL INFOS
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
public function updateExternalInfos(Request $request, Response $response){
$data = $request->getParams();
if(empty($data['externalInfos'])){
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
if(empty($data['status'])){
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$externalInfos = $data['externalInfos'];
foreach($externalInfos as $mail){
$check = Validator::intType()->validate($mail['res_id']);
$check = $check && Validator::StringType()->notEmpty()->validate($mail['external_id']);
$check = $check && Validator::StringType()->notEmpty()->validate($mail['external_link']);
if(!$check){
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$document = ResModel::getById(['resId' => $mail['res_id'], 'select' => ['res_id']]);
if (empty($document)) {
return $response->withStatus(400)->withJson(['errors' => _DOCUMENT_NOT_FOUND]);
}
if (!ResController::hasRightByResId(['resId' => $document['res_id'], 'userId' => $GLOBALS['userId']])) {
return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
}
ResModel::update(['set' => ['external_id' => $mail['external_id'] , 'external_link' => $mail['external_link'], 'status' => $data['status']], 'where' => ['res_id = ?'], 'data' => [$document['res_id']]]);
}
return $response->withJson(['success' => 'success']);
}
Pegane Nestor
committed
public function isLock(Request $request, Response $response, array $aArgs)
return $response->withJson(ResModel::isLock(['resId' => $aArgs['resId'], 'userId' => $GLOBALS['userId']]));
public function getNotesCountForCurrentUserById(Request $request, Response $response, array $aArgs)
return $response->withJson(NoteModel::countByResId(['resId' => $aArgs['resId'], 'userId' => $GLOBALS['userId']]));
public static function hasRightByResId(array $aArgs)
ValidatorModel::notEmpty($aArgs, ['resId', 'userId']);
ValidatorModel::stringType($aArgs, ['userId']);
ValidatorModel::intVal($aArgs, ['resId']);
if ($aArgs['userId'] == 'superadmin') {
return true;
}
$groups = UserModel::getGroupsByUserId(['userId' => $aArgs['userId']]);
$groupsClause = '';
foreach ($groups as $key => $group) {
if (!empty($group['where_clause'])) {
$groupClause = PreparedClauseController::getPreparedClause(['clause' => $group['where_clause'], 'userId' => $aArgs['userId']]);
if ($key > 0) {
$groupsClause .= ' or ';
}
$groupsClause .= "({$groupClause})";
}
if (!empty($groupsClause)) {
$res = ResModel::getOnView(['select' => [1], 'where' => ['res_id = ?', "({$groupsClause})"], 'data' => [$aArgs['resId']]]);
if (!empty($res)) {
return true;
}
$baskets = BasketModel::getBasketsByUserId(['userId' => $aArgs['userId'], 'unneededBasketId' => ['IndexingBasket']]);
$basketsClause = '';
foreach ($baskets as $key => $basket) {
if (!empty($basket['basket_clause'])) {
$basketClause = PreparedClauseController::getPreparedClause(['clause' => $basket['basket_clause'], 'userId' => $aArgs['userId']]);
if ($key > 0) {
$basketsClause .= ' or ';
}
$basketsClause .= "({$basketClause})";
}
if (!empty($basketsClause)) {
$res = ResModel::getOnView(['select' => [1], 'where' => ['res_id = ?', "({$basketsClause})"], 'data' => [$aArgs['resId']]]);
if (!empty($res)) {
return true;
public function getList(Request $request, Response $response)
$check = Validator::stringType()->notEmpty()->validate($data['clause']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['select']);
if(!empty($data['withFile'])){
$withFile = $data['withFile'] === 'true'? true: false;
$check = $check && Validator::boolType()->validate($withFile);
}
if(!empty($data['orderBy'])){
$check = $check && Validator::stringType()->validate($data['orderBy']);
$orderBy = explode(',',$data['orderBy']);
}
if(!empty($data['limit'])){
$limit = (int) $data['limit'];
$check = $check && Validator::intType()->validate($limit);
}
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
if (!PreparedClauseController::isRequestValid(['select' => $select,'clause' => $data['clause'], 'orderBy' => $orderBy, 'limit' => $limit, 'userId' => $GLOBALS['userId']])) {
return $response->withStatus(400)->withJson(['errors' => _INVALID_REQUEST]);
$where = [$data['clause']];
if ($GLOBALS['userId'] != 'superadmin') {
$groupsClause = GroupController::getGroupsClause(['userId' => $GLOBALS['userId']]);
if (empty($groupsClause)) {
return $response->withStatus(400)->withJson(['errors' => 'User has no groups']);
if($data['withFile'] === true){
$select[] = 'res_id';
}
$resources = ResModel::getOnView(['select' => $select, 'where' => $where, 'orderBy' => $orderBy, 'limit' => $limit]);
if($withFile === true){
foreach($resources as &$res){
$path = ResDocserverModel::getSourceResourcePath(['resId' => $res['res_id'], 'resTable' => 'res_view_letterbox', 'adrTable' => 'null']);
$file = file_get_contents($path);
$base64Content = base64_encode($file);
$res['fileBase64Content'] = $base64Content;
};
}
unset($res);
return $response->withJson(['resources' => $resources, 'count' => count($resources)]);