PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/kdms/sh-magento
PHP | 227 lines | 147 code | 23 blank | 57 comment | 15 complexity | 11096570c0e051d0b90f69e0850d39a7 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  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://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Product type model
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Type
  34. {
  35. /**
  36. * Available product types
  37. */
  38. const TYPE_SIMPLE = 'simple';
  39. const TYPE_BUNDLE = 'bundle';
  40. const TYPE_CONFIGURABLE = 'configurable';
  41. const TYPE_GROUPED = 'grouped';
  42. const TYPE_VIRTUAL = 'virtual';
  43. const DEFAULT_TYPE = 'simple';
  44. const DEFAULT_TYPE_MODEL = 'catalog/product_type_simple';
  45. const DEFAULT_PRICE_MODEL = 'catalog/product_type_price';
  46. static protected $_types;
  47. static protected $_compositeTypes;
  48. static protected $_priceModels;
  49. static protected $_typesPriority;
  50. /**
  51. * Product type instance factory
  52. *
  53. * @param Mage_Catalog_Model_Product $product
  54. * @param bool $singleton
  55. * @return Mage_Catalog_Model_Product_Type_Abstract
  56. */
  57. public static function factory($product, $singleton = false)
  58. {
  59. $types = self::getTypes();
  60. $typeId = $product->getTypeId();
  61. if (!empty($types[$typeId]['model'])) {
  62. $typeModelName = $types[$typeId]['model'];
  63. } else {
  64. $typeModelName = self::DEFAULT_TYPE_MODEL;
  65. $typeId = self::DEFAULT_TYPE;
  66. }
  67. if ($singleton === true) {
  68. $typeModel = Mage::getSingleton($typeModelName);
  69. }
  70. else {
  71. $typeModel = Mage::getModel($typeModelName);
  72. $typeModel->setProduct($product);
  73. }
  74. $typeModel->setConfig($types[$typeId]);
  75. return $typeModel;
  76. }
  77. /**
  78. * Product type price model factory
  79. *
  80. * @param string $productType
  81. * @return Mage_Catalog_Model_Product_Type_Price
  82. */
  83. public static function priceFactory($productType)
  84. {
  85. if (isset(self::$_priceModels[$productType])) {
  86. return self::$_priceModels[$productType];
  87. }
  88. $types = self::getTypes();
  89. if (!empty($types[$productType]['price_model'])) {
  90. $priceModelName = $types[$productType]['price_model'];
  91. } else {
  92. $priceModelName = self::DEFAULT_PRICE_MODEL;
  93. }
  94. self::$_priceModels[$productType] = Mage::getModel($priceModelName);
  95. return self::$_priceModels[$productType];
  96. }
  97. static public function getOptionArray()
  98. {
  99. $options = array();
  100. foreach(self::getTypes() as $typeId=>$type) {
  101. $options[$typeId] = Mage::helper('catalog')->__($type['label']);
  102. }
  103. return $options;
  104. }
  105. static public function getAllOption()
  106. {
  107. $options = self::getOptionArray();
  108. array_unshift($options, array('value'=>'', 'label'=>''));
  109. return $options;
  110. }
  111. static public function getAllOptions()
  112. {
  113. $res = array();
  114. $res[] = array('value'=>'', 'label'=>'');
  115. foreach (self::getOptionArray() as $index => $value) {
  116. $res[] = array(
  117. 'value' => $index,
  118. 'label' => $value
  119. );
  120. }
  121. return $res;
  122. }
  123. static public function getOptions()
  124. {
  125. $res = array();
  126. foreach (self::getOptionArray() as $index => $value) {
  127. $res[] = array(
  128. 'value' => $index,
  129. 'label' => $value
  130. );
  131. }
  132. return $res;
  133. }
  134. static public function getOptionText($optionId)
  135. {
  136. $options = self::getOptionArray();
  137. return isset($options[$optionId]) ? $options[$optionId] : null;
  138. }
  139. static public function getTypes()
  140. {
  141. if (is_null(self::$_types)) {
  142. $productTypes = Mage::getConfig()->getNode('global/catalog/product/type')->asArray();
  143. foreach ($productTypes as $productKey => $productConfig) {
  144. $moduleName = 'catalog';
  145. if (isset($productConfig['@']['module'])) {
  146. $moduleName = $productConfig['@']['module'];
  147. }
  148. $translatedLabel = Mage::helper($moduleName)->__($productConfig['label']);
  149. $productTypes[$productKey]['label'] = $translatedLabel;
  150. }
  151. self::$_types = $productTypes;
  152. }
  153. return self::$_types;
  154. }
  155. /**
  156. * Return composite product type Ids
  157. *
  158. * @return array
  159. */
  160. static public function getCompositeTypes()
  161. {
  162. if (is_null(self::$_compositeTypes)) {
  163. self::$_compositeTypes = array();
  164. $types = self::getTypes();
  165. foreach ($types as $typeId=>$typeInfo) {
  166. if (array_key_exists('composite', $typeInfo) && $typeInfo['composite']) {
  167. self::$_compositeTypes[] = $typeId;
  168. }
  169. }
  170. }
  171. return self::$_compositeTypes;
  172. }
  173. /**
  174. * Return product types by type indexing priority
  175. *
  176. * @return array
  177. */
  178. public static function getTypesByPriority()
  179. {
  180. if (is_null(self::$_typesPriority)) {
  181. self::$_typesPriority = array();
  182. $a = array();
  183. $b = array();
  184. $types = self::getTypes();
  185. foreach ($types as $typeId => $typeInfo) {
  186. $priority = isset($typeInfo['index_priority']) ? abs(intval($typeInfo['index_priority'])) : 0;
  187. if (!empty($typeInfo['composite'])) {
  188. $b[$typeId] = $priority;
  189. } else {
  190. $a[$typeId] = $priority;
  191. }
  192. }
  193. asort($a, SORT_NUMERIC);
  194. asort($b, SORT_NUMERIC);
  195. foreach (array_keys($a) as $typeId) {
  196. self::$_typesPriority[$typeId] = $types[$typeId];
  197. }
  198. foreach (array_keys($b) as $typeId) {
  199. self::$_typesPriority[$typeId] = $types[$typeId];
  200. }
  201. }
  202. return self::$_typesPriority;
  203. }
  204. }