1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
element.php
См. документацию.
1<?php
2
4
12use CCatalogStoreDocsElement;
18
19class Element extends Controller
20{
21 private const DOCUMENT_STATUS_ALLOWED = 0;
22 private const DOCUMENT_STATUS_CONDUCT = -1;
23 private const DOCUMENT_STATUS_ABSENT = -2;
24
25 //region Actions
29 public function getFieldsAction(): array
30 {
31 return ['DOCUMENT_ELEMENT' => $this->getViewFields()];
32 }
33
38 public function addAction(array $fields): ?array
39 {
40 $documentId = (int)($fields['DOC_ID'] ?? 0);
41 if (!$documentId)
42 {
43 $this->setDocumentNotFoundError();
44
45 return null;
46 }
47
48 $documentFields = $this->getDocumentFields($documentId);
49 if (!$documentFields)
50 {
51 $this->setDocumentNotFoundError();
52
53 return null;
54 }
55 elseif (!$this->checkDocumentAccess(ActionDictionary::ACTION_STORE_DOCUMENT_MODIFY, $documentFields))
56 {
57 return null;
58 }
59 elseif (!$this->checkStoresAccess(0, $fields))
60 {
61 return null;
62 }
63
64 switch ($this->getDocumentStatusById($documentId))
65 {
66 case self::DOCUMENT_STATUS_ALLOWED:
67 $addResult = CCatalogStoreDocsElement::add($fields);
68 if ($addResult)
69 {
70 return ['DOCUMENT_ELEMENT' => $this->get($addResult)];
71 }
72
73 $this->addError(new Error('Error of adding new document element'));
74 break;
75
76 case self::DOCUMENT_STATUS_CONDUCT:
77 $this->setDocumentConductError();
78 break;
79
80 default:
81 $this->setDocumentNotFoundError();
82 break;
83 }
84
85 return null;
86 }
87
93 public function updateAction(int $id, array $fields): ?array
94 {
95 $documentFields = $this->getDocumentFieldsByElementId($id);
96 if (!$documentFields)
97 {
98 $this->setDocumentNotFoundError();
99
100 return null;
101 }
102 elseif (!$this->checkDocumentAccess(ActionDictionary::ACTION_STORE_DOCUMENT_MODIFY, $documentFields))
103 {
104 return null;
105 }
106 elseif (!$this->checkStoresAccess($id, $fields))
107 {
108 return null;
109 }
110
111 switch ($this->getDocumentStatusByElementId($id))
112 {
113 case self::DOCUMENT_STATUS_ALLOWED:
114 $result = CCatalogStoreDocsElement::update($id, $fields);
115
116 if ($result)
117 {
118 return ['DOCUMENT_ELEMENT' => $this->get($id)];
119 }
120
121 $this->addError(new Error('Error of modifying new document'));
122 break;
123
124 case self::DOCUMENT_STATUS_CONDUCT:
125 $this->setDocumentConductError();
126 break;
127
128 default:
129 $this->setDocumentNotFoundError();
130 break;
131 }
132
133 return null;
134 }
135
140 public function deleteAction(int $id): ?bool
141 {
142 $documentFields = $this->getDocumentFieldsByElementId($id);
143 if (!$documentFields)
144 {
145 $this->setDocumentNotFoundError();
146
147 return null;
148 }
149 elseif (!$this->checkDocumentAccess(ActionDictionary::ACTION_STORE_DOCUMENT_MODIFY, $documentFields))
150 {
151 return null;
152 }
153 elseif (!$this->checkStoresAccess($id))
154 {
155 return null;
156 }
157
158 switch ($this->getDocumentStatusByElementId($id))
159 {
160 case self::DOCUMENT_STATUS_ALLOWED:
161 $result = CCatalogStoreDocsElement::delete($id);
162 if ($result)
163 {
164 return true;
165 }
166
167 $this->addError(new Error('Error of deleting document'));
168 break;
169
170 case self::DOCUMENT_STATUS_CONDUCT:
171 $this->setDocumentConductError();
172 break;
173
174 default:
175 $this->setDocumentNotFoundError();
176 break;
177 }
178
179 return null;
180 }
181
190 public function listAction(
191 PageNavigation $pageNavigation,
192 array $order = [],
193 array $filter = [],
194 array $select = [],
195 bool $__calculateTotalCount = true
196 ): Page
197 {
198 $filter['@DOCUMENT.DOC_TYPE'] = array_keys(Catalog\Controller\Document::getAvailableRestDocumentTypes());
199
200 $accessFilter = AccessController::getCurrent()->getEntityFilter(
201 ActionDictionary::ACTION_STORE_DOCUMENT_VIEW,
202 get_class($this->getEntityTable())
203 );
204 if ($accessFilter)
205 {
206 // combines through a new array so that the `OR` condition does not bypass the access filter.
207 $filter = [
208 $accessFilter,
209 $filter,
210 ];
211 }
212
213 return new Page(
214 'DOCUMENT_ELEMENTS',
215 $this->getList($select, $filter, $order, $pageNavigation),
216 $__calculateTotalCount ? $this->count($filter) : 0
217 );
218 }
219
225 public function fieldsAction(): ?array
226 {
227 return [$this->getViewFields()];
228 }
229
230 protected function getDefaultPreFilters(): array
231 {
232 return array_merge(
233 parent::getDefaultPreFilters(),
234 [
236 ]
237 );
238 }
239
243 protected function getEntityTable()
244 {
245 return new \Bitrix\Catalog\StoreDocumentElementTable();
246 }
247
251 protected function checkModifyPermissionEntity()
252 {
253 $r = new Result();
254
255 if (!AccessController::getCurrent()->check(Controller::CATALOG_STORE))
256 {
257 $r->addError(new Error(
258 Controller::ERROR_ACCESS_DENIED,
259 'ERROR_DOCUMENT_RIGHTS'
260 ));
261 }
262
263 return $r;
264 }
265
266 protected function checkReadPermissionEntity()
267 {
268 $r = new Result();
269
270 if (
271 !AccessController::getCurrent()->check(Controller::CATALOG_STORE)
272 && !AccessController::getCurrent()->check(Controller::CATALOG_READ)
273 )
274 {
275 $r->addError(new Error(
276 Controller::ERROR_ACCESS_DENIED,
277 'ERROR_DOCUMENT_RIGHTS'
278 ));
279 }
280
281 return $r;
282 }
283
290 protected function checkPermissionEntity($name, $arguments=[])
291 {
292 if ($name === 'fields')
293 {
294 return $this->checkGetFieldsPermissionEntity();
295 }
296
297 return parent::checkPermissionEntity($name, $arguments);
298 }
299
300 private function getDocumentStatusById(int $documentId): int
301 {
302 return $this->getDocumentStatus(
303 $this->getDocumentFields($documentId)
304 );
305 }
306
307 private function getDocumentFields(int $documentId): ?array
308 {
310 'select' => [
311 'ID',
312 'STATUS',
313 'DOC_TYPE',
314 ],
315 'filter' => [
316 '=ID' => $documentId,
317 ],
318 ]);
319 }
320
321 private function getDocumentStatusByElementId(int $elementId): int
322 {
323 return $this->getDocumentStatus(
324 $this->getDocumentFieldsByElementId($elementId)
325 );
326 }
327
328 public function getDocumentFieldsByElementId(int $elementId): ?array
329 {
331 'select' => [
332 'STORE_DOCUMENT_ID' => 'DOC_ID',
333 'STATUS' => 'DOCUMENT.STATUS',
334 'DOC_TYPE' => 'DOCUMENT.DOC_TYPE',
335 ],
336 'filter' => [
337 '=ID' => $elementId,
338 ],
339 ]);
340 if ($row)
341 {
342 $row['ID'] = $row['STORE_DOCUMENT_ID'];
343 unset($row['STORE_DOCUMENT_ID']);
344 }
345
346 return $row;
347 }
348
349 private function getDocumentStatus($row): int
350 {
351 if (empty($row) || !is_array($row))
352 {
353 return self::DOCUMENT_STATUS_ABSENT;
354 }
355
356 $documentTypes = Catalog\Controller\Document::getAvailableRestDocumentTypes();
357 if (!isset($documentTypes[$row['DOC_TYPE']]))
358 {
359 return self::DOCUMENT_STATUS_ABSENT;
360 }
361
362 return ($row['STATUS'] === 'N'
363 ? self::DOCUMENT_STATUS_ALLOWED
364 : self::DOCUMENT_STATUS_CONDUCT
365 );
366 }
367
368 private function setDocumentRightsError(): void
369 {
370 $this->addError(new \Bitrix\Main\Error(
371 Controller::ERROR_ACCESS_DENIED,
372 'ERROR_DOCUMENT_RIGHTS'
373 ));
374 }
375
376 private function setDocumentNotFoundError(): void
377 {
378 $this->addError(new \Bitrix\Main\Error(
379 'Document not found',
380 'ERROR_DOCUMENT_STATUS'
381 ));
382 }
383
384 private function setDocumentConductError(): void
385 {
386 $this->addError(new \Bitrix\Main\Error(
387 'Conducted document',
388 'ERROR_DOCUMENT_STATUS'
389 ));
390 }
391
400 private function checkDocumentAccess(string $action, array $documentFields): bool
401 {
402 $can = AccessController::getCurrent()->check(
403 $action,
404 StoreDocument::createFromArray($documentFields)
405 );
406 if (!$can)
407 {
408 $this->setDocumentRightsError();
409
410 return false;
411 }
412
413 return true;
414 }
415
424 private function checkStoresAccess(int $id, array $fields = []): bool
425 {
426 $fields['ID'] = $id;
427
428 $can = AccessController::getCurrent()->check(
429 ActionDictionary::ACTION_STORE_VIEW,
430 StoreDocumentElement::createFromArray($fields)
431 );
432 if (!$can)
433 {
434 $this->setDocumentRightsError();
435
436 return false;
437 }
438
439 return true;
440 }
441}
addAction(array $fields)
Определения element.php:38
getDocumentFieldsByElementId(int $elementId)
Определения element.php:328
listAction(PageNavigation $pageNavigation, array $order=[], array $filter=[], array $select=[], bool $__calculateTotalCount=true)
Определения element.php:190
updateAction(int $id, array $fields)
Определения element.php:93
checkPermissionEntity($name, $arguments=[])
Определения element.php:290
addError(Error $error)
Определения controller.php:1070
Определения error.php:15
static getRow(array $parameters)
Определения datamanager.php:398
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$select
Определения iblock_catalog_list.php:194
$filter
Определения iblock_catalog_list.php:54
$name
Определения menu_edit.php:35
getList(array $select, array $filter, array $order, PageNavigation $pageNavigation=null)
Определения action.php:3
Определения aliases.php:54
trait Error
Определения error.php:11
$order
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$action
Определения file_dialog.php:21
$fields
Определения yandex_run.php:501