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_Edit_Tab_Crosssell extends Mage_Adminhtml_Block_Widget_Grid
35: {
36: 37: 38: 39:
40: public function __construct()
41: {
42: parent::__construct();
43: $this->setId('cross_sell_product_grid');
44: $this->setDefaultSort('entity_id');
45: $this->setUseAjax(true);
46: if ($this->_getProduct()->getId()) {
47: $this->setDefaultFilter(array('in_products'=>1));
48: }
49: if ($this->isReadonly()) {
50: $this->setFilterVisibility(false);
51: }
52: }
53:
54: 55: 56: 57: 58:
59: protected function _getProduct()
60: {
61: return Mage::registry('current_product');
62: }
63:
64: 65: 66: 67: 68: 69:
70: protected function _addColumnFilterToCollection($column)
71: {
72:
73: if ($column->getId() == 'in_products') {
74: $productIds = $this->_getSelectedProducts();
75: if (empty($productIds)) {
76: $productIds = 0;
77: }
78: if ($column->getFilter()->getValue()) {
79: $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
80: } else {
81: if($productIds) {
82: $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
83: }
84: }
85: } else {
86: parent::_addColumnFilterToCollection($column);
87: }
88: return $this;
89: }
90:
91: 92: 93: 94: 95:
96: protected function _prepareCollection()
97: {
98:
99: $collection = Mage::getModel('catalog/product_link')->useCrossSellLinks()
100: ->getProductCollection()
101: ->setProduct($this->_getProduct())
102: ->addAttributeToSelect('*');
103:
104: if ($this->isReadonly()) {
105: $productIds = $this->_getSelectedProducts();
106: if (empty($productIds)) {
107: $productIds = array(0);
108: }
109: $collection->addFieldToFilter('entity_id', array('in' => $productIds));
110: }
111:
112:
113: $this->setCollection($collection);
114:
115: return parent::_prepareCollection();
116: }
117:
118: 119: 120: 121: 122:
123: public function isReadonly()
124: {
125: return $this->_getProduct()->getCrosssellReadonly();
126: }
127:
128: 129: 130: 131: 132:
133: protected function _prepareColumns()
134: {
135: if (!$this->isReadonly()) {
136: $this->addColumn('in_products', array(
137: 'header_css_class' => 'a-center',
138: 'type' => 'checkbox',
139: 'name' => 'in_products',
140: 'values' => $this->_getSelectedProducts(),
141: 'align' => 'center',
142: 'index' => 'entity_id'
143: ));
144: }
145:
146: $this->addColumn('entity_id', array(
147: 'header' => Mage::helper('catalog')->__('ID'),
148: 'sortable' => true,
149: 'width' => 60,
150: 'index' => 'entity_id'
151: ));
152:
153: $this->addColumn('name', array(
154: 'header' => Mage::helper('catalog')->__('Name'),
155: 'index' => 'name'
156: ));
157:
158: $this->addColumn('type', array(
159: 'header' => Mage::helper('catalog')->__('Type'),
160: 'width' => 100,
161: 'index' => 'type_id',
162: 'type' => 'options',
163: 'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
164: ));
165:
166: $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
167: ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
168: ->load()
169: ->toOptionHash();
170:
171: $this->addColumn('set_name', array(
172: 'header' => Mage::helper('catalog')->__('Attrib. Set Name'),
173: 'width' => 130,
174: 'index' => 'attribute_set_id',
175: 'type' => 'options',
176: 'options' => $sets,
177: ));
178:
179: $this->addColumn('status', array(
180: 'header' => Mage::helper('catalog')->__('Status'),
181: 'width' => 90,
182: 'index' => 'status',
183: 'type' => 'options',
184: 'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(),
185: ));
186:
187: $this->addColumn('visibility', array(
188: 'header' => Mage::helper('catalog')->__('Visibility'),
189: 'width' => 90,
190: 'index' => 'visibility',
191: 'type' => 'options',
192: 'options' => Mage::getSingleton('catalog/product_visibility')->getOptionArray(),
193: ));
194:
195: $this->addColumn('sku', array(
196: 'header' => Mage::helper('catalog')->__('SKU'),
197: 'width' => 80,
198: 'index' => 'sku'
199: ));
200:
201: $this->addColumn('price', array(
202: 'header' => Mage::helper('catalog')->__('Price'),
203: 'type' => 'currency',
204: 'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
205: 'index' => 'price'
206: ));
207:
208:
209: $this->addColumn('position', array(
210: 'header' => Mage::helper('catalog')->__('Position'),
211: 'name' => 'position',
212: 'width' => 60,
213: 'type' => 'number',
214: 'validate_class' => 'validate-number',
215: 'index' => 'position',
216: 'editable' => !$this->isReadonly(),
217: 'edit_only' => !$this->_getProduct()->getId()
218: ));
219:
220: return parent::_prepareColumns();
221: }
222:
223: 224: 225: 226: 227:
228: public function getGridUrl()
229: {
230: return $this->_getData('grid_url') ? $this->_getData('grid_url') : $this->getUrl('*/*/crosssellGrid', array('_current'=>true));
231: }
232:
233: 234: 235: 236: 237:
238: protected function _getSelectedProducts()
239: {
240: $products = $this->getProductsCrossSell();
241: if (!is_array($products)) {
242: $products = array_keys($this->getSelectedCrossSellProducts());
243: }
244: return $products;
245: }
246:
247: 248: 249: 250: 251:
252: public function getSelectedCrossSellProducts()
253: {
254: $products = array();
255: foreach (Mage::registry('current_product')->getCrossSellProducts() as $product) {
256: $products[$product->getId()] = array('position' => $product->getPosition());
257: }
258: return $products;
259: }
260: }
261: