Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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 Link Controller
* @author dev@maarch.org
*/
namespace Resource\controllers;
use Entity\models\EntityModel;
use Entity\models\ListInstanceModel;
use Resource\models\ResModel;
use Resource\models\ResourceContactModel;
use Respect\Validation\Validator;
use Slim\Http\Request;
use Slim\Http\Response;
use Status\models\StatusModel;
use User\models\UserModel;
class LinkController
{
public function getLinkedResources(Request $request, Response $response, array $args)
{
if (!Validator::intVal()->validate($args['resId']) || !ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Resource out of perimeter']);
}
$resource = ResModel::getById(['resId' => $args['resId'], 'select' => ['linked_resources']]);
$linkedResourcesIds = json_decode($resource['linked_resources'], true);
$linkedResources = [];
if (!empty($linkedResourcesIds)) {
$linkedResources = ResModel::get([
'select' => ['res_id as "resId"', 'subject', 'doc_date as "documentDate"', 'status', 'dest_user as "destUser"', 'destination', 'alt_identifier as chrono', 'category_id as "categoryId"', 'confidentiality'],
'where' => ['res_id in (?)'],
'data' => [$linkedResourcesIds]
]);
foreach ($linkedResources as $key => $value) {
$linkedResources[$key]['confidentiality'] = $value['confidentiality'] == 'Y';
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
if (!empty($value['status'])) {
$status = StatusModel::getById(['id' => $value['status'], 'select' => ['label_status', 'img_filename']]);
$linkedResources[$key]['statusLabel'] = $status['label_status'];
$linkedResources[$key]['statusImage'] = $status['img_filename'];
}
if (!empty($value['destUser'])) {
$linkedResources[$key]['destUserLabel'] = UserModel::getLabelledUserById(['login' => $value['destUser']]);
}
if (!empty($value['destination'])) {
$linkedResources[$key]['destinationLabel'] = EntityModel::getByEntityId(['entityId' => $value['destination'], 'select' => ['short_label']])['short_label'];
}
$contacts = ResourceContactModel::get([
'select' => ['item_id as id', 'type', 'mode'],
'where' => ['res_id = ?'],
'data' => [$value['resId']]
]);
$linkedResources[$key]['senders'] = [];
$linkedResources[$key]['recipients'] = [];
foreach ($contacts as $contact) {
$linkedResources[$key]["{$contact['mode']}s"][] = $contact;
}
$linkedResources[$key]['visaCircuit'] = ListInstanceModel::get(['select' => ['item_id', 'item_mode'], 'where' => ['res_id = ?', 'difflist_type = ?'], 'data' => [$value['resId'], 'VISA_CIRCUIT']]);
foreach ($linkedResources[$key]['visaCircuit'] as $keyCircuit => $valueCircuit) {
$linkedResources[$key]['visaCircuit'][$keyCircuit]['userLabel'] = UserModel::getLabelledUserById(['login' => $valueCircuit['item_id']]);
}
}
}
return $response->withJson(['linkedResources' => $linkedResources]);
}
public function linkResources(Request $request, Response $response, array $args)
{
if (!Validator::intVal()->validate($args['resId']) || !ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Resource out of perimeter']);
}
$body = $request->getParsedBody();
if (!Validator::arrayType()->notEmpty()->validate($body['linkedResources'])) {
return $response->withStatus(403)->withJson(['errors' => 'Body linkedResources is empty or not an array']);
} elseif (!ResController::hasRightByResId(['resId' => $body['linkedResources'], 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Body linkedResources out of perimeter']);
} elseif (in_array($args['resId'], $body['linkedResources'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body linkedResources contains resource']);
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
}
$resource = ResModel::getById(['resId' => $args['resId'], 'select' => ['linked_resources']]);
$linkedResources = json_decode($resource['linked_resources'], true);
$linkedResources = array_merge($linkedResources, $body['linkedResources']);
$linkedResources = array_unique($linkedResources);
foreach ($linkedResources as $key => $value) {
$linkedResources[$key] = (string)$value;
}
ResModel::update([
'set' => ['linked_resources' => json_encode($linkedResources)],
'where' => ['res_id = ?'],
'data' => [$args['resId']]
]);
ResModel::update([
'postSet' => ['linked_resources' => "jsonb_insert(linked_resources, '{0}', '\"{$args['resId']}\"')"],
'where' => ['res_id in (?)', "(linked_resources @> ?) = false"],
'data' => [$body['linkedResources'], "\"{$args['resId']}\""]
]);
return $response->withStatus(204);
}
public function unlinkResources(Request $request, Response $response, array $args)
{
if (!Validator::intVal()->validate($args['resId']) || !ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Resource out of perimeter']);
}
if (!Validator::intVal()->validate($args['id']) || !ResController::hasRightByResId(['resId' => [$args['id']], 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Resource to unlink out of perimeter']);
}
ResModel::update([
'postSet' => ['linked_resources' => "linked_resources - '{$args['id']}'"],
'where' => ['res_id = ?'],
'data' => [$args['resId']]
]);
ResModel::update([
'postSet' => ['linked_resources' => "linked_resources - '{$args['resId']}'"],
'where' => ['res_id = ?'],
'data' => [$args['id']]
]);
return $response->withStatus(204);
}
}