PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Widget/Chooser.php

https://bitbucket.org/jokusafet/magento2
PHP | 292 lines | 183 code | 20 blank | 89 comment | 20 complexity | 8446c3af0cf271148aefb1feaf75224b 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. * Product Chooser for "Product Link" Cms Widget Plugin
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Block_Catalog_Product_Widget_Chooser extends Mage_Adminhtml_Block_Widget_Grid
  34. {
  35. protected $_selectedProducts = array();
  36. /**
  37. * Block construction, prepare grid params
  38. */
  39. protected function _construct()
  40. {
  41. parent::_construct();
  42. $this->setDefaultSort('name');
  43. $this->setUseAjax(true);
  44. }
  45. /**
  46. * Prepare chooser element HTML
  47. *
  48. * @param Varien_Data_Form_Element_Abstract $element Form Element
  49. * @return Varien_Data_Form_Element_Abstract
  50. */
  51. public function prepareElementHtml(Varien_Data_Form_Element_Abstract $element)
  52. {
  53. $uniqId = Mage::helper('Mage_Core_Helper_Data')->uniqHash($element->getId());
  54. $sourceUrl = $this->getUrl('*/catalog_product_widget/chooser', array(
  55. 'uniq_id' => $uniqId,
  56. 'use_massaction' => false,
  57. ));
  58. $chooser = $this->getLayout()->createBlock('Mage_Widget_Block_Adminhtml_Widget_Chooser')
  59. ->setElement($element)
  60. ->setTranslationHelper($this->getTranslationHelper())
  61. ->setConfig($this->getConfig())
  62. ->setFieldsetId($this->getFieldsetId())
  63. ->setSourceUrl($sourceUrl)
  64. ->setUniqId($uniqId);
  65. if ($element->getValue()) {
  66. $value = explode('/', $element->getValue());
  67. $productId = false;
  68. if (isset($value[0]) && isset($value[1]) && $value[0] == 'product') {
  69. $productId = $value[1];
  70. }
  71. $categoryId = isset($value[2]) ? $value[2] : false;
  72. $label = '';
  73. if ($categoryId) {
  74. $label = Mage::getResourceSingleton('Mage_Catalog_Model_Resource_Category')
  75. ->getAttributeRawValue($categoryId, 'name', Mage::app()->getStore()) . '/';
  76. }
  77. if ($productId) {
  78. $label .= Mage::getResourceSingleton('Mage_Catalog_Model_Resource_Product')
  79. ->getAttributeRawValue($productId, 'name', Mage::app()->getStore());
  80. }
  81. $chooser->setLabel($label);
  82. }
  83. $element->setData('after_element_html', $chooser->toHtml());
  84. return $element;
  85. }
  86. /**
  87. * Checkbox Check JS Callback
  88. *
  89. * @return string
  90. */
  91. public function getCheckboxCheckCallback()
  92. {
  93. if ($this->getUseMassaction()) {
  94. return "function (grid, element) {
  95. $(grid.containerId).fire('product:changed', {element: element});
  96. }";
  97. }
  98. }
  99. /**
  100. * Grid Row JS Callback
  101. *
  102. * @return string
  103. */
  104. public function getRowClickCallback()
  105. {
  106. if (!$this->getUseMassaction()) {
  107. $chooserJsObject = $this->getId();
  108. return '
  109. function (grid, event) {
  110. var trElement = Event.findElement(event, "tr");
  111. var productId = trElement.down("td").innerHTML;
  112. var productName = trElement.down("td").next().next().innerHTML;
  113. var optionLabel = productName;
  114. var optionValue = "product/" + productId.replace(/^\s+|\s+$/g,"");
  115. if (grid.categoryId) {
  116. optionValue += "/" + grid.categoryId;
  117. }
  118. if (grid.categoryName) {
  119. optionLabel = grid.categoryName + " / " + optionLabel;
  120. }
  121. '.$chooserJsObject.'.setElementValue(optionValue);
  122. '.$chooserJsObject.'.setElementLabel(optionLabel);
  123. '.$chooserJsObject.'.close();
  124. }
  125. ';
  126. }
  127. }
  128. /**
  129. * Category Tree node onClick listener js function
  130. *
  131. * @return string
  132. */
  133. public function getCategoryClickListenerJs()
  134. {
  135. $js = '
  136. function (node, e) {
  137. {jsObject}.addVarToUrl("category_id", node.attributes.id);
  138. {jsObject}.reload({jsObject}.url);
  139. {jsObject}.categoryId = node.attributes.id != "none" ? node.attributes.id : false;
  140. {jsObject}.categoryName = node.attributes.id != "none" ? node.text : false;
  141. }
  142. ';
  143. $js = str_replace('{jsObject}', $this->getJsObjectName(), $js);
  144. return $js;
  145. }
  146. /**
  147. * Filter checked/unchecked rows in grid
  148. *
  149. * @param Mage_Adminhtml_Block_Widget_Grid_Column $column
  150. * @return Mage_Adminhtml_Block_Catalog_Product_Widget_Chooser
  151. */
  152. protected function _addColumnFilterToCollection($column)
  153. {
  154. if ($column->getId() == 'in_products') {
  155. $selected = $this->getSelectedProducts();
  156. if ($column->getFilter()->getValue()) {
  157. $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$selected));
  158. } else {
  159. $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$selected));
  160. }
  161. } else {
  162. parent::_addColumnFilterToCollection($column);
  163. }
  164. return $this;
  165. }
  166. /**
  167. * Prepare products collection, defined collection filters (category, product type)
  168. *
  169. * @return Mage_Adminhtml_Block_Widget_Grid
  170. */
  171. protected function _prepareCollection()
  172. {
  173. /* @var $collection Mage_Catalog_Model_Resource_Product_Collection */
  174. $collection = Mage::getResourceModel('Mage_Catalog_Model_Resource_Product_Collection')
  175. ->setStoreId(0)
  176. ->addAttributeToSelect('name');
  177. if ($categoryId = $this->getCategoryId()) {
  178. $category = Mage::getModel('Mage_Catalog_Model_Category')->load($categoryId);
  179. if ($category->getId()) {
  180. // $collection->addCategoryFilter($category);
  181. $productIds = $category->getProductsPosition();
  182. $productIds = array_keys($productIds);
  183. if (empty($productIds)) {
  184. $productIds = 0;
  185. }
  186. $collection->addFieldToFilter('entity_id', array('in' => $productIds));
  187. }
  188. }
  189. if ($productTypeId = $this->getProductTypeId()) {
  190. $collection->addAttributeToFilter('type_id', $productTypeId);
  191. }
  192. $this->setCollection($collection);
  193. return parent::_prepareCollection();
  194. }
  195. /**
  196. * Prepare columns for products grid
  197. *
  198. * @return Mage_Adminhtml_Block_Widget_Grid
  199. */
  200. protected function _prepareColumns()
  201. {
  202. if ($this->getUseMassaction()) {
  203. $this->addColumn('in_products', array(
  204. 'header_css_class' => 'a-center',
  205. 'type' => 'checkbox',
  206. 'name' => 'in_products',
  207. 'inline_css' => 'checkbox entities',
  208. 'field_name' => 'in_products',
  209. 'values' => $this->getSelectedProducts(),
  210. 'align' => 'center',
  211. 'index' => 'entity_id',
  212. 'use_index' => true,
  213. ));
  214. }
  215. $this->addColumn('entity_id', array(
  216. 'header' => Mage::helper('Mage_Catalog_Helper_Data')->__('ID'),
  217. 'sortable' => true,
  218. 'width' => '60px',
  219. 'index' => 'entity_id'
  220. ));
  221. $this->addColumn('chooser_sku', array(
  222. 'header' => Mage::helper('Mage_Catalog_Helper_Data')->__('SKU'),
  223. 'name' => 'chooser_sku',
  224. 'width' => '80px',
  225. 'index' => 'sku'
  226. ));
  227. $this->addColumn('chooser_name', array(
  228. 'header' => Mage::helper('Mage_Catalog_Helper_Data')->__('Product Name'),
  229. 'name' => 'chooser_name',
  230. 'index' => 'name'
  231. ));
  232. return parent::_prepareColumns();
  233. }
  234. /**
  235. * Adds additional parameter to URL for loading only products grid
  236. *
  237. * @return string
  238. */
  239. public function getGridUrl()
  240. {
  241. return $this->getUrl('*/catalog_product_widget/chooser', array(
  242. 'products_grid' => true,
  243. '_current' => true,
  244. 'uniq_id' => $this->getId(),
  245. 'use_massaction' => $this->getUseMassaction(),
  246. 'product_type_id' => $this->getProductTypeId()
  247. ));
  248. }
  249. /**
  250. * Setter
  251. *
  252. * @param array $selectedProducts
  253. * @return Mage_Adminhtml_Block_Catalog_Product_Widget_Chooser
  254. */
  255. public function setSelectedProducts($selectedProducts)
  256. {
  257. $this->_selectedProducts = $selectedProducts;
  258. return $this;
  259. }
  260. /**
  261. * Getter
  262. *
  263. * @return array
  264. */
  265. public function getSelectedProducts()
  266. {
  267. if ($selectedProducts = $this->getRequest()->getParam('selected_products', null)) {
  268. $this->setSelectedProducts($selectedProducts);
  269. }
  270. return $this->_selectedProducts;
  271. }
  272. }