00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Super_Config_Grid extends Mage_Adminhtml_Block_Widget_Grid
00035 {
00036
00037 public function __construct()
00038 {
00039 parent::__construct();
00040 $this->setUseAjax(true);
00041 $this->setId('super_product_links');
00042
00043 if ($this->_getProduct()->getId()) {
00044 $this->setDefaultFilter(array('in_products'=>1));
00045 }
00046 }
00047
00048
00049
00050
00051
00052
00053 protected function _getProduct()
00054 {
00055 return Mage::registry('current_product');
00056 }
00057
00058 protected function _addColumnFilterToCollection($column)
00059 {
00060
00061 if ($column->getId() == 'in_products') {
00062 $productIds = $this->_getSelectedProducts();
00063
00064 if (empty($productIds)) {
00065 $productIds = 0;
00066 }
00067
00068 $createdProducts = $this->_getCreatedProducts();
00069
00070 $existsProducts = $productIds;
00071
00072 if(count($createdProducts)>0) {
00073 if(!is_array($existsProducts)) {
00074 $existsProducts = $createdProducts;
00075 } else {
00076 $existsProducts = array_merge($createdProducts);
00077 }
00078 }
00079
00080 if ($column->getFilter()->getValue()) {
00081 $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$existsProducts));
00082 }
00083 else {
00084 if($productIds) {
00085 $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
00086 }
00087 }
00088 }
00089 else {
00090 parent::_addColumnFilterToCollection($column);
00091 }
00092 return $this;
00093 }
00094
00095 protected function _getCreatedProducts()
00096 {
00097 $products = $this->getRequest()->getPost('new_products', null);
00098 if (!is_array($products)) {
00099 $products = array();
00100 }
00101
00102 return $products;
00103 }
00104
00105 protected function _prepareCollection()
00106 {
00107 $allowProductTypes = array();
00108 foreach (Mage::getConfig()->getNode('global/catalog/product/type/configurable/allow_product_types')->children() as $type) {
00109 $allowProductTypes[] = $type->getName();
00110 }
00111
00112 $product = $this->_getProduct();
00113 $collection = $product->getCollection()
00114 ->addAttributeToSelect('name')
00115 ->addAttributeToSelect('sku')
00116 ->addAttributeToSelect('attribute_set_id')
00117 ->addAttributeToSelect('type_id')
00118 ->addAttributeToSelect('price')
00119 ->addFieldToFilter('attribute_set_id',$product->getAttributeSetId())
00120 ->addFieldToFilter('type_id', $allowProductTypes)
00121 ->addFilterByRequiredOptions();
00122
00123 Mage::getModel('cataloginventory/stock_item')->addCatalogInventoryToProductCollection($collection);
00124
00125 foreach ($product->getTypeInstance(true)->getUsedProductAttributes($product) as $attribute) {
00126 $collection->addAttributeToSelect($attribute->getAttributeCode());
00127 $collection->addAttributeToFilter($attribute->getAttributeCode(), array('nin'=>array(null)));
00128 }
00129
00130 $this->setCollection($collection);
00131
00132 if ($this->isReadonly()) {
00133 $collection->addFieldToFilter('entity_id', array('in' => $this->_getSelectedProducts()));
00134 }
00135
00136 parent::_prepareCollection();
00137 return $this;
00138 }
00139
00140 protected function _getSelectedProducts()
00141 {
00142 $products = $this->getRequest()->getPost('products', null);
00143 if (!is_array($products)) {
00144 $products = $this->_getProduct()->getTypeInstance(true)->getUsedProductIds($this->_getProduct());
00145 }
00146 return $products;
00147 }
00148
00149
00150
00151
00152
00153
00154 public function isReadonly()
00155 {
00156 return $this->_getProduct()->getCompositeReadonly();
00157 }
00158
00159 protected function _prepareColumns()
00160 {
00161 $product = $this->_getProduct();
00162 $attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);
00163
00164 if (!$this->isReadonly()) {
00165 $this->addColumn('in_products', array(
00166 'header_css_class' => 'a-center',
00167 'type' => 'checkbox',
00168 'name' => 'in_products',
00169 'values' => $this->_getSelectedProducts(),
00170 'align' => 'center',
00171 'index' => 'entity_id',
00172 'renderer' => 'adminhtml/catalog_product_edit_tab_super_config_grid_renderer_checkbox',
00173 'attributes' => $attributes
00174 ));
00175 }
00176
00177 $this->addColumn('entity_id', array(
00178 'header' => Mage::helper('catalog')->__('ID'),
00179 'sortable' => true,
00180 'width' => '60px',
00181 'index' => 'entity_id'
00182 ));
00183 $this->addColumn('name', array(
00184 'header' => Mage::helper('catalog')->__('Name'),
00185 'index' => 'name'
00186 ));
00187
00188
00189 $sets = Mage::getModel('eav/entity_attribute_set')->getCollection()
00190 ->setEntityTypeFilter($this->_getProduct()->getResource()->getTypeId())
00191 ->load()
00192 ->toOptionHash();
00193
00194 $this->addColumn('set_name',
00195 array(
00196 'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
00197 'width' => '130px',
00198 'index' => 'attribute_set_id',
00199 'type' => 'options',
00200 'options' => $sets,
00201 ));
00202
00203 $this->addColumn('sku', array(
00204 'header' => Mage::helper('catalog')->__('SKU'),
00205 'width' => '80px',
00206 'index' => 'sku'
00207 ));
00208
00209 $this->addColumn('price', array(
00210 'header' => Mage::helper('catalog')->__('Price'),
00211 'type' => 'currency',
00212 'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
00213 'index' => 'price'
00214 ));
00215
00216 $this->addColumn('is_saleable', array(
00217 'header' => Mage::helper('catalog')->__('Inventory'),
00218 'renderer' => 'adminhtml/catalog_product_edit_tab_super_config_grid_renderer_inventory',
00219 'filter' => 'adminhtml/catalog_product_edit_tab_super_config_grid_filter_inventory',
00220 'index' => 'is_saleable'
00221 ));
00222
00223 foreach ($attributes as $attribute) {
00224 $productAttribute = $attribute->getProductAttribute();
00225 $productAttribute->getSource();
00226 $this->addColumn($productAttribute->getAttributeCode(), array(
00227 'header' => $productAttribute->getFrontend()->getLabel(),
00228 'index' => $productAttribute->getAttributeCode(),
00229 'type' => $productAttribute->getSourceModel() ? 'options' : 'number',
00230 'options' => $productAttribute->getSourceModel() ? $this->getOptions($attribute) : ''
00231 ));
00232 }
00233
00234 $this->addColumn('action',
00235 array(
00236 'header' => Mage::helper('catalog')->__('Action'),
00237 'type' => 'action',
00238 'getter' => 'getId',
00239 'actions' => array(
00240 array(
00241 'caption' => Mage::helper('catalog')->__('Edit'),
00242 'url' => $this->getEditParamsForAssociated(),
00243 'field' => 'id',
00244 'onclick' => 'superProduct.createPopup(this.href);return false;'
00245 )
00246 ),
00247 'filter' => false,
00248 'sortable' => false
00249 ));
00250
00251 return parent::_prepareColumns();
00252 }
00253
00254 public function getEditParamsForAssociated()
00255 {
00256 return array(
00257 'base' => '*/*/edit',
00258 'params' => array(
00259 'required' => $this->_getRequiredAttributesIds(),
00260 'popup' => 1,
00261 'product' => $this->_getProduct()->getId()
00262 )
00263 );
00264 }
00265
00266 protected function _getRequiredAttributesIds()
00267 {
00268 $attributesIds = array();
00269 foreach ($this->_getProduct()->getTypeInstance(true)->getConfigurableAttributes($this->_getProduct()) as $attribute) {
00270 $attributesIds[] = $attribute->getProductAttribute()->getId();
00271 }
00272
00273 return implode(',', $attributesIds);
00274 }
00275
00276 public function getOptions($attribute) {
00277 $result = array();
00278 foreach ($attribute->getProductAttribute()->getSource()->getAllOptions() as $option) {
00279 if($option['value']!='') {
00280 $result[$option['value']] = $option['label'];
00281 }
00282 }
00283
00284 return $result;
00285 }
00286
00287 public function getGridUrl()
00288 {
00289 return $this->getUrl('*/*/superConfig', array('_current'=>true));
00290 }
00291
00292 }