1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Catalog_Model_Product_Visibility extends Varien_Object
36: {
37: const VISIBILITY_NOT_VISIBLE = 1;
38: const VISIBILITY_IN_CATALOG = 2;
39: const VISIBILITY_IN_SEARCH = 3;
40: const VISIBILITY_BOTH = 4;
41:
42: 43: 44: 45: 46:
47: protected $_attribute;
48:
49: 50: 51: 52:
53: public function __construct()
54: {
55: parent::__construct();
56: $this->setIdFieldName('visibility_id');
57: }
58:
59: 60: 61: 62: 63: 64: 65:
66: public function addVisibleInCatalogFilterToCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection)
67: {
68: $collection->setVisibility($this->getVisibleInCatalogIds());
69:
70: return $this;
71: }
72:
73: 74: 75: 76: 77: 78: 79:
80: public function addVisibleInSearchFilterToCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection)
81: {
82: $collection->setVisibility($this->getVisibleInSearchIds());
83:
84: return $this;
85: }
86:
87: 88: 89: 90: 91: 92: 93:
94: public function addVisibleInSiteFilterToCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection)
95: {
96: $collection->setVisibility($this->getVisibleInSiteIds());
97:
98: return $this;
99: }
100:
101: 102: 103: 104: 105:
106: public function getVisibleInCatalogIds()
107: {
108: return array(self::VISIBILITY_IN_CATALOG, self::VISIBILITY_BOTH);
109: }
110:
111: 112: 113: 114: 115:
116: public function getVisibleInSearchIds()
117: {
118: return array(self::VISIBILITY_IN_SEARCH, self::VISIBILITY_BOTH);
119: }
120:
121: 122: 123: 124: 125:
126: public function getVisibleInSiteIds()
127: {
128: return array(self::VISIBILITY_IN_SEARCH, self::VISIBILITY_IN_CATALOG, self::VISIBILITY_BOTH);
129: }
130:
131: 132: 133: 134: 135:
136: static public function getOptionArray()
137: {
138: return array(
139: self::VISIBILITY_NOT_VISIBLE=> Mage::helper('catalog')->__('Not Visible Individually'),
140: self::VISIBILITY_IN_CATALOG => Mage::helper('catalog')->__('Catalog'),
141: self::VISIBILITY_IN_SEARCH => Mage::helper('catalog')->__('Search'),
142: self::VISIBILITY_BOTH => Mage::helper('catalog')->__('Catalog, Search')
143: );
144: }
145:
146: 147: 148: 149: 150:
151: static public function getAllOption()
152: {
153: $options = self::getOptionArray();
154: array_unshift($options, array('value'=>'', 'label'=>''));
155: return $options;
156: }
157:
158: 159: 160: 161: 162:
163: static public function getAllOptions()
164: {
165: $res = array();
166: $res[] = array('value'=>'', 'label'=> Mage::helper('catalog')->__('-- Please Select --'));
167: foreach (self::getOptionArray() as $index => $value) {
168: $res[] = array(
169: 'value' => $index,
170: 'label' => $value
171: );
172: }
173: return $res;
174: }
175:
176: 177: 178: 179: 180: 181:
182: static public function getOptionText($optionId)
183: {
184: $options = self::getOptionArray();
185: return isset($options[$optionId]) ? $options[$optionId] : null;
186: }
187:
188: 189: 190: 191: 192:
193: public function getFlatColums()
194: {
195: $attributeCode = $this->getAttribute()->getAttributeCode();
196: $column = array(
197: 'unsigned' => true,
198: 'default' => null,
199: 'extra' => null
200: );
201:
202: if (Mage::helper('core')->useDbCompatibleMode()) {
203: $column['type'] = 'tinyint';
204: $column['is_null'] = true;
205: } else {
206: $column['type'] = Varien_Db_Ddl_Table::TYPE_SMALLINT;
207: $column['nullable'] = true;
208: $column['comment'] = 'Catalog Product Visibility ' . $attributeCode . ' column';
209: }
210:
211: return array($attributeCode => $column);
212: }
213:
214: 215: 216: 217: 218:
219: public function getFlatIndexes()
220: {
221: return array();
222: }
223:
224: 225: 226: 227: 228: 229: 230:
231: public function getFlatUpdateSelect($store)
232: {
233: return Mage::getResourceSingleton('eav/entity_attribute')
234: ->getFlatUpdateSelect($this->getAttribute(), $store);
235: }
236:
237: 238: 239: 240: 241: 242:
243: public function setAttribute($attribute)
244: {
245: $this->_attribute = $attribute;
246: return $this;
247: }
248:
249: 250: 251: 252: 253:
254: public function getAttribute()
255: {
256: return $this->_attribute;
257: }
258:
259: 260: 261: 262: 263: 264: 265:
266: public function addValueSortToCollection($collection, $dir = 'asc')
267: {
268: $attributeCode = $this->getAttribute()->getAttributeCode();
269: $attributeId = $this->getAttribute()->getId();
270: $attributeTable = $this->getAttribute()->getBackend()->getTable();
271:
272: if ($this->getAttribute()->isScopeGlobal()) {
273: $tableName = $attributeCode . '_t';
274: $collection->getSelect()
275: ->joinLeft(
276: array($tableName => $attributeTable),
277: "e.entity_id={$tableName}.entity_id"
278: . " AND {$tableName}.attribute_id='{$attributeId}'"
279: . " AND {$tableName}.store_id='0'",
280: array());
281: $valueExpr = $tableName . '.value';
282: }
283: else {
284: $valueTable1 = $attributeCode . '_t1';
285: $valueTable2 = $attributeCode . '_t2';
286: $collection->getSelect()
287: ->joinLeft(
288: array($valueTable1 => $attributeTable),
289: "e.entity_id={$valueTable1}.entity_id"
290: . " AND {$valueTable1}.attribute_id='{$attributeId}'"
291: . " AND {$valueTable1}.store_id='0'",
292: array())
293: ->joinLeft(
294: array($valueTable2 => $attributeTable),
295: "e.entity_id={$valueTable2}.entity_id"
296: . " AND {$valueTable2}.attribute_id='{$attributeId}'"
297: . " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
298: array()
299: );
300: $valueExpr = $collection->getConnection()->getCheckSql(
301: $valueTable2 . '.value_id > 0',
302: $valueTable2 . '.value',
303: $valueTable1 . '.value'
304: );
305: }
306:
307: $collection->getSelect()->order($valueExpr . ' ' . $dir);
308: return $this;
309: }
310: }
311: