PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/magento/module-catalog/Block/Adminhtml/Product/Edit/Tab/Upsell.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 376 lines | 244 code | 31 blank | 101 comment | 15 complexity | 858b6605f84978d6e4cbcd273f24d0d5 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Upsell products admin grid
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Catalog\Block\Adminhtml\Product\Edit\Tab;
  12. class Upsell extends \Magento\Backend\Block\Widget\Grid\Extended
  13. {
  14. /**
  15. * Core registry
  16. *
  17. * @var \Magento\Framework\Registry
  18. */
  19. protected $_coreRegistry = null;
  20. /**
  21. * @var \Magento\Catalog\Model\Product\LinkFactory
  22. */
  23. protected $_linkFactory;
  24. /**
  25. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory]
  26. */
  27. protected $_setsFactory;
  28. /**
  29. * @var \Magento\Catalog\Model\ProductFactory
  30. */
  31. protected $_productFactory;
  32. /**
  33. * @var \Magento\Catalog\Model\Product\Type
  34. */
  35. protected $_type;
  36. /**
  37. * @var \Magento\Catalog\Model\Product\Attribute\Source\Status
  38. */
  39. protected $_status;
  40. /**
  41. * @var \Magento\Catalog\Model\Product\Visibility
  42. */
  43. protected $_visibility;
  44. /**
  45. * @param \Magento\Backend\Block\Template\Context $context
  46. * @param \Magento\Backend\Helper\Data $backendHelper
  47. * @param \Magento\Catalog\Model\Product\LinkFactory $linkFactory
  48. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setsFactory
  49. * @param \Magento\Catalog\Model\ProductFactory $productFactory
  50. * @param \Magento\Catalog\Model\Product\Type $type
  51. * @param \Magento\Catalog\Model\Product\Attribute\Source\Status $status
  52. * @param \Magento\Catalog\Model\Product\Visibility $visibility
  53. * @param \Magento\Framework\Registry $coreRegistry
  54. * @param array $data
  55. *
  56. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  57. */
  58. public function __construct(
  59. \Magento\Backend\Block\Template\Context $context,
  60. \Magento\Backend\Helper\Data $backendHelper,
  61. \Magento\Catalog\Model\Product\LinkFactory $linkFactory,
  62. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setsFactory,
  63. \Magento\Catalog\Model\ProductFactory $productFactory,
  64. \Magento\Catalog\Model\Product\Type $type,
  65. \Magento\Catalog\Model\Product\Attribute\Source\Status $status,
  66. \Magento\Catalog\Model\Product\Visibility $visibility,
  67. \Magento\Framework\Registry $coreRegistry,
  68. array $data = []
  69. ) {
  70. $this->_linkFactory = $linkFactory;
  71. $this->_setsFactory = $setsFactory;
  72. $this->_productFactory = $productFactory;
  73. $this->_type = $type;
  74. $this->_status = $status;
  75. $this->_visibility = $visibility;
  76. $this->_coreRegistry = $coreRegistry;
  77. parent::__construct($context, $backendHelper, $data);
  78. }
  79. /**
  80. * Set grid params
  81. *
  82. * @return void
  83. */
  84. protected function _construct()
  85. {
  86. parent::_construct();
  87. $this->setId('up_sell_product_grid');
  88. $this->setDefaultSort('entity_id');
  89. $this->setUseAjax(true);
  90. if ($this->getProduct() && $this->getProduct()->getId()) {
  91. $this->setDefaultFilter(['in_products' => 1]);
  92. }
  93. if ($this->isReadonly()) {
  94. $this->setFilterVisibility(false);
  95. }
  96. }
  97. /**
  98. * Retirve currently edited product model
  99. *
  100. * @return \Magento\Catalog\Model\Product
  101. */
  102. public function getProduct()
  103. {
  104. return $this->_coreRegistry->registry('current_product');
  105. }
  106. /**
  107. * Add filter
  108. *
  109. * @param object $column
  110. * @return $this
  111. */
  112. protected function _addColumnFilterToCollection($column)
  113. {
  114. // Set custom filter for in product flag
  115. if ($column->getId() == 'in_products') {
  116. $productIds = $this->_getSelectedProducts();
  117. if (empty($productIds)) {
  118. $productIds = 0;
  119. }
  120. if ($column->getFilter()->getValue()) {
  121. $this->getCollection()->addFieldToFilter('entity_id', ['in' => $productIds]);
  122. } else {
  123. if ($productIds) {
  124. $this->getCollection()->addFieldToFilter('entity_id', ['nin' => $productIds]);
  125. }
  126. }
  127. } else {
  128. parent::_addColumnFilterToCollection($column);
  129. }
  130. return $this;
  131. }
  132. /**
  133. * Checks when this block is readonly
  134. *
  135. * @return boolean
  136. */
  137. public function isReadonly()
  138. {
  139. return $this->getProduct() && $this->getProduct()->getUpsellReadonly();
  140. }
  141. /**
  142. * Prepare collection
  143. *
  144. * @return \Magento\Backend\Block\Widget\Grid\Extended
  145. */
  146. protected function _prepareCollection()
  147. {
  148. $collection = $this->_linkFactory->create()->useUpSellLinks()->getProductCollection()->setProduct(
  149. $this->getProduct()
  150. )->addAttributeToSelect(
  151. '*'
  152. );
  153. if ($this->isReadonly()) {
  154. $productIds = $this->_getSelectedProducts();
  155. if (empty($productIds)) {
  156. $productIds = [0];
  157. }
  158. $collection->addFieldToFilter('entity_id', ['in' => $productIds]);
  159. }
  160. $this->setCollection($collection);
  161. return parent::_prepareCollection();
  162. }
  163. /**
  164. * Add columns to grid
  165. *
  166. * @return $this
  167. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  168. */
  169. protected function _prepareColumns()
  170. {
  171. if (!$this->getProduct()->getUpsellReadonly()) {
  172. $this->addColumn(
  173. 'in_products',
  174. [
  175. 'type' => 'checkbox',
  176. 'name' => 'in_products',
  177. 'values' => $this->_getSelectedProducts(),
  178. 'align' => 'center',
  179. 'index' => 'entity_id',
  180. 'header_css_class' => 'col-select',
  181. 'column_css_class' => 'col-select'
  182. ]
  183. );
  184. }
  185. $this->addColumn(
  186. 'entity_id',
  187. [
  188. 'header' => __('ID'),
  189. 'sortable' => true,
  190. 'index' => 'entity_id',
  191. 'header_css_class' => 'col-id',
  192. 'column_css_class' => 'col-id'
  193. ]
  194. );
  195. $this->addColumn(
  196. 'name',
  197. [
  198. 'header' => __('Name'),
  199. 'index' => 'name',
  200. 'header_css_class' => 'col-name',
  201. 'column_css_class' => 'col-name'
  202. ]
  203. );
  204. $this->addColumn(
  205. 'type',
  206. [
  207. 'header' => __('Type'),
  208. 'index' => 'type_id',
  209. 'type' => 'options',
  210. 'options' => $this->_type->getOptionArray(),
  211. 'header_css_class' => 'col-type',
  212. 'column_css_class' => 'col-type'
  213. ]
  214. );
  215. $sets = $this->_setsFactory->create()->setEntityTypeFilter(
  216. $this->_productFactory->create()->getResource()->getTypeId()
  217. )->load()->toOptionHash();
  218. $this->addColumn(
  219. 'set_name',
  220. [
  221. 'header' => __('Attribute Set'),
  222. 'index' => 'attribute_set_id',
  223. 'type' => 'options',
  224. 'options' => $sets,
  225. 'header_css_class' => 'col-attr-name',
  226. 'column_css_class' => 'col-attr-name'
  227. ]
  228. );
  229. $this->addColumn(
  230. 'status',
  231. [
  232. 'header' => __('Status'),
  233. 'index' => 'status',
  234. 'type' => 'options',
  235. 'options' => $this->_status->getOptionArray(),
  236. 'header_css_class' => 'col-status',
  237. 'column_css_class' => 'col-status'
  238. ]
  239. );
  240. $this->addColumn(
  241. 'visibility',
  242. [
  243. 'header' => __('Visibility'),
  244. 'index' => 'visibility',
  245. 'type' => 'options',
  246. 'options' => $this->_visibility->getOptionArray(),
  247. 'header_css_class' => 'col-visibility',
  248. 'column_css_class' => 'col-visibility'
  249. ]
  250. );
  251. $this->addColumn(
  252. 'sku',
  253. [
  254. 'header' => __('SKU'),
  255. 'index' => 'sku',
  256. 'header_css_class' => 'col-sku',
  257. 'column_css_class' => 'col-sku'
  258. ]
  259. );
  260. $this->addColumn(
  261. 'price',
  262. [
  263. 'header' => __('Price'),
  264. 'type' => 'currency',
  265. 'currency_code' => (string)$this->_scopeConfig->getValue(
  266. \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
  267. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  268. ),
  269. 'index' => 'price',
  270. 'header_css_class' => 'col-price',
  271. 'column_css_class' => 'col-price'
  272. ]
  273. );
  274. $this->addColumn(
  275. 'position',
  276. [
  277. 'header' => __('Position'),
  278. 'name' => 'position',
  279. 'type' => 'number',
  280. 'validate_class' => 'validate-number',
  281. 'index' => 'position',
  282. 'editable' => !$this->getProduct()->getUpsellReadonly(),
  283. 'edit_only' => !$this->getProduct()->getId(),
  284. 'header_css_class' => 'col-position',
  285. 'column_css_class' => 'col-position',
  286. 'filter_condition_callback' => [$this, 'filterProductPosition']
  287. ]
  288. );
  289. return parent::_prepareColumns();
  290. }
  291. /**
  292. * Rerieve grid URL
  293. *
  294. * @return string
  295. */
  296. public function getGridUrl()
  297. {
  298. return $this->_getData(
  299. 'grid_url'
  300. ) ? $this->_getData(
  301. 'grid_url'
  302. ) : $this->getUrl(
  303. 'catalog/*/upsellGrid',
  304. ['_current' => true]
  305. );
  306. }
  307. /**
  308. * Retrieve selected upsell products
  309. *
  310. * @return array
  311. */
  312. protected function _getSelectedProducts()
  313. {
  314. $products = $this->getProductsUpsell();
  315. if (!is_array($products)) {
  316. $products = array_keys($this->getSelectedUpsellProducts());
  317. }
  318. return $products;
  319. }
  320. /**
  321. * Retrieve upsell products
  322. *
  323. * @return array
  324. */
  325. public function getSelectedUpsellProducts()
  326. {
  327. $products = [];
  328. foreach ($this->_coreRegistry->registry('current_product')->getUpSellProducts() as $product) {
  329. $products[$product->getId()] = ['position' => $product->getPosition()];
  330. }
  331. return $products;
  332. }
  333. /**
  334. * Apply `position` filter to cross-sell grid.
  335. *
  336. * @param \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection $collection
  337. * @param \Magento\Backend\Block\Widget\Grid\Column\Extended $column
  338. * @return $this
  339. */
  340. public function filterProductPosition($collection, $column)
  341. {
  342. $collection->addLinkAttributeToFilter($column->getIndex(), $column->getFilter()->getCondition());
  343. return $this;
  344. }
  345. }