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: class Mage_Adminhtml_Block_Promo_Widget_Chooser_Sku extends Mage_Adminhtml_Block_Widget_Grid
35: {
36:
37: public function __construct($arguments=array())
38: {
39: parent::__construct($arguments);
40:
41: if ($this->getRequest()->getParam('current_grid_id')) {
42: $this->setId($this->getRequest()->getParam('current_grid_id'));
43: } else {
44: $this->setId('skuChooserGrid_'.$this->getId());
45: }
46:
47: $form = $this->getJsFormObject();
48: $this->setRowClickCallback("$form.chooserGridRowClick.bind($form)");
49: $this->setCheckboxCheckCallback("$form.chooserGridCheckboxCheck.bind($form)");
50: $this->setRowInitCallback("$form.chooserGridRowInit.bind($form)");
51: $this->setDefaultSort('sku');
52: $this->setUseAjax(true);
53: if ($this->getRequest()->getParam('collapse')) {
54: $this->setIsCollapsed(true);
55: }
56: }
57:
58: 59: 60: 61:
62: public function getStore()
63: {
64: return Mage::app()->getStore();
65: }
66:
67: protected function _addColumnFilterToCollection($column)
68: {
69:
70: if ($column->getId() == 'in_products') {
71: $selected = $this->_getSelectedProducts();
72: if (empty($selected)) {
73: $selected = '';
74: }
75: if ($column->getFilter()->getValue()) {
76: $this->getCollection()->addFieldToFilter('sku', array('in'=>$selected));
77: } else {
78: $this->getCollection()->addFieldToFilter('sku', array('nin'=>$selected));
79: }
80: } else {
81: parent::_addColumnFilterToCollection($column);
82: }
83: return $this;
84: }
85:
86: 87: 88: 89: 90:
91: protected function _prepareCollection()
92: {
93: $collection = Mage::getResourceModel('catalog/product_collection')
94: ->setStoreId(0)
95: ->addAttributeToSelect('name', 'type_id', 'attribute_set_id');
96:
97: $this->setCollection($collection);
98:
99: return parent::_prepareCollection();
100: }
101:
102: 103: 104: 105: 106:
107: protected function _prepareColumns()
108: {
109: $this->addColumn('in_products', array(
110: 'header_css_class' => 'a-center',
111: 'type' => 'checkbox',
112: 'name' => 'in_products',
113: 'values' => $this->_getSelectedProducts(),
114: 'align' => 'center',
115: 'index' => 'sku',
116: 'use_index' => true,
117: ));
118:
119: $this->addColumn('entity_id', array(
120: 'header' => Mage::helper('sales')->__('ID'),
121: 'sortable' => true,
122: 'width' => '60px',
123: 'index' => 'entity_id'
124: ));
125:
126: $this->addColumn('type',
127: array(
128: 'header'=> Mage::helper('catalog')->__('Type'),
129: 'width' => '60px',
130: 'index' => 'type_id',
131: 'type' => 'options',
132: 'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
133: ));
134:
135: $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
136: ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
137: ->load()
138: ->toOptionHash();
139:
140: $this->addColumn('set_name',
141: array(
142: 'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
143: 'width' => '100px',
144: 'index' => 'attribute_set_id',
145: 'type' => 'options',
146: 'options' => $sets,
147: ));
148:
149: $this->addColumn('chooser_sku', array(
150: 'header' => Mage::helper('sales')->__('SKU'),
151: 'name' => 'chooser_sku',
152: 'width' => '80px',
153: 'index' => 'sku'
154: ));
155: $this->addColumn('chooser_name', array(
156: 'header' => Mage::helper('sales')->__('Product Name'),
157: 'name' => 'chooser_name',
158: 'index' => 'name'
159: ));
160:
161: return parent::_prepareColumns();
162: }
163:
164: public function getGridUrl()
165: {
166: return $this->getUrl('*/*/chooser', array(
167: '_current' => true,
168: 'current_grid_id' => $this->getId(),
169: 'collapse' => null
170: ));
171: }
172:
173: protected function _getSelectedProducts()
174: {
175: $products = $this->getRequest()->getPost('selected', array());
176:
177: return $products;
178: }
179:
180: }
181:
182: