PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/dnejedly/eaparts
PHP | 333 lines | 158 code | 30 blank | 145 comment | 20 complexity | 3556f1be757ee15779d9a73f2973ab6f 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_Catalog
  23. * @copyright Copyright (c) 2012 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. * 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. }
  110. return $this;
  111. }
  112. /**
  113. * Retrieve collection base product object
  114. *
  115. * @return Mage_Catalog_Model_Product
  116. */
  117. public function getProduct()
  118. {
  119. return $this->_product;
  120. }
  121. /**
  122. * Exclude products from filter
  123. *
  124. * @param array $products
  125. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  126. */
  127. public function addExcludeProductFilter($products)
  128. {
  129. if (!empty($products)) {
  130. if (!is_array($products)) {
  131. $products = array($products);
  132. }
  133. $this->_hasLinkFilter = true;
  134. $this->getSelect()->where('links.linked_product_id NOT IN (?)', $products);
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Add products to filter
  140. *
  141. * @param array|int|string $products
  142. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  143. */
  144. public function addProductFilter($products)
  145. {
  146. if (!empty($products)) {
  147. if (!is_array($products)) {
  148. $products = array($products);
  149. }
  150. $this->getSelect()->where('links.product_id IN (?)', $products);
  151. $this->_hasLinkFilter = true;
  152. }
  153. return $this;
  154. }
  155. /**
  156. * Add random sorting order
  157. *
  158. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  159. */
  160. public function setRandomOrder()
  161. {
  162. $this->getSelect()->orderRand('main_table.entity_id');
  163. return $this;
  164. }
  165. /**
  166. * Setting group by to exclude duplications in collection
  167. *
  168. * @param string $groupBy
  169. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  170. */
  171. public function setGroupBy($groupBy = 'e.entity_id')
  172. {
  173. $this->getSelect()->group($groupBy);
  174. /*
  175. * Allow Analytic functions usage
  176. */
  177. $this->_useAnalyticFunction = true;
  178. return $this;
  179. }
  180. /**
  181. * Join linked products when specified link model
  182. *
  183. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  184. */
  185. protected function _beforeLoad()
  186. {
  187. if ($this->getLinkModel()) {
  188. $this->_joinLinks();
  189. }
  190. return parent::_beforeLoad();
  191. }
  192. /**
  193. * Join linked products and their attributes
  194. *
  195. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  196. */
  197. protected function _joinLinks()
  198. {
  199. $select = $this->getSelect();
  200. $adapter = $select->getAdapter();
  201. $joinCondition = array(
  202. 'links.linked_product_id = e.entity_id',
  203. $adapter->quoteInto('links.link_type_id = ?', $this->_linkTypeId)
  204. );
  205. $joinType = 'join';
  206. if ($this->getProduct() && $this->getProduct()->getId()) {
  207. $productId = $this->getProduct()->getId();
  208. if ($this->_isStrongMode) {
  209. $this->getSelect()->where('links.product_id = ?', (int)$productId);
  210. } else {
  211. $joinType = 'joinLeft';
  212. $joinCondition[] = $adapter->quoteInto('links.product_id = ?', $productId);
  213. }
  214. $this->addFieldToFilter('entity_id', array('neq' => $productId));
  215. } else if ($this->_isStrongMode) {
  216. $this->addFieldToFilter('entity_id', array('eq' => -1));
  217. }
  218. if($this->_hasLinkFilter) {
  219. $select->$joinType(
  220. array('links' => $this->getTable('catalog/product_link')),
  221. implode(' AND ', $joinCondition),
  222. array('link_id')
  223. );
  224. $this->joinAttributes();
  225. }
  226. return $this;
  227. }
  228. /**
  229. * Enable sorting products by its position
  230. *
  231. * @param string $dir sort type asc|desc
  232. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  233. */
  234. public function setPositionOrder($dir = self::SORT_ORDER_ASC)
  235. {
  236. if ($this->_hasLinkFilter) {
  237. $this->getSelect()->order('position ' . $dir);
  238. }
  239. return $this;
  240. }
  241. /**
  242. * Enable sorting products by its attribute set name
  243. *
  244. * @param string $dir sort type asc|desc
  245. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  246. */
  247. public function setAttributeSetIdOrder($dir = self::SORT_ORDER_ASC)
  248. {
  249. $this->getSelect()
  250. ->joinLeft(
  251. array('set' => $this->getTable('eav/attribute_set')),
  252. 'e.attribute_set_id = set.attribute_set_id',
  253. array('attribute_set_name')
  254. )
  255. ->order('set.attribute_set_name ' . $dir);
  256. return $this;
  257. }
  258. /**
  259. * Join attributes
  260. *
  261. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  262. */
  263. public function joinAttributes()
  264. {
  265. if (!$this->getLinkModel()) {
  266. return $this;
  267. }
  268. $attributes = $this->getLinkModel()->getAttributes();
  269. $attributesByType = array();
  270. foreach ($attributes as $attribute) {
  271. $table = $this->getLinkModel()->getAttributeTypeTable($attribute['type']);
  272. $alias = sprintf('link_attribute_%s_%s', $attribute['code'], $attribute['type']);
  273. $joinCondiotion = array(
  274. "{$alias}.link_id = links.link_id",
  275. $this->getSelect()->getAdapter()->quoteInto("{$alias}.product_link_attribute_id = ?", $attribute['id'])
  276. );
  277. $this->getSelect()->joinLeft(
  278. array($alias => $table),
  279. implode(' AND ', $joinCondiotion),
  280. array($attribute['code'] => 'value')
  281. );
  282. }
  283. return $this;
  284. }
  285. /**
  286. * Set sorting order
  287. *
  288. * $attribute can also be an array of attributes
  289. *
  290. * @param string|array $attribute
  291. * @param string $dir
  292. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  293. */
  294. public function setOrder($attribute, $dir = self::SORT_ORDER_ASC)
  295. {
  296. if ($attribute == 'position') {
  297. return $this->setPositionOrder($dir);
  298. } elseif ($attribute == 'attribute_set_id') {
  299. return $this->setAttributeSetIdOrder($dir);
  300. }
  301. return parent::setOrder($attribute, $dir);
  302. }
  303. }