PageRenderTime 1955ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/gryzz/crystal_magento
PHP | 256 lines | 155 code | 24 blank | 77 comment | 12 complexity | 2b8e22bc9d096fb8ca8efd01157ff072 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Crossell products admin grid
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Crosssell extends Mage_Adminhtml_Block_Widget_Grid
  34. {
  35. /**
  36. * Set grid params
  37. *
  38. */
  39. public function __construct()
  40. {
  41. parent::__construct();
  42. $this->setId('cross_sell_product_grid');
  43. $this->setDefaultSort('entity_id');
  44. $this->setUseAjax(true);
  45. if ($this->_getProduct()->getId()) {
  46. $this->setDefaultFilter(array('in_products'=>1));
  47. }
  48. }
  49. /**
  50. * Retirve currently edited product model
  51. *
  52. * @return Mage_Catalog_Model_Product
  53. */
  54. protected function _getProduct()
  55. {
  56. return Mage::registry('current_product');
  57. }
  58. /**
  59. * Add filter
  60. *
  61. * @param object $column
  62. * @return Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Crosssell
  63. */
  64. protected function _addColumnFilterToCollection($column)
  65. {
  66. // Set custom filter for in product flag
  67. if ($column->getId() == 'in_products') {
  68. $productIds = $this->_getSelectedProducts();
  69. if (empty($productIds)) {
  70. $productIds = 0;
  71. }
  72. if ($column->getFilter()->getValue()) {
  73. $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
  74. } else {
  75. if($productIds) {
  76. $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
  77. }
  78. }
  79. } else {
  80. parent::_addColumnFilterToCollection($column);
  81. }
  82. return $this;
  83. }
  84. /**
  85. * Prepare collection
  86. *
  87. * @return Mage_Adminhtml_Block_Widget_Grid
  88. */
  89. protected function _prepareCollection()
  90. {
  91. $collection = Mage::getModel('catalog/product_link')->useCrossSellLinks()
  92. ->getProductCollection()
  93. ->setProduct($this->_getProduct())
  94. ->setPositionOrder()
  95. ->addAttributeToSelect('*');
  96. if ($this->isReadonly()) {
  97. $productIds = $this->_getSelectedProducts();
  98. if (empty($productIds)) {
  99. $productIds = array(0);
  100. }
  101. $collection->addFieldToFilter('entity_id', array('in'=>$productIds));
  102. }
  103. $this->setCollection($collection);
  104. return parent::_prepareCollection();
  105. }
  106. /**
  107. * Checks when this block is readonly
  108. *
  109. * @return boolean
  110. */
  111. public function isReadonly()
  112. {
  113. return $this->_getProduct()->getCrosssellReadonly();
  114. }
  115. /**
  116. * Add columns to grid
  117. *
  118. * @return Mage_Adminhtml_Block_Widget_Grid
  119. */
  120. protected function _prepareColumns()
  121. {
  122. if (!$this->isReadonly()) {
  123. $this->addColumn('in_products', array(
  124. 'header_css_class' => 'a-center',
  125. 'type' => 'checkbox',
  126. 'name' => 'in_products',
  127. 'values' => $this->_getSelectedProducts(),
  128. 'align' => 'center',
  129. 'index' => 'entity_id'
  130. ));
  131. }
  132. $this->addColumn('entity_id', array(
  133. 'header' => Mage::helper('catalog')->__('ID'),
  134. 'sortable' => true,
  135. 'width' => 60,
  136. 'index' => 'entity_id'
  137. ));
  138. $this->addColumn('name', array(
  139. 'header' => Mage::helper('catalog')->__('Name'),
  140. 'index' => 'name'
  141. ));
  142. $this->addColumn('type', array(
  143. 'header' => Mage::helper('catalog')->__('Type'),
  144. 'width' => 100,
  145. 'index' => 'type_id',
  146. 'type' => 'options',
  147. 'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
  148. ));
  149. $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
  150. ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
  151. ->load()
  152. ->toOptionHash();
  153. $this->addColumn('set_name', array(
  154. 'header' => Mage::helper('catalog')->__('Attrib. Set Name'),
  155. 'width' => 130,
  156. 'index' => 'attribute_set_id',
  157. 'type' => 'options',
  158. 'options' => $sets,
  159. ));
  160. $this->addColumn('status', array(
  161. 'header' => Mage::helper('catalog')->__('Status'),
  162. 'width' => 90,
  163. 'index' => 'status',
  164. 'type' => 'options',
  165. 'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(),
  166. ));
  167. $this->addColumn('visibility', array(
  168. 'header' => Mage::helper('catalog')->__('Visibility'),
  169. 'width' => 90,
  170. 'index' => 'visibility',
  171. 'type' => 'options',
  172. 'options' => Mage::getSingleton('catalog/product_visibility')->getOptionArray(),
  173. ));
  174. $this->addColumn('sku', array(
  175. 'header' => Mage::helper('catalog')->__('SKU'),
  176. 'width' => 80,
  177. 'index' => 'sku'
  178. ));
  179. $this->addColumn('price', array(
  180. 'header' => Mage::helper('catalog')->__('Price'),
  181. 'type' => 'currency',
  182. 'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
  183. 'index' => 'price'
  184. ));
  185. $this->addColumn('position', array(
  186. 'header' => Mage::helper('catalog')->__('Position'),
  187. 'name' => 'position',
  188. 'width' => 60,
  189. 'type' => 'number',
  190. 'validate_class' => 'validate-number',
  191. 'index' => 'position',
  192. 'editable' => !$this->isReadonly(),
  193. 'edit_only' => !$this->_getProduct()->getId()
  194. ));
  195. return parent::_prepareColumns();
  196. }
  197. /**
  198. * Rerieve grid URL
  199. *
  200. * @return string
  201. */
  202. public function getGridUrl()
  203. {
  204. return $this->_getData('grid_url') ? $this->_getData('grid_url') : $this->getUrl('*/*/crosssellGrid', array('_current'=>true));
  205. }
  206. /**
  207. * Retrieve selected crosssell products
  208. *
  209. * @return array
  210. */
  211. protected function _getSelectedProducts()
  212. {
  213. $products = $this->getProductsCrossSell();
  214. if (!is_array($products)) {
  215. $products = array_keys($this->getSelectedCrossSellProducts());
  216. }
  217. return $products;
  218. }
  219. /**
  220. * Retrieve crosssell products
  221. *
  222. * @return array
  223. */
  224. public function getSelectedCrossSellProducts()
  225. {
  226. $products = array();
  227. foreach (Mage::registry('current_product')->getCrossSellProducts() as $product) {
  228. $products[$product->getId()] = array('position' => $product->getPosition());
  229. }
  230. return $products;
  231. }
  232. }