/app/code/local/Vilpjsc/Brand/Block/Adminhtml/Tab/Brand.php

https://bitbucket.org/acidel/buykoala · PHP · 145 lines · 91 code · 13 blank · 41 comment · 6 complexity · d0c9e21b86f0cb120d06fdfc225b4be6 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento Commercial Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Commercial Edition License
  8. * that is available at: http://www.magentocommerce.com/license/commercial-edition
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@magentocommerce.com so we can send you a copy immediately.
  12. *
  13. * DISCLAIMER
  14. *
  15. * Do not edit or add to this file if you wish to upgrade Magento to newer
  16. * versions in the future. If you wish to customize Magento for your
  17. * needs please refer to http://www.magentocommerce.com for more information.
  18. *
  19. * @category Mage
  20. * @package Mage_Adminhtml
  21. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  22. * @license http://www.magentocommerce.com/license/commercial-edition
  23. */
  24. /**
  25. * Product in category grid
  26. *
  27. * @category Mage
  28. * @package Mage_Adminhtml
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. class Vilpjsc_Brand_Block_Adminhtml_Tab_Brand extends Mage_Adminhtml_Block_Widget_Grid {
  32. public function __construct() {
  33. parent::__construct();
  34. $this->setId('catalog_category_products');
  35. $this->setDefaultSort('value');
  36. $this->setDefaultLimit(300);
  37. $this->setUseAjax(true);
  38. }
  39. public function getCategory() {
  40. return Mage::registry('category');
  41. }
  42. protected function _addColumnFilterToCollection($column) {
  43. // Set custom filter for in category flag
  44. if ($column->getId() == 'in_category') {
  45. $brandIds = $this->_getSelectedBrands();
  46. if (empty($brandIds)) {
  47. $brandIds = 0;
  48. }
  49. if ($column->getFilter()->getValue()) {
  50. $this->getCollection()->addFieldToFilter('entity_id', array('in' => $brandIds));
  51. } elseif (!empty($brandIds)) {
  52. $this->getCollection()->addFieldToFilter('entity_id', array('nin' => $brandIds));
  53. }
  54. } elseif($column->getId() == 'title'){
  55. $this->getCollection()->addFieldToFilter('store_value.value', array('like' => $column->getFilter()->getValue()."%"));
  56. }else {
  57. parent::_addColumnFilterToCollection($column);
  58. }
  59. return $this;
  60. }
  61. protected function _prepareCollection() {
  62. $attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
  63. ->setCodeFilter('manufacturer')->getFirstItem();
  64. $collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
  65. ->setPositionOrder('asc')
  66. ->setAttributeFilter($attributeInfo->getAttributeId())
  67. ->setStoreFilter(Mage::app()->getStore()->getId());
  68. /* $collection = Mage::getModel('brand/brand')->getCollection(); */
  69. $collection->getSelect()->joinLeft(
  70. array('brand' => $collection->getTable('brand/brand')),
  71. 'brand.manufacturer_option_id = main_table.option_id',
  72. array('brand.status as brand_status', 'brand.filename as logo', 'brand.content as brand_content')
  73. );
  74. $this->setCollection($collection);
  75. return parent::_prepareCollection();
  76. }
  77. protected function _prepareColumns() {
  78. $this->addColumn('in_category', array(
  79. 'header_css_class' => 'a-center',
  80. 'type' => 'checkbox',
  81. 'name' => $this->_getName(),
  82. 'values' => $this->_getSelectedBrands(),
  83. 'align' => 'center',
  84. 'index' => 'option_id',
  85. 'renderer' => new Vilpjsc_Brand_Block_Adminhtml_Tab_Brand_Renderer_Checkbox()
  86. ));
  87. $this->addColumn('title', array(
  88. 'header_css_class' => 'a-center',
  89. 'header' => Mage::helper('brand')->__('Brand Name'),
  90. 'width' => '120',
  91. 'index' => 'value',
  92. 'filter_index' => 'store_value',
  93. ));
  94. $this->addColumn('content', array(
  95. 'header_css_class' => 'a-center',
  96. 'header' => Mage::helper('brand')->__('Description'),
  97. 'sortable' => true,
  98. 'index' => 'brand_content'
  99. ));
  100. /*$this->addColumn('logo', array(
  101. 'header_css_class' => 'a-center',
  102. 'header' => Mage::helper('brand')->__('Logo'),
  103. 'sortable' => true,
  104. 'width' => '120',
  105. 'align' => 'center',
  106. 'index' => 'logo',
  107. 'renderer' => new Vilpjsc_Brand_Block_Adminhtml_Brand_Grid_Renderer_Image()
  108. ));
  109. */
  110. return parent::_prepareColumns();
  111. }
  112. public function getGridUrl() {
  113. return $this->getUrl('*/*/grid', array('_current' => true));
  114. }
  115. protected function _getSelectedBrands() {
  116. $brands = $this->getRequest()->getPost('selected_brands');
  117. $categoryId = $this->getCategory()->getId();
  118. if (is_null($brands)) {
  119. $arrayBrands = array();
  120. $brands = Mage::getModel("brand/category")->getCollection()->addFieldToFilter("entity_id", array("eq" => $categoryId));
  121. foreach ($brands as $brand) {
  122. $arrayBrands[] = $brand->getBrandId();
  123. }
  124. return $arrayBrands;
  125. }
  126. else
  127. return $brands;
  128. }
  129. protected function _getName() {
  130. return "brand[id][]";
  131. }
  132. }