PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Helper/Product.php

https://github.com/arush/fancydress
PHP | 441 lines | 234 code | 46 blank | 161 comment | 44 complexity | d030bbc42d52f750790f6b70a5610fbd 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) 2010 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. * Catalog category helper
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. class Mage_Catalog_Helper_Product extends Mage_Core_Helper_Url
  32. {
  33. const XML_PATH_PRODUCT_URL_SUFFIX = 'catalog/seo/product_url_suffix';
  34. const XML_PATH_PRODUCT_URL_USE_CATEGORY = 'catalog/seo/product_use_categories';
  35. const XML_PATH_USE_PRODUCT_CANONICAL_TAG = 'catalog/seo/product_canonical_tag';
  36. /**
  37. * Cache for product rewrite suffix
  38. *
  39. * @var array
  40. */
  41. protected $_productUrlSuffix = array();
  42. protected $_statuses;
  43. protected $_priceBlock;
  44. /**
  45. * Retrieve product view page url
  46. *
  47. * @param mixed $product
  48. * @return string
  49. */
  50. public function getProductUrl($product)
  51. {
  52. if ($product instanceof Mage_Catalog_Model_Product) {
  53. return $product->getProductUrl();
  54. }
  55. elseif (is_numeric($product)) {
  56. return Mage::getModel('catalog/product')->load($product)->getProductUrl();
  57. }
  58. return false;
  59. }
  60. /**
  61. * Retrieve product price
  62. *
  63. * @param Mage_Catalog_Model_Product $product
  64. * @return float
  65. */
  66. public function getPrice($product)
  67. {
  68. return $product->getPrice();
  69. }
  70. /**
  71. * Retrieve product final price
  72. *
  73. * @param Mage_Catalog_Model_Product $product
  74. * @return float
  75. */
  76. public function getFinalPrice($product)
  77. {
  78. return $product->getFinalPrice();
  79. }
  80. /**
  81. * Retrieve base image url
  82. *
  83. * @return string
  84. */
  85. public function getImageUrl($product)
  86. {
  87. $url = false;
  88. if (!$product->getImage()) {
  89. $url = Mage::getDesign()->getSkinUrl('images/no_image.jpg');
  90. }
  91. elseif ($attribute = $product->getResource()->getAttribute('image')) {
  92. $url = $attribute->getFrontend()->getUrl($product);
  93. }
  94. return $url;
  95. }
  96. /**
  97. * Retrieve small image url
  98. *
  99. * @return unknown
  100. */
  101. public function getSmallImageUrl($product)
  102. {
  103. $url = false;
  104. if (!$product->getSmallImage()) {
  105. $url = Mage::getDesign()->getSkinUrl('images/no_image.jpg');
  106. }
  107. elseif ($attribute = $product->getResource()->getAttribute('small_image')) {
  108. $url = $attribute->getFrontend()->getUrl($product);
  109. }
  110. return $url;
  111. }
  112. /**
  113. * Retrieve thumbnail image url
  114. *
  115. * @return unknown
  116. */
  117. public function getThumbnailUrl($product)
  118. {
  119. return '';
  120. }
  121. public function getEmailToFriendUrl($product)
  122. {
  123. $categoryId = null;
  124. if ($category = Mage::registry('current_category')) {
  125. $categoryId = $category->getId();
  126. }
  127. return $this->_getUrl('sendfriend/product/send', array(
  128. 'id' => $product->getId(),
  129. 'cat_id' => $categoryId
  130. ));
  131. }
  132. public function getStatuses()
  133. {
  134. if(is_null($this->_statuses)) {
  135. $this->_statuses = array();//Mage::getModel('catalog/product_status')->getResourceCollection()->load();
  136. }
  137. return $this->_statuses;
  138. }
  139. /**
  140. * Check if a product can be shown
  141. *
  142. * @param Mage_Catalog_Model_Product|int $product
  143. * @return boolean
  144. */
  145. public function canShow($product, $where = 'catalog')
  146. {
  147. if (is_int($product)) {
  148. $product = Mage::getModel('catalog/product')->load($product);
  149. }
  150. /* @var $product Mage_Catalog_Model_Product */
  151. if (!$product->getId()) {
  152. return false;
  153. }
  154. return $product->isVisibleInCatalog() && $product->isVisibleInSiteVisibility();
  155. }
  156. /**
  157. * Retrieve product rewrite sufix for store
  158. *
  159. * @param int $storeId
  160. * @return string
  161. */
  162. public function getProductUrlSuffix($storeId = null)
  163. {
  164. if (is_null($storeId)) {
  165. $storeId = Mage::app()->getStore()->getId();
  166. }
  167. if (!isset($this->_productUrlSuffix[$storeId])) {
  168. $this->_productUrlSuffix[$storeId] = Mage::getStoreConfig(self::XML_PATH_PRODUCT_URL_SUFFIX, $storeId);
  169. }
  170. return $this->_productUrlSuffix[$storeId];
  171. }
  172. /**
  173. * Check if <link rel="canonical"> can be used for product
  174. *
  175. * @param $store
  176. * @return bool
  177. */
  178. public function canUseCanonicalTag($store = null)
  179. {
  180. return Mage::getStoreConfig(self::XML_PATH_USE_PRODUCT_CANONICAL_TAG, $store);
  181. }
  182. /**
  183. * Return information array of product attribute input types
  184. * Only a small number of settings returned, so we won't break anything in current dataflow
  185. * As soon as development process goes on we need to add there all possible settings
  186. *
  187. * @param string $inputType
  188. * @return array
  189. */
  190. public function getAttributeInputTypes($inputType = null)
  191. {
  192. /**
  193. * @todo specify there all relations for properties depending on input type
  194. */
  195. $inputTypes = array(
  196. 'multiselect' => array(
  197. 'backend_model' => 'eav/entity_attribute_backend_array'
  198. ),
  199. 'boolean' => array(
  200. 'source_model' => 'eav/entity_attribute_source_boolean'
  201. )
  202. );
  203. if (is_null($inputType)) {
  204. return $inputTypes;
  205. } else if (isset($inputTypes[$inputType])) {
  206. return $inputTypes[$inputType];
  207. }
  208. return array();
  209. }
  210. /**
  211. * Return default attribute backend model by input type
  212. *
  213. * @param string $inputType
  214. * @return string|null
  215. */
  216. public function getAttributeBackendModelByInputType($inputType)
  217. {
  218. $inputTypes = $this->getAttributeInputTypes();
  219. if (!empty($inputTypes[$inputType]['backend_model'])) {
  220. return $inputTypes[$inputType]['backend_model'];
  221. }
  222. return null;
  223. }
  224. /**
  225. * Return default attribute source model by input type
  226. *
  227. * @param string $inputType
  228. * @return string|null
  229. */
  230. public function getAttributeSourceModelByInputType($inputType)
  231. {
  232. $inputTypes = $this->getAttributeInputTypes();
  233. if (!empty($inputTypes[$inputType]['source_model'])) {
  234. return $inputTypes[$inputType]['source_model'];
  235. }
  236. return null;
  237. }
  238. /**
  239. * Inits product to be used for product controller actions and layouts
  240. * $params can have following data:
  241. * 'category_id' - id of category to check and append to product as current.
  242. * If empty (except FALSE) - will be guessed (e.g. from last visited) to load as current.
  243. *
  244. * @param int $productId
  245. * @param Mage_Core_Controller_Front_Action $controller
  246. * @param Varien_Object $params
  247. *
  248. * @return false|Mage_Catalog_Model_Product
  249. */
  250. public function initProduct($productId, $controller, $params = null)
  251. {
  252. // Prepare data for routine
  253. if (!$params) {
  254. $params = new Varien_Object();
  255. }
  256. // Init and load product
  257. Mage::dispatchEvent('catalog_controller_product_init_before', array('controller_action' => $controller));
  258. if (!$productId) {
  259. return false;
  260. }
  261. $product = Mage::getModel('catalog/product')
  262. ->setStoreId(Mage::app()->getStore()->getId())
  263. ->load($productId);
  264. if (!$this->canShow($product)) {
  265. return false;
  266. }
  267. if (!in_array(Mage::app()->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
  268. return false;
  269. }
  270. // Load product current category
  271. $categoryId = $params->getCategoryId();
  272. if (!$categoryId && ($categoryId !== false)) {
  273. $lastId = Mage::getSingleton('catalog/session')->getLastVisitedCategoryId();
  274. if ($product->canBeShowInCategory($lastId)) {
  275. $categoryId = $lastId;
  276. }
  277. }
  278. if ($categoryId) {
  279. $category = Mage::getModel('catalog/category')->load($categoryId);
  280. $product->setCategory($category);
  281. Mage::register('current_category', $category);
  282. }
  283. // Register current data and dispatch final events
  284. Mage::register('current_product', $product);
  285. Mage::register('product', $product);
  286. try {
  287. Mage::dispatchEvent('catalog_controller_product_init', array('product' => $product));
  288. Mage::dispatchEvent('catalog_controller_product_init_after',
  289. array('product' => $product,
  290. 'controller_action' => $controller
  291. )
  292. );
  293. } catch (Mage_Core_Exception $e) {
  294. Mage::logException($e);
  295. return false;
  296. }
  297. return $product;
  298. }
  299. /**
  300. * Prepares product options by buyRequest: retrieves values and assigns them as default.
  301. * Also parses and adds product management related values - e.g. qty
  302. *
  303. * @param Mage_Catalog_Model_Product $product
  304. * @param Varien_Object $buyRequest
  305. * @return Mage_Catalog_Helper_Product
  306. */
  307. public function prepareProductOptions($product, $buyRequest)
  308. {
  309. $optionValues = $product->processBuyRequest($buyRequest);
  310. $optionValues->setQty($buyRequest->getQty());
  311. $product->setPreconfiguredValues($optionValues);
  312. return $this;
  313. }
  314. /**
  315. * Process $buyRequest and sets its options before saving configuration to some product item.
  316. * This method is used to attach additional parameters to processed buyRequest.
  317. *
  318. * $params holds parameters of what operation must be performed:
  319. * - 'current_config', Varien_Object or array - current buyRequest that configures product in this item,
  320. * used to restore currently attached files
  321. * - 'files_prefix': string[a-z0-9_] - prefix that was added at frontend to names of file inputs,
  322. * so they won't intersect with other submitted options
  323. *
  324. * @param Varien_Object|array $buyRequest
  325. * @param Varien_Object|array $params
  326. * @return Varien_Object
  327. */
  328. public function addParamsToBuyRequest($buyRequest, $params)
  329. {
  330. if (is_array($buyRequest)) {
  331. $buyRequest = new Varien_Object($buyRequest);
  332. }
  333. if (is_array($params)) {
  334. $params = new Varien_Object($params);
  335. }
  336. // Ensure that currentConfig goes as Varien_Object - for easier work with it later
  337. $currentConfig = $params->getCurrentConfig();
  338. if ($currentConfig) {
  339. if (is_array($currentConfig)) {
  340. $params->setCurrentConfig(new Varien_Object($currentConfig));
  341. } else if (!($currentConfig instanceof Varien_Object)) {
  342. $params->unsCurrentConfig();
  343. }
  344. }
  345. /*
  346. * Notice that '_processing_params' must always be object to protect processing forged requests
  347. * where '_processing_params' comes in $buyRequest as array from user input
  348. */
  349. $processingParams = $buyRequest->getData('_processing_params');
  350. if (!$processingParams || !($processingParams instanceof Varien_Object)) {
  351. $processingParams = new Varien_Object();
  352. $buyRequest->setData('_processing_params', $processingParams);
  353. }
  354. $processingParams->addData($params->getData());
  355. return $buyRequest;
  356. }
  357. /**
  358. * Return loaded product instance
  359. *
  360. * @param int|string $productId (SKU or ID)
  361. * @param int $store
  362. * @param string $identifierType
  363. * @return Mage_Catalog_Model_Product
  364. */
  365. public function getProduct($productId, $store, $identifierType = null) {
  366. $loadByIdOnFalse = false;
  367. if ($identifierType == null) {
  368. if (is_string($productId) && !preg_match("/^[+-]?[1-9][0-9]*$|^0$/", $productId)) {
  369. $identifierType = 'sku';
  370. $loadByIdOnFalse = true;
  371. } else {
  372. $identifierType = 'id';
  373. }
  374. }
  375. /** @var $product Mage_Catalog_Model_Product */
  376. $product = Mage::getModel('catalog/product');
  377. if ($store !== null) {
  378. $product->setStoreId($store);
  379. }
  380. if ($identifierType == 'sku') {
  381. $idBySku = $product->getIdBySku($productId);
  382. if ($idBySku) {
  383. $productId = $idBySku;
  384. }
  385. if ($loadByIdOnFalse) {
  386. $identifierType = 'id';
  387. }
  388. }
  389. if ($identifierType == 'id' && is_numeric($productId)) {
  390. $productId = !is_float($productId) ? (int) $productId : 0;
  391. $product->load($productId);
  392. }
  393. return $product;
  394. }
  395. }