Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Extended.php
Go to the documentation of this file.
1 <?php
8 
21 {
27  protected $_items = [];
28 
34  protected $_template = 'Magento_Backend::widget/grid/massaction_extended.phtml';
35 
41  protected $_backendData = null;
42 
46  protected $_jsonEncoder;
47 
54  public function __construct(
55  \Magento\Backend\Block\Template\Context $context,
56  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
57  \Magento\Backend\Helper\Data $backendData,
58  array $data = []
59  ) {
60  $this->_jsonEncoder = $jsonEncoder;
61  $this->_backendData = $backendData;
62  parent::__construct($context, $data);
63  }
64 
70  public function _construct()
71  {
72  parent::_construct();
73  $this->setErrorText($this->escapeHtml(__('An item needs to be selected. Select and try again.')));
74  }
75 
92  public function addItem($itemId, array $item)
93  {
94  $this->_items[$itemId] = $this->getLayout()->createBlock(
95  \Magento\Backend\Block\Widget\Grid\Massaction\Item::class
96  )->setData(
97  $item
98  )->setMassaction(
99  $this
100  )->setId(
101  $itemId
102  );
103 
104  if ($this->_items[$itemId]->getAdditional()) {
105  $this->_items[$itemId]->setAdditionalActionBlock($this->_items[$itemId]->getAdditional());
106  $this->_items[$itemId]->unsAdditional();
107  }
108 
109  return $this;
110  }
111 
118  public function getItem($itemId)
119  {
120  if (isset($this->_items[$itemId])) {
121  return $this->_items[$itemId];
122  }
123 
124  return null;
125  }
126 
132  public function getItems()
133  {
134  return $this->_items;
135  }
136 
142  public function getItemsJson()
143  {
144  $result = [];
145  foreach ($this->getItems() as $itemId => $item) {
146  $result[$itemId] = $item->toArray();
147  }
148 
149  return $this->_jsonEncoder->encode($result);
150  }
151 
157  public function getCount()
158  {
159  return sizeof($this->_items);
160  }
161 
167  public function isAvailable()
168  {
169  return $this->getCount() > 0 && $this->getParentBlock()->getMassactionIdField();
170  }
171 
177  public function getFormFieldName()
178  {
179  return $this->getData('form_field_name') ? $this->getData('form_field_name') : 'massaction';
180  }
181 
187  public function getFormFieldNameInternal()
188  {
189  return 'internal_' . $this->getFormFieldName();
190  }
191 
197  public function getJsObjectName()
198  {
199  return $this->getHtmlId() . 'JsObject';
200  }
201 
207  public function getGridJsObjectName()
208  {
209  return $this->getParentBlock()->getJsObjectName();
210  }
211 
217  public function getSelectedJson()
218  {
219  if ($selected = $this->getRequest()->getParam($this->getFormFieldNameInternal())) {
220  $selected = explode(',', $selected);
221  return join(',', $selected);
222  }
223  return '';
224  }
225 
231  public function getSelected()
232  {
233  if ($selected = $this->getRequest()->getParam($this->getFormFieldNameInternal())) {
234  $selected = explode(',', $selected);
235  return $selected;
236  }
237  return [];
238  }
239 
245  public function getApplyButtonHtml()
246  {
247  return $this->getButtonHtml(__('Submit'), $this->getJsObjectName() . ".apply()");
248  }
249 
253  public function getJavaScript()
254  {
255  return " {$this->getJsObjectName()} = new varienGridMassaction('{$this->getHtmlId()}', " .
256  "{$this->getGridJsObjectName()}, '{$this->getSelectedJson()}'" .
257  ", '{$this->getFormFieldNameInternal()}', '{$this->getFormFieldName()}');" .
258  "{$this->getJsObjectName()}.setItems({$this->getItemsJson()}); " .
259  "{$this->getJsObjectName()}.setGridIds('{$this->getGridIdsJson()}');" .
260  ($this->getUseAjax() ? "{$this->getJsObjectName()}.setUseAjax(true);" : '') .
261  ($this->getUseSelectAll() ? "{$this->getJsObjectName()}.setUseSelectAll(true);" : '') .
262  "{$this->getJsObjectName()}.errorText = '{$this->getErrorText()}';" . "\n" .
263  "window.{$this->getJsObjectName()} = {$this->getJsObjectName()};";
264  }
265 
269  public function getGridIdsJson()
270  {
271  if (!$this->getUseSelectAll()) {
272  return '';
273  }
274 
276  $allIdsCollection = clone $this->getParentBlock()->getCollection();
277 
278  if ($this->getMassactionIdField()) {
279  $massActionIdField = $this->getMassactionIdField();
280  } else {
281  $massActionIdField = $this->getParentBlock()->getMassactionIdField();
282  }
283 
284  $gridIds = $allIdsCollection->setPageSize(0)->getColumnValues($massActionIdField);
285 
286  if (!empty($gridIds)) {
287  return join(",", $gridIds);
288  }
289  return '';
290  }
291 
295  public function getHtmlId()
296  {
297  return $this->getParentBlock()->getHtmlId() . '_massaction';
298  }
299 
306  public function removeItem($itemId)
307  {
308  if (isset($this->_items[$itemId])) {
309  unset($this->_items[$itemId]);
310  }
311 
312  return $this;
313  }
314 
321  public function getUseSelectAll()
322  {
323  return $this->_getData('use_select_all') === null || $this->_getData('use_select_all');
324  }
325 
332  public function setUseSelectAll($flag)
333  {
334  $this->setData('use_select_all', (bool)$flag);
335  return $this;
336  }
337 }
getData($key='', $index=null)
Definition: DataObject.php:119
getButtonHtml($label, $onclick, $class='', $buttonId=null, $dataAttr=[])
Definition: Widget.php:82
__()
Definition: __.php:13
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Backend\Helper\Data $backendData, array $data=[])
Definition: Extended.php:54
setData($key, $value=null)
Definition: DataObject.php:72
getParam($paramName, $default=null)
Definition: Grid.php:729