PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Wishlist/Block/Abstract.php

https://github.com/rgranadino/magento-mirror
PHP | 405 lines | 184 code | 35 blank | 186 comment | 20 complexity | 20d1841405413c0815c0c319446a9057 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_Wishlist
  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. * Wishlist Product Items abstract Block
  28. *
  29. * @category Mage
  30. * @package Mage_Wishlist
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Wishlist_Block_Abstract extends Mage_Catalog_Block_Product_Abstract
  34. {
  35. /**
  36. * Wishlist Product Items Collection
  37. *
  38. * @var Mage_Wishlist_Model_Mysql4_Item_Collection
  39. */
  40. protected $_collection;
  41. /**
  42. * Wishlist Model
  43. *
  44. * @var Mage_Wishlist_Model_Wishlist
  45. */
  46. protected $_wishlist;
  47. /**
  48. * List of block settings to render prices for different product types
  49. *
  50. * @var array
  51. */
  52. protected $_itemPriceBlockTypes = array();
  53. /**
  54. * List of block instances to render prices for different product types
  55. *
  56. * @var array
  57. */
  58. protected $_cachedItemPriceBlocks = array();
  59. /**
  60. * Internal constructor, that is called from real constructor
  61. *
  62. */
  63. protected function _construct()
  64. {
  65. parent::_construct();
  66. $this->addItemPriceBlockType('default', 'wishlist/render_item_price', 'wishlist/render/item/price.phtml');
  67. }
  68. /**
  69. * Retrieve Wishlist Data Helper
  70. *
  71. * @return Mage_Wishlist_Helper_Data
  72. */
  73. protected function _getHelper()
  74. {
  75. return Mage::helper('wishlist');
  76. }
  77. /**
  78. * Retrieve Customer Session instance
  79. *
  80. * @return Mage_Customer_Model_Session
  81. */
  82. protected function _getCustomerSession()
  83. {
  84. return Mage::getSingleton('customer/session');
  85. }
  86. /**
  87. * Retrieve Wishlist model
  88. *
  89. * @return Mage_Wishlist_Model_Wishlist
  90. */
  91. protected function _getWishlist()
  92. {
  93. if (is_null($this->_wishlist)) {
  94. if (Mage::registry('shared_wishlist')) {
  95. $this->_wishlist = Mage::registry('shared_wishlist');
  96. }
  97. elseif (Mage::registry('wishlist')) {
  98. $this->_wishlist = Mage::registry('wishlist');
  99. }
  100. else {
  101. $this->_wishlist = Mage::getModel('wishlist/wishlist');
  102. if ($this->_getCustomerSession()->isLoggedIn()) {
  103. $this->_wishlist->loadByCustomer($this->_getCustomerSession()->getCustomer());
  104. }
  105. }
  106. }
  107. return $this->_wishlist;
  108. }
  109. /**
  110. * Prepare additional conditions to collection
  111. *
  112. * @param Mage_Wishlist_Model_Mysql4_Item_Collection $collection
  113. * @return Mage_Wishlist_Block_Customer_Wishlist
  114. */
  115. protected function _prepareCollection($collection)
  116. {
  117. return $this;
  118. }
  119. /**
  120. * Retrieve Wishlist Product Items collection
  121. *
  122. * @return Mage_Wishlist_Model_Mysql4_Item_Collection
  123. */
  124. public function getWishlistItems()
  125. {
  126. if (is_null($this->_collection)) {
  127. $this->_collection = $this->_getWishlist()
  128. ->getItemCollection()
  129. ->addStoreFilter();
  130. $this->_prepareCollection($this->_collection);
  131. }
  132. return $this->_collection;
  133. }
  134. /**
  135. * Back compatibility retrieve wishlist product items
  136. *
  137. * @deprecated after 1.4.2.0
  138. * @return Mage_Wishlist_Model_Mysql4_Item_Collection
  139. */
  140. public function getWishlist()
  141. {
  142. return $this->getWishlistItems();
  143. }
  144. /**
  145. * Retrieve URL for Removing item from wishlist
  146. *
  147. * @param Mage_Catalog_Model_Product|Mage_Wishlist_Model_Item $item
  148. * @return string
  149. */
  150. public function getItemRemoveUrl($product)
  151. {
  152. return $this->_getHelper()->getRemoveUrl($product);
  153. }
  154. /**
  155. * Retrieve Add Item to shopping cart URL
  156. *
  157. * @param string|Mage_Catalog_Model_Product|Mage_Wishlist_Model_Item $item
  158. * @return string
  159. */
  160. public function getItemAddToCartUrl($item)
  161. {
  162. return $this->_getHelper()->getAddToCartUrl($item);
  163. }
  164. /**
  165. * Retrieve Add Item to shopping cart URL from shared wishlist
  166. *
  167. * @param string|Mage_Catalog_Model_Product|Mage_Wishlist_Model_Item $item
  168. * @return string
  169. */
  170. public function getSharedItemAddToCartUrl($item)
  171. {
  172. return $this->_getHelper()->getSharedAddToCartUrl($item);
  173. }
  174. /**
  175. * Retrieve URL for adding Product to wishlist
  176. *
  177. * @param Mage_Catalog_Model_Product $product
  178. * @return string
  179. */
  180. public function getAddToWishlistUrl($product)
  181. {
  182. return $this->_getHelper()->getAddUrl($product);
  183. }
  184. /**
  185. * Returns item configure url in wishlist
  186. *
  187. * @param Mage_Catalog_Model_Product|Mage_Wishlist_Model_Item $product
  188. *
  189. * @return string
  190. */
  191. public function getItemConfigureUrl($product)
  192. {
  193. if ($product instanceof Mage_Catalog_Model_Product) {
  194. $id = $product->getWishlistItemId();
  195. } else {
  196. $id = $product->getId();
  197. }
  198. $params = array('id' => $id);
  199. return $this->getUrl('wishlist/index/configure/', $params);
  200. }
  201. /**
  202. * Retrieve Escaped Description for Wishlist Item
  203. *
  204. * @param Mage_Catalog_Model_Product $item
  205. * @return string
  206. */
  207. public function getEscapedDescription($item)
  208. {
  209. if ($item->getDescription()) {
  210. return $this->htmlEscape($item->getDescription());
  211. }
  212. return '&nbsp;';
  213. }
  214. /**
  215. * Check Wishlist item has description
  216. *
  217. * @param Mage_Catalog_Model_Product $item
  218. * @return bool
  219. */
  220. public function hasDescription($item)
  221. {
  222. return trim($item->getDescription()) != '';
  223. }
  224. /**
  225. * Retrieve formated Date
  226. *
  227. * @param string $date
  228. * @return string
  229. */
  230. public function getFormatedDate($date)
  231. {
  232. return $this->formatDate($date, Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
  233. }
  234. /**
  235. * Check is the wishlist has a salable product(s)
  236. *
  237. * @return bool
  238. */
  239. public function isSaleable()
  240. {
  241. foreach ($this->getWishlistItems() as $item) {
  242. if ($item->getProduct()->isSaleable()) {
  243. return true;
  244. }
  245. }
  246. return false;
  247. }
  248. /**
  249. * Retrieve wishlist loaded items count
  250. *
  251. * @return int
  252. */
  253. public function getWishlistItemsCount()
  254. {
  255. return $this->getWishlistItems()->count();
  256. }
  257. /**
  258. * Retrieve Qty from item
  259. *
  260. * @param Mage_Wishlist_Model_Item|Mage_Catalog_Model_Product $item
  261. * @return float
  262. */
  263. public function getQty($item)
  264. {
  265. $qty = $item->getQty() * 1;
  266. if (!$qty) {
  267. $qty = 1;
  268. }
  269. return $qty;
  270. }
  271. /**
  272. * Check is the wishlist has items
  273. *
  274. * @return bool
  275. */
  276. public function hasWishlistItems()
  277. {
  278. return $this->getWishlistItemsCount() > 0;
  279. }
  280. /**
  281. * Adds special block to render price for item with specific product type
  282. *
  283. * @param string $type
  284. * @param string $block
  285. * @param string $template
  286. */
  287. public function addItemPriceBlockType($type, $block = '', $template = '')
  288. {
  289. if ($type) {
  290. $this->_itemPriceBlockTypes[$type] = array(
  291. 'block' => $block,
  292. 'template' => $template
  293. );
  294. }
  295. }
  296. /**
  297. * Returns block to render item with some product type
  298. *
  299. * @param string $productType
  300. * @return Mage_Core_Block_Template
  301. */
  302. protected function _getItemPriceBlock($productType)
  303. {
  304. if (!isset($this->_itemPriceBlockTypes[$productType])) {
  305. $productType = 'default';
  306. }
  307. if (!isset($this->_cachedItemPriceBlocks[$productType])) {
  308. $blockType = $this->_itemPriceBlockTypes[$productType]['block'];
  309. $template = $this->_itemPriceBlockTypes[$productType]['template'];
  310. $block = $this->getLayout()->createBlock($blockType)
  311. ->setTemplate($template);
  312. $this->_cachedItemPriceBlocks[$productType] = $block;
  313. }
  314. return $this->_cachedItemPriceBlocks[$productType];
  315. }
  316. /**
  317. * Returns product price block html
  318. * Overwrites parent price html return to be ready to show configured, partially configured and
  319. * non-configured products
  320. *
  321. * @param Mage_Catalog_Model_Product $product
  322. * @param boolean $displayMinimalPrice
  323. * @param string $idSuffix
  324. */
  325. public function getPriceHtml($product, $displayMinimalPrice = false, $idSuffix = '')
  326. {
  327. $type_id = $product->getTypeId();
  328. if (Mage::helper('catalog')->canApplyMsrp($product)) {
  329. $realPriceHtml = $this->_preparePriceRenderer($type_id)
  330. ->setProduct($product)
  331. ->setDisplayMinimalPrice($displayMinimalPrice)
  332. ->setIdSuffix($idSuffix)
  333. ->setIsEmulateMode(true)
  334. ->toHtml();
  335. $product->setAddToCartUrl($this->getAddToCartUrl($product));
  336. $product->setRealPriceHtml($realPriceHtml);
  337. $type_id = $this->_mapRenderer;
  338. }
  339. return $this->_preparePriceRenderer($type_id)
  340. ->setProduct($product)
  341. ->setDisplayMinimalPrice($displayMinimalPrice)
  342. ->setIdSuffix($idSuffix)
  343. ->toHtml();
  344. }
  345. /**
  346. * Retrieve URL to item Product
  347. *
  348. * @param Mage_Wishlist_Model_Item|Mage_Catalog_Model_Product $item
  349. * @param array $additional
  350. * @return string
  351. */
  352. public function getProductUrl($item, $additional = array())
  353. {
  354. if ($item instanceof Mage_Catalog_Model_Product) {
  355. $product = $item;
  356. } else {
  357. $product = $item->getProduct();
  358. }
  359. $buyRequest = $item->getBuyRequest();
  360. if (is_object($buyRequest)) {
  361. $config = $buyRequest->getSuperProductConfig();
  362. if ($config && !empty($config['product_id'])) {
  363. $product = Mage::getModel('catalog/product')
  364. ->setStoreId(Mage::app()->getStore()->getStoreId())
  365. ->load($config['product_id']);
  366. }
  367. }
  368. return parent::getProductUrl($product, $additional);
  369. }
  370. }