-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.php
162 lines (141 loc) · 4.22 KB
/
Main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* @package Shippo
* @author Iurii Makukh <gplcart.software@gmail.com>
* @copyright Copyright (c) 2017, Iurii Makukh <gplcart.software@gmail.com>
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
*/
namespace gplcart\modules\shippo;
use gplcart\core\Container;
use gplcart\core\Module;
/**
* Main class for Shippo module
*/
class Main
{
/**
* Module class instance
* @var \gplcart\core\Module $module
*/
protected $module;
/**
* @param Module $module
*/
public function __construct(Module $module)
{
$this->module = $module;
}
/**
* Implements hook "library.list"
* @param array $libraries
*/
public function hookLibraryList(array &$libraries)
{
$libraries['shippo'] = array(
'name' => 'Shippo', // @text
'description' => 'Shipping API PHP library (USPS, FedEx, UPS and more)', // @text
'url' => 'https://github.com/goshippo/shippo-php-client',
'download' => 'https://github.com/goshippo/shippo-php-client/archive/v1.3.2.zip',
'type' => 'php',
'version' => '1.3.2',
'module' => 'shippo',
'vendor' => 'shippo/shippo-php'
);
}
/**
* Implements hook "route.list"
* @param array $routes
*/
public function hookRouteList(array &$routes)
{
$routes['admin/module/settings/shippo'] = array(
'access' => 'module_edit',
'handlers' => array(
'controller' => array('gplcart\\modules\\shippo\\controllers\\Settings', 'editSettings')
)
);
$routes['admin/tool/shippo'] = array(
'access' => 'shippo_label',
'menu' => array(
'admin' => 'Shipping labels' // @text
),
'handlers' => array(
'controller' => array('gplcart\\modules\\shippo\\controllers\\Label', 'listLabel')
)
);
}
/**
* Implements hook "user.role.permissions"
* @param array $permissions
*/
public function hookUserRolePermissions(array &$permissions)
{
$permissions['shippo_label'] = 'Shippo: view and buy labels'; // @text
}
/**
* Implements hook "order.calculate.before"
* @param mixed $data
*/
public function hookOrderCalculateBefore(array &$data)
{
if (!empty($data['request_shipping_methods'])) {
$this->calculate($data);
}
}
/**
* Implements hook "order.submit.before"
* @param array $order
* @param array $options
* @param array $result
*/
public function hookOrderSubmitBefore(&$order, $options, &$result)
{
$this->getModel()->validate($order, $options, $result);
}
/**
* Implements hook "shipping.methods"
* @param mixed $methods
*/
public function hookShippingMethods(array &$methods)
{
$methods = array_merge($methods, $this->getShippingMethods());
}
/**
* Returns an array of Shippo's shipping methods
* @param bool $only_enabled
* @return array
*/
public function getShippingMethods($only_enabled = true)
{
$methods = array();
$settings = $this->module->getSettings('shippo');
foreach ($this->getModel()->getServiceNames() as $id => $info) {
list($carrier, $service) = $info;
$methods["shippo_$id"] = array(
'dynamic' => true,
'module' => 'shippo',
'status' => $only_enabled ? in_array("shippo_$id", $settings['enabled']) : null,
'title' => gplcart_text('@carrier - @service', array('@carrier' => $carrier, '@service' => $service))
);
}
return $methods;
}
/**
* Calculate shipping
* @param $data
*/
public function calculate(&$data)
{
$this->getModel()->calculate($data);
}
/**
* Returns Shippo's model instance
* @return \gplcart\modules\shippo\models\Shippo
*/
public function getModel()
{
/** @var \gplcart\modules\shippo\models\Shippo $instance */
$instance = Container::get('gplcart\\modules\\shippo\\models\\Shippo');
return $instance;
}
}