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
41
42
43
44
45
46
47
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
96
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
195
196
197
198
199
200
201
202
203
204
205
206
<?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 Registered Number Range Controller
* @author dev@maarch.org
*/
namespace RegisteredMail\controllers;
use Group\controllers\PrivilegeController;
use History\controllers\HistoryController;
use RegisteredMail\models\IssuingSiteModel;
use RegisteredMail\models\RegisteredNumberRangeModel;
use Respect\Validation\Validator;
use Slim\Http\Request;
use Slim\Http\Response;
class RegisteredNumberRangeController
{
public function get(Request $request, Response $response)
{
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_registered_mail', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$ranges = RegisteredNumberRangeModel::get();
foreach ($ranges as $key => $range) {
$ranges[$key] = [
'id' => $range['id'],
'type' => $range['type'],
'trackingAccountNumber' => $range['tracking_account_number'] ?? null,
'rangeStart' => $range['range_start'] ?? null,
'rangeEnd' => $range['range_end'] ?? null,
'creator' => $range['creator'] ?? null,
'created' => $range['created'] ?? null,
'siteId' => $range['site_id'] ?? null
];
}
return $response->withJson(['ranges' => $ranges]);
}
public function getById(Request $request, Response $response, array $args)
{
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_registered_mail', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$range = RegisteredNumberRangeModel::getById(['id' => $args['id']]);
if (empty($range)) {
return $response->withStatus(400)->withJson(['errors' => 'Range not found']);
}
$range = [
'id' => $range['id'],
'type' => $range['type'],
'trackingAccountNumber' => $range['tracking_account_number'] ?? null,
'rangeStart' => $range['range_start'] ?? null,
'rangeEnd' => $range['range_end'] ?? null,
'creator' => $range['creator'] ?? null,
'created' => $range['created'] ?? null,
'siteId' => $range['site_id'] ?? null
];
return $response->withJson(['range' => $range]);
}
public function create(Request $request, Response $response)
{
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_registered_mail', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$body = $request->getParsedBody();
if (!Validator::stringType()->notEmpty()->validate($body['type']) || !in_array($body['type'], ['nationalWithAr', 'nationalNoAr', 'international'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body type is empty or not a value between nationalWithAr, nationalNoAr or international']);
}
if (!Validator::intVal()->notEmpty()->validate($body['rangeStart'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body rangeStart is empty or not an integer']);
}
if (!Validator::intVal()->notEmpty()->validate($body['rangeEnd'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body rangeEnd is empty or not an integer']);
}
if (!Validator::intVal()->notEmpty()->validate($body['siteId'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body siteId is empty or not an integer']);
}
$site = IssuingSiteModel::getById(['id' => $body['siteId']]);
if (empty($site)) {
return $response->withStatus(400)->withJson(['errors' => 'Body siteId does not exist']);
}
$id = RegisteredNumberRangeModel::create([
'type' => $body['type'],
'trackingAccountNumber' => $body['trackingAccountNumber'] ?? null,
'rangeStart' => $body['rangeStart'],
'rangeEnd' => $body['rangeEnd'],
'creator' => $GLOBALS['id'],
'siteId' => $body['siteId']
]);
HistoryController::add([
'tableName' => 'registered_number_range',
'recordId' => $id,
'eventType' => 'ADD',
'info' => _REGISTERED_NUMBER_RANGE_CREATED . " : {$id}",
'moduleId' => 'registered_number_range',
'eventId' => 'registered_number_rangeCreation',
]);
return $response->withJson(['id' => $id]);
}
public function update(Request $request, Response $response, array $args)
{
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_registered_mail', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$range = RegisteredNumberRangeModel::getById(['id' => $args['id']]);
if (empty($range)) {
return $response->withStatus(400)->withJson(['errors' => 'Range not found']);
}
$body = $request->getParsedBody();
if (!Validator::stringType()->notEmpty()->validate($body['type']) || !in_array($body['type'], ['nationalWithAr', 'nationalNoAr', 'international'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body type is empty or not a value between nationalWithAr, nationalNoAr or international']);
}
if (!Validator::intVal()->notEmpty()->validate($body['rangeStart'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body rangeStart is empty or not an integer']);
}
if (!Validator::intVal()->notEmpty()->validate($body['rangeEnd'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body rangeEnd is empty or not an integer']);
}
if (!Validator::intVal()->notEmpty()->validate($body['siteId'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body siteId is empty or not an integer']);
}
$site = IssuingSiteModel::getById(['id' => $body['siteId']]);
if (empty($site)) {
return $response->withStatus(400)->withJson(['errors' => 'Body siteId does not exist']);
}
RegisteredNumberRangeModel::update([
'set' => [
'type' => $body['type'],
'tracking_account_number' => $body['trackingAccountNumber'],
'range_start' => $body['rangeStart'],
'range_end' => $body['rangeEnd'],
'creator' => $GLOBALS['id'],
'site_id' => $body['siteId']
],
'where' => ['id = ?'],
'data' => [$args['id']]
]);
HistoryController::add([
'tableName' => 'issuing_sites',
'recordId' => $args['id'],
'eventType' => 'UP',
'info' => _REGISTERED_NUMBER_RANGE_UPDATED . " : {$args['id']}",
'moduleId' => 'issuing_sites',
'eventId' => 'issuingSitesModification',
]);
return $response->withStatus(204);
}
public function delete(Request $request, Response $response, array $args)
{
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_registered_mail', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$site = RegisteredNumberRangeModel::getById(['id' => $args['id']]);
if (empty($site)) {
return $response->withStatus(204);
}
RegisteredNumberRangeModel::delete([
'where' => ['id = ?'],
'data' => [$args['id']]
]);
HistoryController::add([
'tableName' => 'registered_number_range',
'recordId' => $args['id'],
'eventType' => 'DEL',
'info' => _REGISTERED_NUMBER_RANGE_DELETED . " : {$args['id']}",
'moduleId' => 'registered_number_range',
'eventId' => 'registeredNumberRangeSuppression',
]);
return $response->withStatus(204);
}
}