Skip to content
Snippets Groups Projects
ResController.php 58.2 KiB
Newer Older
  • Learn to ignore specific revisions
  •                     $diffusion['id'] = $item['user_id'];
                    } else {
                        $item = EntityModel::getById(['id' => $diffusion['id'], 'select' => ['entity_id']]);
                        $diffusion['id'] = $item['entity_id'];
                    }
    
    
                    if ($diffusion['mode'] == 'dest') {
                        ResModel::update(['set' => ['dest_user' => $diffusion['id']], 'where' => ['res_id = ?'], 'data' => [$args['resId']]]);
                    }
    
                    ListInstanceModel::create([
                        'res_id'            => $args['resId'],
                        'sequence'          => 0,
    
                        'item_id'           => $diffusion['id'],
    
                        'item_type'         => $diffusion['type'] == 'user' ? 'user_id' : 'entity_id',
    
                        'item_mode'         => $diffusion['mode'],
    
                        'added_by_user'     => $GLOBALS['userId'],
                        'difflist_type'     => 'entity_id'
                    ]);
                }
            }
    
            if (!empty($body['folders'])) {
                foreach ($body['folders'] as $folder) {
                    ResourceFolderModel::create(['res_id' => $args['resId'], 'folder_id' => $folder]);
    
            if (!empty($body['tags'])) {
                foreach ($body['tags'] as $tag) {
    
                    ResourceTagModel::create(['res_id' => $args['resId'], 'tag_id' => $tag]);
    
            if (!empty($body['senders'])) {
                foreach ($body['senders'] as $sender) {
                    ResourceContactModel::create(['res_id' => $args['resId'], 'item_id' => $sender['id'], 'type' => $sender['type'], 'mode' => 'sender']);
                }
            }
            if (!empty($body['recipients'])) {
                foreach ($body['recipients'] as $recipient) {
                    ResourceContactModel::create(['res_id' => $args['resId'], 'item_id' => $recipient['id'], 'type' => $recipient['type'], 'mode' => 'recipient']);
                }
            }
    
        private static function updateAdjacentData(array $args)
        {
            ValidatorModel::notEmpty($args, ['resId', 'body']);
            ValidatorModel::intVal($args, ['resId']);
            ValidatorModel::arrayType($args, ['body']);
    
            $body = $args['body'];
    
    
            $entities = EntityModel::getWithUserEntities([
                'select' => ['entities.id'],
                'where'  => ['user_id = ?'],
                'data'   => [$GLOBALS['userId']]
            ]);
            $entities = array_column($entities, 'id');
            $idToDelete = FolderModel::getWithEntitiesAndResources([
                'select'    => ['resources_folders.id'],
    
                'where'     => ['resources_folders.res_id = ?', '(entities_folders.entity_id in (?) OR folders.user_id = ? OR keyword = ?)'],
                'data'      => [$args['resId'], $entities, $GLOBALS['id'], 'ALL_ENTITIES']
    
            ]);
            $idToDelete = array_column($idToDelete, 'id');
            if (!empty($idToDelete)) {
                ResourceFolderModel::delete(['where' => ['id in (?)'], 'data' => [$idToDelete]]);
            }
    
            if (!empty($body['folders'])) {
                foreach ($body['folders'] as $folder) {
                    ResourceFolderModel::create(['res_id' => $args['resId'], 'folder_id' => $folder]);
                }
            }
    
            ResourceTagModel::delete(['where' => ['res_id = ?'], 'data' => [$args['resId']]]);
    
            if (!empty($body['tags'])) {
                foreach ($body['tags'] as $tag) {
    
                    ResourceTagModel::create(['res_id' => $args['resId'], 'tag_id' => $tag]);
    
            ResourceContactModel::delete(['where' => ['res_id = ?'], 'data' => [$args['resId']]]);
            if (!empty($body['senders'])) {
                foreach ($body['senders'] as $sender) {
                    ResourceContactModel::create(['res_id' => $args['resId'], 'item_id' => $sender['id'], 'type' => $sender['type'], 'mode' => 'sender']);
                }
            }
            if (!empty($body['recipients'])) {
                foreach ($body['recipients'] as $recipient) {
                    ResourceContactModel::create(['res_id' => $args['resId'], 'item_id' => $recipient['id'], 'type' => $recipient['type'], 'mode' => 'recipient']);
                }
            }
    
        public function getList(Request $request, Response $response)
        {
            $data = $request->getParams();
    
            if (!Validator::stringType()->notEmpty()->validate($data['select'])) {
                return $response->withStatus(400)->withJson(['errors' => 'Bad Request: select is not valid']);
            }
            if (!Validator::stringType()->notEmpty()->validate($data['clause'])) {
                return $response->withStatus(400)->withJson(['errors' => 'Bad Request: clause is not valid']);
            }
            if (!empty($data['withFile'])) {
                if (!Validator::boolType()->validate($data['withFile'])) {
                    return $response->withStatus(400)->withJson(['errors' => 'Bad Request: withFile parameter is not a boolean']);
                }
            }
    
            if (!empty($data['orderBy'])) {
                if (!Validator::arrayType()->notEmpty()->validate($data['orderBy'])) {
                    return $response->withStatus(400)->withJson(['errors' => 'Bad Request: orderBy parameter not valid']);
                }
            }
    
            if (!empty($data['limit'])) {
                if (!Validator::intType()->validate($data['limit'])) {
                    return $response->withStatus(400)->withJson(['errors' => 'Bad Request: limit parameter not valid']);
                }
            }
            $select = explode(',', $data['select']);
    
            $sve_start_date = false;
            $keySve = array_search('sve_start_date', array_map('trim', $select));
            if ($keySve !== false) {
                unset($select[$keySve]);
                $sve_start_date = true;
            }
    
            if ($sve_start_date && empty($select)) {
                $select[] = 'res_id';
            }
    
            if (!PreparedClauseController::isRequestValid(['select' => $select, 'clause' => $data['clause'], 'orderBy' => $data['orderBy'], 'limit' => $data['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']);
                }
                $where[] = "({$groupsClause})";
            }
    
            if ($data['withFile'] === true) {
                $select[] = 'res_id';
            }
    
            $resources = ResModel::getOnView(['select' => $select, 'where' => $where, 'orderBy' => $data['orderBy'], 'limit' => $data['limit']]);
            if (!empty($resources) && $data['withFile'] === true) {
                foreach ($resources as $key => $res) {
                    $document = ResModel::getById(['resId' => $res['res_id'], 'select' => ['path', 'filename', 'docserver_id']]);
                    $docserver = DocserverModel::getByDocserverId(['docserverId' => $document['docserver_id'], 'select' => ['path_template', 'docserver_type_id']]);
                    if (empty($docserver['path_template']) || !file_exists($docserver['path_template'])) {
                        continue;
                    }
                    $pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $document['path']) . $document['filename'];
                    if (!file_exists($pathToDocument)) {
                        continue;
                    }
                    $file = file_get_contents($pathToDocument);
                    $base64Content = base64_encode($file);
                    $resources[$key]['fileBase64Content'] = $base64Content;
                }
            }
            if (!empty($resources) && $sve_start_date) {
                $aResId = [];
                foreach ($resources as $res) {
                    $aResId[] = $res['res_id'];
                }
                $aSveStartDate = AcknowledgementReceiptModel::getByResIds([
                    'select'  => ['res_id', 'min(send_date) as send_date'],
                    'resIds'  => $aResId,
                    'where'   => ['send_date IS NOT NULL', 'send_date != \'\''],
                    'groupBy' => ['res_id']
                ]);
                foreach ($resources as $key => $res) {
                    $resources[$key]['sve_start_date'] = null;
                    foreach ($aSveStartDate as $valueSveStartDate) {
                        if ($res['res_id'] == $valueSveStartDate['res_id']) {
                            $resources[$key]['sve_start_date'] = $valueSveStartDate['send_date'];
                            break;
                        }
                    }
                }
            }
    
            return $response->withJson(['resources' => $resources, 'count' => count($resources)]);
        }
    
    
        public function getProcessingData(Request $request, Response $response, array $args)
        {
            if (!Validator::intVal()->validate($args['groupId'])) {
                return $response->withStatus(403)->withJson(['errors' => 'resId param is not an integer']);
            }
            if (!Validator::intVal()->validate($args['userId'])) {
                return $response->withStatus(403)->withJson(['errors' => 'userId param is not an integer']);
            }
            if (!Validator::intVal()->validate($args['basketId'])) {
                return $response->withStatus(403)->withJson(['errors' => 'basketId param is not an integer']);
            }
            if (!Validator::intVal()->validate($args['resId'])) {
                return $response->withStatus(403)->withJson(['errors' => 'resId param is not an integer']);
            }
    
            $control = ResourceListController::listControl(['groupId' => $args['groupId'], 'userId' => $args['userId'], 'basketId' => $args['basketId'], 'currentUserId' => $GLOBALS['id']]);
            if (!empty($control['errors'])) {
                return $response->withStatus($control['code'])->withJson(['errors' => $control['errors']]);
            }
    
            $basket = BasketModel::getById(['id' => $args['basketId'], 'select' => ['basket_id']]);
            $group = GroupModel::getById(['id' => $args['groupId'], 'select' => ['group_id']]);
    
            $groupBasket = GroupBasketModel::get(['select' => ['list_event_data'], 'where' => ['basket_id = ?', 'group_id = ?'], 'data' => [$basket['basket_id'], $group['group_id']]]);
    
            if (empty($groupBasket[0]['list_event_data'])) {
                return $response->withJson(['listEventData' => null]);
            }
    
            $listEventData = json_decode($groupBasket[0]['list_event_data'], true);
    
            $resource = ResModel::getById(['resId' => $args['resId'], 'select' => ['status']]);
            if (empty($resource['status'])) {
    
                return $response->withStatus(400)->withJson(['errors' => 'Status does not exist']);
    
            }
            $status = StatusModel::getById(['id' => $resource['status'], 'select' => ['can_be_modified']]);
            if ($status['can_be_modified'] != 'Y') {
                $listEventData['canUpdate'] = false;
            }
    
            return $response->withJson(['listEventData' => $listEventData]);
        }
    
    
        public function getResourceFileInformation(Request $request, Response $response, array $args)
        {
            if (!ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) {
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
            }
    
            $resource = ResModel::getById([
                'resId'  => $args['resId'],
                'select' => ['format', 'fingerprint', 'filesize', 'fulltext_result']
            ]);
    
            $allowedFiles = StoreController::getAllowedFiles();
            $allowedFiles = array_column($allowedFiles, 'extension');
    
            $resource['canConvert'] = in_array(strtoupper($resource['format']), $allowedFiles);
    
            return $response->withJson(['information' => $resource]);
        }