PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/rgranadino/magento-mirror
PHP | 350 lines | 158 code | 29 blank | 163 comment | 6 complexity | f0c6ef34696c6f8fd0654ea2761bb3c7 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) 2011 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. * 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('catalog/product_status');
  59. }
  60. /**
  61. * Retrieve resource model wraper
  62. *
  63. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_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. * Add visible filter to Product Collection
  81. *
  82. * @deprecated remove on new builds
  83. * @param Mage_Eav_Model_Entity_Collection_Abstract $collection
  84. * @return Mage_Catalog_Model_Product_Status
  85. */
  86. public function addVisibleFilterToCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection)
  87. {
  88. //$collection->addAttributeToFilter('status', array('in'=>$this->getVisibleStatusIds()));
  89. return $this;
  90. }
  91. /**
  92. * Add saleable filter to Product Collection
  93. *
  94. * @deprecated remove on new builds
  95. * @param Mage_Eav_Model_Entity_Collection_Abstract $collection
  96. * @return Mage_Catalog_Model_Product_Status
  97. */
  98. public function addSaleableFilterToCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection)
  99. {
  100. //$collection->addAttributeToFilter('status', array('in'=>$this->getSaleableStatusIds()));
  101. return $this;
  102. }
  103. /**
  104. * Retrieve Visible Status Ids
  105. *
  106. * @return array
  107. */
  108. public function getVisibleStatusIds()
  109. {
  110. return array(self::STATUS_ENABLED);
  111. }
  112. /**
  113. * Retrieve Saleable Status Ids
  114. * Default Product Enable status
  115. *
  116. * @return array
  117. */
  118. public function getSaleableStatusIds()
  119. {
  120. return array(self::STATUS_ENABLED);
  121. }
  122. /**
  123. * Retrieve option array
  124. *
  125. * @return array
  126. */
  127. static public function getOptionArray()
  128. {
  129. return array(
  130. self::STATUS_ENABLED => Mage::helper('catalog')->__('Enabled'),
  131. self::STATUS_DISABLED => Mage::helper('catalog')->__('Disabled')
  132. );
  133. }
  134. /**
  135. * Retrieve option array with empty value
  136. *
  137. * @return array
  138. */
  139. static public function getAllOption()
  140. {
  141. $options = self::getOptionArray();
  142. array_unshift($options, array('value'=>'', 'label'=>''));
  143. return $options;
  144. }
  145. /**
  146. * Retrieve option array with empty value
  147. *
  148. * @return array
  149. */
  150. static public function getAllOptions()
  151. {
  152. $res = array(
  153. array(
  154. 'value' => '',
  155. 'label' => Mage::helper('catalog')->__('-- Please Select --')
  156. )
  157. );
  158. foreach (self::getOptionArray() as $index => $value) {
  159. $res[] = array(
  160. 'value' => $index,
  161. 'label' => $value
  162. );
  163. }
  164. return $res;
  165. }
  166. /**
  167. * Retrieve option text by option value
  168. *
  169. * @param string $optionId
  170. * @return string
  171. */
  172. static public function getOptionText($optionId)
  173. {
  174. $options = self::getOptionArray();
  175. return isset($options[$optionId]) ? $options[$optionId] : null;
  176. }
  177. /**
  178. * Update status value for product
  179. *
  180. * @param int $productId
  181. * @param int $storeId
  182. * @param int $value
  183. * @return Mage_Catalog_Model_Product_Status
  184. */
  185. public function updateProductStatus($productId, $storeId, $value)
  186. {
  187. Mage::getSingleton('catalog/product_action')
  188. ->updateAttributes(array($productId), array('status' => $value), $storeId);
  189. // add back compatibility event
  190. $status = $this->_getResource()->getProductAttribute('status');
  191. if ($status->isScopeWebsite()) {
  192. $website = Mage::app()->getStore($storeId)->getWebsite();
  193. $stores = $website->getStoreIds();
  194. } else if ($status->isScopeStore()) {
  195. $stores = array($storeId);
  196. } else {
  197. $stores = array_keys(Mage::app()->getStores());
  198. }
  199. foreach ($stores as $storeId) {
  200. Mage::dispatchEvent('catalog_product_status_update', array(
  201. 'product_id' => $productId,
  202. 'store_id' => $storeId,
  203. 'status' => $value
  204. ));
  205. }
  206. return $this;
  207. }
  208. /**
  209. * Retrieve Product(s) status for store
  210. * Return array where key is product, value - status
  211. *
  212. * @param int|array $productIds
  213. * @param int $storeId
  214. * @return array
  215. */
  216. public function getProductStatus($productIds, $storeId = null)
  217. {
  218. return $this->getResource()->getProductStatus($productIds, $storeId);
  219. }
  220. /**
  221. * ---------------- Eav Source methods for Flat data -----------------------
  222. */
  223. /**
  224. * Retrieve flat column definition
  225. *
  226. * @return array
  227. */
  228. public function getFlatColums()
  229. {
  230. return array();
  231. }
  232. /**
  233. * Retrieve Indexes for Flat
  234. *
  235. * @return array
  236. */
  237. public function getFlatIndexes()
  238. {
  239. return array();
  240. }
  241. /**
  242. * Retrieve Select For Flat Attribute update
  243. *
  244. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  245. * @param int $store
  246. * @return Varien_Db_Select|null
  247. */
  248. public function getFlatUpdateSelect($store)
  249. {
  250. return null;
  251. }
  252. /**
  253. * Set attribute instance
  254. *
  255. * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
  256. * @return Mage_Eav_Model_Entity_Attribute_Frontend_Abstract
  257. */
  258. public function setAttribute($attribute)
  259. {
  260. $this->_attribute = $attribute;
  261. return $this;
  262. }
  263. /**
  264. * Get attribute instance
  265. *
  266. * @return Mage_Catalog_Model_Resource_Eav_Attribute
  267. */
  268. public function getAttribute()
  269. {
  270. return $this->_attribute;
  271. }
  272. /**
  273. * Add Value Sort To Collection Select
  274. *
  275. * @param Mage_Eav_Model_Entity_Collection_Abstract $collection
  276. * @param string $dir direction
  277. * @return Mage_Eav_Model_Entity_Attribute_Source_Abstract
  278. */
  279. public function addValueSortToCollection($collection, $dir = 'asc')
  280. {
  281. $attributeCode = $this->getAttribute()->getAttributeCode();
  282. $attributeId = $this->getAttribute()->getId();
  283. $attributeTable = $this->getAttribute()->getBackend()->getTable();
  284. if ($this->getAttribute()->isScopeGlobal()) {
  285. $tableName = $attributeCode . '_t';
  286. $collection->getSelect()
  287. ->joinLeft(
  288. array($tableName => $attributeTable),
  289. "e.entity_id={$tableName}.entity_id"
  290. . " AND {$tableName}.attribute_id='{$attributeId}'"
  291. . " AND {$tableName}.store_id='0'",
  292. array());
  293. $valueExpr = $tableName . '.value';
  294. }
  295. else {
  296. $valueTable1 = $attributeCode . '_t1';
  297. $valueTable2 = $attributeCode . '_t2';
  298. $collection->getSelect()
  299. ->joinLeft(
  300. array($valueTable1 => $attributeTable),
  301. "e.entity_id={$valueTable1}.entity_id"
  302. . " AND {$valueTable1}.attribute_id='{$attributeId}'"
  303. . " AND {$valueTable1}.store_id='0'",
  304. array())
  305. ->joinLeft(
  306. array($valueTable2 => $attributeTable),
  307. "e.entity_id={$valueTable2}.entity_id"
  308. . " AND {$valueTable2}.attribute_id='{$attributeId}'"
  309. . " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
  310. array()
  311. );
  312. $valueExpr = $collection->getConnection()->getCheckSql(
  313. $valueTable2 . '.value_id > 0',
  314. $valueTable2 . '.value',
  315. $valueTable1 . '.value'
  316. );
  317. }
  318. $collection->getSelect()->order($valueExpr . ' ' . $dir);
  319. return $this;
  320. }
  321. }