/app/code/core/Mage/Adminhtml/Block/Catalog/Category/Tab/Product.php

https://bitbucket.org/kdms/sh-magento · PHP · 162 lines · 116 code · 13 blank · 33 comment · 10 complexity · 8ee4072fcf731ec2ec2ee2dfc71d10de MD5 · raw file

  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Product in category grid
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Block_Catalog_Category_Tab_Product extends Mage_Adminhtml_Block_Widget_Grid
  34. {
  35. public function __construct()
  36. {
  37. parent::__construct();
  38. $this->setId('catalog_category_products');
  39. $this->setDefaultSort('entity_id');
  40. $this->setUseAjax(true);
  41. }
  42. public function getCategory()
  43. {
  44. return Mage::registry('category');
  45. }
  46. protected function _addColumnFilterToCollection($column)
  47. {
  48. // Set custom filter for in category flag
  49. if ($column->getId() == 'in_category') {
  50. $productIds = $this->_getSelectedProducts();
  51. if (empty($productIds)) {
  52. $productIds = 0;
  53. }
  54. if ($column->getFilter()->getValue()) {
  55. $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
  56. }
  57. elseif(!empty($productIds)) {
  58. $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
  59. }
  60. }
  61. else {
  62. parent::_addColumnFilterToCollection($column);
  63. }
  64. return $this;
  65. }
  66. protected function _prepareCollection()
  67. {
  68. if ($this->getCategory()->getId()) {
  69. $this->setDefaultFilter(array('in_category'=>1));
  70. }
  71. $collection = Mage::getModel('catalog/product')->getCollection()
  72. ->addAttributeToSelect('name')
  73. ->addAttributeToSelect('sku')
  74. ->addAttributeToSelect('price')
  75. ->addStoreFilter($this->getRequest()->getParam('store'))
  76. ->joinField('position',
  77. 'catalog/category_product',
  78. 'position',
  79. 'product_id=entity_id',
  80. 'category_id='.(int) $this->getRequest()->getParam('id', 0),
  81. 'left');
  82. $this->setCollection($collection);
  83. if ($this->getCategory()->getProductsReadonly()) {
  84. $productIds = $this->_getSelectedProducts();
  85. if (empty($productIds)) {
  86. $productIds = 0;
  87. }
  88. $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
  89. }
  90. return parent::_prepareCollection();
  91. }
  92. protected function _prepareColumns()
  93. {
  94. if (!$this->getCategory()->getProductsReadonly()) {
  95. $this->addColumn('in_category', array(
  96. 'header_css_class' => 'a-center',
  97. 'type' => 'checkbox',
  98. 'name' => 'in_category',
  99. 'values' => $this->_getSelectedProducts(),
  100. 'align' => 'center',
  101. 'index' => 'entity_id'
  102. ));
  103. }
  104. $this->addColumn('entity_id', array(
  105. 'header' => Mage::helper('catalog')->__('ID'),
  106. 'sortable' => true,
  107. 'width' => '60',
  108. 'index' => 'entity_id'
  109. ));
  110. $this->addColumn('name', array(
  111. 'header' => Mage::helper('catalog')->__('Name'),
  112. 'index' => 'name'
  113. ));
  114. $this->addColumn('sku', array(
  115. 'header' => Mage::helper('catalog')->__('SKU'),
  116. 'width' => '80',
  117. 'index' => 'sku'
  118. ));
  119. $this->addColumn('price', array(
  120. 'header' => Mage::helper('catalog')->__('Price'),
  121. 'type' => 'currency',
  122. 'width' => '1',
  123. 'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
  124. 'index' => 'price'
  125. ));
  126. $this->addColumn('position', array(
  127. 'header' => Mage::helper('catalog')->__('Position'),
  128. 'width' => '1',
  129. 'type' => 'number',
  130. 'index' => 'position',
  131. 'editable' => !$this->getCategory()->getProductsReadonly()
  132. //'renderer' => 'adminhtml/widget_grid_column_renderer_input'
  133. ));
  134. return parent::_prepareColumns();
  135. }
  136. public function getGridUrl()
  137. {
  138. return $this->getUrl('*/*/grid', array('_current'=>true));
  139. }
  140. protected function _getSelectedProducts()
  141. {
  142. $products = $this->getRequest()->getPost('selected_products');
  143. if (is_null($products)) {
  144. $products = $this->getCategory()->getProductsPosition();
  145. return array_keys($products);
  146. }
  147. return $products;
  148. }
  149. }