Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Combine.php
Go to the documentation of this file.
1 <?php
7 
13 {
17  protected $_conditionFactory;
18 
22  protected $_logger;
23 
28  public function __construct(Context $context, array $data = [])
29  {
30  $this->_conditionFactory = $context->getConditionFactory();
31  $this->_logger = $context->getLogger();
32 
33  parent::__construct($context, $data);
34  $this->setType(
35  \Magento\Rule\Model\Condition\Combine::class
36  )->setAggregator(
37  'all'
38  )->setValue(
39  true
40  )->setConditions(
41  []
42  )->setActions(
43  []
44  );
45 
46  $this->loadAggregatorOptions();
47  $options = $this->getAggregatorOptions();
48  if ($options) {
49  reset($options);
50  $this->setAggregator(key($options));
51  }
52  }
53 
54  /* start aggregator methods */
55 
59  public function loadAggregatorOptions()
60  {
61  $this->setAggregatorOption(['all' => __('ALL'), 'any' => __('ANY')]);
62  return $this;
63  }
64 
68  public function getAggregatorSelectOptions()
69  {
70  $opt = [];
71  foreach ($this->getAggregatorOption() as $key => $value) {
72  $opt[] = ['value' => $key, 'label' => $value];
73  }
74  return $opt;
75  }
76 
80  public function getAggregatorName()
81  {
82  return $this->getAggregatorOption($this->getAggregator());
83  }
84 
88  public function getAggregatorElement()
89  {
90  if ($this->getAggregator() === null) {
91  $options = $this->getAggregatorOption();
92  if ($options) {
93  reset($options);
94  $this->setAggregator(key($options));
95  }
96  }
97  return $this->getForm()->addField(
98  $this->getPrefix() . '__' . $this->getId() . '__aggregator',
99  'select',
100  [
101  'name' => $this->elementName . '[' . $this->getPrefix() . '][' . $this->getId() . '][aggregator]',
102  'values' => $this->getAggregatorSelectOptions(),
103  'value' => $this->getAggregator(),
104  'value_name' => $this->getAggregatorName(),
105  'data-form-part' => $this->getFormName()
106  ]
107  )->setRenderer(
108  $this->_layout->getBlockSingleton(\Magento\Rule\Block\Editable::class)
109  );
110  }
111 
112  /* end aggregator methods */
113 
117  public function loadValueOptions()
118  {
119  $this->setValueOption([1 => __('TRUE'), 0 => __('FALSE')]);
120  return $this;
121  }
122 
127  public function addCondition($condition)
128  {
129  $condition->setRule($this->getRule());
130  $condition->setObject($this->getObject());
131  $condition->setPrefix($this->getPrefix());
132 
133  $conditions = $this->getConditions();
134  $conditions[] = $condition;
135 
136  if (!$condition->getId()) {
137  $condition->setId($this->getId() . '--' . sizeof($conditions));
138  }
139 
140  $this->setData($this->getPrefix(), $conditions);
141  return $this;
142  }
143 
147  public function getValueElementType()
148  {
149  return 'select';
150  }
151 
171  public function asArray(array $arrAttributes = [])
172  {
173  $out = parent::asArray();
174  $out['aggregator'] = $this->getAggregator();
175 
176  foreach ($this->getConditions() as $condition) {
177  $out['conditions'][] = $condition->asArray();
178  }
179 
180  return $out;
181  }
182 
188  public function asXml($containerKey = 'conditions', $itemKey = 'condition')
189  {
190  $xml = "<aggregator>" .
191  $this->getAggregator() .
192  "</aggregator>" .
193  "<value>" .
194  $this->getValue() .
195  "</value>" .
196  "<{$containerKey}>";
197  foreach ($this->getConditions() as $condition) {
198  $xml .= "<{$itemKey}>" . $condition->asXml() . "</{$itemKey}>";
199  }
200  $xml .= "</{$containerKey}>";
201  return $xml;
202  }
203 
210  public function loadArray($arr, $key = 'conditions')
211  {
212  $this->setAggregator(
213  isset($arr['aggregator']) ? $arr['aggregator'] : (isset($arr['attribute']) ? $arr['attribute'] : null)
214  )->setValue(
215  isset($arr['value']) ? $arr['value'] : (isset($arr['operator']) ? $arr['operator'] : null)
216  );
217 
218  if (!empty($arr[$key]) && is_array($arr[$key])) {
219  foreach ($arr[$key] as $conditionArr) {
220  try {
221  $condition = $this->_conditionFactory->create($conditionArr['type']);
222  $this->addCondition($condition);
223  $condition->loadArray($conditionArr, $key);
224  } catch (\Exception $e) {
225  $this->_logger->critical($e);
226  }
227  }
228  }
229  return $this;
230  }
231 
236  public function loadXml($xml)
237  {
238  if (is_string($xml)) {
239  $xml = simplexml_load_string($xml);
240  }
241  $arr = parent::loadXml($xml);
242  foreach ($xml->conditions->children() as $condition) {
243  $arr['conditions'] = parent::loadXml($condition);
244  }
245  $this->loadArray($arr);
246  return $this;
247  }
248 
252  public function asHtml()
253  {
254  $html = $this->getTypeElement()->getHtml() . __(
255  'If %1 of these conditions are %2:',
256  $this->getAggregatorElement()->getHtml(),
257  $this->getValueElement()->getHtml()
258  );
259  if ($this->getId() != '1') {
260  $html .= $this->getRemoveLinkHtml();
261  }
262  return $html;
263  }
264 
268  public function getNewChildElement()
269  {
270  return $this->getForm()->addField(
271  $this->getPrefix() . '__' . $this->getId() . '__new_child',
272  'select',
273  [
274  'name' => $this->elementName . '[' . $this->getPrefix() . '][' . $this->getId() . '][new_child]',
275  'values' => $this->getNewChildSelectOptions(),
276  'value_name' => $this->getNewChildName(),
277  'data-form-part' => $this->getFormName()
278  ]
279  )->setRenderer(
280  $this->_layout->getBlockSingleton(\Magento\Rule\Block\Newchild::class)
281  );
282  }
283 
287  public function asHtmlRecursive()
288  {
289  $html = $this->asHtml() .
290  '<ul id="' .
291  $this->getPrefix() .
292  '__' .
293  $this->getId() .
294  '__children" class="rule-param-children">';
295  foreach ($this->getConditions() as $cond) {
296  $html .= '<li>' . $cond->asHtmlRecursive() . '</li>';
297  }
298  $html .= '<li>' . $this->getNewChildElement()->getHtml() . '</li></ul>';
299  return $html;
300  }
301 
307  public function asString($format = '')
308  {
309  $str = __("If %1 of these conditions are %2:", $this->getAggregatorName(), $this->getValueName());
310  return $str;
311  }
312 
317  public function asStringRecursive($level = 0)
318  {
319  $str = parent::asStringRecursive($level);
320  foreach ($this->getConditions() as $cond) {
321  $str .= "\n" . $cond->asStringRecursive($level + 1);
322  }
323  return $str;
324  }
325 
330  public function validate(\Magento\Framework\Model\AbstractModel $model)
331  {
332  return $this->_isValid($model);
333  }
334 
341  public function validateByEntityId($entityId)
342  {
343  return $this->_isValid($entityId);
344  }
345 
352  protected function _isValid($entity)
353  {
354  if (!$this->getConditions()) {
355  return true;
356  }
357 
358  $all = $this->getAggregator() === 'all';
359  $true = (bool)$this->getValue();
360 
361  foreach ($this->getConditions() as $cond) {
362  if ($entity instanceof \Magento\Framework\Model\AbstractModel) {
363  $validated = $cond->validate($entity);
364  } else {
365  $validated = $cond->validateByEntityId($entity);
366  }
367  if ($all && $validated !== $true) {
368  return false;
369  } elseif (!$all && $validated === $true) {
370  return true;
371  }
372  }
373  return $all ? true : false;
374  }
375 
380  public function setJsFormObject($form)
381  {
382  $this->setData('js_form_object', $form);
383  foreach ($this->getConditions() as $condition) {
384  $condition->setJsFormObject($form);
385  }
386  return $this;
387  }
388 
394  public function getConditions()
395  {
396  $key = $this->getPrefix() ? $this->getPrefix() : 'conditions';
397  return $this->getData($key);
398  }
399 
406  public function setConditions($conditions)
407  {
408  $key = $this->getPrefix() ? $this->getPrefix() : 'conditions';
409  return $this->setData($key, $conditions);
410  }
411 
417  protected function _getRecursiveChildSelectOption()
418  {
419  return ['value' => $this->getType(), 'label' => __('Conditions Combination')];
420  }
421 }
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(Context $context, array $data=[])
Definition: Combine.php:28
__()
Definition: __.php:13
asXml($containerKey='conditions', $itemKey='condition')
Definition: Combine.php:188
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
validate(\Magento\Framework\Model\AbstractModel $model)
Definition: Combine.php:330
$entity
Definition: element.phtml:22
setData($key, $value=null)
Definition: DataObject.php:72
asArray(array $arrAttributes=[])
Definition: Combine.php:171
loadArray($arr, $key='conditions')
Definition: Combine.php:210