PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Resource/Product/Link/Product/Collection.php

https://gitlab.com/blingbang2016/shop
PHP | 334 lines | 159 code | 30 blank | 145 comment | 20 complexity | 23f8256bb24434e0a95e39ba241de1bc 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog product linked products collection
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Resource_Product_Link_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection
  34. {
  35. /**
  36. * Store product model
  37. *
  38. * @var Mage_Catalog_Model_Product
  39. */
  40. protected $_product;
  41. /**
  42. * Store product link model
  43. *
  44. * @var Mage_Catalog_Model_Product_Link
  45. */
  46. protected $_linkModel;
  47. /**
  48. * Store link type id
  49. *
  50. * @var int
  51. */
  52. protected $_linkTypeId;
  53. /**
  54. * Store strong mode flag that determine if needed for inner join or left join of linked products
  55. *
  56. * @var bool
  57. */
  58. protected $_isStrongMode;
  59. /**
  60. * Store flag that determine if product filter was enabled
  61. *
  62. * @var bool
  63. */
  64. protected $_hasLinkFilter = false;
  65. /**
  66. * Declare link model and initialize type attributes join
  67. *
  68. * @param Mage_Catalog_Model_Product_Link $linkModel
  69. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  70. */
  71. public function setLinkModel(Mage_Catalog_Model_Product_Link $linkModel)
  72. {
  73. $this->_linkModel = $linkModel;
  74. if ($linkModel->getLinkTypeId()) {
  75. $this->_linkTypeId = $linkModel->getLinkTypeId();
  76. }
  77. return $this;
  78. }
  79. /**
  80. * Enable strong mode for inner join of linked products
  81. *
  82. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  83. */
  84. public function setIsStrongMode()
  85. {
  86. $this->_isStrongMode = true;
  87. return $this;
  88. }
  89. /**
  90. * Retrieve collection link model
  91. *
  92. * @return Mage_Catalog_Model_Product_Link
  93. */
  94. public function getLinkModel()
  95. {
  96. return $this->_linkModel;
  97. }
  98. /**
  99. * Initialize collection parent product and add limitation join
  100. *
  101. * @param Mage_Catalog_Model_Product $product
  102. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  103. */
  104. public function setProduct(Mage_Catalog_Model_Product $product)
  105. {
  106. $this->_product = $product;
  107. if ($product && $product->getId()) {
  108. $this->_hasLinkFilter = true;
  109. $this->setStore($product->getStore());
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Retrieve collection base product object
  115. *
  116. * @return Mage_Catalog_Model_Product
  117. */
  118. public function getProduct()
  119. {
  120. return $this->_product;
  121. }
  122. /**
  123. * Exclude products from filter
  124. *
  125. * @param array $products
  126. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  127. */
  128. public function addExcludeProductFilter($products)
  129. {
  130. if (!empty($products)) {
  131. if (!is_array($products)) {
  132. $products = array($products);
  133. }
  134. $this->_hasLinkFilter = true;
  135. $this->getSelect()->where('links.linked_product_id NOT IN (?)', $products);
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Add products to filter
  141. *
  142. * @param array|int|string $products
  143. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  144. */
  145. public function addProductFilter($products)
  146. {
  147. if (!empty($products)) {
  148. if (!is_array($products)) {
  149. $products = array($products);
  150. }
  151. $this->getSelect()->where('links.product_id IN (?)', $products);
  152. $this->_hasLinkFilter = true;
  153. }
  154. return $this;
  155. }
  156. /**
  157. * Add random sorting order
  158. *
  159. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  160. */
  161. public function setRandomOrder()
  162. {
  163. $this->getSelect()->orderRand('main_table.entity_id');
  164. return $this;
  165. }
  166. /**
  167. * Setting group by to exclude duplications in collection
  168. *
  169. * @param string $groupBy
  170. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  171. */
  172. public function setGroupBy($groupBy = 'e.entity_id')
  173. {
  174. $this->getSelect()->group($groupBy);
  175. /*
  176. * Allow Analytic functions usage
  177. */
  178. $this->_useAnalyticFunction = true;
  179. return $this;
  180. }
  181. /**
  182. * Join linked products when specified link model
  183. *
  184. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  185. */
  186. protected function _beforeLoad()
  187. {
  188. if ($this->getLinkModel()) {
  189. $this->_joinLinks();
  190. }
  191. return parent::_beforeLoad();
  192. }
  193. /**
  194. * Join linked products and their attributes
  195. *
  196. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  197. */
  198. protected function _joinLinks()
  199. {
  200. $select = $this->getSelect();
  201. $adapter = $select->getAdapter();
  202. $joinCondition = array(
  203. 'links.linked_product_id = e.entity_id',
  204. $adapter->quoteInto('links.link_type_id = ?', $this->_linkTypeId)
  205. );
  206. $joinType = 'join';
  207. if ($this->getProduct() && $this->getProduct()->getId()) {
  208. $productId = $this->getProduct()->getId();
  209. if ($this->_isStrongMode) {
  210. $this->getSelect()->where('links.product_id = ?', (int)$productId);
  211. } else {
  212. $joinType = 'joinLeft';
  213. $joinCondition[] = $adapter->quoteInto('links.product_id = ?', $productId);
  214. }
  215. $this->addFieldToFilter('entity_id', array('neq' => $productId));
  216. } else if ($this->_isStrongMode) {
  217. $this->addFieldToFilter('entity_id', array('eq' => -1));
  218. }
  219. if($this->_hasLinkFilter) {
  220. $select->$joinType(
  221. array('links' => $this->getTable('catalog/product_link')),
  222. implode(' AND ', $joinCondition),
  223. array('link_id')
  224. );
  225. $this->joinAttributes();
  226. }
  227. return $this;
  228. }
  229. /**
  230. * Enable sorting products by its position
  231. *
  232. * @param string $dir sort type asc|desc
  233. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  234. */
  235. public function setPositionOrder($dir = self::SORT_ORDER_ASC)
  236. {
  237. if ($this->_hasLinkFilter) {
  238. $this->getSelect()->order('position ' . $dir);
  239. }
  240. return $this;
  241. }
  242. /**
  243. * Enable sorting products by its attribute set name
  244. *
  245. * @param string $dir sort type asc|desc
  246. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  247. */
  248. public function setAttributeSetIdOrder($dir = self::SORT_ORDER_ASC)
  249. {
  250. $this->getSelect()
  251. ->joinLeft(
  252. array('set' => $this->getTable('eav/attribute_set')),
  253. 'e.attribute_set_id = set.attribute_set_id',
  254. array('attribute_set_name')
  255. )
  256. ->order('set.attribute_set_name ' . $dir);
  257. return $this;
  258. }
  259. /**
  260. * Join attributes
  261. *
  262. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  263. */
  264. public function joinAttributes()
  265. {
  266. if (!$this->getLinkModel()) {
  267. return $this;
  268. }
  269. $attributes = $this->getLinkModel()->getAttributes();
  270. $attributesByType = array();
  271. foreach ($attributes as $attribute) {
  272. $table = $this->getLinkModel()->getAttributeTypeTable($attribute['type']);
  273. $alias = sprintf('link_attribute_%s_%s', $attribute['code'], $attribute['type']);
  274. $joinCondiotion = array(
  275. "{$alias}.link_id = links.link_id",
  276. $this->getSelect()->getAdapter()->quoteInto("{$alias}.product_link_attribute_id = ?", $attribute['id'])
  277. );
  278. $this->getSelect()->joinLeft(
  279. array($alias => $table),
  280. implode(' AND ', $joinCondiotion),
  281. array($attribute['code'] => 'value')
  282. );
  283. }
  284. return $this;
  285. }
  286. /**
  287. * Set sorting order
  288. *
  289. * $attribute can also be an array of attributes
  290. *
  291. * @param string|array $attribute
  292. * @param string $dir
  293. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  294. */
  295. public function setOrder($attribute, $dir = self::SORT_ORDER_ASC)
  296. {
  297. if ($attribute == 'position') {
  298. return $this->setPositionOrder($dir);
  299. } elseif ($attribute == 'attribute_set_id') {
  300. return $this->setAttributeSetIdOrder($dir);
  301. }
  302. return parent::setOrder($attribute, $dir);
  303. }
  304. }