21 private const DOCUMENT_STATUS_ALLOWED = 0;
22 private const DOCUMENT_STATUS_CONDUCT = -1;
23 private const DOCUMENT_STATUS_ABSENT = -2;
40 $documentId = (int)($fields[
'DOC_ID'] ?? 0);
43 $this->setDocumentNotFoundError();
48 $documentFields = $this->getDocumentFields($documentId);
51 $this->setDocumentNotFoundError();
55 elseif (!$this->checkDocumentAccess(ActionDictionary::ACTION_STORE_DOCUMENT_MODIFY, $documentFields))
59 elseif (!$this->checkStoresAccess(0, $fields))
64 switch ($this->getDocumentStatusById($documentId))
66 case self::DOCUMENT_STATUS_ALLOWED:
67 $addResult = CCatalogStoreDocsElement::add($fields);
70 return [
'DOCUMENT_ELEMENT' => $this->
get($addResult)];
73 $this->addError(
new Error(
'Error of adding new document element'));
76 case self::DOCUMENT_STATUS_CONDUCT:
77 $this->setDocumentConductError();
81 $this->setDocumentNotFoundError();
98 $this->setDocumentNotFoundError();
102 elseif (!$this->checkDocumentAccess(ActionDictionary::ACTION_STORE_DOCUMENT_MODIFY, $documentFields))
106 elseif (!$this->checkStoresAccess($id, $fields))
111 switch ($this->getDocumentStatusByElementId($id))
113 case self::DOCUMENT_STATUS_ALLOWED:
114 $result = CCatalogStoreDocsElement::update($id, $fields);
118 return [
'DOCUMENT_ELEMENT' => $this->
get($id)];
121 $this->addError(
new Error(
'Error of modifying new document'));
124 case self::DOCUMENT_STATUS_CONDUCT:
125 $this->setDocumentConductError();
129 $this->setDocumentNotFoundError();
143 if (!$documentFields)
145 $this->setDocumentNotFoundError();
149 elseif (!$this->checkDocumentAccess(ActionDictionary::ACTION_STORE_DOCUMENT_MODIFY, $documentFields))
153 elseif (!$this->checkStoresAccess($id))
158 switch ($this->getDocumentStatusByElementId($id))
160 case self::DOCUMENT_STATUS_ALLOWED:
161 $result = CCatalogStoreDocsElement::delete($id);
167 $this->addError(
new Error(
'Error of deleting document'));
170 case self::DOCUMENT_STATUS_CONDUCT:
171 $this->setDocumentConductError();
175 $this->setDocumentNotFoundError();
196 $filter[
'@DOCUMENT.DOC_TYPE'] = array_keys(Catalog\
Controller\Document::getAvailableRestDocumentTypes());
198 $accessFilter = AccessController::getCurrent()->getEntityFilter(
199 ActionDictionary::ACTION_STORE_DOCUMENT_VIEW,
213 $this->getList($select, $filter, $order, $pageNavigation),
214 $this->count($filter)
231 parent::getDefaultPreFilters(),
243 return new \Bitrix\Catalog\StoreDocumentElementTable();
253 if (!AccessController::getCurrent()->check(Controller::CATALOG_STORE))
255 $r->addError(
new Error(
256 Controller::ERROR_ACCESS_DENIED,
257 'ERROR_DOCUMENT_RIGHTS'
269 !AccessController::getCurrent()->check(Controller::CATALOG_STORE)
270 && !AccessController::getCurrent()->check(Controller::CATALOG_READ)
273 $r->addError(
new Error(
274 Controller::ERROR_ACCESS_DENIED,
275 'ERROR_DOCUMENT_RIGHTS'
290 if ($name ===
'fields')
292 return $this->checkGetFieldsPermissionEntity();
295 return parent::checkPermissionEntity($name, $arguments);
298 private function getDocumentStatusById(
int $documentId): int
300 return $this->getDocumentStatus(
301 $this->getDocumentFields($documentId)
305 private function getDocumentFields(
int $documentId): ?array
307 return Catalog\StoreDocumentTable::getRow([
314 '=ID' => $documentId,
319 private function getDocumentStatusByElementId(
int $elementId): int
321 return $this->getDocumentStatus(
328 $row = Catalog\StoreDocumentElementTable::getRow([
330 'STORE_DOCUMENT_ID' =>
'DOC_ID',
331 'STATUS' =>
'DOCUMENT.STATUS',
332 'DOC_TYPE' =>
'DOCUMENT.DOC_TYPE',
340 $row[
'ID'] = $row[
'STORE_DOCUMENT_ID'];
341 unset($row[
'STORE_DOCUMENT_ID']);
347 private function getDocumentStatus($row): int
349 if (empty($row) || !is_array($row))
351 return self::DOCUMENT_STATUS_ABSENT;
354 $documentTypes = Catalog\Controller\Document::getAvailableRestDocumentTypes();
355 if (!isset($documentTypes[$row[
'DOC_TYPE']]))
357 return self::DOCUMENT_STATUS_ABSENT;
360 return ($row[
'STATUS'] ===
'N'
361 ? self::DOCUMENT_STATUS_ALLOWED
362 : self::DOCUMENT_STATUS_CONDUCT
366 private function setDocumentRightsError(): void
368 $this->addError(
new \
Bitrix\Main\Error(
369 Controller::ERROR_ACCESS_DENIED,
370 'ERROR_DOCUMENT_RIGHTS'
374 private function setDocumentNotFoundError(): void
376 $this->addError(
new \
Bitrix\Main\Error(
377 'Document not found',
378 'ERROR_DOCUMENT_STATUS'
382 private function setDocumentConductError(): void
385 'Conducted document',
386 'ERROR_DOCUMENT_STATUS'
398 private function checkDocumentAccess(
string $action, array $documentFields): bool
400 $can = AccessController::getCurrent()->check(
402 StoreDocument::createFromArray($documentFields)
406 $this->setDocumentRightsError();
422 private function checkStoresAccess(
int $id, array $fields = []): bool
426 $can = AccessController::getCurrent()->check(
427 ActionDictionary::ACTION_STORE_VIEW,
428 StoreDocumentElement::createFromArray($fields)
432 $this->setDocumentRightsError();