Collection.php 4.4 KB

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