Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
stresslevel.php
1<?php
2
4
10
11class StressLevel extends Base
12{
13 private function isCurrentUserAdmin(): bool
14 {
15 Loader::includeModule('socialnetwork');
16
17 return \CSocNetUser::isCurrentUserModuleAdmin(SITE_ID, false);
18 }
19
20 public function addAction(array $fields = []): ?array
21 {
22 $value = (
23 isset($fields['value'])
24 ? (int)$fields['value']
25 : false
26 );
27
28 if ($value === false)
29 {
30 $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_USER_STRESSLEVEL_ADD_NOSTRESS'), 'SONET_CONTROLLER_USER_STRESSLEVEL_ADD_NOSTRESS'));
31 return null;
32 }
33
34 $userId = (int)(
35 isset($fields['userId'])
36 && $this->isCurrentUserAdmin()
37 ? $fields['userId']
38 : $this->getCurrentUser()->getId()
39 );
40
41 if (
42 !$this->isCurrentUserAdmin()
43 && $userId !== (int)$this->getCurrentUser()->getId()
44 )
45 {
46 $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_USER_STRESSLEVEL_NO_PERMISSIONS'), 'SONET_CONTROLLER_USER_STRESSLEVEL_NO_PERMISSIONS'));
47 return null;
48 }
49
50 Loader::includeModule('socialnetwork');
51
52 $disclaimerData = $this->getDisclaimer([
53 'userId' => $userId
54 ]);
55 if (empty($disclaimerData))
56 {
57 $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_USER_STRESSLEVEL_NO_SIGNED_DISCLAIMER'), 'SONET_CONTROLLER_USER_STRESSLEVEL_NO_SIGNED_DISCLAIMER'));
58 return null;
59 }
60
61 UserWelltoryTable::add([
62 'USER_ID' => $userId,
63 'STRESS' => $value,
64 'STRESS_TYPE' => ($fields['type'] ?? ''),
65 'STRESS_COMMENT' => ($fields['comment'] ?? ''),
66 'DATE_MEASURE' => new \Bitrix\Main\DB\SqlExpression(\Bitrix\Main\Application::getConnection()->getSqlHelper()->getCurrentDateTimeFunction()),
67 'HASH' => ($fields['hash'] ?? '')
68 ]);
69
70 return [
71 'success' => true,
72 ];
73 }
74
75 public function getAction(array $fields = [])
76 {
77 $result = [];
78
79 $userId = (int)($fields['userId'] ?? $this->getCurrentUser()->getId());
80
81 if ($userId <= 0)
82 {
83 $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_USER_STRESSLEVEL_GET_NOUSER_ID'), 'SONET_CONTROLLER_USER_STRESSLEVEL_GET_NOUSER_ID'));
84 return null;
85 }
86
87 if (
88 $this->getAccess([
89 'userId' => $userId
90 ]) !== 'Y'
91 && $userId !== (int)$this->getCurrentUser()->getId()
92 )
93 {
94 return $result;
95 }
96
97 $data = \Bitrix\Socialnetwork\Item\UserWelltory::getHistoricData([
98 'userId' => $userId,
99 'limit' => 1
100 ]);
101 if (!empty($data))
102 {
103 $result = $data[0];
104 }
105
106 $parameters = $this->getUnsignedParameters();
107
108 if (
109 !empty($parameters)
110 && !empty($parameters['PATH_TO_USER_STRESSLEVEL'])
111 )
112 {
113 $url = \CComponentEngine::makePathFromTemplate($parameters["PATH_TO_USER_STRESSLEVEL"], array("user_id" => $userId));
114
115 $uri = new \Bitrix\Main\Web\Uri($url);
116 $uri->addParams([
117 'page' => 'result'
118 ]);
119
120 $result['url'] = [
121 'check' => $url,
122 'result' => $uri->getUri()
123 ];
124 }
125
126 return $result;
127 }
128
129 public function getAccess(array $fields = [])
130 {
131 $userId = (int)($fields['userId'] ?? $this->getCurrentUser()->getId());
132
133 return \Bitrix\Socialnetwork\Item\UserWelltory::getAccess([
134 'userId' => $userId
135 ]);
136 }
137
138 public function setAccess(array $fields = [])
139 {
140 $userId = (
141 isset($fields['userId'])
142 ? (int)$fields['userId']
143 : $this->getCurrentUser()->getId()
144 );
145
146 $value = (
147 isset($fields['value'])
148 && $fields['value'] === 'Y'
149 ? 'Y'
150 : 'N'
151 );
152
153 return \Bitrix\Socialnetwork\Item\UserWelltory::setAccess([
154 'userId' => $userId,
155 'value' => $value
156 ]);
157 }
158
159 public function getAccessAction(array $fields = []): ?array
160 {
161 $userId = (int)($fields['userId'] ?? 0);
162
163 if ($userId <= 0)
164 {
165 $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_USER_STRESSLEVEL_GET_NOUSER_ID'), 'SONET_CONTROLLER_USER_STRESSLEVEL_GET_NOUSER_ID'));
166 return null;
167 }
168
169 if (
170 !$this->isCurrentUserAdmin()
171 && $userId !== (int)$this->getCurrentUser()->getId()
172
173 )
174 {
175 $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_USER_STRESSLEVEL_NO_PERMISSIONS'), 'SONET_CONTROLLER_USER_STRESSLEVEL_NO_PERMISSIONS'));
176 return null;
177 }
178
179 return [
180 'value' => $this->getAccess([
181 'userId' => $userId
182 ])
183 ];
184 }
185
186 public function setAccessAction(array $fields = []): ?array
187 {
188 $userId = (int)($fields['userId'] ?? 0);
189
190 $value = (
191 isset($fields['value'])
192 && $fields['value'] === 'Y'
193 ? 'Y'
194 : 'N'
195 );
196
197 if ($userId <= 0)
198 {
199 $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_USER_STRESSLEVEL_GET_NOUSER_ID'), 'SONET_CONTROLLER_USER_STRESSLEVEL_GET_NOUSER_ID'));
200 return null;
201 }
202
203 if (
204 !$this->isCurrentUserAdmin()
205 && $userId !== (int)$this->getCurrentUser()->getId()
206 )
207 {
208 $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_USER_STRESSLEVEL_NO_PERMISSIONS'), 'SONET_CONTROLLER_USER_STRESSLEVEL_NO_PERMISSIONS'));
209 return null;
210 }
211
212 return [
213 'value' => $this->setAccess([
214 'userId' => $userId,
215 'value' => $value
216 ])
217 ];
218 }
219
220 public function getValueDescriptionAction($type = '', $value = false): ?array
221 {
222 if ($value !== false)
223 {
224 $value = (int)$value;
225 }
226 else
227 {
228 $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_USER_STRESSLEVEL_ADD_NOSTRESS'), 'SONET_CONTROLLER_USER_STRESSLEVEL_ADD_NOSTRESS'));
229 return null;
230 }
231
232 $result = (Loader::includeModule('intranet') ? \Bitrix\Intranet\Component\UserProfile\StressLevel::getValueDescription($type, $value) : '');
233
234 return [
235 'description' => $result
236 ];
237 }
238
239 private function getDisclaimer(array $fields = [])
240 {
241 $result = [];
242 $userId = (int)($fields['userId'] ?? 0);
243
244 if ($userId <= 0)
245 {
246 $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_USER_STRESSLEVEL_GET_NOUSER_ID'), 'SONET_CONTROLLER_USER_STRESSLEVEL_GET_NOUSER_ID'));
247 return null;
248 }
249
250 $res = \Bitrix\Socialnetwork\UserWelltoryDisclaimerTable::getList([
251 'filter' => [
252 'USER_ID' => $userId
253 ],
254 'order' => [
255 'ID' => 'ASC'
256 ],
257 'select' => [ 'ID', 'DATE_SIGNED' ],
258 'limit' => 1
259 ]);
260 if ($disclaimerFields = $res->fetch())
261 {
262 $result = $disclaimerFields;
263 }
264
265 return $result;
266 }
267
268 public function setDisclaimerAction()
269 {
270 $userId = $this->getCurrentUser()->getId();
271
272 $result = $this->getDisclaimer([
273 'userId' => $userId
274 ]);
275 if (!empty($result))
276 {
277 return $result;
278 }
279
280 if (\Bitrix\Socialnetwork\UserWelltoryDisclaimerTable::add([
281 'USER_ID' => $this->getCurrentUser()->getId(),
282 'DATE_SIGNED' => new \Bitrix\Main\DB\SqlExpression(\Bitrix\Main\Application::getConnection()->getSqlHelper()->getCurrentDateTimeFunction()),
283 ])->isSuccess())
284 {
285 $result = $this->getDisclaimer([
286 'userId' => $userId
287 ]);
288 }
289
290 return $result;
291 }
292
293 public function getDisclaimerAction()
294 {
295 $result = [];
296
297 $res = \Bitrix\Socialnetwork\UserWelltoryDisclaimerTable::getList([
298 'filter' => [
299 'USER_ID' => $this->getCurrentUser()->getId()
300 ],
301 'order' => [
302 'ID' => 'ASC'
303 ],
304 'select' => [ 'ID', 'DATE_SIGNED' ],
305 'limit' => 1
306 ]);
307
308 if ($disclaimerFields = $res->fetch())
309 {
310 $result = $disclaimerFields;
311 }
312
313 return $result;
314 }
315}
316
static getConnection($name="")
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29