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_Category_Tab_Product extends Mage_Adminhtml_Block_Widget_Grid
00035 {
00036
00037 public function __construct()
00038 {
00039 parent::__construct();
00040 $this->setId('catalog_category_products');
00041 $this->setDefaultSort('entity_id');
00042 $this->setUseAjax(true);
00043 }
00044
00045 public function getCategory()
00046 {
00047 return Mage::registry('category');
00048 }
00049
00050 protected function _addColumnFilterToCollection($column)
00051 {
00052
00053 if ($column->getId() == 'in_category') {
00054 $productIds = $this->_getSelectedProducts();
00055 if (empty($productIds)) {
00056 $productIds = 0;
00057 }
00058 if ($column->getFilter()->getValue()) {
00059 $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
00060 }
00061 elseif(!empty($productIds)) {
00062 $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
00063 }
00064 }
00065 else {
00066 parent::_addColumnFilterToCollection($column);
00067 }
00068 return $this;
00069 }
00070
00071 protected function _prepareCollection()
00072 {
00073 if ($this->getCategory()->getId()) {
00074 $this->setDefaultFilter(array('in_category'=>1));
00075 }
00076 $collection = Mage::getModel('catalog/product')->getCollection()
00077 ->addAttributeToSelect('name')
00078 ->addAttributeToSelect('sku')
00079 ->addAttributeToSelect('price')
00080 ->addStoreFilter($this->getRequest()->getParam('store'))
00081 ->joinField('position',
00082 'catalog/category_product',
00083 'position',
00084 'product_id=entity_id',
00085 'category_id='.(int) $this->getRequest()->getParam('id', 0),
00086 'left');
00087 $this->setCollection($collection);
00088
00089 if ($this->getCategory()->getProductsReadonly()) {
00090 $productIds = $this->_getSelectedProducts();
00091 if (empty($productIds)) {
00092 $productIds = 0;
00093 }
00094 $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
00095 }
00096
00097 return parent::_prepareCollection();
00098 }
00099
00100 protected function _prepareColumns()
00101 {
00102 if (!$this->getCategory()->getProductsReadonly()) {
00103 $this->addColumn('in_category', array(
00104 'header_css_class' => 'a-center',
00105 'type' => 'checkbox',
00106 'name' => 'in_category',
00107 'values' => $this->_getSelectedProducts(),
00108 'align' => 'center',
00109 'index' => 'entity_id'
00110 ));
00111 }
00112 $this->addColumn('entity_id', array(
00113 'header' => Mage::helper('catalog')->__('ID'),
00114 'sortable' => true,
00115 'width' => '60',
00116 'index' => 'entity_id'
00117 ));
00118 $this->addColumn('name', array(
00119 'header' => Mage::helper('catalog')->__('Name'),
00120 'index' => 'name'
00121 ));
00122 $this->addColumn('sku', array(
00123 'header' => Mage::helper('catalog')->__('SKU'),
00124 'width' => '80',
00125 'index' => 'sku'
00126 ));
00127 $this->addColumn('price', array(
00128 'header' => Mage::helper('catalog')->__('Price'),
00129 'type' => 'currency',
00130 'width' => '1',
00131 'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
00132 'index' => 'price'
00133 ));
00134 $this->addColumn('position', array(
00135 'header' => Mage::helper('catalog')->__('Position'),
00136 'width' => '1',
00137 'type' => 'number',
00138 'index' => 'position',
00139 'editable' => !$this->getCategory()->getProductsReadonly()
00140
00141 ));
00142
00143 return parent::_prepareColumns();
00144 }
00145
00146 public function getGridUrl()
00147 {
00148 return $this->getUrl('*/*/grid', array('_current'=>true));
00149 }
00150
00151 protected function _getSelectedProducts()
00152 {
00153 $products = $this->getRequest()->getPost('selected_products');
00154 if (is_null($products)) {
00155 $products = $this->getCategory()->getProductsPosition();
00156 return array_keys($products);
00157 }
00158 return $products;
00159 }
00160
00161 }
00162