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

https://bitbucket.org/acidel/buykoala · PHP · 197 lines · 104 code · 17 blank · 76 comment · 5 complexity · ff60894c1d2b6c19ff2ebf66f8df40cb 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) 2011 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 options 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_Option_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
  34. {
  35. /**
  36. * Resource initialization
  37. */
  38. protected function _construct()
  39. {
  40. $this->_init('catalog/product_option');
  41. }
  42. /**
  43. * Adds title, price & price_type attributes to result
  44. *
  45. * @param int $storeId
  46. * @return Mage_Catalog_Model_Resource_Product_Option_Collection
  47. */
  48. public function getOptions($storeId)
  49. {
  50. $this->addPriceToResult($storeId)
  51. ->addTitleToResult($storeId);
  52. return $this;
  53. }
  54. /**
  55. * Add title to result
  56. *
  57. * @param int $storeId
  58. * @return Mage_Catalog_Model_Resource_Product_Option_Collection
  59. */
  60. public function addTitleToResult($storeId)
  61. {
  62. $productOptionTitleTable = $this->getTable('catalog/product_option_title');
  63. $adapter = $this->getConnection();
  64. $titleExpr = $adapter->getCheckSql('store_option_title.title IS NULL', 'default_option_title.title', 'store_option_title.title');
  65. $this->getSelect()
  66. ->join(array('default_option_title' => $productOptionTitleTable),
  67. 'default_option_title.option_id = main_table.option_id',
  68. array('default_title' => 'title'))
  69. ->joinLeft(array('store_option_title' => $productOptionTitleTable),
  70. 'store_option_title.option_id = main_table.option_id AND ' . $adapter->quoteInto('store_option_title.store_id = ?', $storeId),
  71. array(
  72. 'store_title' => 'title',
  73. 'title' => $titleExpr
  74. ))
  75. ->where('default_option_title.store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
  76. return $this;
  77. }
  78. /**
  79. * Add price to result
  80. *
  81. * @param int $storeId
  82. * @return Mage_Catalog_Model_Resource_Product_Option_Collection
  83. */
  84. public function addPriceToResult($storeId)
  85. {
  86. $productOptionPriceTable = $this->getTable('catalog/product_option_price');
  87. $adapter = $this->getConnection();
  88. $priceExpr = $adapter->getCheckSql('store_option_price.price IS NULL', 'default_option_price.price', 'store_option_price.price');
  89. $priceTypeExpr = $adapter->getCheckSql('store_option_price.price_type IS NULL', 'default_option_price.price_type', 'store_option_price.price_type');
  90. $this->getSelect()
  91. ->joinLeft(array('default_option_price' => $productOptionPriceTable),
  92. 'default_option_price.option_id = main_table.option_id AND ' . $adapter->quoteInto('default_option_price.store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID),
  93. array(
  94. 'default_price' => 'price',
  95. 'default_price_type' => 'price_type'
  96. ))
  97. ->joinLeft(array('store_option_price' => $productOptionPriceTable),
  98. 'store_option_price.option_id = main_table.option_id AND ' . $adapter->quoteInto('store_option_price.store_id = ?', $storeId),
  99. array(
  100. 'store_price' => 'price',
  101. 'store_price_type' => 'price_type',
  102. 'price' => $priceExpr,
  103. 'price_type' => $priceTypeExpr
  104. ));
  105. return $this;
  106. }
  107. /**
  108. * Add value to result
  109. *
  110. * @param int $storeId
  111. * @return Mage_Catalog_Model_Resource_Product_Option_Collection
  112. */
  113. public function addValuesToResult($storeId = null)
  114. {
  115. if ($storeId === null) {
  116. $storeId = Mage::app()->getStore()->getId();
  117. }
  118. $optionIds = array();
  119. foreach ($this as $option) {
  120. $optionIds[] = $option->getId();
  121. }
  122. if (!empty($optionIds)) {
  123. /** @var $values Mage_Catalog_Model_Option_Value_Collection */
  124. $values = Mage::getModel('catalog/product_option_value')
  125. ->getCollection()
  126. ->addTitleToResult($storeId)
  127. ->addPriceToResult($storeId)
  128. ->addOptionToFilter($optionIds)
  129. ->setOrder('sort_order', self::SORT_ORDER_ASC)
  130. ->setOrder('title', self::SORT_ORDER_ASC);
  131. foreach ($values as $value) {
  132. $optionId = $value->getOptionId();
  133. if($this->getItemById($optionId)) {
  134. $this->getItemById($optionId)->addValue($value);
  135. $value->setOption($this->getItemById($optionId));
  136. }
  137. }
  138. }
  139. return $this;
  140. }
  141. /**
  142. * Add product_id filter to select
  143. *
  144. * @param array|Mage_Catalog_Model_Product|int $product
  145. * @return Mage_Catalog_Model_Resource_Product_Option_Collection
  146. */
  147. public function addProductToFilter($product)
  148. {
  149. if (empty($product)) {
  150. $this->addFieldToFilter('product_id', '');
  151. } elseif (is_array($product)) {
  152. $this->addFieldToFilter('product_id', array('in' => $product));
  153. } elseif ($product instanceof Mage_Catalog_Model_Product) {
  154. $this->addFieldToFilter('product_id', $product->getId());
  155. } else {
  156. $this->addFieldToFilter('product_id', $product);
  157. }
  158. return $this;
  159. }
  160. /**
  161. * Add filtering by option ids
  162. *
  163. * @param mixed $optionIds
  164. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Option_Collection
  165. */
  166. public function addIdsToFilter($optionIds)
  167. {
  168. $this->addFieldToFilter('main_table.option_id', $optionIds);
  169. return $this;
  170. }
  171. /**
  172. * Call of protected method reset
  173. *
  174. * @return Mage_Catalog_Model_Resource_Product_Option_Collection
  175. */
  176. public function reset()
  177. {
  178. return $this->_reset();
  179. }
  180. }