Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractMassaction.php
Go to the documentation of this file.
1 <?php
8 
11 
22 {
26  protected $_jsonEncoder;
27 
33  protected $_items = [];
34 
38  protected $_template = 'Magento_Backend::widget/grid/massaction.phtml';
39 
45  public function __construct(
46  \Magento\Backend\Block\Template\Context $context,
47  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
48  array $data = []
49  ) {
50  $this->_jsonEncoder = $jsonEncoder;
51  parent::__construct($context, $data);
52  }
53 
57  protected function _construct()
58  {
59  parent::_construct();
60 
61  $this->setErrorText($this->escapeHtml(__('An item needs to be selected. Select and try again.')));
62 
63  if (null !== $this->getOptions()) {
64  foreach ($this->getOptions() as $optionId => $option) {
65  $this->addItem($optionId, $option);
66  }
67  $this->unsetData('options');
68  }
69  }
70 
88  public function addItem($itemId, $item)
89  {
90  if (is_array($item)) {
91  $item = new DataObject($item);
92  }
93 
94  if ($item instanceof DataObject && $this->isVisible($item)) {
95  $item->setId($itemId);
96  $item->setUrl($this->getUrl($item->getUrl()));
97  $this->_items[$itemId] = $item;
98  }
99 
100  return $this;
101  }
102 
109  private function isVisible(DataObject $item)
110  {
112  $checker = $item->getData('visible');
113  return (!$checker instanceof VisibilityChecker) || $checker->isVisible();
114  }
115 
122  public function getItem($itemId)
123  {
124  if (isset($this->_items[$itemId])) {
125  return $this->_items[$itemId];
126  }
127 
128  return null;
129  }
130 
136  public function getItems()
137  {
138  return $this->_items;
139  }
140 
146  public function getItemsJson()
147  {
148  $result = [];
149  foreach ($this->getItems() as $itemId => $item) {
150  $result[$itemId] = $item->toArray();
151  }
152 
153  return $this->_jsonEncoder->encode($result);
154  }
155 
161  public function getCount()
162  {
163  return sizeof($this->_items);
164  }
165 
171  public function isAvailable()
172  {
173  return $this->getCount() > 0 && $this->getMassactionIdField();
174  }
175 
181  public function getFormFieldName()
182  {
183  return $this->getData('form_field_name') ? $this->getData('form_field_name') : 'massaction';
184  }
185 
191  public function getFormFieldNameInternal()
192  {
193  return 'internal_' . $this->getFormFieldName();
194  }
195 
201  public function getJsObjectName()
202  {
203  return $this->getHtmlId() . 'JsObject';
204  }
205 
211  public function getGridJsObjectName()
212  {
213  return $this->getParentBlock()->getJsObjectName();
214  }
215 
221  public function getSelectedJson()
222  {
223  if ($selected = $this->getRequest()->getParam($this->getFormFieldNameInternal())) {
224  $selected = explode(',', $selected);
225  return join(',', $selected);
226  }
227  return '';
228  }
229 
235  public function getSelected()
236  {
237  if ($selected = $this->getRequest()->getParam($this->getFormFieldNameInternal())) {
238  $selected = explode(',', $selected);
239  return $selected;
240  }
241  return [];
242  }
243 
249  public function getApplyButtonHtml()
250  {
251  return $this->getButtonHtml(__('Submit'), $this->getJsObjectName() . ".apply()");
252  }
253 
257  public function getJavaScript()
258  {
259  return " {$this->getJsObjectName()} = new varienGridMassaction('{$this->getHtmlId()}', " .
260  "{$this->getGridJsObjectName()}, '{$this->getSelectedJson()}'" .
261  ", '{$this->getFormFieldNameInternal()}', '{$this->getFormFieldName()}');" .
262  "{$this->getJsObjectName()}.setItems({$this->getItemsJson()}); " .
263  "{$this->getJsObjectName()}.setGridIds('{$this->getGridIdsJson()}');" .
264  ($this->getUseAjax() ? "{$this->getJsObjectName()}.setUseAjax(true);" : '') .
265  ($this->getUseSelectAll() ? "{$this->getJsObjectName()}.setUseSelectAll(true);" : '') .
266  "{$this->getJsObjectName()}.errorText = '{$this->getErrorText()}';" . "\n" .
267  "window.{$this->getJsObjectName()} = {$this->getJsObjectName()};";
268  }
269 
273  public function getGridIdsJson()
274  {
275  if (!$this->getUseSelectAll()) {
276  return '';
277  }
279  $allIdsCollection = clone $this->getParentBlock()->getCollection();
280 
281  if ($this->getMassactionIdField()) {
282  $massActionIdField = $this->getMassactionIdField();
283  } else {
284  $massActionIdField = $this->getParentBlock()->getMassactionIdField();
285  }
286 
287  $gridIds = $allIdsCollection->setPageSize(0)->getColumnValues($massActionIdField);
288  if (!empty($gridIds)) {
289  return join(",", $gridIds);
290  }
291  return '';
292  }
293 
297  public function getHtmlId()
298  {
299  return $this->getParentBlock()->getHtmlId() . '_massaction';
300  }
301 
308  public function removeItem($itemId)
309  {
310  if (isset($this->_items[$itemId])) {
311  unset($this->_items[$itemId]);
312  }
313 
314  return $this;
315  }
316 
323  public function getUseSelectAll()
324  {
325  return $this->_getData('use_select_all') === null || $this->_getData('use_select_all');
326  }
327 
334  public function setUseSelectAll($flag)
335  {
336  $this->setData('use_select_all', (bool)$flag);
337  return $this;
338  }
339 
345  public function prepareMassactionColumn()
346  {
347  $columnId = 'massaction';
348  $massactionColumn = $this->getLayout()->createBlock(
349  \Magento\Backend\Block\Widget\Grid\Column::class
350  )->setData(
351  [
352  'index' => $this->getMassactionIdField(),
353  'filter_index' => $this->getMassactionIdFilter(),
354  'type' => 'massaction',
355  'name' => $this->getFormFieldName(),
356  'is_system' => true,
357  'header_css_class' => 'col-select',
358  'column_css_class' => 'col-select',
359  ]
360  );
361 
362  if ($this->getNoFilterMassactionColumn()) {
363  $massactionColumn->setData('filter', false);
364  }
365 
366  $gridBlock = $this->getParentBlock();
367  $massactionColumn->setSelected($this->getSelected())->setGrid($gridBlock)->setId($columnId);
368 
370  $columnSetBlock = $gridBlock->getColumnSet();
371  $childNames = $columnSetBlock->getChildNames();
372  $siblingElement = count($childNames) ? current($childNames) : 0;
373  $columnSetBlock->insert($massactionColumn, $siblingElement, false, $columnId);
374  return $this;
375  }
376 }
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, array $data=[])
setData($key, $value=null)
Definition: DataObject.php:72
getParam($paramName, $default=null)
Definition: Grid.php:729