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_Related extends Mage_Adminhtml_Block_Widget_Grid
00035 {
00036 public function __construct()
00037 {
00038 parent::__construct();
00039 $this->setId('related_product_grid');
00040 $this->setDefaultSort('entity_id');
00041 $this->setUseAjax(true);
00042 if ($this->_getProduct()->getId()) {
00043 $this->setDefaultFilter(array('in_products'=>1));
00044 }
00045 }
00046
00047
00048
00049
00050
00051
00052 protected function _getProduct()
00053 {
00054 return Mage::registry('current_product');
00055 }
00056
00057 protected function _addColumnFilterToCollection($column)
00058 {
00059
00060 if ($column->getId() == 'in_products') {
00061 $productIds = $this->_getSelectedProducts();
00062 if (empty($productIds)) {
00063 $productIds = 0;
00064 }
00065 if ($column->getFilter()->getValue()) {
00066 $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
00067 }
00068 else {
00069 if($productIds) {
00070 $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
00071 }
00072 }
00073 }
00074 else {
00075 parent::_addColumnFilterToCollection($column);
00076 }
00077 return $this;
00078 }
00079
00080 protected function _prepareCollection()
00081 {
00082 $collection = Mage::getModel('catalog/product_link')->useRelatedLinks()
00083 ->getProductCollection()
00084 ->setProduct($this->_getProduct())
00085 ->addAttributeToSelect('*');
00086
00087 if ($this->isReadonly()) {
00088 $productIds = $this->_getSelectedProducts();
00089 if (empty($productIds)) {
00090 $productIds = array(0);
00091 }
00092 $collection->addFieldToFilter('entity_id', array('in'=>$productIds));
00093 }
00094
00095 $this->setCollection($collection);
00096 return parent::_prepareCollection();
00097 }
00098
00099
00100
00101
00102
00103
00104 public function isReadonly()
00105 {
00106 return $this->_getProduct()->getRelatedReadonly();
00107 }
00108
00109
00110 protected function _prepareColumns()
00111 {
00112 if (!$this->isReadonly()) {
00113 $this->addColumn('in_products', array(
00114 'header_css_class' => 'a-center',
00115 'type' => 'checkbox',
00116 'name' => 'in_products',
00117 'values' => $this->_getSelectedProducts(),
00118 'align' => 'center',
00119 'index' => 'entity_id'
00120 ));
00121 }
00122
00123 $this->addColumn('entity_id', array(
00124 'header' => Mage::helper('catalog')->__('ID'),
00125 'sortable' => true,
00126 'width' => '60px',
00127 'index' => 'entity_id'
00128 ));
00129 $this->addColumn('name', array(
00130 'header' => Mage::helper('catalog')->__('Name'),
00131 'index' => 'name'
00132 ));
00133
00134 $this->addColumn('type',
00135 array(
00136 'header'=> Mage::helper('catalog')->__('Type'),
00137 'width' => '100px',
00138 'index' => 'type_id',
00139 'type' => 'options',
00140 'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
00141 ));
00142
00143 $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
00144 ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
00145 ->load()
00146 ->toOptionHash();
00147
00148 $this->addColumn('set_name',
00149 array(
00150 'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
00151 'width' => '130px',
00152 'index' => 'attribute_set_id',
00153 'type' => 'options',
00154 'options' => $sets,
00155 ));
00156
00157 $this->addColumn('status',
00158 array(
00159 'header'=> Mage::helper('catalog')->__('Status'),
00160 'width' => '90px',
00161 'index' => 'status',
00162 'type' => 'options',
00163 'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(),
00164 ));
00165
00166 $this->addColumn('visibility',
00167 array(
00168 'header'=> Mage::helper('catalog')->__('Visibility'),
00169 'width' => '90px',
00170 'index' => 'visibility',
00171 'type' => 'options',
00172 'options' => Mage::getSingleton('catalog/product_visibility')->getOptionArray(),
00173 ));
00174
00175 $this->addColumn('sku', array(
00176 'header' => Mage::helper('catalog')->__('SKU'),
00177 'width' => '80px',
00178 'index' => 'sku'
00179 ));
00180 $this->addColumn('price', array(
00181 'header' => Mage::helper('catalog')->__('Price'),
00182 'type' => 'currency',
00183 'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
00184 'index' => 'price'
00185 ));
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 $this->addColumn('position', array(
00199 'header' => Mage::helper('catalog')->__('Position'),
00200 'name' => 'position',
00201 'type' => 'number',
00202 'validate_class' => 'validate-number',
00203 'index' => 'position',
00204 'width' => '60px',
00205 'editable' => !$this->isReadonly(),
00206 'edit_only' => !$this->_getProduct()->getId()
00207 ));
00208
00209 return parent::_prepareColumns();
00210 }
00211
00212 public function getGridUrl()
00213 {
00214 return $this->getData('grid_url') ? $this->getData('grid_url') : $this->getUrl('*/*/related', array('_current'=>true));
00215 }
00216
00217 protected function _getSelectedProducts()
00218 {
00219 $products = $this->getRequest()->getPost('products', null);
00220 if (!is_array($products)) {
00221 $products = $this->_getProduct()->getRelatedProductIds();
00222 }
00223 return $products;
00224 }
00225 }