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_Tag_Assigned_Grid extends Mage_Adminhtml_Block_Widget_Grid
35: {
36: protected $_currentTagModel;
37:
38: 39: 40: 41:
42: public function __construct()
43: {
44: parent::__construct();
45: $this->_currentTagModel = Mage::registry('current_tag');
46: $this->setId('tag_assigned_product_grid');
47: $this->setDefaultSort('entity_id');
48: $this->setDefaultDir('DESC');
49: $this->setUseAjax(true);
50: if ($this->_getTagId()) {
51: $this->setDefaultFilter(array('in_products'=>1));
52: }
53: }
54:
55: 56: 57: 58: 59:
60: protected function _getTagId()
61: {
62: return $this->_currentTagModel->getId();
63: }
64:
65: 66: 67: 68: 69:
70: protected function _getStore()
71: {
72: $storeId = (int) $this->getRequest()->getParam('store', 0);
73: return Mage::app()->getStore($storeId);
74: }
75:
76: 77: 78: 79: 80: 81:
82: protected function _addColumnFilterToCollection($column)
83: {
84:
85: if ($column->getId() == 'in_products') {
86: $productIds = $this->_getSelectedProducts();
87: if (empty($productIds)) {
88: $productIds = 0;
89: }
90: if ($column->getFilter()->getValue()) {
91: $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
92: } else {
93: if($productIds) {
94: $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
95: }
96: }
97: } else {
98: parent::_addColumnFilterToCollection($column);
99: }
100: return $this;
101: }
102:
103: 104: 105: 106: 107:
108: protected function _prepareCollection()
109: {
110: $store = $this->_getStore();
111: $collection = Mage::getModel('catalog/product')->getCollection()
112: ->addAttributeToSelect('sku')
113: ->addAttributeToSelect('name')
114: ->addAttributeToSelect('attribute_set_id')
115: ->addAttributeToSelect('type_id')
116:
117: ->joinField('qty',
118: 'cataloginventory/stock_item',
119: 'qty',
120: 'product_id=entity_id',
121: '{{table}}.stock_id=1',
122: 'left');
123:
124: if ($store->getId()) {
125: $collection->addStoreFilter($store);
126: $collection->joinAttribute('custom_name', 'catalog_product/name', 'entity_id', null, 'inner', $store->getId());
127: $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner', $store->getId());
128: $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId());
129: $collection->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId());
130: } else {
131: $collection->addAttributeToSelect('price');
132: $collection->addAttributeToSelect('status');
133: $collection->addAttributeToSelect('visibility');
134: }
135:
136: $this->setCollection($collection);
137:
138: parent::_prepareCollection();
139: $this->getCollection()->addWebsiteNamesToResult();
140: return $this;
141: }
142:
143: 144: 145: 146: 147:
148: protected function _prepareColumns()
149: {
150: $this->addColumn('in_products', array(
151: 'header_css_class' => 'a-center',
152: 'type' => 'checkbox',
153: 'field_name' => 'in_products',
154: 'values' => $this->_getSelectedProducts(),
155: 'align' => 'center',
156: 'index' => 'entity_id'
157: ));
158:
159: $this->addColumn('entity_id',
160: array(
161: 'header'=> Mage::helper('catalog')->__('ID'),
162: 'width' => 50,
163: 'sortable' => true,
164: 'type' => 'number',
165: 'index' => 'entity_id',
166: ));
167: $this->addColumn('name',
168: array(
169: 'header'=> Mage::helper('catalog')->__('Name'),
170: 'index' => 'name',
171: ));
172:
173: $store = $this->_getStore();
174: if ($store->getId()) {
175: $this->addColumn('custom_name',
176: array(
177: 'header'=> Mage::helper('catalog')->__('Name in %s', $store->getName()),
178: 'index' => 'custom_name',
179: ));
180: }
181:
182: $this->addColumn('type',
183: array(
184: 'header' => Mage::helper('catalog')->__('Type'),
185: 'width' => 100,
186: 'index' => 'type_id',
187: 'type' => 'options',
188: 'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
189: ));
190:
191: $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
192: ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
193: ->load()
194: ->toOptionHash();
195:
196: $this->addColumn('set_name',
197: array(
198: 'header' => Mage::helper('catalog')->__('Attrib. Set Name'),
199: 'width' => 100,
200: 'index' => 'attribute_set_id',
201: 'type' => 'options',
202: 'options' => $sets,
203: ));
204:
205: $this->addColumn('sku',
206: array(
207: 'header'=> Mage::helper('catalog')->__('SKU'),
208: 'width' => 80,
209: 'index' => 'sku',
210: ));
211:
212: $store = $this->_getStore();
213: $this->addColumn('price',
214: array(
215: 'header' => Mage::helper('catalog')->__('Price'),
216: 'type' => 'price',
217: 'currency_code' => $store->getBaseCurrency()->getCode(),
218: 'index' => 'price',
219: ));
220:
221: $this->addColumn('visibility',
222: array(
223: 'header' => Mage::helper('catalog')->__('Visibility'),
224: 'width' => 100,
225: 'index' => 'visibility',
226: 'type' => 'options',
227: 'options' => Mage::getModel('catalog/product_visibility')->getOptionArray(),
228: ));
229:
230: $this->addColumn('status',
231: array(
232: 'header' => Mage::helper('catalog')->__('Status'),
233: 'width' => 70,
234: 'index' => 'status',
235: 'type' => 'options',
236: 'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(),
237: ));
238:
239: return parent::_prepareColumns();
240: }
241:
242: 243: 244: 245: 246:
247: protected function _getSelectedProducts()
248: {
249: $products = $this->getRequest()->getPost('assigned_products', null);
250: if (!is_array($products)) {
251: $products = $this->getRelatedProducts();
252: }
253: return $products;
254: }
255:
256: 257: 258: 259: 260:
261: public function getGridUrl()
262: {
263: return $this->getUrl('*/*/assignedGridOnly', array('_current' => true));
264: }
265:
266: 267: 268: 269: 270:
271: public function getRelatedProducts()
272: {
273: return $this->_currentTagModel
274: ->setStatusFilter(Mage_Tag_Model_Tag::STATUS_APPROVED)
275: ->getRelatedProductIds();
276: }
277: }
278: