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_Related extends Mage_Adminhtml_Block_Widget_Grid
35: {
36: 37: 38: 39:
40: public function __construct()
41: {
42: parent::__construct();
43: $this->setId('related_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: $collection = Mage::getModel('catalog/product_link')->useRelatedLinks()
99: ->getProductCollection()
100: ->setProduct($this->_getProduct())
101: ->addAttributeToSelect('*');
102:
103: if ($this->isReadonly()) {
104: $productIds = $this->_getSelectedProducts();
105: if (empty($productIds)) {
106: $productIds = array(0);
107: }
108: $collection->addFieldToFilter('entity_id', array('in' => $productIds));
109: }
110:
111: $this->setCollection($collection);
112: return parent::_prepareCollection();
113: }
114:
115: 116: 117: 118: 119:
120: public function isReadonly()
121: {
122: return $this->_getProduct()->getRelatedReadonly();
123: }
124:
125: 126: 127: 128: 129:
130: protected function _prepareColumns()
131: {
132: if (!$this->isReadonly()) {
133: $this->addColumn('in_products', array(
134: 'header_css_class' => 'a-center',
135: 'type' => 'checkbox',
136: 'name' => 'in_products',
137: 'values' => $this->_getSelectedProducts(),
138: 'align' => 'center',
139: 'index' => 'entity_id'
140: ));
141: }
142:
143: $this->addColumn('entity_id', array(
144: 'header' => Mage::helper('catalog')->__('ID'),
145: 'sortable' => true,
146: 'width' => 60,
147: 'index' => 'entity_id'
148: ));
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: 'validate_class' => 'validate-number',
210: 'index' => 'position',
211: 'width' => 60,
212: 'editable' => !$this->_getProduct()->getRelatedReadonly(),
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')
227: ? $this->getData('grid_url')
228: : $this->getUrl('*/*/relatedGrid', array('_current' => true));
229: }
230:
231: 232: 233: 234: 235:
236: protected function _getSelectedProducts()
237: {
238: $products = $this->getProductsRelated();
239: if (!is_array($products)) {
240: $products = array_keys($this->getSelectedRelatedProducts());
241: }
242: return $products;
243: }
244:
245: 246: 247: 248: 249:
250: public function getSelectedRelatedProducts()
251: {
252: $products = array();
253: foreach (Mage::registry('current_product')->getRelatedProducts() as $product) {
254: $products[$product->getId()] = array('position' => $product->getPosition());
255: }
256: return $products;
257: }
258: }
259: