Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mask.php
1<?php
2
4
7use Bitrix\UI;
8use Bitrix\UI\Avatar;
9use \Bitrix\Main\Engine\Response;
10use \Bitrix\Main\UI\PageNavigation;
11
13{
14 public function getSystemListAction(PageNavigation $pageNavigation, Main\Engine\CurrentUser $currentUser): Response\DataType\Page
15 {
16 return static::getList(
17 Avatar\Model\ItemTable::query()
18 ->setFilter([
19 '=OWNER_TYPE' => [
20 Avatar\Mask\Owner\System::class,
21 Avatar\Mask\Owner\RestApp::class
22 ],
23 ])
24 ,
25 $pageNavigation,
26 $currentUser
27 );
28 }
29
30 public function getUserListAction(PageNavigation $pageNavigation, Main\Engine\CurrentUser $currentUser): Response\DataType\Page
31 {
32 return static::getList(
33 Avatar\Model\ItemTable::query()
34 ->setFilter([
35 '=OWNER_TYPE' => Avatar\Mask\Owner\User::class,
36 '=OWNER_ID' => $currentUser->getId()
37 ])
38 ,
39 $pageNavigation,
40 $currentUser
41 );
42 }
43
44 public function getSharedListAction(PageNavigation $pageNavigation, Main\Engine\CurrentUser $currentUser): Response\DataType\Page
45 {
46 return static::getList(
47 Avatar\Model\ItemTable::query()
48 ->setFilter([
49 '=SHARED_FOR.USER_ACCESS.USER_ID' => $currentUser->getId(),
50 '=OWNER_TYPE' => Avatar\Mask\Owner\User::class,
51 '!=OWNER_ID' => $currentUser->getId()
52 ])
53 ->setDistinct()
54 ->addOrder('SHARED_FOR.ID', 'DESC')
55 ,
56 $pageNavigation,
57 $currentUser
58 );
59 }
60
61 public function getRecentlyUsedListAction(PageNavigation $pageNavigation, Main\Engine\CurrentUser $currentUser): Response\DataType\Page
62 {
63 return static::getList(
64 Avatar\Model\ItemTable::query()
65 ->setSelect(['ID', 'FILE_ID', 'TITLE', 'DESCRIPTION', 'SORT'])
66 ->setOrder(['RECENTLY_USED_BY.ID' => 'DESC', 'ID' => 'DESC'])
67 ->setFilter([
68 '=RECENTLY_USED_BY.USER_ID' => $currentUser->getId()
69 ])
70 ->addOrder('RECENTLY_USED_BY.ID', 'DESC')
71 ->setDistinct()
72 ,
73 $pageNavigation,
74 $currentUser
75 );
76 }
77
78 protected function getList(
79 Main\ORM\Query\Query $query,
80 PageNavigation $pageNavigation,
81 Main\Engine\CurrentUser $currentUser
82 ): Response\DataType\Page
83 {
84 if (count($query->getSelect()) <= 0)
85 {
86 $query
87 ->setSelect(['ID', 'GROUP_ID', 'FILE_ID', 'TITLE', 'DESCRIPTION', 'SORT'])
88 ->setOrder(['GROUP_ID' => 'ASC', 'SORT' => 'ASC', 'ID' => 'DESC'])
89 ;
90 }
91 $dbRes = $query
92 ->setLimit($pageNavigation->getLimit())
93 ->setOffset($pageNavigation->getOffset())
94 ->setCacheTtl(86400)
95 ->exec();
96 $result = [];
97 while (($res = $dbRes->fetch()) && $res)
98 {
99 if ($file = \CFile::GetFileArray($res['FILE_ID']))
100 {
101 $groupId = (int) ($res['GROUP_ID'] ?? 0);
102 if (!isset($result[$groupId]))
103 {
104 $result[$groupId] = ['items' => []];
105 }
106 $result[$groupId]['items'][] = [
107 'id' => $res['ID'],
108 'title' => $res['TITLE'],
109 'description' => $res['DESCRIPTION'],
110 'src' => $file['SRC'],
111 //TODO load ,
112 'editable' => Avatar\Mask\Item::getInstance($res['ID'])->isEditableBy(
113 Avatar\Mask\Consumer::createFromId($currentUser->getId())
114 )
115 ];
116 }
117 }
118
119 $groupIds = array_keys($result);
120 if (array_sum($groupIds) > 0)
121 {
122 $dbRes = Avatar\Model\GroupTable::getList([
123 'select' => [
124 'ID', 'TITLE'
125 ],
126 'filter' => [
127 '=ID' => $groupIds,
128 ],
129 'order' => [
130 'SORT' => 'ASC',
131 'ID' => 'ASC'
132 ],
133 'cache' => [
134 'ttl' => 86400
135 ]
136 ]);
137 while ($res = $dbRes->fetch())
138 {
139 if (isset($result[$res['ID']]))
140 {
141 $result[$res['ID']]['title'] = $res['TITLE'];
142 $result[$res['ID']]['id'] = $res['ID'];
143 }
144 }
145 }
146
147 return new Response\DataType\Page('groupedItems', array_values($result), null);
148 }
149
150 public function getMaskAccessCodeAction($id, Main\Engine\CurrentUser $currentUser): Response\AjaxJson
151 {
152 $result = $this->checkEditability($id, $currentUser);
153 if ($result->isSuccess())
154 {
155 ['item' => $item] = $result->getData();
156 return Response\AjaxJson::createSuccess(
157 ['accessCode' => Main\UI\EntitySelector\Converter::convertFromFinderCodes($item->getAccessCode())]
158 );
159 }
160 return Response\AjaxJson::createDenied();
161 }
162
163 protected function checkEditability($id, Main\Engine\CurrentUser $currentUser): Main\Result
164 {
165 $result = new Main\Result();
166 $consumer = Avatar\Mask\Consumer::createFromId($currentUser->getId());
167
168 if (!($item = Avatar\Mask\Item::getInstance($id)))
169 {
170 $result->addError(new Main\Error("Mask with id {$id} is not found.", 'Not found.'));
171 }
172 elseif (!$item->isEditableBy($consumer))
173 {
174 $result->addError(new Main\Error("Mask with id {$id} is not editable.", 'Access denied.'));
175 }
176 else
177 {
178 $result->setData(['item' => $item]);
179 }
180 return $result;
181 }
182
183 public function saveAction($id, $title, $accessCode, $file, Main\Engine\CurrentUser $currentUser): Response\AjaxJson
184 {
185 $destCodesList = Main\UI\EntitySelector\Converter::convertToFinderCodes($accessCode);
186 $file = ($file['changed'] === 'Y' ? $this->getRequest()->getFile('file') : null);
187 if ($id > 0)
188 {
189 $result = $this->checkEditability($id, $currentUser);
190 if ($result->isSuccess())
191 {
192 ['item' => $item] = $result->getData();
193 /* @var Avatar\Mask\Item $item*/
194 $result = $item->update([
195 'TITLE' => $title,
196 'ACCESS_CODE' => $destCodesList
197 ] + (!empty($file) ? [
198 'FILE' => $file] : [])
199 );
200 }
201 }
202 else
203 {
204 $result = Avatar\Mask\Item::create(
205 new Avatar\Mask\Owner\User($currentUser->getId()),
206 $file,
207 [
208 'TITLE' => $title,
209 'ACCESS_CODE' => $destCodesList
210 ]
211 );
212 if ($result->isSuccess())
213 {
214 $id = $result->getId();
215 }
216 }
217 if ($result->isSuccess())
218 {
219 $responsePage = static::getList(
220 Avatar\Model\ItemTable::query()
221 ->setFilter([
222 '=ID' => $id
223 ])
224 ,
225 (new PageNavigation('justBuffNav'))->setPageSize(1),
226 $currentUser
227 );
228 if (($groupedItems = $responsePage->getItems())
229 && ($itemsFromOneGroup = reset($groupedItems))
230 && isset($itemsFromOneGroup['items'])
231 && ($itemData = reset($itemsFromOneGroup['items']))
232 )
233 {
234 $result = $this->checkEditability($id, $currentUser);
235 if ($result->isSuccess())
236 {
237 ['item' => $item] = $result->getData();
238 $itemData['accessCode'] = Main\UI\EntitySelector\Converter::convertFromFinderCodes($item->getAccessCode());
239 }
240 return Response\AjaxJson::createSuccess(
241 $itemData
242 );
243 }
244 }
245
246 return Response\AjaxJson::createError($result->getErrorCollection());
247 }
248
249 public function deleteAction($id, Main\Engine\CurrentUser $currentUser): Response\AjaxJson
250 {
251 $result = $this->checkEditability($id, $currentUser);
252 if ($result->isSuccess())
253 {
254 /* @var Avatar\Mask\Item $item*/
255 ['item' => $item] = $result->getData();
256 $item->delete();
257 }
258 if ($result->isSuccess())
259 {
260 return Response\AjaxJson::createSuccess(
261 $result
262 );
263 }
264 return Response\AjaxJson::createError($result->getErrorCollection());
265 }
266
267 public function getMaskInitialInfoAction($recentlyUsedListSize, PageNavigation $pageNavigation, Main\Engine\CurrentUser $currentUser)
268 {
269 $pageNav = new PageNavigation('recentlyUsedListSize');
270 $pageNav->setPageSize($recentlyUsedListSize);
271
272 return new Response\DataType\Page('initialInfo', [
273 'recentlyUsedItems' => static::getRecentlyUsedListAction($pageNav, $currentUser)->getItems(),
274 'systemItems' => static::getSystemListAction($pageNavigation, $currentUser)->getItems(),
275 'myOwnItems' => static::getUserListAction($pageNavigation, $currentUser)->getItems(),
276 'sharedItems' => static::getSharedListAction($pageNavigation, $currentUser)->getItems(),
277 'restMarketInfo' => Main\Loader::includeModule('rest') ? [
278 'available' => 'Y',
279 'exportUrl' => Rest\Configuration\Helper::getInstance()->enabledZipMod() ? Rest\Marketplace\Url::getConfigurationExportElementUrl(
280 UI\Integration\Rest\MaskManifest::CODE,
281 $currentUser->getId()
282 ) : null,
283 'importUrl' => Rest\Marketplace\Url::getConfigurationImportManifestUrl(
284 UI\Integration\Rest\MaskManifest::CODE
285 ),
286 'marketUrl' => Rest\Marketplace\Url::getCategoryUrl('user_frame')
287 ] : [
288 'available' => 'N',
289 ]
290 ], null);
291 }
292
293 public function useRecentlyAction($id, Main\Engine\CurrentUser $currentUser): Response\AjaxJson
294 {
295 $consumer = Avatar\Mask\Consumer::createFromId($currentUser->getId());
296 if (Avatar\Mask\Item::getInstance($id)->isReadableBy($consumer))
297 {
298 $consumer->useRecentlyMaskId($id);
299 }
300 return Response\AjaxJson::createSuccess();
301 }
302
303 public function cleanUpAction(Main\Engine\CurrentUser $currentUser): Response\AjaxJson
304 {
305 (new Avatar\Mask\Owner\User($currentUser->getId()))->delete();
306 return Response\AjaxJson::createSuccess();
307 }
308}
getMaskAccessCodeAction($id, Main\Engine\CurrentUser $currentUser)
Definition mask.php:150
useRecentlyAction($id, Main\Engine\CurrentUser $currentUser)
Definition mask.php:293
getMaskInitialInfoAction($recentlyUsedListSize, PageNavigation $pageNavigation, Main\Engine\CurrentUser $currentUser)
Definition mask.php:267
getUserListAction(PageNavigation $pageNavigation, Main\Engine\CurrentUser $currentUser)
Definition mask.php:30
getRecentlyUsedListAction(PageNavigation $pageNavigation, Main\Engine\CurrentUser $currentUser)
Definition mask.php:61
checkEditability($id, Main\Engine\CurrentUser $currentUser)
Definition mask.php:163
cleanUpAction(Main\Engine\CurrentUser $currentUser)
Definition mask.php:303
saveAction($id, $title, $accessCode, $file, Main\Engine\CurrentUser $currentUser)
Definition mask.php:183
getList(Main\ORM\Query\Query $query, PageNavigation $pageNavigation, Main\Engine\CurrentUser $currentUser)
Definition mask.php:78
getSystemListAction(PageNavigation $pageNavigation, Main\Engine\CurrentUser $currentUser)
Definition mask.php:14
getSharedListAction(PageNavigation $pageNavigation, Main\Engine\CurrentUser $currentUser)
Definition mask.php:44
deleteAction($id, Main\Engine\CurrentUser $currentUser)
Definition mask.php:249