Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractAction.php
Go to the documentation of this file.
1 <?php
7 
10 
18 {
22  protected $_assetRepo;
23 
27  protected $_layout;
28 
33  protected $elementName = 'rule';
34 
40  public function __construct(
41  \Magento\Framework\View\Asset\Repository $assetRepo,
42  \Magento\Framework\View\LayoutInterface $layout,
43  array $data = []
44  ) {
45  $this->_assetRepo = $assetRepo;
46  $this->_layout = $layout;
47 
48  parent::__construct($data);
49 
50  $this->loadAttributeOptions()->loadOperatorOptions()->loadValueOptions();
51 
52  $attributes = $this->getAttributeOption();
53  if ($attributes) {
54  reset($attributes);
55  $this->setAttribute(key($attributes));
56  }
57 
58  $operators = $this->getOperatorOption();
59  if ($operators) {
60  reset($operators);
61  $this->setOperator(key($operators));
62  }
63  }
64 
68  public function getForm()
69  {
70  return $this->getRule()->getForm();
71  }
72 
78  public function asArray(array $arrAttributes = [])
79  {
80  $out = [
81  'type' => $this->getType(),
82  'attribute' => $this->getAttribute(),
83  'operator' => $this->getOperator(),
84  'value' => $this->getValue(),
85  ];
86  return $out;
87  }
88 
92  public function asXml()
93  {
94  $xml = "<type>" .
95  $this->getType() .
96  "</type>" .
97  "<attribute>" .
98  $this->getAttribute() .
99  "</attribute>" .
100  "<operator>" .
101  $this->getOperator() .
102  "</operator>" .
103  "<value>" .
104  $this->getValue() .
105  "</value>";
106  return $xml;
107  }
108 
113  public function loadArray(array $arr)
114  {
115  $this->addData(
116  [
117  'type' => $arr['type'],
118  'attribute' => $arr['attribute'],
119  'operator' => $arr['operator'],
120  'value' => $arr['value'],
121  ]
122  );
123  $this->loadAttributeOptions();
124  $this->loadOperatorOptions();
125  $this->loadValueOptions();
126  return $this;
127  }
128 
132  public function loadAttributeOptions()
133  {
134  $this->setAttributeOption([]);
135  return $this;
136  }
137 
141  public function getAttributeSelectOptions()
142  {
143  $opt = [];
144  foreach ($this->getAttributeOption() as $key => $value) {
145  $opt[] = ['value' => $key, 'label' => $value];
146  }
147  return $opt;
148  }
149 
153  public function getAttributeName()
154  {
155  return $this->getAttributeOption($this->getAttribute());
156  }
157 
161  public function loadOperatorOptions()
162  {
163  $this->setOperatorOption(['=' => __('to'), '+=' => __('by')]);
164  return $this;
165  }
166 
170  public function getOperatorSelectOptions()
171  {
172  $opt = [];
173  foreach ($this->getOperatorOption() as $k => $v) {
174  $opt[] = ['value' => $k, 'label' => $v];
175  }
176  return $opt;
177  }
178 
182  public function getOperatorName()
183  {
184  return $this->getOperatorOption($this->getOperator());
185  }
186 
190  public function loadValueOptions()
191  {
192  $this->setValueOption([]);
193  return $this;
194  }
195 
199  public function getValueSelectOptions()
200  {
201  $opt = [];
202  foreach ($this->getValueOption() as $key => $value) {
203  $opt[] = ['value' => $key, 'label' => $value];
204  }
205  return $opt;
206  }
207 
211  public function getValueName()
212  {
213  $value = $this->getValue();
214  return !empty($value) || 0 === $value ? $value : '...';
215  }
216 
220  public function getNewChildSelectOptions()
221  {
222  return [['value' => '', 'label' => __('Please choose an action to add.')]];
223  }
224 
228  public function getNewChildName()
229  {
230  return $this->getAddLinkHtml();
231  }
232 
236  public function asHtml()
237  {
238  return '';
239  }
240 
244  public function asHtmlRecursive()
245  {
246  $str = $this->asHtml();
247  return $str;
248  }
249 
253  public function getTypeElement()
254  {
255  return $this->getForm()->addField(
256  'action:' . $this->getId() . ':type',
257  'hidden',
258  [
259  'name' => $this->elementName . '[actions][' . $this->getId() . '][type]',
260  'value' => $this->getType(),
261  'no_span' => true
262  ]
263  );
264  }
265 
269  public function getAttributeElement()
270  {
271  return $this->getForm()->addField(
272  'action:' . $this->getId() . ':attribute',
273  'select',
274  [
275  'name' => $this->elementName . '[actions][' . $this->getId() . '][attribute]',
276  'values' => $this->getAttributeSelectOptions(),
277  'value' => $this->getAttribute(),
278  'value_name' => $this->getAttributeName()
279  ]
280  )->setRenderer(
281  $this->_layout->getBlockSingleton(\Magento\Rule\Block\Editable::class)
282  );
283  }
284 
288  public function getOperatorElement()
289  {
290  return $this->getForm()->addField(
291  'action:' . $this->getId() . ':operator',
292  'select',
293  [
294  'name' => $this->elementName . '[actions][' . $this->getId() . '][operator]',
295  'values' => $this->getOperatorSelectOptions(),
296  'value' => $this->getOperator(),
297  'value_name' => $this->getOperatorName()
298  ]
299  )->setRenderer(
300  $this->_layout->getBlockSingleton(\Magento\Rule\Block\Editable::class)
301  );
302  }
303 
307  public function getValueElement()
308  {
309  return $this->getForm()->addField(
310  'action:' . $this->getId() . ':value',
311  'text',
312  [
313  'name' => $this->elementName . '[actions][' . $this->getId() . '][value]',
314  'value' => $this->getValue(),
315  'value_name' => $this->getValueName()
316  ]
317  )->setRenderer(
318  $this->_layout->getBlockSingleton(\Magento\Rule\Block\Editable::class)
319  );
320  }
321 
325  public function getAddLinkHtml()
326  {
327  $src = $this->_assetRepo->getUrl('images/rule_component_add.gif');
328  $html = '<img src="' . $src . '" alt="" class="rule-param-add v-middle" />';
329  return $html;
330  }
331 
335  public function getRemoveLinkHtml()
336  {
337  $src = $this->_assetRepo->getUrl('images/rule_component_remove.gif');
338  $html = '<span class="rule-param"><a href="javascript:void(0)" class="rule-param-remove"><img src="' .
339  $src .
340  '" alt="" class="v-middle" /></a></span>';
341  return $html;
342  }
343 
349  public function asString($format = '')
350  {
351  return "";
352  }
353 
358  public function asStringRecursive($level = 0)
359  {
360  $str = str_pad('', $level * 3, ' ', STR_PAD_LEFT) . $this->asString();
361  return $str;
362  }
363 
367  public function process()
368  {
369  return $this;
370  }
371 }
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
$attributes
Definition: matrix.phtml:13
__construct(\Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\View\LayoutInterface $layout, array $data=[])