Extended.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. /**
  8. * Grid widget massaction block
  9. *
  10. * @api
  11. * @deprecated 100.2.0 in favour of UI component implementation
  12. * @method \Magento\Quote\Model\Quote setHideFormElement(boolean $value) Hide Form element to prevent IE errors
  13. * @method boolean getHideFormElement()
  14. * @author Magento Core Team <core@magentocommerce.com>
  15. * @TODO MAGETWO-31510: Remove deprecated class
  16. * @since 100.0.2
  17. */
  18. class Extended extends \Magento\Backend\Block\Widget
  19. {
  20. /**
  21. * Massaction items
  22. *
  23. * @var array
  24. */
  25. protected $_items = [];
  26. /**
  27. * Path to template file in theme
  28. *
  29. * @var string
  30. */
  31. protected $_template = 'Magento_Backend::widget/grid/massaction_extended.phtml';
  32. /**
  33. * Backend data
  34. *
  35. * @var \Magento\Backend\Helper\Data
  36. */
  37. protected $_backendData = null;
  38. /**
  39. * @var \Magento\Framework\Json\EncoderInterface
  40. */
  41. protected $_jsonEncoder;
  42. /**
  43. * @param \Magento\Backend\Block\Template\Context $context
  44. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  45. * @param \Magento\Backend\Helper\Data $backendData
  46. * @param array $data
  47. */
  48. public function __construct(
  49. \Magento\Backend\Block\Template\Context $context,
  50. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  51. \Magento\Backend\Helper\Data $backendData,
  52. array $data = []
  53. ) {
  54. $this->_jsonEncoder = $jsonEncoder;
  55. $this->_backendData = $backendData;
  56. parent::__construct($context, $data);
  57. }
  58. /**
  59. * Sets Massaction template
  60. *
  61. * @return void
  62. */
  63. public function _construct()
  64. {
  65. parent::_construct();
  66. $this->setErrorText($this->escapeHtml(__('An item needs to be selected. Select and try again.')));
  67. }
  68. /**
  69. * Add new massaction item
  70. *
  71. * The item array should look like:
  72. * $item = array(
  73. * 'label' => string,
  74. * 'complete' => string, // Only for ajax enabled grid (optional)
  75. * 'url' => string,
  76. * 'confirm' => string, // text of confirmation of this action (optional)
  77. * 'additional' => string|array|\Magento\Framework\View\Element\AbstractBlock // (optional)
  78. * );
  79. *
  80. * @param string $itemId
  81. * @param array $item
  82. * @return $this
  83. */
  84. public function addItem($itemId, array $item)
  85. {
  86. $this->_items[$itemId] = $this->getLayout()->createBlock(
  87. \Magento\Backend\Block\Widget\Grid\Massaction\Item::class
  88. )->setData(
  89. $item
  90. )->setMassaction(
  91. $this
  92. )->setId(
  93. $itemId
  94. );
  95. if ($this->_items[$itemId]->getAdditional()) {
  96. $this->_items[$itemId]->setAdditionalActionBlock($this->_items[$itemId]->getAdditional());
  97. $this->_items[$itemId]->unsAdditional();
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Retrieve massaction item with id $itemId
  103. *
  104. * @param string $itemId
  105. * @return \Magento\Backend\Block\Widget\Grid\Massaction\Item|null
  106. */
  107. public function getItem($itemId)
  108. {
  109. if (isset($this->_items[$itemId])) {
  110. return $this->_items[$itemId];
  111. }
  112. return null;
  113. }
  114. /**
  115. * Retrieve massaction items
  116. *
  117. * @return array
  118. */
  119. public function getItems()
  120. {
  121. return $this->_items;
  122. }
  123. /**
  124. * Retrieve massaction items JSON
  125. *
  126. * @return string
  127. */
  128. public function getItemsJson()
  129. {
  130. $result = [];
  131. foreach ($this->getItems() as $itemId => $item) {
  132. $result[$itemId] = $item->toArray();
  133. }
  134. return $this->_jsonEncoder->encode($result);
  135. }
  136. /**
  137. * Retrieve massaction items count
  138. *
  139. * @return integer
  140. */
  141. public function getCount()
  142. {
  143. return sizeof($this->_items);
  144. }
  145. /**
  146. * Checks are massactions available
  147. *
  148. * @return boolean
  149. */
  150. public function isAvailable()
  151. {
  152. return $this->getCount() > 0 && $this->getParentBlock()->getMassactionIdField();
  153. }
  154. /**
  155. * Retrieve global form field name for all massaction items
  156. *
  157. * @return string
  158. */
  159. public function getFormFieldName()
  160. {
  161. return $this->getData('form_field_name') ? $this->getData('form_field_name') : 'massaction';
  162. }
  163. /**
  164. * Retrieve form field name for internal use. Based on $this->getFormFieldName()
  165. *
  166. * @return string
  167. */
  168. public function getFormFieldNameInternal()
  169. {
  170. return 'internal_' . $this->getFormFieldName();
  171. }
  172. /**
  173. * Retrieve massaction block js object name
  174. *
  175. * @return string
  176. */
  177. public function getJsObjectName()
  178. {
  179. return $this->getHtmlId() . 'JsObject';
  180. }
  181. /**
  182. * Retrieve grid block js object name
  183. *
  184. * @return string
  185. */
  186. public function getGridJsObjectName()
  187. {
  188. return $this->getParentBlock()->getJsObjectName();
  189. }
  190. /**
  191. * Retrieve JSON string of selected checkboxes
  192. *
  193. * @return string
  194. */
  195. public function getSelectedJson()
  196. {
  197. if ($selected = $this->getRequest()->getParam($this->getFormFieldNameInternal())) {
  198. $selected = explode(',', $selected);
  199. return join(',', $selected);
  200. }
  201. return '';
  202. }
  203. /**
  204. * Retrieve array of selected checkboxes
  205. *
  206. * @return string[]
  207. */
  208. public function getSelected()
  209. {
  210. if ($selected = $this->getRequest()->getParam($this->getFormFieldNameInternal())) {
  211. $selected = explode(',', $selected);
  212. return $selected;
  213. }
  214. return [];
  215. }
  216. /**
  217. * Retrieve apply button html
  218. *
  219. * @return string
  220. */
  221. public function getApplyButtonHtml()
  222. {
  223. return $this->getButtonHtml(__('Submit'), $this->getJsObjectName() . ".apply()");
  224. }
  225. /**
  226. * @return string
  227. */
  228. public function getJavaScript()
  229. {
  230. return " {$this->getJsObjectName()} = new varienGridMassaction('{$this->getHtmlId()}', " .
  231. "{$this->getGridJsObjectName()}, '{$this->getSelectedJson()}'" .
  232. ", '{$this->getFormFieldNameInternal()}', '{$this->getFormFieldName()}');" .
  233. "{$this->getJsObjectName()}.setItems({$this->getItemsJson()}); " .
  234. "{$this->getJsObjectName()}.setGridIds('{$this->getGridIdsJson()}');" .
  235. ($this->getUseAjax() ? "{$this->getJsObjectName()}.setUseAjax(true);" : '') .
  236. ($this->getUseSelectAll() ? "{$this->getJsObjectName()}.setUseSelectAll(true);" : '') .
  237. "{$this->getJsObjectName()}.errorText = '{$this->getErrorText()}';" . "\n" .
  238. "window.{$this->getJsObjectName()} = {$this->getJsObjectName()};";
  239. }
  240. /**
  241. * @return string
  242. */
  243. public function getGridIdsJson()
  244. {
  245. if (!$this->getUseSelectAll()) {
  246. return '';
  247. }
  248. /** @var \Magento\Framework\Data\Collection $allIdsCollection */
  249. $allIdsCollection = clone $this->getParentBlock()->getCollection();
  250. if ($this->getMassactionIdField()) {
  251. $massActionIdField = $this->getMassactionIdField();
  252. } else {
  253. $massActionIdField = $this->getParentBlock()->getMassactionIdField();
  254. }
  255. $gridIds = $allIdsCollection->setPageSize(0)->getColumnValues($massActionIdField);
  256. if (!empty($gridIds)) {
  257. return join(",", $gridIds);
  258. }
  259. return '';
  260. }
  261. /**
  262. * @return string
  263. */
  264. public function getHtmlId()
  265. {
  266. return $this->getParentBlock()->getHtmlId() . '_massaction';
  267. }
  268. /**
  269. * Remove existing massaction item by its id
  270. *
  271. * @param string $itemId
  272. * @return $this
  273. */
  274. public function removeItem($itemId)
  275. {
  276. if (isset($this->_items[$itemId])) {
  277. unset($this->_items[$itemId]);
  278. }
  279. return $this;
  280. }
  281. /**
  282. * Retrieve select all functionality flag check
  283. *
  284. * @return boolean
  285. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  286. */
  287. public function getUseSelectAll()
  288. {
  289. return $this->_getData('use_select_all') === null || $this->_getData('use_select_all');
  290. }
  291. /**
  292. * Retrieve select all functionality flag check
  293. *
  294. * @param boolean $flag
  295. * @return $this
  296. */
  297. public function setUseSelectAll($flag)
  298. {
  299. $this->setData('use_select_all', (bool)$flag);
  300. return $this;
  301. }
  302. }