Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
globalfield.php
1<?php
2
4
5class GlobalField extends Base
6{
7 public function deleteAction($fieldId, $mode, $documentType): array
8 {
9 $documentType = \CBPDocument::unSignDocumentType($documentType);
10 $fieldId = htmlspecialcharsback($fieldId);
11 switch ($mode)
12 {
13 case 'variable':
14 return $this->deleteVariable($fieldId, $documentType);
15 case 'constant':
16 return $this->deleteConstant($fieldId, $documentType);
17 default:
18 return [
19 'error' => \Bitrix\Main\Localization\Loc::getMessage(
20 'BIZPROC_CONTROLLER_GLOBALFIELD_MODE_NOT_DEFINED'
21 ),
22 ];
23 }
24 }
25
26 private function deleteVariable($variableId, $documentType): array
27 {
28 $userId = (int)($this->getCurrentUser()->getId());
29 $canDelete = \Bitrix\Bizproc\Workflow\Type\GlobalVar::canUserDelete($documentType, $userId);
30 if (!$canDelete)
31 {
32 return [
33 'error' => \Bitrix\Main\Localization\Loc::getMessage(
34 'BIZPROC_CONTROLLER_GLOBALFIELD_CANT_DELETE_VARIABLE_RIGHT'
35 ),
36 ];
37 }
38
39 $field = \Bitrix\Bizproc\Workflow\Type\GlobalVar::getById($variableId);
40 if (!$field)
41 {
42 return [
43 'error' => \Bitrix\Main\Localization\Loc::getMessage(
44 'BIZPROC_CONTROLLER_GLOBALFIELD_NOT_EXISTS_VARIABLE'
45 ),
46 ];
47 }
48
49 $result = \Bitrix\Bizproc\Workflow\Type\GlobalVar::delete($variableId);
50 if (!$result)
51 {
52 return [
53 'error' => \Bitrix\Main\Localization\Loc::getMessage(
54 'BIZPROC_CONTROLLER_GLOBALFIELD_CANT_DELETE_VARIABLE'
55 ),
56 ];
57 }
58
59 return ['status' => 'success'];
60 }
61
62 private function deleteConstant($constantId, $documentType): array
63 {
64 $userId = (int)($this->getCurrentUser()->getId());
65 $canDelete = \Bitrix\Bizproc\Workflow\Type\GlobalConst::canUserDelete($documentType, $userId);
66 if (!$canDelete)
67 {
68 return [
69 'error' => \Bitrix\Main\Localization\Loc::getMessage(
70 'BIZPROC_CONTROLLER_GLOBALFIELD_CANT_DELETE_CONSTANT_RIGHT'
71 ),
72 ];
73 }
74
75 $field = \Bitrix\Bizproc\Workflow\Type\GlobalConst::getById($constantId);
76 if (!$field)
77 {
78 return [
79 'error' => \Bitrix\Main\Localization\Loc::getMessage(
80 'BIZPROC_CONTROLLER_GLOBALFIELD_NOT_EXISTS_CONSTANT'
81 ),
82 ];
83 }
84
85 $result = \Bitrix\Bizproc\Workflow\Type\GlobalConst::delete($constantId);
86 if (!$result)
87 {
88 return [
89 'error' => \Bitrix\Main\Localization\Loc::getMessage(
90 'BIZPROC_CONTROLLER_GLOBALFIELD_CANT_DELETE_CONSTANT'
91 ),
92 ];
93 }
94
95 return ['status' => 'success'];
96 }
97
98 public function upsertAction($fieldId, $property, $documentType, $mode): array
99 {
100 $documentType = \CBPDocument::unSignDocumentType($documentType);
101 switch ($mode)
102 {
103 case 'variable':
104 return $this->upsertVariable($fieldId, $property, $documentType);
105 case 'constant':
106 return $this->upsertConstant($fieldId, $property, $documentType);
107 default:
108 return [
109 'error' => \Bitrix\Main\Localization\Loc::getMessage(
110 'BIZPROC_CONTROLLER_GLOBALFIELD_MODE_NOT_DEFINED'
111 ),
112 ];
113 }
114 }
115
116 private function upsertVariable($variableId, $property, $documentType): array
117 {
118 $userId = (int)($this->getCurrentUser()->getId());
119 $canUpsert = \Bitrix\Bizproc\Workflow\Type\GlobalVar::canUserUpsert($documentType, $userId);
120 if (!$canUpsert)
121 {
122 return [
123 'error' => \Bitrix\Main\Localization\Loc::getMessage(
124 'BIZPROC_CONTROLLER_GLOBALFIELD_CANT_UPSERT_VARIABLE_RIGHT'
125 ),
126 ];
127 }
128
129 $error = [];
130 $value = $this->getDefaultValue($documentType, $property, $error);
131 if ($error)
132 {
133 return ['error' => $error[0]['message']];
134 }
135 $property['Default'] = $value ?? '';
136 $property['Name'] = trim($property['Name']);
137
138 $userId = (int)($this->getCurrentUser()->getId());
139 $result = \Bitrix\Bizproc\Workflow\Type\GlobalVar::upsertByProperty($variableId, $property, $userId);
140 if (!$result->isSuccess())
141 {
142 return [
143 'error' => $result->getErrorMessages()[0],
144 ];
145 }
146
147 return ['status' => 'success'];
148 }
149
150 private function upsertConstant($constantId, $property, $documentType): array
151 {
152 $userId = (int)($this->getCurrentUser()->getId());
153 $canUpsert = \Bitrix\Bizproc\Workflow\Type\GlobalConst::canUserUpsert($documentType, $userId);
154 if (!$canUpsert)
155 {
156 return [
157 'error' => \Bitrix\Main\Localization\Loc::getMessage(
158 'BIZPROC_CONTROLLER_GLOBALFIELD_CANT_UPSERT_CONSTANT_RIGHT'
159 ),
160 ];
161 }
162
163 $error = [];
164 $value = $this->getDefaultValue($documentType, $property, $error);
165 if ($error)
166 {
167 return ['error' => $error[0]['message']];
168 }
169 $property['Default'] = $value ?? '';
170 $property['Name'] = trim($property['Name']);
171
172 $userId = (int)($this->getCurrentUser()->getId());
173 $result = \Bitrix\Bizproc\Workflow\Type\GlobalConst::upsertByProperty($constantId, $property, $userId);
174 if (!$result->isSuccess())
175 {
176 return [
177 'error' => $result->getErrorMessages()[0],
178 ];
179 }
180
181 return ['status' => 'success'];
182 }
183
184 private function getDefaultValue($documentType, $property, &$error)
185 {
186 $error = [];
187 $runtime = \CBPRuntime::GetRuntime();
188 $runtime->StartRuntime();
189 $documentService = $runtime->GetService("DocumentService");
190 $value = $documentService->GetFieldInputValue(
191 $documentType,
192 $property,
193 'Default',
194 $property,
195 $error
196 );
197
198 return $value;
199 }
200
201 public function reloadAction($mode, $documentTypeSigned): array
202 {
203 $documentType = \CBPDocument::unSignDocumentType($documentTypeSigned);
204
205 switch ($mode)
206 {
207 case 'variable':
208 return ['list' => \Bitrix\Bizproc\Workflow\Type\GlobalVar::getAll($documentType)];
209 case 'constant':
210 return ['list' => \Bitrix\Bizproc\Workflow\Type\GlobalConst::getAll($documentType)];
211 default:
212 return [
213 'error' => \Bitrix\Main\Localization\Loc::getMessage(
214 'BIZPROC_CONTROLLER_GLOBALFIELD_MODE_NOT_DEFINED'
215 ),
216 ];
217 }
218 }
219
220}
reloadAction($mode, $documentTypeSigned)
deleteAction($fieldId, $mode, $documentType)
upsertAction($fieldId, $property, $documentType, $mode)