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_Super_Group extends Mage_Adminhtml_Block_Widget_Grid
35: implements Mage_Adminhtml_Block_Widget_Tab_Interface
36: {
37: public function __construct()
38: {
39: parent::__construct();
40: $this->setId('super_product_grid');
41: $this->setDefaultSort('entity_id');
42: $this->setSkipGenerateContent(true);
43: $this->setUseAjax(true);
44: if ($this->_getProduct()->getId()) {
45: $this->setDefaultFilter(array('in_products'=>1));
46: }
47: }
48:
49: public function getTabUrl()
50: {
51: return $this->getUrl('*/*/superGroup', array('_current'=>true));
52: }
53:
54: public function getTabClass()
55: {
56: return 'ajax';
57: }
58:
59: 60: 61: 62: 63:
64: protected function _getProduct()
65: {
66: return Mage::registry('current_product');
67: }
68:
69: protected function _addColumnFilterToCollection($column)
70: {
71:
72: if ($column->getId() == 'in_products') {
73: $productIds = $this->_getSelectedProducts();
74: if (empty($productIds)) {
75: $productIds = 0;
76: }
77: if ($column->getFilter()->getValue()) {
78: $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
79: }
80: else {
81: $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
82: }
83: }
84: else {
85: parent::_addColumnFilterToCollection($column);
86: }
87:
88: return $this;
89: }
90:
91: 92: 93: 94: 95:
96: protected function _prepareCollection()
97: {
98: $allowProductTypes = array();
99: $allowProductTypeNodes = Mage::getConfig()
100: ->getNode('global/catalog/product/type/grouped/allow_product_types')->children();
101: foreach ($allowProductTypeNodes as $type) {
102: $allowProductTypes[] = $type->getName();
103: }
104:
105: $collection = Mage::getModel('catalog/product_link')->useGroupedLinks()
106: ->getProductCollection()
107: ->setProduct($this->_getProduct())
108: ->addAttributeToSelect('*')
109: ->addFilterByRequiredOptions()
110: ->addAttributeToFilter('type_id', $allowProductTypes);
111:
112: if ($this->getIsReadonly() === true) {
113: $collection->addFieldToFilter('entity_id', array('in' => $this->_getSelectedProducts()));
114: }
115: $this->setCollection($collection);
116: return parent::_prepareCollection();
117: }
118:
119: protected function _prepareColumns()
120: {
121: $this->addColumn('in_products', array(
122: 'header_css_class' => 'a-center',
123: 'type' => 'checkbox',
124: 'name' => 'in_products',
125: 'values' => $this->_getSelectedProducts(),
126: 'align' => 'center',
127: 'index' => 'entity_id'
128: ));
129:
130: $this->addColumn('entity_id', array(
131: 'header' => Mage::helper('catalog')->__('ID'),
132: 'sortable' => true,
133: 'width' => '60px',
134: 'index' => 'entity_id'
135: ));
136: $this->addColumn('name', array(
137: 'header' => Mage::helper('catalog')->__('Name'),
138: 'index' => 'name'
139: ));
140: $this->addColumn('sku', array(
141: 'header' => Mage::helper('catalog')->__('SKU'),
142: 'width' => '80px',
143: 'index' => 'sku'
144: ));
145: $this->addColumn('price', array(
146: 'header' => Mage::helper('catalog')->__('Price'),
147: 'type' => 'currency',
148: 'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
149: 'index' => 'price'
150: ));
151:
152: $this->addColumn('qty', array(
153: 'header' => Mage::helper('catalog')->__('Default Qty'),
154: 'name' => 'qty',
155: 'type' => 'number',
156: 'validate_class' => 'validate-number',
157: 'index' => 'qty',
158: 'width' => '1',
159: 'editable' => true
160: ));
161:
162: $this->addColumn('position', array(
163: 'header' => Mage::helper('catalog')->__('Position'),
164: 'name' => 'position',
165: 'type' => 'number',
166: 'validate_class' => 'validate-number',
167: 'index' => 'position',
168: 'width' => '1',
169: 'editable' => true,
170: 'edit_only' => !$this->_getProduct()->getId()
171: ));
172:
173: return parent::_prepareColumns();
174: }
175:
176: 177: 178: 179: 180:
181: public function getGridUrl()
182: {
183: return $this->_getData('grid_url')
184: ? $this->_getData('grid_url') : $this->getUrl('*/*/superGroupGridOnly', array('_current'=>true));
185: }
186:
187: 188: 189: 190: 191:
192: protected function _getSelectedProducts()
193: {
194: $products = $this->getProductsGrouped();
195: if (!is_array($products)) {
196: $products = array_keys($this->getSelectedGroupedProducts());
197: }
198: return $products;
199: }
200:
201: 202: 203: 204: 205:
206: public function getSelectedGroupedProducts()
207: {
208: $associatedProducts = Mage::registry('current_product')->getTypeInstance(true)
209: ->getAssociatedProducts(Mage::registry('current_product'));
210: $products = array();
211: foreach ($associatedProducts as $product) {
212: $products[$product->getId()] = array(
213: 'qty' => $product->getQty(),
214: 'position' => $product->getPosition()
215: );
216: }
217: return $products;
218: }
219:
220: public function getTabLabel()
221: {
222: return Mage::helper('catalog')->__('Associated Products');
223: }
224: public function getTabTitle()
225: {
226: return Mage::helper('catalog')->__('Associated Products');
227: }
228: public function canShowTab()
229: {
230: return true;
231: }
232: public function isHidden()
233: {
234: return false;
235: }
236: }
237: