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