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_Category_Tab_Product extends Mage_Adminhtml_Block_Widget_Grid
35: {
36:
37: public function __construct()
38: {
39: parent::__construct();
40: $this->setId('catalog_category_products');
41: $this->setDefaultSort('entity_id');
42: $this->setUseAjax(true);
43: }
44:
45: public function getCategory()
46: {
47: return Mage::registry('category');
48: }
49:
50: protected function _addColumnFilterToCollection($column)
51: {
52:
53: if ($column->getId() == 'in_category') {
54: $productIds = $this->_getSelectedProducts();
55: if (empty($productIds)) {
56: $productIds = 0;
57: }
58: if ($column->getFilter()->getValue()) {
59: $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
60: }
61: elseif(!empty($productIds)) {
62: $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
63: }
64: }
65: else {
66: parent::_addColumnFilterToCollection($column);
67: }
68: return $this;
69: }
70:
71: protected function _prepareCollection()
72: {
73: if ($this->getCategory()->getId()) {
74: $this->setDefaultFilter(array('in_category'=>1));
75: }
76: $collection = Mage::getModel('catalog/product')->getCollection()
77: ->addAttributeToSelect('name')
78: ->addAttributeToSelect('sku')
79: ->addAttributeToSelect('price')
80: ->addStoreFilter($this->getRequest()->getParam('store'))
81: ->joinField('position',
82: 'catalog/category_product',
83: 'position',
84: 'product_id=entity_id',
85: 'category_id='.(int) $this->getRequest()->getParam('id', 0),
86: 'left');
87: $this->setCollection($collection);
88:
89: if ($this->getCategory()->getProductsReadonly()) {
90: $productIds = $this->_getSelectedProducts();
91: if (empty($productIds)) {
92: $productIds = 0;
93: }
94: $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
95: }
96:
97: return parent::_prepareCollection();
98: }
99:
100: protected function _prepareColumns()
101: {
102: if (!$this->getCategory()->getProductsReadonly()) {
103: $this->addColumn('in_category', array(
104: 'header_css_class' => 'a-center',
105: 'type' => 'checkbox',
106: 'name' => 'in_category',
107: 'values' => $this->_getSelectedProducts(),
108: 'align' => 'center',
109: 'index' => 'entity_id'
110: ));
111: }
112: $this->addColumn('entity_id', array(
113: 'header' => Mage::helper('catalog')->__('ID'),
114: 'sortable' => true,
115: 'width' => '60',
116: 'index' => 'entity_id'
117: ));
118: $this->addColumn('name', array(
119: 'header' => Mage::helper('catalog')->__('Name'),
120: 'index' => 'name'
121: ));
122: $this->addColumn('sku', array(
123: 'header' => Mage::helper('catalog')->__('SKU'),
124: 'width' => '80',
125: 'index' => 'sku'
126: ));
127: $this->addColumn('price', array(
128: 'header' => Mage::helper('catalog')->__('Price'),
129: 'type' => 'currency',
130: 'width' => '1',
131: 'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
132: 'index' => 'price'
133: ));
134: $this->addColumn('position', array(
135: 'header' => Mage::helper('catalog')->__('Position'),
136: 'width' => '1',
137: 'type' => 'number',
138: 'index' => 'position',
139: 'editable' => !$this->getCategory()->getProductsReadonly()
140:
141: ));
142:
143: return parent::_prepareColumns();
144: }
145:
146: public function getGridUrl()
147: {
148: return $this->getUrl('*/*/grid', array('_current'=>true));
149: }
150:
151: protected function _getSelectedProducts()
152: {
153: $products = $this->getRequest()->getPost('selected_products');
154: if (is_null($products)) {
155: $products = $this->getCategory()->getProductsPosition();
156: return array_keys($products);
157: }
158: return $products;
159: }
160:
161: }
162:
163: