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