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_Config_Grid extends Mage_Adminhtml_Block_Widget_Grid
35: {
36:
37: public function __construct()
38: {
39: parent::__construct();
40: $this->setUseAjax(true);
41: $this->setId('super_product_links');
42:
43: if ($this->_getProduct()->getId()) {
44: $this->setDefaultFilter(array('in_products'=>1));
45: }
46: }
47:
48: 49: 50: 51: 52:
53: protected function _getProduct()
54: {
55: return Mage::registry('current_product');
56: }
57:
58: protected function _addColumnFilterToCollection($column)
59: {
60:
61: if ($column->getId() == 'in_products') {
62: $productIds = $this->_getSelectedProducts();
63:
64: if (empty($productIds)) {
65: $productIds = 0;
66: }
67:
68: $createdProducts = $this->_getCreatedProducts();
69:
70: $existsProducts = $productIds;
71:
72: if(count($createdProducts)>0) {
73: if(!is_array($existsProducts)) {
74: $existsProducts = $createdProducts;
75: } else {
76: $existsProducts = array_merge($createdProducts);
77: }
78: }
79:
80: if ($column->getFilter()->getValue()) {
81: $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$existsProducts));
82: }
83: else {
84: if($productIds) {
85: $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
86: }
87: }
88: }
89: else {
90: parent::_addColumnFilterToCollection($column);
91: }
92: return $this;
93: }
94:
95: protected function _getCreatedProducts()
96: {
97: $products = $this->getRequest()->getPost('new_products', null);
98: if (!is_array($products)) {
99: $products = array();
100: }
101:
102: return $products;
103: }
104:
105: 106: 107: 108: 109:
110: protected function _prepareCollection()
111: {
112: $allowProductTypes = array();
113: foreach (Mage::helper('catalog/product_configuration')->getConfigurableAllowedTypes() as $type) {
114: $allowProductTypes[] = $type->getName();
115: }
116:
117: $product = $this->_getProduct();
118: $collection = $product->getCollection()
119: ->addAttributeToSelect('name')
120: ->addAttributeToSelect('sku')
121: ->addAttributeToSelect('attribute_set_id')
122: ->addAttributeToSelect('type_id')
123: ->addAttributeToSelect('price')
124: ->addFieldToFilter('attribute_set_id',$product->getAttributeSetId())
125: ->addFieldToFilter('type_id', $allowProductTypes)
126: ->addFilterByRequiredOptions()
127: ->joinAttribute('name', 'catalog_product/name', 'entity_id', null, 'inner');
128:
129: if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
130: Mage::getModel('cataloginventory/stock_item')->addCatalogInventoryToProductCollection($collection);
131: }
132:
133: foreach ($product->getTypeInstance(true)->getUsedProductAttributes($product) as $attribute) {
134: $collection->addAttributeToSelect($attribute->getAttributeCode());
135: $collection->addAttributeToFilter($attribute->getAttributeCode(), array('notnull'=>1));
136: }
137:
138: $this->setCollection($collection);
139:
140: if ($this->isReadonly()) {
141: $collection->addFieldToFilter('entity_id', array('in' => $this->_getSelectedProducts()));
142: }
143:
144: parent::_prepareCollection();
145: return $this;
146: }
147:
148: protected function _getSelectedProducts()
149: {
150: $products = $this->getRequest()->getPost('products', null);
151: if (!is_array($products)) {
152: $products = $this->_getProduct()->getTypeInstance(true)->getUsedProductIds($this->_getProduct());
153: }
154: return $products;
155: }
156:
157: 158: 159: 160: 161:
162: public function isReadonly()
163: {
164: if ($this->hasData('is_readonly')) {
165: return $this->getData('is_readonly');
166: }
167: return $this->_getProduct()->getCompositeReadonly();
168: }
169:
170: protected function _prepareColumns()
171: {
172: $product = $this->_getProduct();
173: $attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);
174:
175: if (!$this->isReadonly()) {
176: $this->addColumn('in_products', array(
177: 'header_css_class' => 'a-center',
178: 'type' => 'checkbox',
179: 'name' => 'in_products',
180: 'values' => $this->_getSelectedProducts(),
181: 'align' => 'center',
182: 'index' => 'entity_id',
183: 'renderer' => 'adminhtml/catalog_product_edit_tab_super_config_grid_renderer_checkbox',
184: 'attributes' => $attributes
185: ));
186: }
187:
188: $this->addColumn('entity_id', array(
189: 'header' => Mage::helper('catalog')->__('ID'),
190: 'sortable' => true,
191: 'width' => '60px',
192: 'index' => 'entity_id'
193: ));
194: $this->addColumn('name', array(
195: 'header' => Mage::helper('catalog')->__('Name'),
196: 'index' => 'name'
197: ));
198:
199:
200: $sets = Mage::getModel('eav/entity_attribute_set')->getCollection()
201: ->setEntityTypeFilter($this->_getProduct()->getResource()->getTypeId())
202: ->load()
203: ->toOptionHash();
204:
205: $this->addColumn('set_name',
206: array(
207: 'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
208: 'width' => '130px',
209: 'index' => 'attribute_set_id',
210: 'type' => 'options',
211: 'options' => $sets,
212: ));
213:
214: $this->addColumn('sku', array(
215: 'header' => Mage::helper('catalog')->__('SKU'),
216: 'width' => '80px',
217: 'index' => 'sku'
218: ));
219:
220: $this->addColumn('price', array(
221: 'header' => Mage::helper('catalog')->__('Price'),
222: 'type' => 'currency',
223: 'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
224: 'index' => 'price'
225: ));
226:
227: $this->addColumn('is_saleable', array(
228: 'header' => Mage::helper('catalog')->__('Inventory'),
229: 'renderer' => 'adminhtml/catalog_product_edit_tab_super_config_grid_renderer_inventory',
230: 'filter' => 'adminhtml/catalog_product_edit_tab_super_config_grid_filter_inventory',
231: 'index' => 'is_saleable'
232: ));
233:
234: foreach ($attributes as $attribute) {
235: $productAttribute = $attribute->getProductAttribute();
236: $productAttribute->getSource();
237: $this->addColumn($productAttribute->getAttributeCode(), array(
238: 'header' => $productAttribute->getFrontend()->getLabel(),
239: 'index' => $productAttribute->getAttributeCode(),
240: 'type' => $productAttribute->getSourceModel() ? 'options' : 'number',
241: 'options' => $productAttribute->getSourceModel() ? $this->getOptions($attribute) : ''
242: ));
243: }
244:
245: $this->addColumn('action',
246: array(
247: 'header' => Mage::helper('catalog')->__('Action'),
248: 'type' => 'action',
249: 'getter' => 'getId',
250: 'actions' => array(
251: array(
252: 'caption' => Mage::helper('catalog')->__('Edit'),
253: 'url' => $this->getEditParamsForAssociated(),
254: 'field' => 'id',
255: 'onclick' => 'superProduct.createPopup(this.href);return false;'
256: )
257: ),
258: 'filter' => false,
259: 'sortable' => false
260: ));
261:
262: return parent::_prepareColumns();
263: }
264:
265: public function getEditParamsForAssociated()
266: {
267: return array(
268: 'base' => '*/*/edit',
269: 'params' => array(
270: 'required' => $this->_getRequiredAttributesIds(),
271: 'popup' => 1,
272: 'product' => $this->_getProduct()->getId()
273: )
274: );
275: }
276:
277: 278: 279: 280: 281:
282: protected function _getRequiredAttributesIds()
283: {
284: $attributesIds = array();
285: foreach (
286: $this->_getProduct()
287: ->getTypeInstance(true)
288: ->getConfigurableAttributes($this->_getProduct()) as $attribute
289: ) {
290: $attributesIds[] = $attribute->getProductAttribute()->getId();
291: }
292:
293: return implode(',', $attributesIds);
294: }
295:
296: public function getOptions($attribute) {
297: $result = array();
298: foreach ($attribute->getProductAttribute()->getSource()->getAllOptions() as $option) {
299: if($option['value']!='') {
300: $result[$option['value']] = $option['label'];
301: }
302: }
303:
304: return $result;
305: }
306:
307: public function getGridUrl()
308: {
309: return $this->getUrl('*/*/superConfig', array('_current'=>true));
310: }
311:
312: }
313: