PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/arush/desparation-deprecated
PHP | 392 lines | 169 code | 34 blank | 189 comment | 16 complexity | ce19d0ddaedb8d3141afeced273f612c 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. * Store 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. return $this->_getHelper()->getWishlist();
  94. }
  95. /**
  96. * Prepare additional conditions to collection
  97. *
  98. * @param Mage_Wishlist_Model_Mysql4_Item_Collection $collection
  99. * @return Mage_Wishlist_Block_Customer_Wishlist
  100. */
  101. protected function _prepareCollection($collection)
  102. {
  103. return $this;
  104. }
  105. /**
  106. * Retrieve Wishlist Product Items collection
  107. *
  108. * @return Mage_Wishlist_Model_Mysql4_Item_Collection
  109. */
  110. public function getWishlistItems()
  111. {
  112. if (is_null($this->_collection)) {
  113. $this->_collection = $this->_getWishlist()
  114. ->getItemCollection();
  115. $this->_prepareCollection($this->_collection);
  116. }
  117. return $this->_collection;
  118. }
  119. /**
  120. * Back compatibility retrieve wishlist product items
  121. *
  122. * @deprecated after 1.4.2.0
  123. * @return Mage_Wishlist_Model_Mysql4_Item_Collection
  124. */
  125. public function getWishlist()
  126. {
  127. return $this->getWishlistItems();
  128. }
  129. /**
  130. * Retrieve URL for Removing item from wishlist
  131. *
  132. * @param Mage_Catalog_Model_Product|Mage_Wishlist_Model_Item $item
  133. *
  134. * @return string
  135. */
  136. public function getItemRemoveUrl($item)
  137. {
  138. return $this->_getHelper()->getRemoveUrl($item);
  139. }
  140. /**
  141. * Retrieve Add Item to shopping cart URL
  142. *
  143. * @param string|Mage_Catalog_Model_Product|Mage_Wishlist_Model_Item $item
  144. * @return string
  145. */
  146. public function getItemAddToCartUrl($item)
  147. {
  148. return $this->_getHelper()->getAddToCartUrl($item);
  149. }
  150. /**
  151. * Retrieve Add Item to shopping cart URL from shared wishlist
  152. *
  153. * @param string|Mage_Catalog_Model_Product|Mage_Wishlist_Model_Item $item
  154. * @return string
  155. */
  156. public function getSharedItemAddToCartUrl($item)
  157. {
  158. return $this->_getHelper()->getSharedAddToCartUrl($item);
  159. }
  160. /**
  161. * Retrieve URL for adding Product to wishlist
  162. *
  163. * @param Mage_Catalog_Model_Product $product
  164. * @return string
  165. */
  166. public function getAddToWishlistUrl($product)
  167. {
  168. return $this->_getHelper()->getAddUrl($product);
  169. }
  170. /**
  171. * Returns item configure url in wishlist
  172. *
  173. * @param Mage_Catalog_Model_Product|Mage_Wishlist_Model_Item $product
  174. *
  175. * @return string
  176. */
  177. public function getItemConfigureUrl($product)
  178. {
  179. if ($product instanceof Mage_Catalog_Model_Product) {
  180. $id = $product->getWishlistItemId();
  181. } else {
  182. $id = $product->getId();
  183. }
  184. $params = array('id' => $id);
  185. return $this->getUrl('wishlist/index/configure/', $params);
  186. }
  187. /**
  188. * Retrieve Escaped Description for Wishlist Item
  189. *
  190. * @param Mage_Catalog_Model_Product $item
  191. * @return string
  192. */
  193. public function getEscapedDescription($item)
  194. {
  195. if ($item->getDescription()) {
  196. return $this->escapeHtml($item->getDescription());
  197. }
  198. return '&nbsp;';
  199. }
  200. /**
  201. * Check Wishlist item has description
  202. *
  203. * @param Mage_Catalog_Model_Product $item
  204. * @return bool
  205. */
  206. public function hasDescription($item)
  207. {
  208. return trim($item->getDescription()) != '';
  209. }
  210. /**
  211. * Retrieve formated Date
  212. *
  213. * @param string $date
  214. * @return string
  215. */
  216. public function getFormatedDate($date)
  217. {
  218. return $this->formatDate($date, Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
  219. }
  220. /**
  221. * Check is the wishlist has a salable product(s)
  222. *
  223. * @return bool
  224. */
  225. public function isSaleable()
  226. {
  227. foreach ($this->getWishlistItems() as $item) {
  228. if ($item->getProduct()->isSaleable()) {
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. /**
  235. * Retrieve wishlist loaded items count
  236. *
  237. * @return int
  238. */
  239. public function getWishlistItemsCount()
  240. {
  241. return $this->_getWishlist()->getItemsCount();
  242. }
  243. /**
  244. * Retrieve Qty from item
  245. *
  246. * @param Mage_Wishlist_Model_Item|Mage_Catalog_Model_Product $item
  247. * @return float
  248. */
  249. public function getQty($item)
  250. {
  251. $qty = $item->getQty() * 1;
  252. if (!$qty) {
  253. $qty = 1;
  254. }
  255. return $qty;
  256. }
  257. /**
  258. * Check is the wishlist has items
  259. *
  260. * @return bool
  261. */
  262. public function hasWishlistItems()
  263. {
  264. return $this->getWishlistItemsCount() > 0;
  265. }
  266. /**
  267. * Adds special block to render price for item with specific product type
  268. *
  269. * @param string $type
  270. * @param string $block
  271. * @param string $template
  272. */
  273. public function addItemPriceBlockType($type, $block = '', $template = '')
  274. {
  275. if ($type) {
  276. $this->_itemPriceBlockTypes[$type] = array(
  277. 'block' => $block,
  278. 'template' => $template
  279. );
  280. }
  281. }
  282. /**
  283. * Returns block to render item with some product type
  284. *
  285. * @param string $productType
  286. * @return Mage_Core_Block_Template
  287. */
  288. protected function _getItemPriceBlock($productType)
  289. {
  290. if (!isset($this->_itemPriceBlockTypes[$productType])) {
  291. $productType = 'default';
  292. }
  293. if (!isset($this->_cachedItemPriceBlocks[$productType])) {
  294. $blockType = $this->_itemPriceBlockTypes[$productType]['block'];
  295. $template = $this->_itemPriceBlockTypes[$productType]['template'];
  296. $block = $this->getLayout()->createBlock($blockType)
  297. ->setTemplate($template);
  298. $this->_cachedItemPriceBlocks[$productType] = $block;
  299. }
  300. return $this->_cachedItemPriceBlocks[$productType];
  301. }
  302. /**
  303. * Returns product price block html
  304. * Overwrites parent price html return to be ready to show configured, partially configured and
  305. * non-configured products
  306. *
  307. * @param Mage_Catalog_Model_Product $product
  308. * @param bool $displayMinimalPrice
  309. * @param string $idSuffix
  310. *
  311. * @return string
  312. */
  313. public function getPriceHtml($product, $displayMinimalPrice = false, $idSuffix = '')
  314. {
  315. $type_id = $product->getTypeId();
  316. if (Mage::helper('catalog')->canApplyMsrp($product)) {
  317. $realPriceHtml = $this->_preparePriceRenderer($type_id)
  318. ->setProduct($product)
  319. ->setDisplayMinimalPrice($displayMinimalPrice)
  320. ->setIdSuffix($idSuffix)
  321. ->setIsEmulateMode(true)
  322. ->toHtml();
  323. $product->setAddToCartUrl($this->getAddToCartUrl($product));
  324. $product->setRealPriceHtml($realPriceHtml);
  325. $type_id = $this->_mapRenderer;
  326. }
  327. return $this->_preparePriceRenderer($type_id)
  328. ->setProduct($product)
  329. ->setDisplayMinimalPrice($displayMinimalPrice)
  330. ->setIdSuffix($idSuffix)
  331. ->toHtml();
  332. }
  333. /**
  334. * Retrieve URL to item Product
  335. *
  336. * @param Mage_Wishlist_Model_Item|Mage_Catalog_Model_Product $item
  337. * @param array $additional
  338. * @return string
  339. */
  340. public function getProductUrl($item, $additional = array())
  341. {
  342. if ($item instanceof Mage_Catalog_Model_Product) {
  343. $product = $item;
  344. } else {
  345. $product = $item->getProduct();
  346. }
  347. $buyRequest = $item->getBuyRequest();
  348. if (is_object($buyRequest)) {
  349. $config = $buyRequest->getSuperProductConfig();
  350. if ($config && !empty($config['product_id'])) {
  351. $product = Mage::getModel('catalog/product')
  352. ->setStoreId(Mage::app()->getStore()->getStoreId())
  353. ->load($config['product_id']);
  354. }
  355. }
  356. return parent::getProductUrl($product, $additional);
  357. }
  358. }