AbstractAction.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Rule\Model\Action;
  7. use Magento\Framework\Data\Form;
  8. use Magento\Framework\Data\Form\Element\AbstractElement;
  9. /**
  10. * Abstract rule action
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. abstract class AbstractAction extends \Magento\Framework\DataObject implements ActionInterface
  16. {
  17. /**
  18. * @var \Magento\Framework\View\Asset\Repository
  19. */
  20. protected $_assetRepo;
  21. /**
  22. * @var \Magento\Framework\View\LayoutInterface
  23. */
  24. protected $_layout;
  25. /**
  26. * Base name for hidden elements
  27. * @var string
  28. */
  29. protected $elementName = 'rule';
  30. /**
  31. * @param \Magento\Framework\View\Asset\Repository $assetRepo
  32. * @param \Magento\Framework\View\LayoutInterface $layout
  33. * @param array $data
  34. */
  35. public function __construct(
  36. \Magento\Framework\View\Asset\Repository $assetRepo,
  37. \Magento\Framework\View\LayoutInterface $layout,
  38. array $data = []
  39. ) {
  40. $this->_assetRepo = $assetRepo;
  41. $this->_layout = $layout;
  42. parent::__construct($data);
  43. $this->loadAttributeOptions()->loadOperatorOptions()->loadValueOptions();
  44. $attributes = $this->getAttributeOption();
  45. if ($attributes) {
  46. reset($attributes);
  47. $this->setAttribute(key($attributes));
  48. }
  49. $operators = $this->getOperatorOption();
  50. if ($operators) {
  51. reset($operators);
  52. $this->setOperator(key($operators));
  53. }
  54. }
  55. /**
  56. * @return Form
  57. */
  58. public function getForm()
  59. {
  60. return $this->getRule()->getForm();
  61. }
  62. /**
  63. * @param array $arrAttributes
  64. * @return array
  65. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  66. */
  67. public function asArray(array $arrAttributes = [])
  68. {
  69. $out = [
  70. 'type' => $this->getType(),
  71. 'attribute' => $this->getAttribute(),
  72. 'operator' => $this->getOperator(),
  73. 'value' => $this->getValue(),
  74. ];
  75. return $out;
  76. }
  77. /**
  78. * @return string
  79. */
  80. public function asXml()
  81. {
  82. $xml = "<type>" .
  83. $this->getType() .
  84. "</type>" .
  85. "<attribute>" .
  86. $this->getAttribute() .
  87. "</attribute>" .
  88. "<operator>" .
  89. $this->getOperator() .
  90. "</operator>" .
  91. "<value>" .
  92. $this->getValue() .
  93. "</value>";
  94. return $xml;
  95. }
  96. /**
  97. * @param array $arr
  98. * @return $this
  99. */
  100. public function loadArray(array $arr)
  101. {
  102. $this->addData(
  103. [
  104. 'type' => $arr['type'],
  105. 'attribute' => $arr['attribute'],
  106. 'operator' => $arr['operator'],
  107. 'value' => $arr['value'],
  108. ]
  109. );
  110. $this->loadAttributeOptions();
  111. $this->loadOperatorOptions();
  112. $this->loadValueOptions();
  113. return $this;
  114. }
  115. /**
  116. * @return $this
  117. */
  118. public function loadAttributeOptions()
  119. {
  120. $this->setAttributeOption([]);
  121. return $this;
  122. }
  123. /**
  124. * @return array
  125. */
  126. public function getAttributeSelectOptions()
  127. {
  128. $opt = [];
  129. foreach ($this->getAttributeOption() as $key => $value) {
  130. $opt[] = ['value' => $key, 'label' => $value];
  131. }
  132. return $opt;
  133. }
  134. /**
  135. * @return string
  136. */
  137. public function getAttributeName()
  138. {
  139. return $this->getAttributeOption($this->getAttribute());
  140. }
  141. /**
  142. * @return $this
  143. */
  144. public function loadOperatorOptions()
  145. {
  146. $this->setOperatorOption(['=' => __('to'), '+=' => __('by')]);
  147. return $this;
  148. }
  149. /**
  150. * @return array
  151. */
  152. public function getOperatorSelectOptions()
  153. {
  154. $opt = [];
  155. foreach ($this->getOperatorOption() as $k => $v) {
  156. $opt[] = ['value' => $k, 'label' => $v];
  157. }
  158. return $opt;
  159. }
  160. /**
  161. * @return string
  162. */
  163. public function getOperatorName()
  164. {
  165. return $this->getOperatorOption($this->getOperator());
  166. }
  167. /**
  168. * @return $this
  169. */
  170. public function loadValueOptions()
  171. {
  172. $this->setValueOption([]);
  173. return $this;
  174. }
  175. /**
  176. * @return array
  177. */
  178. public function getValueSelectOptions()
  179. {
  180. $opt = [];
  181. foreach ($this->getValueOption() as $key => $value) {
  182. $opt[] = ['value' => $key, 'label' => $value];
  183. }
  184. return $opt;
  185. }
  186. /**
  187. * @return string
  188. */
  189. public function getValueName()
  190. {
  191. $value = $this->getValue();
  192. return !empty($value) || 0 === $value ? $value : '...';
  193. }
  194. /**
  195. * @return array
  196. */
  197. public function getNewChildSelectOptions()
  198. {
  199. return [['value' => '', 'label' => __('Please choose an action to add.')]];
  200. }
  201. /**
  202. * @return string
  203. */
  204. public function getNewChildName()
  205. {
  206. return $this->getAddLinkHtml();
  207. }
  208. /**
  209. * @return string
  210. */
  211. public function asHtml()
  212. {
  213. return '';
  214. }
  215. /**
  216. * @return string
  217. */
  218. public function asHtmlRecursive()
  219. {
  220. $str = $this->asHtml();
  221. return $str;
  222. }
  223. /**
  224. * @return AbstractElement
  225. */
  226. public function getTypeElement()
  227. {
  228. return $this->getForm()->addField(
  229. 'action:' . $this->getId() . ':type',
  230. 'hidden',
  231. [
  232. 'name' => $this->elementName . '[actions][' . $this->getId() . '][type]',
  233. 'value' => $this->getType(),
  234. 'no_span' => true
  235. ]
  236. );
  237. }
  238. /**
  239. * @return $this
  240. */
  241. public function getAttributeElement()
  242. {
  243. return $this->getForm()->addField(
  244. 'action:' . $this->getId() . ':attribute',
  245. 'select',
  246. [
  247. 'name' => $this->elementName . '[actions][' . $this->getId() . '][attribute]',
  248. 'values' => $this->getAttributeSelectOptions(),
  249. 'value' => $this->getAttribute(),
  250. 'value_name' => $this->getAttributeName()
  251. ]
  252. )->setRenderer(
  253. $this->_layout->getBlockSingleton(\Magento\Rule\Block\Editable::class)
  254. );
  255. }
  256. /**
  257. * @return $this
  258. */
  259. public function getOperatorElement()
  260. {
  261. return $this->getForm()->addField(
  262. 'action:' . $this->getId() . ':operator',
  263. 'select',
  264. [
  265. 'name' => $this->elementName . '[actions][' . $this->getId() . '][operator]',
  266. 'values' => $this->getOperatorSelectOptions(),
  267. 'value' => $this->getOperator(),
  268. 'value_name' => $this->getOperatorName()
  269. ]
  270. )->setRenderer(
  271. $this->_layout->getBlockSingleton(\Magento\Rule\Block\Editable::class)
  272. );
  273. }
  274. /**
  275. * @return $this
  276. */
  277. public function getValueElement()
  278. {
  279. return $this->getForm()->addField(
  280. 'action:' . $this->getId() . ':value',
  281. 'text',
  282. [
  283. 'name' => $this->elementName . '[actions][' . $this->getId() . '][value]',
  284. 'value' => $this->getValue(),
  285. 'value_name' => $this->getValueName()
  286. ]
  287. )->setRenderer(
  288. $this->_layout->getBlockSingleton(\Magento\Rule\Block\Editable::class)
  289. );
  290. }
  291. /**
  292. * @return string
  293. */
  294. public function getAddLinkHtml()
  295. {
  296. $src = $this->_assetRepo->getUrl('images/rule_component_add.gif');
  297. $html = '<img src="' . $src . '" alt="" class="rule-param-add v-middle" />';
  298. return $html;
  299. }
  300. /**
  301. * @return string
  302. */
  303. public function getRemoveLinkHtml()
  304. {
  305. $src = $this->_assetRepo->getUrl('images/rule_component_remove.gif');
  306. $html = '<span class="rule-param"><a href="javascript:void(0)" class="rule-param-remove"><img src="' .
  307. $src .
  308. '" alt="" class="v-middle" /></a></span>';
  309. return $html;
  310. }
  311. /**
  312. * @param string $format
  313. * @return string
  314. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  315. */
  316. public function asString($format = '')
  317. {
  318. return "";
  319. }
  320. /**
  321. * @param int $level
  322. * @return string
  323. */
  324. public function asStringRecursive($level = 0)
  325. {
  326. $str = str_pad('', $level * 3, ' ', STR_PAD_LEFT) . $this->asString();
  327. return $str;
  328. }
  329. /**
  330. * @return $this
  331. */
  332. public function process()
  333. {
  334. return $this;
  335. }
  336. }