1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Rule\Model\Action;
/**
* @api
* @since 100.0.2
*/
class Collection extends AbstractAction
{
/**
* @var \Magento\Rule\Model\ActionFactory
*/
protected $_actionFactory;
/**
* @param \Magento\Framework\View\Asset\Repository $assetRepo
* @param \Magento\Framework\View\LayoutInterface $layout
* @param \Magento\Rule\Model\ActionFactory $actionFactory
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Asset\Repository $assetRepo,
\Magento\Framework\View\LayoutInterface $layout,
\Magento\Rule\Model\ActionFactory $actionFactory,
array $data = []
) {
$this->_actionFactory = $actionFactory;
$this->_layout = $layout;
parent::__construct($assetRepo, $layout, $data);
$this->setActions([]);
$this->setType(\Magento\Rule\Model\Action\Collection::class);
}
/**
* Returns array containing actions in the collection
*
* Output example:
* array(
* {action::asArray},
* {action::asArray}
* )
*
* @param array $arrAttributes
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function asArray(array $arrAttributes = [])
{
$out = parent::asArray();
foreach ($this->getActions() as $item) {
$out['actions'][] = $item->asArray();
}
return $out;
}
/**
* @param array $arr
* @return $this
*/
public function loadArray(array $arr)
{
if (!empty($arr['actions']) && is_array($arr['actions'])) {
foreach ($arr['actions'] as $actArr) {
if (empty($actArr['type'])) {
continue;
}
$action = $this->_actionFactory->create($actArr['type']);
$action->loadArray($actArr);
$this->addAction($action);
}
}
return $this;
}
/**
* @param ActionInterface $action
* @return $this
*/
public function addAction(ActionInterface $action)
{
$actions = $this->getActions();
$action->setRule($this->getRule());
$actions[] = $action;
if (!$action->getId()) {
$action->setId($this->getId() . '.' . sizeof($actions));
}
$this->setActions($actions);
return $this;
}
/**
* @return string
*/
public function asHtml()
{
$html = $this->getTypeElement()->toHtml() . 'Perform following actions: ';
if ($this->getId() != '1') {
$html .= $this->getRemoveLinkHtml();
}
return $html;
}
/**
* @return $this
*/
public function getNewChildElement()
{
return $this->getForm()->addField(
'action:' . $this->getId() . ':new_child',
'select',
[
'name' => $this->elementName . '[actions][' . $this->getId() . '][new_child]',
'values' => $this->getNewChildSelectOptions(),
'value_name' => $this->getNewChildName()
]
)->setRenderer(
$this->_layout->getBlockSingleton(\Magento\Rule\Block\Newchild::class)
);
}
/**
* @return string
*/
public function asHtmlRecursive()
{
$html = $this->asHtml() . '<ul id="action:' . $this->getId() . ':children">';
foreach ($this->getActions() as $cond) {
$html .= '<li>' . $cond->asHtmlRecursive() . '</li>';
}
$html .= '<li>' . $this->getNewChildElement()->getHtml() . '</li></ul>';
return $html;
}
/**
* @param string $format
* @return string
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function asString($format = '')
{
$str = __("Perform following actions");
return $str;
}
/**
* @param int $level
* @return string
*/
public function asStringRecursive($level = 0)
{
$str = $this->asString();
foreach ($this->getActions() as $action) {
$str .= "\n" . $action->asStringRecursive($level + 1);
}
return $str;
}
/**
* @return $this
*/
public function process()
{
foreach ($this->getActions() as $action) {
$action->process();
}
return $this;
}
}