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_Catalog_Product_Grid extends Mage_Adminhtml_Block_Widget_Grid
35: {
36:
37: public function __construct()
38: {
39: parent::__construct();
40: $this->setId('productGrid');
41: $this->setDefaultSort('entity_id');
42: $this->setDefaultDir('DESC');
43: $this->setSaveParametersInSession(true);
44: $this->setUseAjax(true);
45: $this->setVarNameFilter('product_filter');
46:
47: }
48:
49: protected function _getStore()
50: {
51: $storeId = (int) $this->getRequest()->getParam('store', 0);
52: return Mage::app()->getStore($storeId);
53: }
54:
55: protected function _prepareCollection()
56: {
57: $store = $this->_getStore();
58: $collection = Mage::getModel('catalog/product')->getCollection()
59: ->addAttributeToSelect('sku')
60: ->addAttributeToSelect('name')
61: ->addAttributeToSelect('attribute_set_id')
62: ->addAttributeToSelect('type_id');
63:
64: if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
65: $collection->joinField('qty',
66: 'cataloginventory/stock_item',
67: 'qty',
68: 'product_id=entity_id',
69: '{{table}}.stock_id=1',
70: 'left');
71: }
72: if ($store->getId()) {
73:
74: $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
75: $collection->addStoreFilter($store);
76: $collection->joinAttribute(
77: 'name',
78: 'catalog_product/name',
79: 'entity_id',
80: null,
81: 'inner',
82: $adminStore
83: );
84: $collection->joinAttribute(
85: 'custom_name',
86: 'catalog_product/name',
87: 'entity_id',
88: null,
89: 'inner',
90: $store->getId()
91: );
92: $collection->joinAttribute(
93: 'status',
94: 'catalog_product/status',
95: 'entity_id',
96: null,
97: 'inner',
98: $store->getId()
99: );
100: $collection->joinAttribute(
101: 'visibility',
102: 'catalog_product/visibility',
103: 'entity_id',
104: null,
105: 'inner',
106: $store->getId()
107: );
108: $collection->joinAttribute(
109: 'price',
110: 'catalog_product/price',
111: 'entity_id',
112: null,
113: 'left',
114: $store->getId()
115: );
116: }
117: else {
118: $collection->addAttributeToSelect('price');
119: $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
120: $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
121: }
122:
123: $this->setCollection($collection);
124:
125: parent::_prepareCollection();
126: $this->getCollection()->addWebsiteNamesToResult();
127: return $this;
128: }
129:
130: protected function _addColumnFilterToCollection($column)
131: {
132: if ($this->getCollection()) {
133: if ($column->getId() == 'websites') {
134: $this->getCollection()->joinField('websites',
135: 'catalog/product_website',
136: 'website_id',
137: 'product_id=entity_id',
138: null,
139: 'left');
140: }
141: }
142: return parent::_addColumnFilterToCollection($column);
143: }
144:
145: protected function _prepareColumns()
146: {
147: $this->addColumn('entity_id',
148: array(
149: 'header'=> Mage::helper('catalog')->__('ID'),
150: 'width' => '50px',
151: 'type' => 'number',
152: 'index' => 'entity_id',
153: ));
154: $this->addColumn('name',
155: array(
156: 'header'=> Mage::helper('catalog')->__('Name'),
157: 'index' => 'name',
158: ));
159:
160: $store = $this->_getStore();
161: if ($store->getId()) {
162: $this->addColumn('custom_name',
163: array(
164: 'header'=> Mage::helper('catalog')->__('Name in %s', $store->getName()),
165: 'index' => 'custom_name',
166: ));
167: }
168:
169: $this->addColumn('type',
170: array(
171: 'header'=> Mage::helper('catalog')->__('Type'),
172: 'width' => '60px',
173: 'index' => 'type_id',
174: 'type' => 'options',
175: 'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
176: ));
177:
178: $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
179: ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
180: ->load()
181: ->toOptionHash();
182:
183: $this->addColumn('set_name',
184: array(
185: 'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
186: 'width' => '100px',
187: 'index' => 'attribute_set_id',
188: 'type' => 'options',
189: 'options' => $sets,
190: ));
191:
192: $this->addColumn('sku',
193: array(
194: 'header'=> Mage::helper('catalog')->__('SKU'),
195: 'width' => '80px',
196: 'index' => 'sku',
197: ));
198:
199: $store = $this->_getStore();
200: $this->addColumn('price',
201: array(
202: 'header'=> Mage::helper('catalog')->__('Price'),
203: 'type' => 'price',
204: 'currency_code' => $store->getBaseCurrency()->getCode(),
205: 'index' => 'price',
206: ));
207:
208: if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
209: $this->addColumn('qty',
210: array(
211: 'header'=> Mage::helper('catalog')->__('Qty'),
212: 'width' => '100px',
213: 'type' => 'number',
214: 'index' => 'qty',
215: ));
216: }
217:
218: $this->addColumn('visibility',
219: array(
220: 'header'=> Mage::helper('catalog')->__('Visibility'),
221: 'width' => '70px',
222: 'index' => 'visibility',
223: 'type' => 'options',
224: 'options' => Mage::getModel('catalog/product_visibility')->getOptionArray(),
225: ));
226:
227: $this->addColumn('status',
228: array(
229: 'header'=> Mage::helper('catalog')->__('Status'),
230: 'width' => '70px',
231: 'index' => 'status',
232: 'type' => 'options',
233: 'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(),
234: ));
235:
236: if (!Mage::app()->isSingleStoreMode()) {
237: $this->addColumn('websites',
238: array(
239: 'header'=> Mage::helper('catalog')->__('Websites'),
240: 'width' => '100px',
241: 'sortable' => false,
242: 'index' => 'websites',
243: 'type' => 'options',
244: 'options' => Mage::getModel('core/website')->getCollection()->toOptionHash(),
245: ));
246: }
247:
248: $this->addColumn('action',
249: array(
250: 'header' => Mage::helper('catalog')->__('Action'),
251: 'width' => '50px',
252: 'type' => 'action',
253: 'getter' => 'getId',
254: 'actions' => array(
255: array(
256: 'caption' => Mage::helper('catalog')->__('Edit'),
257: 'url' => array(
258: 'base'=>'*/*/edit',
259: 'params'=>array('store'=>$this->getRequest()->getParam('store'))
260: ),
261: 'field' => 'id'
262: )
263: ),
264: 'filter' => false,
265: 'sortable' => false,
266: 'index' => 'stores',
267: ));
268:
269: if (Mage::helper('catalog')->isModuleEnabled('Mage_Rss')) {
270: $this->addRssList('rss/catalog/notifystock', Mage::helper('catalog')->__('Notify Low Stock RSS'));
271: }
272:
273: return parent::_prepareColumns();
274: }
275:
276: protected function _prepareMassaction()
277: {
278: $this->setMassactionIdField('entity_id');
279: $this->getMassactionBlock()->setFormFieldName('product');
280:
281: $this->getMassactionBlock()->addItem('delete', array(
282: 'label'=> Mage::helper('catalog')->__('Delete'),
283: 'url' => $this->getUrl('*/*/massDelete'),
284: 'confirm' => Mage::helper('catalog')->__('Are you sure?')
285: ));
286:
287: $statuses = Mage::getSingleton('catalog/product_status')->getOptionArray();
288:
289: array_unshift($statuses, array('label'=>'', 'value'=>''));
290: $this->getMassactionBlock()->addItem('status', array(
291: 'label'=> Mage::helper('catalog')->__('Change status'),
292: 'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)),
293: 'additional' => array(
294: 'visibility' => array(
295: 'name' => 'status',
296: 'type' => 'select',
297: 'class' => 'required-entry',
298: 'label' => Mage::helper('catalog')->__('Status'),
299: 'values' => $statuses
300: )
301: )
302: ));
303:
304: if (Mage::getSingleton('admin/session')->isAllowed('catalog/update_attributes')){
305: $this->getMassactionBlock()->addItem('attributes', array(
306: 'label' => Mage::helper('catalog')->__('Update Attributes'),
307: 'url' => $this->getUrl('*/catalog_product_action_attribute/edit', array('_current'=>true))
308: ));
309: }
310:
311: Mage::dispatchEvent('adminhtml_catalog_product_grid_prepare_massaction', array('block' => $this));
312: return $this;
313: }
314:
315: public function getGridUrl()
316: {
317: return $this->getUrl('*/*/grid', array('_current'=>true));
318: }
319:
320: public function getRowUrl($row)
321: {
322: return $this->getUrl('*/*/edit', array(
323: 'store'=>$this->getRequest()->getParam('store'),
324: 'id'=>$row->getId())
325: );
326: }
327: }
328: