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: * Sales order view block
29: *
30: * @method int|null getCustomerId()
31: *
32: * @category Mage
33: * @package Mage_Sales
34: * @author Magento Core Team <core@magentocommerce.com>
35: */
36: class Mage_Sales_Block_Reorder_Sidebar extends Mage_Core_Block_Template
37: {
38: /**
39: * Init orders and templates
40: */
41: public function __construct()
42: {
43: parent::__construct();
44:
45: if ($this->_getCustomerSession()->isLoggedIn()) {
46: $this->setTemplate('sales/order/history.phtml');
47: $this->initOrders();
48: }
49:
50: }
51:
52: /**
53: * Init customer order for display on front
54: */
55: public function initOrders()
56: {
57: $customerId = $this->getCustomerId() ? $this->getCustomerId()
58: : $this->_getCustomerSession()->getCustomer()->getId();
59:
60: $orders = Mage::getResourceModel('sales/order_collection')
61: ->addAttributeToFilter('customer_id', $customerId)
62: ->addAttributeToFilter('state',
63: array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates())
64: )
65: ->addAttributeToSort('created_at', 'desc')
66: ->setPage(1,1);
67: //TODO: add filter by current website
68:
69: $this->setOrders($orders);
70: }
71:
72: /**
73: * Get list of last ordered products
74: *
75: * @return array
76: */
77: public function getItems()
78: {
79: $items = array();
80: $order = $this->getLastOrder();
81: $limit = 5;
82:
83: if ($order) {
84: $website = Mage::app()->getStore()->getWebsiteId();
85: foreach ($order->getParentItemsRandomCollection($limit) as $item) {
86: if ($item->getProduct() && in_array($website, $item->getProduct()->getWebsiteIds())) {
87: $items[] = $item;
88: }
89: }
90: }
91:
92: return $items;
93: }
94:
95: /**
96: * Check item product availability for reorder
97: *
98: * @param Mage_Sales_Model_Order_Item $orderItem
99: * @return boolean
100: */
101: public function isItemAvailableForReorder(Mage_Sales_Model_Order_Item $orderItem)
102: {
103: if ($orderItem->getProduct()) {
104: return $orderItem->getProduct()->getStockItem()->getIsInStock();
105: }
106: return false;
107: }
108:
109: /**
110: * Retrieve form action url and set "secure" param to avoid confirm
111: * message when we submit form from secure page to unsecure
112: *
113: * @return string
114: */
115: public function getFormActionUrl()
116: {
117: return $this->getUrl('checkout/cart/addgroup', array('_secure' => true));
118: }
119:
120: /**
121: * Last order getter
122: *
123: * @return Mage_Sales_Model_Order|false
124: */
125: public function getLastOrder()
126: {
127: foreach ($this->getOrders() as $order) {
128: return $order;
129: }
130: return false;
131: }
132:
133: /**
134: * Render "My Orders" sidebar block
135: *
136: * @return string
137: */
138: protected function _toHtml()
139: {
140: return $this->_getCustomerSession()->isLoggedIn() || $this->getCustomerId() ? parent::_toHtml() : '';
141: }
142:
143: /**
144: * Retrieve customer session instance
145: *
146: * @return Mage_Customer_Model_Session
147: */
148: protected function _getCustomerSession()
149: {
150: return Mage::getSingleton('customer/session');
151: }
152: }
153: