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:
28: /**
29: * Product status functionality model
30: *
31: * @method Mage_Catalog_Model_Resource_Product_Status _getResource()
32: * @method Mage_Catalog_Model_Resource_Product_Status getResource()
33: * @method int getProductId()
34: * @method Mage_Catalog_Model_Product_Status setProductId(int $value)
35: * @method int getStoreId()
36: * @method Mage_Catalog_Model_Product_Status setStoreId(int $value)
37: * @method int getVisibility()
38: * @method Mage_Catalog_Model_Product_Status setVisibility(int $value)
39: *
40: * @category Mage
41: * @package Mage_Catalog
42: * @author Magento Core Team <core@magentocommerce.com>
43: */
44: class Mage_Catalog_Model_Product_Status extends Mage_Core_Model_Abstract
45: {
46: const STATUS_ENABLED = 1;
47: const STATUS_DISABLED = 2;
48:
49: /**
50: * Reference to the attribute instance
51: *
52: * @var Mage_Catalog_Model_Resource_Eav_Attribute
53: */
54: protected $_attribute;
55:
56: /**
57: * Initialize resource model
58: *
59: */
60: protected function _construct()
61: {
62: $this->_init('catalog/product_status');
63: }
64:
65: /**
66: * Retrieve resource model wrapper
67: *
68: * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Status
69: */
70: protected function _getResource()
71: {
72: return parent::_getResource();
73: }
74:
75: /**
76: * Retrieve Product Attribute by code
77: *
78: * @param string $attributeCode
79: * @return Mage_Eav_Model_Entity_Attribute_Abstract
80: */
81: public function getProductAttribute($attributeCode)
82: {
83: return $this->_getResource()->getProductAttribute($attributeCode);
84: }
85:
86: /**
87: * Add visible filter to Product Collection
88: *
89: * @deprecated remove on new builds
90: * @param Mage_Eav_Model_Entity_Collection_Abstract $collection
91: * @return Mage_Catalog_Model_Product_Status
92: */
93: public function addVisibleFilterToCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection)
94: {
95: //$collection->addAttributeToFilter('status', array('in'=>$this->getVisibleStatusIds()));
96: return $this;
97: }
98:
99: /**
100: * Add saleable filter to Product Collection
101: *
102: * @deprecated remove on new builds
103: * @param Mage_Eav_Model_Entity_Collection_Abstract $collection
104: * @return Mage_Catalog_Model_Product_Status
105: */
106: public function addSaleableFilterToCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection)
107: {
108: //$collection->addAttributeToFilter('status', array('in'=>$this->getSaleableStatusIds()));
109: return $this;
110: }
111:
112: /**
113: * Retrieve Visible Status Ids
114: *
115: * @return array
116: */
117: public function getVisibleStatusIds()
118: {
119: return array(self::STATUS_ENABLED);
120: }
121:
122: /**
123: * Retrieve Saleable Status Ids
124: * Default Product Enable status
125: *
126: * @return array
127: */
128: public function getSaleableStatusIds()
129: {
130: return array(self::STATUS_ENABLED);
131: }
132:
133: /**
134: * Retrieve option array
135: *
136: * @return array
137: */
138: static public function getOptionArray()
139: {
140: return array(
141: self::STATUS_ENABLED => Mage::helper('catalog')->__('Enabled'),
142: self::STATUS_DISABLED => Mage::helper('catalog')->__('Disabled')
143: );
144: }
145:
146: /**
147: * Retrieve option array with empty value
148: *
149: * @return array
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: * Retrieve option array with empty value
160: *
161: * @return array
162: */
163: static public function getAllOptions()
164: {
165: $res = array(
166: array(
167: 'value' => '',
168: 'label' => Mage::helper('catalog')->__('-- Please Select --')
169: )
170: );
171: foreach (self::getOptionArray() as $index => $value) {
172: $res[] = array(
173: 'value' => $index,
174: 'label' => $value
175: );
176: }
177: return $res;
178: }
179:
180: /**
181: * Retrieve option text by option value
182: *
183: * @param string $optionId
184: * @return string
185: */
186: static public function getOptionText($optionId)
187: {
188: $options = self::getOptionArray();
189: return isset($options[$optionId]) ? $options[$optionId] : null;
190: }
191:
192: /**
193: * Update status value for product
194: *
195: * @param int $productId
196: * @param int $storeId
197: * @param int $value
198: * @return Mage_Catalog_Model_Product_Status
199: */
200: public function updateProductStatus($productId, $storeId, $value)
201: {
202: Mage::getSingleton('catalog/product_action')
203: ->updateAttributes(array($productId), array('status' => $value), $storeId);
204:
205: // add back compatibility event
206: $status = $this->_getResource()->getProductAttribute('status');
207: if ($status->isScopeWebsite()) {
208: $website = Mage::app()->getStore($storeId)->getWebsite();
209: $stores = $website->getStoreIds();
210: } else if ($status->isScopeStore()) {
211: $stores = array($storeId);
212: } else {
213: $stores = array_keys(Mage::app()->getStores());
214: }
215:
216: foreach ($stores as $storeId) {
217: Mage::dispatchEvent('catalog_product_status_update', array(
218: 'product_id' => $productId,
219: 'store_id' => $storeId,
220: 'status' => $value
221: ));
222: }
223:
224: return $this;
225: }
226:
227: /**
228: * Retrieve Product(s) status for store
229: * Return array where key is product, value - status
230: *
231: * @param int|array $productIds
232: * @param int $storeId
233: * @return array
234: */
235: public function getProductStatus($productIds, $storeId = null)
236: {
237: return $this->getResource()->getProductStatus($productIds, $storeId);
238: }
239:
240: /**
241: * ---------------- Eav Source methods for Flat data -----------------------
242: */
243:
244: /**
245: * Retrieve flat column definition
246: *
247: * @return array
248: */
249: public function getFlatColums()
250: {
251: return array();
252: }
253:
254: /**
255: * Retrieve Indexes for Flat
256: *
257: * @return array
258: */
259: public function getFlatIndexes()
260: {
261: return array();
262: }
263:
264: /**
265: * Retrieve Select For Flat Attribute update
266: *
267: * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
268: * @param int $store
269: * @return Varien_Db_Select|null
270: */
271: public function getFlatUpdateSelect($store)
272: {
273: return null;
274: }
275:
276: /**
277: * Set attribute instance
278: *
279: * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
280: * @return Mage_Eav_Model_Entity_Attribute_Frontend_Abstract
281: */
282: public function setAttribute($attribute)
283: {
284: $this->_attribute = $attribute;
285: return $this;
286: }
287:
288: /**
289: * Get attribute instance
290: *
291: * @return Mage_Catalog_Model_Resource_Eav_Attribute
292: */
293: public function getAttribute()
294: {
295: return $this->_attribute;
296: }
297:
298: /**
299: * Add Value Sort To Collection Select
300: *
301: * @param Mage_Eav_Model_Entity_Collection_Abstract $collection
302: * @param string $dir direction
303: * @return Mage_Eav_Model_Entity_Attribute_Source_Abstract
304: */
305: public function addValueSortToCollection($collection, $dir = 'asc')
306: {
307: $attributeCode = $this->getAttribute()->getAttributeCode();
308: $attributeId = $this->getAttribute()->getId();
309: $attributeTable = $this->getAttribute()->getBackend()->getTable();
310:
311: if ($this->getAttribute()->isScopeGlobal()) {
312: $tableName = $attributeCode . '_t';
313: $collection->getSelect()
314: ->joinLeft(
315: array($tableName => $attributeTable),
316: "e.entity_id={$tableName}.entity_id"
317: . " AND {$tableName}.attribute_id='{$attributeId}'"
318: . " AND {$tableName}.store_id='0'",
319: array());
320: $valueExpr = $tableName . '.value';
321: }
322: else {
323: $valueTable1 = $attributeCode . '_t1';
324: $valueTable2 = $attributeCode . '_t2';
325: $collection->getSelect()
326: ->joinLeft(
327: array($valueTable1 => $attributeTable),
328: "e.entity_id={$valueTable1}.entity_id"
329: . " AND {$valueTable1}.attribute_id='{$attributeId}'"
330: . " AND {$valueTable1}.store_id='0'",
331: array())
332: ->joinLeft(
333: array($valueTable2 => $attributeTable),
334: "e.entity_id={$valueTable2}.entity_id"
335: . " AND {$valueTable2}.attribute_id='{$attributeId}'"
336: . " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
337: array()
338: );
339:
340: $valueExpr = $collection->getConnection()->getCheckSql(
341: $valueTable2 . '.value_id > 0',
342: $valueTable2 . '.value',
343: $valueTable1 . '.value'
344: );
345: }
346:
347: $collection->getSelect()->order($valueExpr . ' ' . $dir);
348: return $this;
349: }
350: }
351: