PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/jokusafet/magento2
PHP | 257 lines | 157 code | 23 blank | 77 comment | 15 complexity | 44b06ec65ef63f004bd219d2b87837a2 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) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Upsell 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_Upsell extends Mage_Adminhtml_Block_Widget_Grid
  34. {
  35. /**
  36. * Set grid params
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. parent::_construct();
  42. $this->setId('up_sell_product_grid');
  43. $this->setDefaultSort('entity_id');
  44. $this->setUseAjax(true);
  45. if ($this->_getProduct() && $this->_getProduct()->getId()) {
  46. $this->setDefaultFilter(array('in_products'=>1));
  47. }
  48. if ($this->isReadonly()) {
  49. $this->setFilterVisibility(false);
  50. }
  51. }
  52. /**
  53. * Retirve currently edited product model
  54. *
  55. * @return Mage_Catalog_Model_Product
  56. */
  57. protected function _getProduct()
  58. {
  59. return Mage::registry('current_product');
  60. }
  61. /**
  62. * Add filter
  63. *
  64. * @param object $column
  65. * @return Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Upsell
  66. */
  67. protected function _addColumnFilterToCollection($column)
  68. {
  69. // Set custom filter for in product flag
  70. if ($column->getId() == 'in_products') {
  71. $productIds = $this->_getSelectedProducts();
  72. if (empty($productIds)) {
  73. $productIds = 0;
  74. }
  75. if ($column->getFilter()->getValue()) {
  76. $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
  77. } else {
  78. if($productIds) {
  79. $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
  80. }
  81. }
  82. } else {
  83. parent::_addColumnFilterToCollection($column);
  84. }
  85. return $this;
  86. }
  87. /**
  88. * Checks when this block is readonly
  89. *
  90. * @return boolean
  91. */
  92. public function isReadonly()
  93. {
  94. return $this->_getProduct() && $this->_getProduct()->getUpsellReadonly();
  95. }
  96. /**
  97. * Prepare collection
  98. *
  99. * @return Mage_Adminhtml_Block_Widget_Grid
  100. */
  101. protected function _prepareCollection()
  102. {
  103. $collection = Mage::getModel('Mage_Catalog_Model_Product_Link')->useUpSellLinks()
  104. ->getProductCollection()
  105. ->setProduct($this->_getProduct())
  106. ->addAttributeToSelect('*');
  107. if ($this->isReadonly()) {
  108. $productIds = $this->_getSelectedProducts();
  109. if (empty($productIds)) {
  110. $productIds = array(0);
  111. }
  112. $collection->addFieldToFilter('entity_id', array('in'=>$productIds));
  113. }
  114. $this->setCollection($collection);
  115. return parent::_prepareCollection();
  116. }
  117. /**
  118. * Add columns to grid
  119. *
  120. * @return Mage_Adminhtml_Block_Widget_Grid
  121. */
  122. protected function _prepareColumns()
  123. {
  124. if (!$this->_getProduct()->getUpsellReadonly()) {
  125. $this->addColumn('in_products', array(
  126. 'header_css_class' => 'a-center',
  127. 'type' => 'checkbox',
  128. 'name' => 'in_products',
  129. 'values' => $this->_getSelectedProducts(),
  130. 'align' => 'center',
  131. 'index' => 'entity_id'
  132. ));
  133. }
  134. $this->addColumn('entity_id', array(
  135. 'header' => Mage::helper('Mage_Catalog_Helper_Data')->__('ID'),
  136. 'sortable' => true,
  137. 'width' => 60,
  138. 'index' => 'entity_id'
  139. ));
  140. $this->addColumn('name', array(
  141. 'header' => Mage::helper('Mage_Catalog_Helper_Data')->__('Name'),
  142. 'index' => 'name'
  143. ));
  144. $this->addColumn('type', array(
  145. 'header' => Mage::helper('Mage_Catalog_Helper_Data')->__('Type'),
  146. 'width' => 100,
  147. 'index' => 'type_id',
  148. 'type' => 'options',
  149. 'options' => Mage::getSingleton('Mage_Catalog_Model_Product_Type')->getOptionArray(),
  150. ));
  151. $sets = Mage::getResourceModel('Mage_Eav_Model_Resource_Entity_Attribute_Set_Collection')
  152. ->setEntityTypeFilter(Mage::getModel('Mage_Catalog_Model_Product')->getResource()->getTypeId())
  153. ->load()
  154. ->toOptionHash();
  155. $this->addColumn('set_name', array(
  156. 'header' => Mage::helper('Mage_Catalog_Helper_Data')->__('Attrib. Set Name'),
  157. 'width' => 130,
  158. 'index' => 'attribute_set_id',
  159. 'type' => 'options',
  160. 'options' => $sets,
  161. ));
  162. $this->addColumn('status', array(
  163. 'header' => Mage::helper('Mage_Catalog_Helper_Data')->__('Status'),
  164. 'width' => 90,
  165. 'index' => 'status',
  166. 'type' => 'options',
  167. 'options' => Mage::getSingleton('Mage_Catalog_Model_Product_Status')->getOptionArray(),
  168. ));
  169. $this->addColumn('visibility', array(
  170. 'header' => Mage::helper('Mage_Catalog_Helper_Data')->__('Visibility'),
  171. 'width' => 90,
  172. 'index' => 'visibility',
  173. 'type' => 'options',
  174. 'options' => Mage::getSingleton('Mage_Catalog_Model_Product_Visibility')->getOptionArray(),
  175. ));
  176. $this->addColumn('sku', array(
  177. 'header' => Mage::helper('Mage_Catalog_Helper_Data')->__('SKU'),
  178. 'width' => 80,
  179. 'index' => 'sku'
  180. ));
  181. $this->addColumn('price', array(
  182. 'header' => Mage::helper('Mage_Catalog_Helper_Data')->__('Price'),
  183. 'type' => 'currency',
  184. 'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
  185. 'index' => 'price'
  186. ));
  187. $this->addColumn('position', array(
  188. 'header' => Mage::helper('Mage_Catalog_Helper_Data')->__('Position'),
  189. 'name' => 'position',
  190. 'type' => 'number',
  191. 'width' => 60,
  192. 'validate_class' => 'validate-number',
  193. 'index' => 'position',
  194. 'editable' => !$this->_getProduct()->getUpsellReadonly(),
  195. 'edit_only' => !$this->_getProduct()->getId()
  196. ));
  197. return parent::_prepareColumns();
  198. }
  199. /**
  200. * Rerieve grid URL
  201. *
  202. * @return string
  203. */
  204. public function getGridUrl()
  205. {
  206. return $this->_getData('grid_url') ? $this->_getData('grid_url') : $this->getUrl('*/*/upsellGrid', array('_current'=>true));
  207. }
  208. /**
  209. * Retrieve selected upsell products
  210. *
  211. * @return array
  212. */
  213. protected function _getSelectedProducts()
  214. {
  215. $products = $this->getProductsUpsell();
  216. if (!is_array($products)) {
  217. $products = array_keys($this->getSelectedUpsellProducts());
  218. }
  219. return $products;
  220. }
  221. /**
  222. * Retrieve upsell products
  223. *
  224. * @return array
  225. */
  226. public function getSelectedUpsellProducts()
  227. {
  228. $products = array();
  229. foreach (Mage::registry('current_product')->getUpSellProducts() as $product) {
  230. $products[$product->getId()] = array('position' => $product->getPosition());
  231. }
  232. return $products;
  233. }
  234. }