Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
7 
13 {
17  protected $_actionFactory;
18 
25  public function __construct(
26  \Magento\Framework\View\Asset\Repository $assetRepo,
27  \Magento\Framework\View\LayoutInterface $layout,
28  \Magento\Rule\Model\ActionFactory $actionFactory,
29  array $data = []
30  ) {
31  $this->_actionFactory = $actionFactory;
32  $this->_layout = $layout;
33 
34  parent::__construct($assetRepo, $layout, $data);
35 
36  $this->setActions([]);
37  $this->setType(\Magento\Rule\Model\Action\Collection::class);
38  }
39 
53  public function asArray(array $arrAttributes = [])
54  {
55  $out = parent::asArray();
56 
57  foreach ($this->getActions() as $item) {
58  $out['actions'][] = $item->asArray();
59  }
60  return $out;
61  }
62 
67  public function loadArray(array $arr)
68  {
69  if (!empty($arr['actions']) && is_array($arr['actions'])) {
70  foreach ($arr['actions'] as $actArr) {
71  if (empty($actArr['type'])) {
72  continue;
73  }
74  $action = $this->_actionFactory->create($actArr['type']);
75  $action->loadArray($actArr);
76  $this->addAction($action);
77  }
78  }
79  return $this;
80  }
81 
86  public function addAction(ActionInterface $action)
87  {
88  $actions = $this->getActions();
89 
90  $action->setRule($this->getRule());
91 
92  $actions[] = $action;
93  if (!$action->getId()) {
94  $action->setId($this->getId() . '.' . sizeof($actions));
95  }
96 
97  $this->setActions($actions);
98  return $this;
99  }
100 
104  public function asHtml()
105  {
106  $html = $this->getTypeElement()->toHtml() . 'Perform following actions: ';
107  if ($this->getId() != '1') {
108  $html .= $this->getRemoveLinkHtml();
109  }
110  return $html;
111  }
112 
116  public function getNewChildElement()
117  {
118  return $this->getForm()->addField(
119  'action:' . $this->getId() . ':new_child',
120  'select',
121  [
122  'name' => $this->elementName . '[actions][' . $this->getId() . '][new_child]',
123  'values' => $this->getNewChildSelectOptions(),
124  'value_name' => $this->getNewChildName()
125  ]
126  )->setRenderer(
127  $this->_layout->getBlockSingleton(\Magento\Rule\Block\Newchild::class)
128  );
129  }
130 
134  public function asHtmlRecursive()
135  {
136  $html = $this->asHtml() . '<ul id="action:' . $this->getId() . ':children">';
137  foreach ($this->getActions() as $cond) {
138  $html .= '<li>' . $cond->asHtmlRecursive() . '</li>';
139  }
140  $html .= '<li>' . $this->getNewChildElement()->getHtml() . '</li></ul>';
141  return $html;
142  }
143 
149  public function asString($format = '')
150  {
151  $str = __("Perform following actions");
152  return $str;
153  }
154 
159  public function asStringRecursive($level = 0)
160  {
161  $str = $this->asString();
162  foreach ($this->getActions() as $action) {
163  $str .= "\n" . $action->asStringRecursive($level + 1);
164  }
165  return $str;
166  }
167 
171  public function process()
172  {
173  foreach ($this->getActions() as $action) {
174  $action->process();
175  }
176  return $this;
177  }
178 }
asArray(array $arrAttributes=[])
Definition: Collection.php:53
__()
Definition: __.php:13
$format
Definition: list.phtml:12
__construct(\Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\View\LayoutInterface $layout, \Magento\Rule\Model\ActionFactory $actionFactory, array $data=[])
Definition: Collection.php:25
addAction(ActionInterface $action)
Definition: Collection.php:86