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_Sales
23: * @copyright Copyright (c) 2012 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: /**
28: * Abstract block for display sales (quote/order/invoice etc.) items
29: *
30: * @category Mage
31: * @package Mage_Sales
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Sales_Block_Items_Abstract extends Mage_Core_Block_Template
35: {
36: /**
37: * Renderers with render type key
38: * block => the block name
39: * template => the template file
40: * renderer => the block object
41: *
42: * @var array
43: */
44: protected $_itemRenders = array();
45:
46: /**
47: * Initialize default item renderer
48: */
49: protected function _construct()
50: {
51: parent::_construct();
52: $this->addItemRender('default', 'checkout/cart_item_renderer', 'checkout/cart/item/default.phtml');
53: }
54:
55: /**
56: * Add renderer for item product type
57: *
58: * @param string $type
59: * @param string $block
60: * @param string $template
61: * @return Mage_Checkout_Block_Cart_Abstract
62: */
63: public function addItemRender($type, $block, $template)
64: {
65: $this->_itemRenders[$type] = array(
66: 'block' => $block,
67: 'template' => $template,
68: 'renderer' => null
69: );
70:
71: return $this;
72: }
73:
74: /**
75: * Retrieve item renderer block
76: *
77: * @param string $type
78: * @return Mage_Core_Block_Abstract
79: */
80: public function getItemRenderer($type)
81: {
82: if (!isset($this->_itemRenders[$type])) {
83: $type = 'default';
84: }
85:
86: if (is_null($this->_itemRenders[$type]['renderer'])) {
87: $this->_itemRenders[$type]['renderer'] = $this->getLayout()
88: ->createBlock($this->_itemRenders[$type]['block'])
89: ->setTemplate($this->_itemRenders[$type]['template'])
90: ->setRenderedBlock($this);
91: }
92: return $this->_itemRenders[$type]['renderer'];
93: }
94:
95: /**
96: * Prepare item before output
97: *
98: * @param Mage_Core_Block_Abstract $renderer
99: * @return Mage_Sales_Block_Items_Abstract
100: */
101: protected function _prepareItem(Mage_Core_Block_Abstract $renderer)
102: {
103: return $this;
104: }
105:
106: /**
107: * Return product type for quote/order item
108: *
109: * @param Varien_Object $item
110: * @return string
111: */
112: protected function _getItemType(Varien_Object $item)
113: {
114: if ($item->getOrderItem()) {
115: $type = $item->getOrderItem()->getProductType();
116: } elseif ($item instanceof Mage_Sales_Model_Quote_Address_Item) {
117: $type = $item->getQuoteItem()->getProductType();
118: } else {
119: $type = $item->getProductType();
120: }
121: return $type;
122: }
123:
124: /**
125: * Get item row html
126: *
127: * @param Varien_Object $item
128: * @return string
129: */
130: public function getItemHtml(Varien_Object $item)
131: {
132: $type = $this->_getItemType($item);
133:
134: $block = $this->getItemRenderer($type)
135: ->setItem($item);
136: $this->_prepareItem($block);
137: return $block->toHtml();
138: }
139: }
140: