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