Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
propertiesdialog.php
1<?php
5
7{
8 protected $activityFile;
9 protected $dialogFileName = 'properties_dialog.php';
10 protected $map;
11 protected $mapCallback;
12 protected $documentType;
13 protected $activityName;
17 protected $currentValues;
18 protected $formName;
19 protected $siteId;
20 protected $renderer;
21 protected $context;
22
24 protected $runtimeData = array();
25
26 public function __construct($activityFile, array $data = null)
27 {
28 $this->activityFile = $activityFile;
29
30 if (is_array($data))
31 {
32 if (isset($data['documentType']) && is_array($data['documentType']))
33 $this->setDocumentType($data['documentType']);
34 if (isset($data['activityName']))
35 $this->setActivityName($data['activityName']);
36 if (isset($data['workflowTemplate']) && is_array($data['workflowTemplate']))
37 $this->setWorkflowTemplate($data['workflowTemplate']);
38 if (isset($data['workflowParameters']) && is_array($data['workflowParameters']))
39 $this->setWorkflowParameters($data['workflowParameters']);
40 if (isset($data['currentValues']) && is_array($data['currentValues']))
41 $this->setCurrentValues($data['currentValues']);
42 if (isset($data['formName']))
43 $this->setFormName($data['formName']);
44 if (isset($data['siteId']))
45 $this->setSiteId($data['siteId']);
46 }
47 }
48
49 public function getActivityFile(): string
50 {
52 }
53
54 public function setActivityFile(string $file): self
55 {
56 $this->activityFile = $file;
57 return $this;
58 }
59
63 public function getDocumentType()
64 {
66 }
67
72 public function setDocumentType(array $documentType)
73 {
74 $this->documentType = $documentType;
75
76 return $this;
77 }
78
84 {
85 $this->activityName = (string)$activityName;
86
87 return $this;
88 }
89
93 public function getActivityName()
94 {
96 }
97
103 {
104 $this->workflowTemplate = $workflowTemplate;
105
106 return $this;
107 }
108
112 public function getWorkflowTemplate()
113 {
115 }
116
122 {
123 $this->workflowParameters = $workflowParameters;
124
125 return $this;
126 }
127
131 public function getWorkflowParameters()
132 {
134 }
135
141 {
142 $this->workflowVariables = $workflowVariables;
143
144 return $this;
145 }
146
150 public function getWorkflowVariables()
151 {
153 }
154
159 public function getCurrentValues($compatible = false)
160 {
162 if (!is_array($this->currentValues))
163 {
164 // Get current values from saved activity properties.
165 $this->currentValues = array();
166 $currentActivity = \CBPWorkflowTemplateLoader::findActivityByName(
168 $this->getActivityName()
169 );
170
171 if (is_array($currentActivity) && isset($currentActivity['Properties']) && is_array($currentActivity['Properties']))
172 {
173 $map = $this->getMap();
174 foreach ($map as $id => $property)
175 {
176 if (!isset($property['FieldName']))
177 continue;
178
179 $this->currentValues[$property['FieldName']] = null;
180
181 if (isset($currentActivity['Properties'][$id]))
182 {
183 if (
184 isset($property['Getter'])
185 && is_callable($property['Getter'])
186 && $property['Getter'] instanceof \Closure
187 )
188 {
189 $getter = $property['Getter'];
190 $property['Id'] = $id;
191 $this->currentValues[$property['FieldName']] = $getter($this, $property, $currentActivity, $compatible);
192 }
193 else
194 {
195 $this->currentValues[$property['FieldName']] = $currentActivity['Properties'][$id];
196 }
197 }
198
199 if (
200 (
201 $this->currentValues[$property['FieldName']] === null
202 ||
203 $this->currentValues[$property['FieldName']] === ''
204 )
205 && isset($property['Default'])
206 )
207 {
208 $this->currentValues[$property['FieldName']] = $property['Default'];
209 }
210 }
211 }
212 }
213
214 if ($compatible && $this->currentValues)
215 {
216 $compatibleValues = $this->currentValues;
217
218 foreach ($this->getMap() as $id => $property)
219 {
220 if (!isset($property['FieldName']))
221 {
222 continue;
223 }
224
225 if (isset($property['Type']) && $property['Type'] === FieldType::USER && !isset($property['Getter']))
226 {
227 $compatibleValues[$property['FieldName']] = \CBPHelper::usersArrayToString(
228 $compatibleValues[$property['FieldName']],
230 $this->getDocumentType()
231 );
232 }
233 }
234
235 return $compatibleValues;
236 }
237
239 }
240
246 public function getCurrentValue($valueKey, $default = null)
247 {
248 if (is_array($valueKey))
249 {
250 if ($default === null && isset($valueKey['Default']))
251 {
252 $default = $valueKey['Default'];
253 }
254 $valueKey = isset($valueKey['FieldName']) ? $valueKey['FieldName'] : '';
255 }
256
257 $values = $this->getCurrentValues();
258 return (is_array($values) && isset($values[$valueKey])) ? $values[$valueKey] : $default;
259 }
260
266 {
267 $this->currentValues = $currentValues;
268
269 return $this;
270 }
271
275 public function getFormName()
276 {
277 return $this->formName;
278 }
279
284 public function setFormName($formName)
285 {
286 $this->formName = (string)$formName;
287
288 return $this;
289 }
290
294 public function getSiteId()
295 {
296 return $this->siteId;
297 }
298
302 public function setSiteId($siteId)
303 {
304 $this->siteId = (string)$siteId;
305 }
306
311 public function setMap(array $map)
312 {
313 $this->map = $map;
314
315 return $this;
316 }
317
323 public function setMapCallback($callback)
324 {
325 if (!is_callable($callback))
326 throw new ArgumentException('Wrong callable argument.');
327
328 $this->mapCallback = $callback;
329
330 return $this;
331 }
332
336 public function getMap()
337 {
338 if ($this->map === null && $this->mapCallback !== null)
339 {
340 $this->map = call_user_func($this->mapCallback, $this);
341 }
342
343 return is_array($this->map) ? $this->map : array();
344 }
345
350 public function getFieldTypeObject(array $field)
351 {
352 $runtime = \CBPRuntime::getRuntime();
353 $runtime->startRuntime();
354
356 $documentService = $runtime->getService('DocumentService');
357
358 $field = FieldType::normalizeProperty($field);
359
360 $typeClass = $documentService->getTypeClass($this->getDocumentType(), $field['Type']);
361 if ($typeClass && class_exists($typeClass))
362 {
363 return new FieldType($field, $this->getDocumentType(), $typeClass);
364 }
365 return null;
366 }
367
368 public function renderFieldControl($field, $value = null, $allowSelection = true, $renderMode = FieldType::RENDER_MODE_PUBLIC)
369 {
370 if (is_string($field))
371 {
372 $field = $this->getMap()[$field];
373 }
374
375 $fieldType = $field ? $this->getFieldTypeObject($field) : null;
376
377 if (!$fieldType)
378 {
379 return 'incorrect field type';
380 }
381
382 if ($value === null)
383 {
384 $value = $this->getCurrentValue($field, $field['Default'] ?? null);
385 }
386
387 return $fieldType->renderControl(
388 ['Form' => $this->getFormName(), 'Field' => $field['FieldName']],
389 $value,
390 $allowSelection,
391 $renderMode
392 );
393 }
394
395 public function setRenderer($callable)
396 {
397 if (!is_callable($callable))
398 throw new ArgumentException('Wrong callable argument.');
399
400 $this->renderer = $callable;
401 }
402
403 public function __toString()
404 {
405 if ($this->renderer !== null)
406 {
407 return call_user_func($this->renderer, $this);
408 }
409
410 $runtime = \CBPRuntime::getRuntime();
411 $runtime->startRuntime();
412
413 return (string)$runtime->executeResourceFile(
414 $this->activityFile,
415 $this->dialogFileName,
416 array_merge(array(
417 'dialog' => $this,
418 //compatible parameters
419 'arCurrentValues' => $this->getCurrentValues($this->dialogFileName === 'properties_dialog.php'),
420 'formName' => $this->getFormName()
421 ), $this->getRuntimeData()
422 )
423 );
424 }
425
430 public function setContext(array $context)
431 {
432 $this->context = $context;
433
434 return $this;
435 }
436
440 public function getContext()
441 {
442 return $this->context;
443 }
444
450 {
452 if (mb_strpos($dialogFileName, '.') === false)
453 $dialogFileName .= '.php';
454
455 $this->dialogFileName = $dialogFileName;
456
457 return $this;
458 }
459
463 public function getDialogFileName()
464 {
466 }
467
472 public function getRuntimeData()
473 {
474 return $this->runtimeData;
475 }
476
482 public function setRuntimeData(array $runtimeData)
483 {
484 $this->runtimeData = $runtimeData;
485
486 return $this;
487 }
488}
renderFieldControl($field, $value=null, $allowSelection=true, $renderMode=FieldType::RENDER_MODE_PUBLIC)
setWorkflowParameters(array $workflowParameters)
setWorkflowVariables(array $workflowVariables)
__construct($activityFile, array $data=null)
static normalizeProperty($property)