1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33: 34: 35:
36: abstract class Mage_Adminhtml_Block_Widget_Grid_Massaction_Abstract extends Mage_Adminhtml_Block_Widget
37: {
38: 39: 40: 41: 42:
43: protected $_items = array();
44:
45: 46: 47:
48: public function __construct()
49: {
50: parent::__construct();
51: $this->setTemplate('widget/grid/massaction.phtml');
52: $this->setErrorText(Mage::helper('catalog')->jsQuoteEscape(Mage::helper('catalog')->__('Please select items.')));
53: }
54:
55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69:
70: public function addItem($itemId, array $item)
71: {
72: $this->_items[$itemId] = $this->getLayout()->createBlock('adminhtml/widget_grid_massaction_item')
73: ->setData($item)
74: ->setMassaction($this)
75: ->setId($itemId);
76:
77: if($this->_items[$itemId]->getAdditional()) {
78: $this->_items[$itemId]->setAdditionalActionBlock($this->_items[$itemId]->getAdditional());
79: $this->_items[$itemId]->unsAdditional();
80: }
81:
82: return $this;
83: }
84:
85: 86: 87: 88: 89: 90:
91: public function getItem($itemId)
92: {
93: if(isset($this->_items[$itemId])) {
94: return $this->_items[$itemId];
95: }
96:
97: return null;
98: }
99:
100: 101: 102: 103: 104:
105: public function getItems()
106: {
107: return $this->_items;
108: }
109:
110: 111: 112: 113: 114:
115: public function getItemsJson()
116: {
117: $result = array();
118: foreach ($this->getItems() as $itemId=>$item) {
119: $result[$itemId] = $item->toArray();
120: }
121:
122: return Mage::helper('core')->jsonEncode($result);
123: }
124:
125: 126: 127: 128: 129:
130: public function getCount()
131: {
132: return sizeof($this->_items);
133: }
134:
135: 136: 137: 138: 139:
140: public function isAvailable()
141: {
142: return $this->getCount() > 0 && $this->getParentBlock()->getMassactionIdField();
143: }
144:
145: 146: 147: 148: 149:
150: public function getFormFieldName()
151: {
152: return ($this->getData('form_field_name') ? $this->getData('form_field_name') : 'massaction');
153: }
154:
155: 156: 157: 158: 159:
160: public function getFormFieldNameInternal()
161: {
162: return 'internal_' . $this->getFormFieldName();
163: }
164:
165: 166: 167: 168: 169:
170: public function getJsObjectName()
171: {
172: return $this->getHtmlId() . 'JsObject';
173: }
174:
175: 176: 177: 178: 179:
180: public function getGridJsObjectName()
181: {
182: return $this->getParentBlock()->getJsObjectName();
183: }
184:
185: 186: 187: 188: 189:
190: public function getSelectedJson()
191: {
192: if($selected = $this->getRequest()->getParam($this->getFormFieldNameInternal())) {
193: $selected = explode(',', $selected);
194: return join(',', $selected);
195: } else {
196: return '';
197: }
198: }
199:
200: 201: 202: 203: 204:
205: public function getSelected()
206: {
207: if($selected = $this->getRequest()->getParam($this->getFormFieldNameInternal())) {
208: $selected = explode(',', $selected);
209: return $selected;
210: } else {
211: return array();
212: }
213: }
214:
215: 216: 217: 218: 219:
220: public function getApplyButtonHtml()
221: {
222: return $this->getButtonHtml($this->__('Submit'), $this->getJsObjectName() . ".apply()");
223: }
224:
225: public function getJavaScript()
226: {
227: return " var {$this->getJsObjectName()} = new varienGridMassaction('{$this->getHtmlId()}', "
228: . "{$this->getGridJsObjectName()}, '{$this->getSelectedJson()}'"
229: . ", '{$this->getFormFieldNameInternal()}', '{$this->getFormFieldName()}');"
230: . "{$this->getJsObjectName()}.setItems({$this->getItemsJson()}); "
231: . "{$this->getJsObjectName()}.setGridIds('{$this->getGridIdsJson()}');"
232: . ($this->getUseAjax() ? "{$this->getJsObjectName()}.setUseAjax(true);" : '')
233: . ($this->getUseSelectAll() ? "{$this->getJsObjectName()}.setUseSelectAll(true);" : '')
234: . "{$this->getJsObjectName()}.errorText = '{$this->getErrorText()}';";
235: }
236:
237: public function getGridIdsJson()
238: {
239: if (!$this->getUseSelectAll()) {
240: return '';
241: }
242:
243: $gridIds = $this->getParentBlock()->getCollection()->getAllIds();
244:
245: if(!empty($gridIds)) {
246: return join(",", $gridIds);
247: }
248: return '';
249: }
250:
251: public function getHtmlId()
252: {
253: return $this->getParentBlock()->getHtmlId() . '_massaction';
254: }
255:
256: 257: 258: 259: 260: 261:
262: public function removeItem($itemId)
263: {
264: if (isset($this->_items[$itemId])) {
265: unset($this->_items[$itemId]);
266: }
267:
268: return $this;
269: }
270:
271: 272: 273: 274: 275:
276: public function getUseSelectAll()
277: {
278: return $this->_getData('use_select_all') === null || $this->_getData('use_select_all');
279: }
280:
281: 282: 283: 284: 285: 286:
287: public function setUseSelectAll($flag)
288: {
289: $this->setData('use_select_all', (bool) $flag);
290: return $this;
291: }
292: }
293: