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_Shipping
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: * Shipping data helper
29: */
30: class Mage_Shipping_Helper_Data extends Mage_Core_Helper_Abstract
31: {
32: /**
33: * Allowed hash keys
34: *
35: * @var array
36: */
37: protected $_allowedHashKeys = array('ship_id', 'order_id', 'track_id');
38:
39: /**
40: * Decode url hash
41: *
42: * @param string $hash
43: * @return array
44: */
45: public function decodeTrackingHash($hash)
46: {
47: $hash = explode(':', Mage::helper('core')->urlDecode($hash));
48: if (count($hash) === 3 && in_array($hash[0], $this->_allowedHashKeys)) {
49: return array('key' => $hash[0], 'id' => (int)$hash[1], 'hash' => $hash[2]);
50: }
51: return array();
52: }
53:
54: /**
55: * Retrieve tracking url with params
56: *
57: * @deprecated the non-model usage
58: *
59: * @param string $key
60: * @param integer|Mage_Sales_Model_Order|Mage_Sales_Model_Order_Shipment|Mage_Sales_Model_Order_Shipment_Track $model
61: * @param string $method - option
62: * @return string
63: */
64: protected function _getTrackingUrl($key, $model, $method = 'getId')
65: {
66: if (empty($model)) {
67: $param = array($key => ''); // @deprecated after 1.4.0.0-alpha3
68: } else if (!is_object($model)) {
69: $param = array($key => $model); // @deprecated after 1.4.0.0-alpha3
70: } else {
71: $param = array(
72: 'hash' => Mage::helper('core')->urlEncode("{$key}:{$model->$method()}:{$model->getProtectCode()}")
73: );
74: }
75: $storeId = is_object($model) ? $model->getStoreId() : null;
76: $storeModel = Mage::app()->getStore($storeId);
77: return $storeModel->getUrl('shipping/tracking/popup', $param);
78: }
79:
80: /**
81: * @deprecated after 1.4.0.0-alpha3
82: * Retrieve tracking pop up url by order id or object
83: *
84: * @param int|Mage_Sales_Model_Order $order
85: * @return string
86: */
87: public function getTrackingPopUpUrlByOrderId($order = '')
88: {
89: if ($order && !is_object($order)) {
90: $order = Mage::getModel('sales/order')->load($order);
91: }
92: return $this->_getTrackingUrl('order_id', $order);
93: }
94:
95: /**
96: * @deprecated after 1.4.0.0-alpha3
97: * Retrieve tracking pop up url by track id or object
98: *
99: * @param int|Mage_Sales_Model_Order_Shipment_Track $track
100: * @return string
101: */
102: public function getTrackingPopUpUrlByTrackId($track = '')
103: {
104: if ($track && !is_object($track)) {
105: $track = Mage::getModel('sales/order_shipment_track')->load($track);
106: }
107: return $this->_getTrackingUrl('track_id', $track, 'getEntityId');
108: }
109:
110: /**
111: * @deprecated after 1.4.0.0-alpha3
112: * Retrieve tracking pop up url by ship id or object
113: *
114: * @param int|Mage_Sales_Model_Order_Shipment $track
115: * @return string
116: */
117: public function getTrackingPopUpUrlByShipId($ship = '')
118: {
119: if ($ship && !is_object($ship)) {
120: $ship = Mage::getModel('sales/order_shipment')->load($ship);
121: }
122: return $this->_getTrackingUrl('ship_id', $ship);
123: }
124:
125: /**
126: * Shipping tracking popup URL getter
127: *
128: * @param Mage_Sales_Model_Abstract $model
129: * @return string
130: */
131: public function getTrackingPopupUrlBySalesModel($model)
132: {
133: if ($model instanceof Mage_Sales_Model_Order) {
134: return $this->_getTrackingUrl('order_id', $model);
135: } elseif ($model instanceof Mage_Sales_Model_Order_Shipment) {
136: return $this->_getTrackingUrl('ship_id', $model);
137: } elseif ($model instanceof Mage_Sales_Model_Order_Shipment_Track) {
138: return $this->_getTrackingUrl('track_id', $model, 'getEntityId');
139: }
140: return '';
141: }
142:
143: /**
144: * Retrieve tracking ajax url
145: *
146: * @return string
147: */
148: public function getTrackingAjaxUrl()
149: {
150: return $this->_getUrl('shipping/tracking/ajax');
151: }
152:
153: public function isFreeMethod($method, $storeId = null)
154: {
155: $arr = explode('_', $method, 2);
156: if (!isset($arr[1])) {
157: return false;
158: }
159: $freeMethod = Mage::getStoreConfig('carriers/' . $arr[0] . '/free_method', $storeId);
160: return $freeMethod == $arr[1];
161: }
162: }
163: