Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
robot.php
1<?php
2
4
6
7class Robot
8{
12 protected $delayInterval;
13 protected $delayName;
15 protected $condition;
16 protected $executeAfterPrevious = false;
17
18 public function __construct(array $bizprocActivity)
19 {
20 if (isset($bizprocActivity['Delay']))
21 {
23 unset($bizprocActivity['Delay']);
24 }
25 if (isset($bizprocActivity['DelayName']))
26 {
27 $this->setDelayName($bizprocActivity['DelayName']);
28 unset($bizprocActivity['DelayName']);
29 }
30 if (isset($bizprocActivity['Condition']))
31 {
32 $this->setCondition(new ConditionGroup($bizprocActivity['Condition']));
33 unset($bizprocActivity['Condition']);
34 }
35
36 if (isset($bizprocActivity['ExecuteAfterPrevious']) && (int)$bizprocActivity['ExecuteAfterPrevious'] === 1)
37 {
39 }
40 unset($bizprocActivity['ExecuteAfterPrevious']);
41
42 $this->bizprocActivity = $bizprocActivity;
43 }
44
49 {
50 $this->delayInterval = $delayInterval;
51 }
52
56 public function getDelayInterval()
57 {
59 }
60
65 {
66 $this->condition = $condition;
67 }
68
72 public function getCondition()
73 {
74 return $this->condition;
75 }
76
80 public function setDelayName($delayName)
81 {
82 $this->delayName = (string)$delayName;
83 }
84
88 public function getDelayName()
89 {
90 return $this->delayName;
91 }
92
93 public function setExecuteAfterPrevious()
94 {
95 $this->executeAfterPrevious = true;
96 }
97
98 public function isExecuteAfterPrevious()
99 {
101 }
102
103 public function getProperties()
104 {
105 return isset($this->bizprocActivity['Properties']) && is_array($this->bizprocActivity['Properties'])
106 ? $this->bizprocActivity['Properties'] : array();
107 }
108
109 public function setProperties(array $properties): self
110 {
111 $this->bizprocActivity['Properties'] = $properties;
112 return $this;
113 }
114
115 public function getProperty(string $name)
116 {
117 $properties = $this->getProperties();
118 return array_key_exists($name, $properties) ? $properties[$name] : null;
119 }
120
121 public function setProperty(string $name, $value)
122 {
123 $properties = $this->getProperties();
124 $properties[$name] = $value;
125 return $this->setProperties($properties);
126 }
127
128 public function getReturnProperties(): array
129 {
130 return \CBPRuntime::GetRuntime(true)->getActivityReturnProperties($this->getType());
131 }
132
133 public function getReturnProperty(string $name): ?array
134 {
135 $props = $this->getReturnProperties();
136 return ($props && isset($props[$name])) ? $props[$name] : null;
137 }
138
139 public function getTitle()
140 {
141 return $this->getProperty('Title');
142 }
143
144 public function getName()
145 {
146 return $this->bizprocActivity['Name'];
147 }
148
149 public function getType()
150 {
151 return $this->bizprocActivity['Type'];
152 }
153
154 public function isActivated(): bool
155 {
156 return \CBPHelper::getBool($this->bizprocActivity['Activated'] ?? true);
157 }
158
159 public function getDescription(): ?array
160 {
161 return \CBPRuntime::GetRuntime(true)->GetActivityDescription($this->getType());
162 }
163
164 public function toArray()
165 {
166 $activity = $this->bizprocActivity;
167 unset($activity['Children']); //Robot activities has no Children
169 if ($delayInterval)
170 {
171 $activity['Delay'] = $delayInterval->toArray();
172 $activity['DelayName'] = $this->getDelayName();
173 }
174 if ($this->isExecuteAfterPrevious())
175 $activity['ExecuteAfterPrevious'] = 1;
176
177 $condition = $this->getCondition();
178 if ($condition)
179 {
180 $activity['Condition'] = $condition->toArray();
181 }
182
183 return $activity;
184 }
185
186 public function getBizprocActivity()
187 {
188 $activity = $this->bizprocActivity;
189 $activity['Children'] = array();
190 return $activity;
191 }
192
193 public static function generateName()
194 {
195 return 'A'.mt_rand(10000, 99999)
196 .'_'.mt_rand(10000, 99999)
197 .'_'.mt_rand(10000, 99999)
198 .'_'.mt_rand(10000, 99999);
199 }
200
201 public function collectUsages(): array
202 {
203 \CBPActivity::IncludeActivityFile($this->getType());
204 try
205 {
206 $activity = \CBPActivity::createInstance($this->getType(), $this->getName());
207 if ($activity)
208 {
209 $activity->initializeFromArray($this->getProperties());
210
211 return $activity->collectUsages();
212 }
213 }
214 catch (\Exception $e)
215 {
216 }
217
218 return [];
219 }
220
221 public function hasBrokenLink(\Bitrix\Bizproc\Automation\Engine\Template $template): bool
222 {
223 $usages = $this->collectUsages();
224 if (!$usages)
225 {
226 return false;
227 }
228
229 $checkObjects = [
235 ];
236
237 foreach ($usages as $usage)
238 {
239 $object = $usage[0];
240 $field = $usage[1];
241
242 if (in_array($object, $checkObjects))
243 {
244 $property = $template->getProperty($object, $field);
245
246 if (!$property)
247 {
248 return true;
249 }
250 }
251 }
252
253 return false;
254 }
255}
setDelayInterval(DelayInterval $delayInterval)
Definition robot.php:48
setCondition(ConditionGroup $condition)
Definition robot.php:64
hasBrokenLink(\Bitrix\Bizproc\Automation\Engine\Template $template)
Definition robot.php:221
setProperty(string $name, $value)
Definition robot.php:121
__construct(array $bizprocActivity)
Definition robot.php:18