PageRenderTime 92ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Product/Status.php

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