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_Checkout
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: * One page common functionality block
29: *
30: * @author Magento Core Team <core@magentocommerce.com>
31: */
32: abstract class Mage_Checkout_Block_Onepage_Abstract extends Mage_Core_Block_Template
33: {
34: protected $_customer;
35: protected $_checkout;
36: protected $_quote;
37: protected $_countryCollection;
38: protected $_regionCollection;
39: protected $_addressesCollection;
40:
41: /**
42: * Get logged in customer
43: *
44: * @return Mage_Customer_Model_Customer
45: */
46: public function getCustomer()
47: {
48: if (empty($this->_customer)) {
49: $this->_customer = Mage::getSingleton('customer/session')->getCustomer();
50: }
51: return $this->_customer;
52: }
53:
54: /**
55: * Retrieve checkout session model
56: *
57: * @return Mage_Checkout_Model_Session
58: */
59: public function getCheckout()
60: {
61: if (empty($this->_checkout)) {
62: $this->_checkout = Mage::getSingleton('checkout/session');
63: }
64: return $this->_checkout;
65: }
66:
67: /**
68: * Retrieve sales quote model
69: *
70: * @return Mage_Sales_Model_Quote
71: */
72: public function getQuote()
73: {
74: if (empty($this->_quote)) {
75: $this->_quote = $this->getCheckout()->getQuote();
76: }
77: return $this->_quote;
78: }
79:
80: public function isCustomerLoggedIn()
81: {
82: return Mage::getSingleton('customer/session')->isLoggedIn();
83: }
84:
85: public function getCountryCollection()
86: {
87: if (!$this->_countryCollection) {
88: $this->_countryCollection = Mage::getSingleton('directory/country')->getResourceCollection()
89: ->loadByStore();
90: }
91: return $this->_countryCollection;
92: }
93:
94: public function getRegionCollection()
95: {
96: if (!$this->_regionCollection) {
97: $this->_regionCollection = Mage::getModel('directory/region')->getResourceCollection()
98: ->addCountryFilter($this->getAddress()->getCountryId())
99: ->load();
100: }
101: return $this->_regionCollection;
102: }
103:
104: public function customerHasAddresses()
105: {
106: return count($this->getCustomer()->getAddresses());
107: }
108:
109: /* */
110: public function getAddressesHtmlSelect($type)
111: {
112: if ($this->isCustomerLoggedIn()) {
113: $options = array();
114: foreach ($this->getCustomer()->getAddresses() as $address) {
115: $options[] = array(
116: 'value' => $address->getId(),
117: 'label' => $address->format('oneline')
118: );
119: }
120:
121: $addressId = $this->getAddress()->getCustomerAddressId();
122: if (empty($addressId)) {
123: if ($type=='billing') {
124: $address = $this->getCustomer()->getPrimaryBillingAddress();
125: } else {
126: $address = $this->getCustomer()->getPrimaryShippingAddress();
127: }
128: if ($address) {
129: $addressId = $address->getId();
130: }
131: }
132:
133: $select = $this->getLayout()->createBlock('core/html_select')
134: ->setName($type.'_address_id')
135: ->setId($type.'-address-select')
136: ->setClass('address-select')
137: ->setExtraParams('onchange="'.$type.'.newAddress(!this.value)"')
138: ->setValue($addressId)
139: ->setOptions($options);
140:
141: $select->addOption('', Mage::helper('checkout')->__('New Address'));
142:
143: return $select->getHtml();
144: }
145: return '';
146: }
147:
148: public function getCountryHtmlSelect($type)
149: {
150: $countryId = $this->getAddress()->getCountryId();
151: if (is_null($countryId)) {
152: $countryId = Mage::helper('core')->getDefaultCountry();
153: }
154: $select = $this->getLayout()->createBlock('core/html_select')
155: ->setName($type.'[country_id]')
156: ->setId($type.':country_id')
157: ->setTitle(Mage::helper('checkout')->__('Country'))
158: ->setClass('validate-select')
159: ->setValue($countryId)
160: ->setOptions($this->getCountryOptions());
161: if ($type === 'shipping') {
162: $select->setExtraParams('onchange="if(window.shipping)shipping.setSameAsBilling(false);"');
163: }
164:
165: return $select->getHtml();
166: }
167:
168:
169: public function getRegionHtmlSelect($type)
170: {
171: $select = $this->getLayout()->createBlock('core/html_select')
172: ->setName($type.'[region]')
173: ->setId($type.':region')
174: ->setTitle(Mage::helper('checkout')->__('State/Province'))
175: ->setClass('required-entry validate-state')
176: ->setValue($this->getAddress()->getRegionId())
177: ->setOptions($this->getRegionCollection()->toOptionArray());
178:
179: return $select->getHtml();
180: }
181:
182: public function getCountryOptions()
183: {
184: $options = false;
185: $useCache = Mage::app()->useCache('config');
186: if ($useCache) {
187: $cacheId = 'DIRECTORY_COUNTRY_SELECT_STORE_' . Mage::app()->getStore()->getCode();
188: $cacheTags = array('config');
189: if ($optionsCache = Mage::app()->loadCache($cacheId)) {
190: $options = unserialize($optionsCache);
191: }
192: }
193:
194: if ($options == false) {
195: $options = $this->getCountryCollection()->toOptionArray();
196: if ($useCache) {
197: Mage::app()->saveCache(serialize($options), $cacheId, $cacheTags);
198: }
199: }
200: return $options;
201: }
202:
203: /**
204: * Get checkout steps codes
205: *
206: * @return array
207: */
208: protected function _getStepCodes()
209: {
210: return array('login', 'billing', 'shipping', 'shipping_method', 'payment', 'review');
211: }
212:
213:
214: /**
215: * Retrieve is allow and show block
216: *
217: * @return bool
218: */
219: public function isShow()
220: {
221: return true;
222: }
223: /* */
224: }
225: