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_Eav
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:
28: /**
29: * Entity attribute option collection
30: *
31: * @category Mage
32: * @package Mage_Eav
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * Option value table
39: *
40: * @var string
41: */
42: protected $_optionValueTable;
43:
44: /**
45: * Resource initialization
46: */
47: protected function _construct()
48: {
49: $this->_init('eav/entity_attribute_option');
50: $this->_optionValueTable = Mage::getSingleton('core/resource')->getTableName('eav/attribute_option_value');
51: }
52:
53: /**
54: * Set attribute filter
55: *
56: * @param int $setId
57: * @return Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection
58: */
59: public function setAttributeFilter($setId)
60: {
61: return $this->addFieldToFilter('attribute_id', $setId);
62: }
63:
64:
65: /**
66: * Add store filter to collection
67: *
68: * @param int $storeId
69: * @param bolean $useDefaultValue
70: * @return Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection
71: */
72: public function setStoreFilter($storeId = null, $useDefaultValue = true)
73: {
74: if (is_null($storeId)) {
75: $storeId = Mage::app()->getStore()->getId();
76: }
77: $adapter = $this->getConnection();
78:
79: $joinCondition = $adapter->quoteInto('tsv.option_id = main_table.option_id AND tsv.store_id = ?', $storeId);
80:
81: if ($useDefaultValue) {
82: $this->getSelect()
83: ->join(
84: array('tdv' => $this->_optionValueTable),
85: 'tdv.option_id = main_table.option_id',
86: array('default_value' => 'value'))
87: ->joinLeft(
88: array('tsv' => $this->_optionValueTable),
89: $joinCondition,
90: array(
91: 'store_default_value' => 'value',
92: 'value' => $adapter->getCheckSql('tsv.value_id > 0', 'tsv.value', 'tdv.value')
93: ))
94: ->where('tdv.store_id = ?', 0);
95: } else {
96: $this->getSelect()
97: ->joinLeft(
98: array('tsv' => $this->_optionValueTable),
99: $joinCondition,
100: 'value')
101: ->where('tsv.store_id = ?', $storeId);
102: }
103:
104: $this->setOrder('value', self::SORT_ORDER_ASC);
105:
106: return $this;
107: }
108:
109: /**
110: * Add option id(s) frilter to collection
111: *
112: * @param int|array $optionId
113: * @return Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection
114: */
115: public function setIdFilter($optionId)
116: {
117: return $this->addFieldToFilter('option_id', array('in' => $optionId));
118: }
119:
120: /**
121: * Convert collection items to select options array
122: *
123: * @param string $valueKey
124: * @return array
125: */
126: public function toOptionArray($valueKey = 'value')
127: {
128: return $this->_toOptionArray('option_id', $valueKey);
129: }
130:
131:
132: /**
133: * Set order by position or alphabetically by values in admin
134: *
135: * @param string $dir direction
136: * @param boolean $sortAlpha sort alphabetically by values in admin
137: * @return Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection
138: */
139: public function setPositionOrder($dir = self::SORT_ORDER_ASC, $sortAlpha = false)
140: {
141: $this->setOrder('main_table.sort_order', $dir);
142: // sort alphabetically by values in admin
143: if ($sortAlpha) {
144: $this->getSelect()
145: ->joinLeft(
146: array('sort_alpha_value' => $this->_optionValueTable),
147: 'sort_alpha_value.option_id = main_table.option_id AND sort_alpha_value.store_id = 0',
148: array('value'));
149: $this->setOrder('sort_alpha_value.value', $dir);
150: }
151:
152: return $this;
153: }
154: }
155: