AbstractMassaction.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget\Grid\Massaction;
  7. use Magento\Backend\Block\Widget\Grid\Massaction\VisibilityCheckerInterface as VisibilityChecker;
  8. use Magento\Framework\Data\Collection\AbstractDb;
  9. use Magento\Framework\DataObject;
  10. /**
  11. * Grid widget massaction block
  12. *
  13. * @api
  14. * @method \Magento\Quote\Model\Quote setHideFormElement(boolean $value) Hide Form element to prevent IE errors
  15. * @method boolean getHideFormElement()
  16. * @deprecated 100.2.0 in favour of UI component implementation
  17. * @since 100.0.2
  18. */
  19. abstract class AbstractMassaction extends \Magento\Backend\Block\Widget
  20. {
  21. /**
  22. * @var \Magento\Framework\Json\EncoderInterface
  23. */
  24. protected $_jsonEncoder;
  25. /**
  26. * Massaction items
  27. *
  28. * @var array
  29. */
  30. protected $_items = [];
  31. /**
  32. * @var string
  33. */
  34. protected $_template = 'Magento_Backend::widget/grid/massaction.phtml';
  35. /**
  36. * @param \Magento\Backend\Block\Template\Context $context
  37. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  38. * @param array $data
  39. */
  40. public function __construct(
  41. \Magento\Backend\Block\Template\Context $context,
  42. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  43. array $data = []
  44. ) {
  45. $this->_jsonEncoder = $jsonEncoder;
  46. parent::__construct($context, $data);
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. protected function _construct()
  52. {
  53. parent::_construct();
  54. $this->setErrorText($this->escapeHtml(__('An item needs to be selected. Select and try again.')));
  55. if (null !== $this->getOptions()) {
  56. foreach ($this->getOptions() as $optionId => $option) {
  57. $this->addItem($optionId, $option);
  58. }
  59. $this->unsetData('options');
  60. }
  61. }
  62. /**
  63. * Add new massaction item
  64. *
  65. * Item array to be passed in looks like:
  66. * $item = array(
  67. * 'label' => string,
  68. * 'complete' => string, // Only for ajax enabled grid (optional)
  69. * 'url' => string,
  70. * 'confirm' => string, // text of confirmation of this action (optional)
  71. * 'additional' => string, // (optional)
  72. * 'visible' => object // instance of VisibilityCheckerInterface (optional)
  73. * );
  74. *
  75. * @param string $itemId
  76. * @param array|DataObject $item
  77. * @return $this
  78. */
  79. public function addItem($itemId, $item)
  80. {
  81. if (is_array($item)) {
  82. $item = new DataObject($item);
  83. }
  84. if ($item instanceof DataObject && $this->isVisible($item)) {
  85. $item->setId($itemId);
  86. $item->setUrl($this->getUrl($item->getUrl()));
  87. $this->_items[$itemId] = $item;
  88. }
  89. return $this;
  90. }
  91. /**
  92. * Check that item can be added to list
  93. *
  94. * @param DataObject $item
  95. * @return bool
  96. */
  97. private function isVisible(DataObject $item)
  98. {
  99. /** @var VisibilityChecker $checker */
  100. $checker = $item->getData('visible');
  101. return (!$checker instanceof VisibilityChecker) || $checker->isVisible();
  102. }
  103. /**
  104. * Retrieve massaction item with id $itemId
  105. *
  106. * @param string $itemId
  107. * @return \Magento\Backend\Block\Widget\Grid\Massaction\Item|null
  108. */
  109. public function getItem($itemId)
  110. {
  111. if (isset($this->_items[$itemId])) {
  112. return $this->_items[$itemId];
  113. }
  114. return null;
  115. }
  116. /**
  117. * Retrieve massaction items
  118. *
  119. * @return array
  120. */
  121. public function getItems()
  122. {
  123. return $this->_items;
  124. }
  125. /**
  126. * Retrieve massaction items JSON
  127. *
  128. * @return string
  129. */
  130. public function getItemsJson()
  131. {
  132. $result = [];
  133. foreach ($this->getItems() as $itemId => $item) {
  134. $result[$itemId] = $item->toArray();
  135. }
  136. return $this->_jsonEncoder->encode($result);
  137. }
  138. /**
  139. * Retrieve massaction items count
  140. *
  141. * @return integer
  142. */
  143. public function getCount()
  144. {
  145. return sizeof($this->_items);
  146. }
  147. /**
  148. * Checks are massactions available
  149. *
  150. * @return boolean
  151. */
  152. public function isAvailable()
  153. {
  154. return $this->getCount() > 0 && $this->getMassactionIdField();
  155. }
  156. /**
  157. * Retrieve global form field name for all massaction items
  158. *
  159. * @return string
  160. */
  161. public function getFormFieldName()
  162. {
  163. return $this->getData('form_field_name') ? $this->getData('form_field_name') : 'massaction';
  164. }
  165. /**
  166. * Retrieve form field name for internal use. Based on $this->getFormFieldName()
  167. *
  168. * @return string
  169. */
  170. public function getFormFieldNameInternal()
  171. {
  172. return 'internal_' . $this->getFormFieldName();
  173. }
  174. /**
  175. * Retrieve massaction block js object name
  176. *
  177. * @return string
  178. */
  179. public function getJsObjectName()
  180. {
  181. return $this->getHtmlId() . 'JsObject';
  182. }
  183. /**
  184. * Retrieve grid block js object name
  185. *
  186. * @return string
  187. */
  188. public function getGridJsObjectName()
  189. {
  190. return $this->getParentBlock()->getJsObjectName();
  191. }
  192. /**
  193. * Retrieve JSON string of selected checkboxes
  194. *
  195. * @return string
  196. * @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
  197. */
  198. public function getSelectedJson()
  199. {
  200. if ($selected = $this->getRequest()->getParam($this->getFormFieldNameInternal())) {
  201. $selected = explode(',', $selected);
  202. return join(',', $selected);
  203. }
  204. return '';
  205. }
  206. /**
  207. * Retrieve array of selected checkboxes
  208. *
  209. * @return string[]
  210. * @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
  211. */
  212. public function getSelected()
  213. {
  214. if ($selected = $this->getRequest()->getParam($this->getFormFieldNameInternal())) {
  215. $selected = explode(',', $selected);
  216. return $selected;
  217. }
  218. return [];
  219. }
  220. /**
  221. * Retrieve apply button html
  222. *
  223. * @return string
  224. */
  225. public function getApplyButtonHtml()
  226. {
  227. return $this->getButtonHtml(__('Submit'), $this->getJsObjectName() . ".apply()");
  228. }
  229. /**
  230. * Get mass action javascript code.
  231. *
  232. * @return string
  233. */
  234. public function getJavaScript()
  235. {
  236. return " {$this->getJsObjectName()} = new varienGridMassaction('{$this->getHtmlId()}', " .
  237. "{$this->getGridJsObjectName()}, '{$this->getSelectedJson()}'" .
  238. ", '{$this->getFormFieldNameInternal()}', '{$this->getFormFieldName()}');" .
  239. "{$this->getJsObjectName()}.setItems({$this->getItemsJson()}); " .
  240. "{$this->getJsObjectName()}.setGridIds('{$this->getGridIdsJson()}');" .
  241. ($this->getUseAjax() ? "{$this->getJsObjectName()}.setUseAjax(true);" : '') .
  242. ($this->getUseSelectAll() ? "{$this->getJsObjectName()}.setUseSelectAll(true);" : '') .
  243. "{$this->getJsObjectName()}.errorText = '{$this->getErrorText()}';" . "\n" .
  244. "window.{$this->getJsObjectName()} = {$this->getJsObjectName()};";
  245. }
  246. /**
  247. * Get grid ids in JSON format.
  248. *
  249. * @return string
  250. */
  251. public function getGridIdsJson()
  252. {
  253. if (!$this->getUseSelectAll()) {
  254. return '';
  255. }
  256. /** @var \Magento\Framework\Data\Collection $allIdsCollection */
  257. $allIdsCollection = clone $this->getParentBlock()->getCollection();
  258. if ($this->getMassactionIdField()) {
  259. $massActionIdField = $this->getMassactionIdField();
  260. } else {
  261. $massActionIdField = $this->getParentBlock()->getMassactionIdField();
  262. }
  263. if ($allIdsCollection instanceof AbstractDb) {
  264. $allIdsCollection->getSelect()->limit();
  265. $allIdsCollection->clear();
  266. }
  267. $gridIds = $allIdsCollection->setPageSize(0)->getColumnValues($massActionIdField);
  268. if (!empty($gridIds)) {
  269. return join(",", $gridIds);
  270. }
  271. return '';
  272. }
  273. /**
  274. * Get Html id.
  275. *
  276. * @return string
  277. */
  278. public function getHtmlId()
  279. {
  280. return $this->getParentBlock()->getHtmlId() . '_massaction';
  281. }
  282. /**
  283. * Remove existing massaction item by its id
  284. *
  285. * @param string $itemId
  286. * @return $this
  287. */
  288. public function removeItem($itemId)
  289. {
  290. if (isset($this->_items[$itemId])) {
  291. unset($this->_items[$itemId]);
  292. }
  293. return $this;
  294. }
  295. /**
  296. * Retrieve select all functionality flag check
  297. *
  298. * @return boolean
  299. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  300. */
  301. public function getUseSelectAll()
  302. {
  303. return $this->_getData('use_select_all') === null || $this->_getData('use_select_all');
  304. }
  305. /**
  306. * Retrieve select all functionality flag check
  307. *
  308. * @param boolean $flag
  309. * @return $this
  310. */
  311. public function setUseSelectAll($flag)
  312. {
  313. $this->setData('use_select_all', (bool)$flag);
  314. return $this;
  315. }
  316. /**
  317. * Prepare grid massaction column
  318. *
  319. * @return $this
  320. */
  321. public function prepareMassactionColumn()
  322. {
  323. $columnId = 'massaction';
  324. $massactionColumn = $this->getLayout()->createBlock(
  325. \Magento\Backend\Block\Widget\Grid\Column::class
  326. )->setData(
  327. [
  328. 'index' => $this->getMassactionIdField(),
  329. 'filter_index' => $this->getMassactionIdFilter(),
  330. 'type' => 'massaction',
  331. 'name' => $this->getFormFieldName(),
  332. 'is_system' => true,
  333. 'header_css_class' => 'col-select',
  334. 'column_css_class' => 'col-select',
  335. ]
  336. );
  337. if ($this->getNoFilterMassactionColumn()) {
  338. $massactionColumn->setData('filter', false);
  339. }
  340. $gridBlock = $this->getParentBlock();
  341. $massactionColumn->setSelected($this->getSelected())->setGrid($gridBlock)->setId($columnId);
  342. /** @var $columnSetBlock \Magento\Backend\Block\Widget\Grid\ColumnSet */
  343. $columnSetBlock = $gridBlock->getColumnSet();
  344. $childNames = $columnSetBlock->getChildNames();
  345. $siblingElement = count($childNames) ? current($childNames) : 0;
  346. $columnSetBlock->insert($massactionColumn, $siblingElement, false, $columnId);
  347. return $this;
  348. }
  349. }