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_GoogleCheckout
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: * GoogleCheckout data helper
29: */
30: class Mage_GoogleCheckout_Helper_Data extends Mage_Core_Helper_Abstract
31: {
32: /**
33: * Google Checkout settings
34: */
35: const XML_PATH_REQUEST_PHONE = 'google/checkout/request_phone';
36: const XML_PATH_DISABLE_DEFAULT_TAX_TABLES = 'google/checkout/disable_default_tax_tables';
37:
38: /**
39: * Google Checkout Shipping - Digital Delivery settings
40: */
41: const XML_PATH_SHIPPING_VIRTUAL_ACTIVE = 'google/checkout_shipping_virtual/active';
42: const XML_PATH_SHIPPING_VIRTUAL_SCHEDULE = 'google/checkout_shipping_virtual/schedule';
43: const XML_PATH_SHIPPING_VIRTUAL_METHOD = 'google/checkout_shipping_virtual/method';
44:
45: /**
46: * Google Checkout Shipping - Carrier Calculated settings
47: */
48: const XML_PATH_SHIPPING_CARRIER_ACTIVE = 'google/checkout_shipping_carrier/active';
49: const XML_PATH_SHIPPING_CARRIER_METHODS = 'google/checkout_shipping_carrier/methods';
50: const XML_PATH_SHIPPING_CARRIER_DEFAULT_PRICE = 'google/checkout_shipping_carrier/default_price';
51: const XML_PATH_SHIPPING_CARRIER_DEFAULT_WIDTH = 'google/checkout_shipping_carrier/default_width';
52: const XML_PATH_SHIPPING_CARRIER_DEFAULT_HEIGHT = 'google/checkout_shipping_carrier/default_height';
53: const XML_PATH_SHIPPING_CARRIER_DEFAULT_LENGTH = 'google/checkout_shipping_carrier/default_length';
54: const XML_PATH_SHIPPING_CARRIER_ADDRESS_CATEGORY = 'google/checkout_shipping_carrier/address_category';
55:
56: /**
57: * Google Checkout Shipping - Flat Rate settings
58: */
59: const XML_PATH_SHIPPING_FLATRATE_ACTIVE = 'google/checkout_shipping_flatrate/active';
60:
61: /**
62: * Google Checkout Shipping - Merchant Calculated settings
63: */
64: const XML_PATH_SHIPPING_MERCHANT_ACTIVE = 'google/checkout_shipping_merchant/active';
65: const XML_PATH_SHIPPING_MERCHANT_ALLOWED_METHODS = 'google/checkout_shipping_merchant/allowed_methods';
66:
67: /**
68: * Google Checkout Shipping - Pickup settings
69: */
70: const XML_PATH_SHIPPING_PICKUP_ACTIVE = 'google/checkout_shipping_pickup/active';
71: const XML_PATH_SHIPPING_PICKUP_TITLE = 'google/checkout_shipping_pickup/title';
72: const XML_PATH_SHIPPING_PICKUP_PRICE = 'google/checkout_shipping_pickup/price';
73:
74: /**
75: * Check if option googlecheckout shipping carrier is enabled
76: *
77: * @param $storeId
78: * @return bool
79: */
80: public function isShippingCarrierActive($storeId)
81: {
82: return (true == Mage::getStoreConfig(self::XML_PATH_SHIPPING_CARRIER_ACTIVE, $storeId));
83: }
84:
85: /**
86: * Convert Magento zip range to array of Google Checkout zip-patterns
87: * (e.g., 12000-13999 -> [12*, 13*])
88: *
89: * @param string $zipRange
90: * @return array
91: */
92: public function zipRangeToZipPattern($zipRange)
93: {
94: $zipLength = 5;
95: $zipPattern = array();
96:
97: if (!preg_match("/^(.+)-(.+)$/", $zipRange, $zipParts)) {
98: return array($zipRange);
99: }
100:
101: if ($zipParts[1] == $zipParts[2]) {
102: return array($zipParts[1]);
103: }
104:
105: if ($zipParts[1] > $zipParts[2]) {
106: list($zipParts[2], $zipParts[1]) = array($zipParts[1], $zipParts[2]);
107: }
108:
109: $from = str_split($zipParts[1]);
110: $to = str_split($zipParts[2]);
111:
112: $startZip = '';
113: $diffPosition = null;
114: for ($pos = 0; $pos < $zipLength; $pos++) {
115: if ($from[$pos] == $to[$pos]) {
116: $startZip .= $from[$pos];
117: } else {
118: $diffPosition = $pos;
119: break;
120: }
121: }
122:
123: /*
124: * calculate zip-patterns
125: */
126: if (min(array_slice($to, $diffPosition)) == 9 && max(array_slice($from, $diffPosition)) == 0) {
127: // particular case like 11000-11999 -> 11*
128: return array($startZip . '*');
129: } else {
130: // calculate approximate zip-patterns
131: $start = $from[$diffPosition];
132: $finish = $to[$diffPosition];
133: if ($diffPosition < $zipLength - 1) {
134: $start++;
135: $finish--;
136: }
137: $end = $diffPosition < $zipLength - 1 ? '*' : '';
138: for ($digit = $start; $digit <= $finish; $digit++) {
139: $zipPattern[] = $startZip . $digit . $end;
140: }
141: }
142:
143: if ($diffPosition == $zipLength - 1) {
144: return $zipPattern;
145: }
146:
147: $nextAsteriskFrom = true;
148: $nextAsteriskTo = true;
149: for ($pos = $zipLength - 1; $pos > $diffPosition; $pos--) {
150: // calculate zip-patterns based on $from value
151: if ($from[$pos] == 0 && $nextAsteriskFrom) {
152: $nextAsteriskFrom = true;
153: } else {
154: $subZip = '';
155: for ($k = $diffPosition; $k < $pos; $k++) {
156: $subZip .= $from[$k];
157: }
158: $delta = $nextAsteriskFrom ? 0 : 1;
159: $end = $pos < $zipLength - 1 ? '*' : '';
160: for ($i = $from[$pos] + $delta; $i <= 9; $i++) {
161: $zipPattern[] = $startZip . $subZip . $i . $end;
162: }
163: $nextAsteriskFrom = false;
164: }
165:
166: // calculate zip-patterns based on $to value
167: if ($to[$pos] == 9 && $nextAsteriskTo) {
168: $nextAsteriskTo = true;
169: } else {
170: $subZip = '';
171: for ($k = $diffPosition; $k < $pos; $k++) {
172: $subZip .= $to[$k];
173: }
174: $delta = $nextAsteriskTo ? 0 : 1;
175: $end = $pos < $zipLength - 1 ? '*' : '';
176: for ($i = 0; $i <= $to[$pos] - $delta; $i++) {
177: $zipPattern[] = $startZip . $subZip . $i . $end;
178: }
179: $nextAsteriskTo = false;
180: }
181: }
182:
183: if ($nextAsteriskFrom) {
184: $zipPattern[] = $startZip . $from[$diffPosition] . '*';
185: }
186: if ($nextAsteriskTo) {
187: $zipPattern[] = $startZip . $to[$diffPosition] . '*';
188: }
189:
190: return $zipPattern;
191: }
192: }
193: