Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
document.php
1<?php
2
4
13
15{
16 protected const ITEM = 'DOCUMENT';
17 protected const LIST = 'DOCUMENTS';
18
19 public function updateAction($documentId, $fields): ?array
20 {
21 global $USER_FIELD_MANAGER;
22
23 $existsResult = $this->exists($documentId);
24 if (!$existsResult->isSuccess())
25 {
26 $this->addErrors($existsResult->getErrors());
27 return null;
28 }
29
30 $documentType = $fields['DOC_TYPE'];
31 unset($fields['DOC_TYPE']);
32
33 $permissionCheckResult = $this->checkUpdatePermissionByType($documentType);
34 if (!$permissionCheckResult->isSuccess())
35 {
36 $this->addErrors($permissionCheckResult->getErrors());
37 return null;
38 }
39
40 $entityId = self::getEntityIdForType($documentType);
41 if (!$entityId)
42 {
43 return [];
44 }
45
46 $fields = $this->prepareFileFieldsForUpdate((int)$documentId, $documentType, $fields);
47
48 $USER_FIELD_MANAGER->Update($entityId, $documentId, $fields);
49
50 return [static::ITEM => $this->get($documentId)];
51 }
52
53 private function prepareFileFieldsForUpdate(int $documentId, string $documentType, array $fields): array
54 {
55 $entityId = self::getEntityIdForType($documentType);
56
57 global $USER_FIELD_MANAGER;
58 $preparedFields = $fields;
59 $userFields = $USER_FIELD_MANAGER->GetUserFields($entityId, $documentId);
60 foreach ($preparedFields as $fieldId => $field)
61 {
62 if (isset($userFields[$fieldId]) && $userFields[$fieldId]['USER_TYPE_ID'] === 'file')
63 {
64 if ($userFields[$fieldId]['MULTIPLE'] === 'Y')
65 {
66 $preparedFields[$fieldId][] = [
67 'old_id' => $userFields[$fieldId]['VALUE'],
68 ];
69 }
70 else
71 {
72 $preparedFields[$fieldId]['old_id'] = $userFields[$fieldId]['VALUE'];
73 }
74 }
75 }
76
77 return $preparedFields;
78 }
79
80 public function getAction($documentId)
81 {
82 $this->addError(new Error('The get method is not implemented; please use the list method and filter by documentId'));
83
84 return null;
85 }
86
87 public function listAction(PageNavigation $pageNavigation, $select = [], $filter = [], $order = []): ?Page
88 {
89 if (!in_array('DOC_TYPE', $select, true))
90 {
91 $this->addError(new Error('The documentType field is not specified in the select parameter'));
92 return null;
93 }
94
95 if (!isset($filter['DOC_TYPE']))
96 {
97 $this->addError(new Error('The documentType field is not specified in the filter parameter'));
98 return null;
99 }
100
101 $documentType = $filter['DOC_TYPE'];
102 unset($filter['DOC_TYPE']);
103
104 $permissionCheckResult = $this->checkReadPermissionByType($documentType);
105 if (!$permissionCheckResult->isSuccess())
106 {
107 $this->addErrors($permissionCheckResult->getErrors());
108 return null;
109 }
110
111 $tableClass = StoreDocumentTableManager::getTableClassByType($documentType);
112 if (!$tableClass)
113 {
114 return null;
115 }
116
117 $result = $tableClass::getList([
118 'select' => $select,
119 'filter' => $filter,
120 'order' => $order,
121 'offset' => $pageNavigation->getOffset(),
122 'limit' => $pageNavigation->getLimit(),
123 ])->fetchAll();
124
125 return new Page(static::LIST, $result, $tableClass::getCount($filter));
126 }
127
128 public function addAction($fields)
129 {
130 $this->addError(new Error('The add method is not implemented; please use the update method to set the values'));
131
132 return null;
133 }
134
135 public function deleteAction($fields)
136 {
137 $this->addError(new Error('The delete method is not implemented; please use the update method to unset the values'));
138
139 return null;
140 }
141
142 protected static function getUserFieldsForType(string $docType): array
143 {
144 global $USER_FIELD_MANAGER;
145 static $fields = [];
146
147 $entityId = self::getEntityIdForType($docType);
148 if (!$entityId)
149 {
150 return [];
151 }
152
153 if (!isset($fields[$docType]))
154 {
155 $fields[$docType] = $USER_FIELD_MANAGER->GetUserFields($entityId, 0);
156 }
157
158 return $fields[$docType];
159 }
160
161 protected static function getEntityIdForType(string $docType): string
162 {
163 return StoreDocumentTableManager::getUfEntityIds()[$docType] ?? '';
164 }
165
166 protected function get($id)
167 {
168 global $USER_FIELD_MANAGER;
169
170 $documentType = StoreDocumentTable::getRow([
171 'select' => ['DOC_TYPE'],
172 'filter' => ['=ID' => $id]
173 ])['DOC_TYPE'];
174 if (!$documentType)
175 {
176 return [];
177 }
178
179 $entityId = self::getEntityIdForType($documentType);
180 if (!$entityId)
181 {
182 return [];
183 }
184
185 $values = $USER_FIELD_MANAGER->GetUserFields($entityId, $id);
186
187 $result = [
188 'ID' => $id,
189 'DOC_TYPE' => $documentType,
190 ];
191
192 foreach ($values as $value)
193 {
194 $result['FIELD_' . $value['ID']] = $value['VALUE'];
195 }
196
197 return $result;
198 }
199
200 protected function exists($id)
201 {
202 $r = new Result();
203 if(isset($this->get($id)['ID']) === false)
204 {
205 $r->addError(new Error('The specified document does not exist'));
206 }
207
208 return $r;
209 }
210
211 protected function checkReadPermissionEntity()
212 {
213 return new Result();
214 }
215
216 protected function checkModifyPermissionEntity()
217 {
218 return new Result();
219 }
220
221 protected function checkReadPermissionByType(string $documentType): Result
222 {
223 $result = new Result();
224 $accessController = AccessController::getCurrent();
225 $basePermission =
226 $accessController->check(ActionDictionary::ACTION_CATALOG_READ)
227 && $accessController->check(ActionDictionary::ACTION_INVENTORY_MANAGEMENT_ACCESS)
228 ;
229 $typePermission = $accessController->checkByValue(
230 ActionDictionary::ACTION_STORE_DOCUMENT_VIEW,
231 $documentType
232 );
233 if (!($basePermission && $typePermission))
234 {
235 $result->addError(new Error('Access Denied'));
236 }
237
238 return $result;
239 }
240
241 protected function checkUpdatePermissionByType(string $documentType): Result
242 {
243 $readPermissionCheckResult = $this->checkReadPermissionByType($documentType);
244 if (!$readPermissionCheckResult->isSuccess())
245 {
246 return $readPermissionCheckResult;
247 }
248
249 $result = new Result();
250 $accessController = AccessController::getCurrent();
251 $modifyPermission = $accessController->checkByValue(
252 ActionDictionary::ACTION_STORE_DOCUMENT_MODIFY,
253 $documentType
254 );
255 if (!$modifyPermission)
256 {
257 $result->addError(new Error('Access Denied'));
258 }
259
260 return $result;
261 }
262}
checkByValue(string $action, string $value)
check(string $action, AccessibleItem $item=null, $params=null)
checkUpdatePermissionByType(string $documentType)
Definition document.php:241
listAction(PageNavigation $pageNavigation, $select=[], $filter=[], $order=[])
Definition document.php:87
checkReadPermissionByType(string $documentType)
Definition document.php:221
static getEntityIdForType(string $docType)
Definition document.php:161
static getUserFieldsForType(string $docType)
Definition document.php:142
static getRow(array $parameters)