|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Google API |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2018, Iurii Makukh <[email protected]> |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0-or-later |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\gapi; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
use gplcart\core\Config; |
|
14
|
|
|
use gplcart\core\Container; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Main class for Google API module |
|
18
|
|
|
*/ |
|
19
|
|
|
class Main |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Database class instance |
|
24
|
|
|
* @var \gplcart\core\Database $db |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $db; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Config $config |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Config $config) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->db = $config->getDb(); |
|
34
|
|
|
$this->db->addScheme($this->getDbScheme()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Implements hook "library.list" |
|
39
|
|
|
* @param array $libraries |
|
40
|
|
|
*/ |
|
41
|
|
|
public function hookLibraryList(array &$libraries) |
|
42
|
|
|
{ |
|
43
|
|
|
$libraries['gapi'] = array( |
|
44
|
|
|
'name' => 'Google API', // @text |
|
45
|
|
|
'description' => 'A PHP client library for accessing Google APIs', // @text |
|
46
|
|
|
'type' => 'php', |
|
47
|
|
|
'module' => 'gapi', |
|
48
|
|
|
'url' => 'https://github.com/google/google-api-php-client', |
|
49
|
|
|
'download' => 'https://github.com/google/google-api-php-client/releases/download/v2.2.1/google-api-php-client-2.2.1_PHP54.zip', |
|
50
|
|
|
'version' => '2.2.1', |
|
51
|
|
|
'vendor' => 'google/apiclient' |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Implements hook "module.install.before" |
|
57
|
|
|
* @param null|string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function hookModuleInstallBefore(&$result) |
|
60
|
|
|
{ |
|
61
|
|
|
try { |
|
62
|
|
|
$this->db->importScheme('module_gapi_credential', $this->getDbScheme()); |
|
63
|
|
|
} catch (Exception $ex) { |
|
64
|
|
|
$result = $ex->getMessage(); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Implements hook "module.uninstall.after" |
|
70
|
|
|
*/ |
|
71
|
|
|
public function hookModuleUninstallAfter() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->db->deleteTable('module_gapi_credential'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Implements hook "route.list" |
|
78
|
|
|
* @param mixed $routes |
|
79
|
|
|
*/ |
|
80
|
|
|
public function hookRouteList(array &$routes) |
|
81
|
|
|
{ |
|
82
|
|
|
$routes['admin/report/gapi'] = array( |
|
83
|
|
|
'menu' => array( |
|
84
|
|
|
'admin' => 'Google API credentials' // @text |
|
85
|
|
|
), |
|
86
|
|
|
'access' => 'module_gapi_credential', |
|
87
|
|
|
'handlers' => array( |
|
88
|
|
|
'controller' => array('gplcart\\modules\\gapi\\controllers\\Credential', 'listCredential') |
|
89
|
|
|
) |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
$routes['admin/module/settings/gapi/credential/add/(\w+)'] = array( |
|
93
|
|
|
'access' => 'module_gapi_credential_add', |
|
94
|
|
|
'handlers' => array( |
|
95
|
|
|
'controller' => array('gplcart\\modules\\gapi\\controllers\\Credential', 'addCredential') |
|
96
|
|
|
) |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
$routes['admin/module/settings/gapi/credential/edit/(\d+)'] = array( |
|
100
|
|
|
'access' => 'module_gapi_credential_edit', |
|
101
|
|
|
'handlers' => array( |
|
102
|
|
|
'controller' => array('gplcart\\modules\\gapi\\controllers\\Credential', 'editCredential') |
|
103
|
|
|
) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Implements hook "user.role.permissions" |
|
109
|
|
|
* @param array $permissions |
|
110
|
|
|
*/ |
|
111
|
|
|
public function hookUserRolePermissions(array &$permissions) |
|
112
|
|
|
{ |
|
113
|
|
|
$permissions['module_gapi_credential'] = 'Google API credential: access'; // @text |
|
114
|
|
|
$permissions['module_gapi_credential_add'] = 'Google API credential: add'; // @text |
|
115
|
|
|
$permissions['module_gapi_credential_edit'] = 'Google API credential: edit'; // @text |
|
116
|
|
|
$permissions['module_gapi_credential_delete'] = 'Google API credential: delete'; // @text |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns Google API credential |
|
121
|
|
|
* @param int $id |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getCredential($id) |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->getCredentialModel()->get($id); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns an array of existing Google API credentials |
|
131
|
|
|
* @param array $options |
|
132
|
|
|
* @return array |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getCredentials(array $options = array()) |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->getCredentialModel()->getList($options); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Returns Google Client object |
|
141
|
|
|
* @param null|int $credential_id |
|
142
|
|
|
* @param bool $use_own_certificate |
|
143
|
|
|
* @return \Google_Client |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getGoogleClient($credential_id = null, $use_own_certificate = true) |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->getApiModel()->getClient($credential_id, $use_own_certificate); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Returns a Google service class instance |
|
152
|
|
|
* @param string $service_name |
|
153
|
|
|
* @param \Google_Client $client |
|
154
|
|
|
* @return object \Google_Service_SERVICE-NAME |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getGoogleService($service_name, \Google_Client $client) |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->getApiModel()->getService($service_name, $client); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Returns an array of Google service names supported by the library |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getGoogleServiceNames() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->getApiModel()->getServiceNames(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Returns the Credential model instance |
|
171
|
|
|
* @return \gplcart\modules\gapi\models\Credential |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function getCredentialModel() |
|
174
|
|
|
{ |
|
175
|
|
|
/** @var \gplcart\modules\gapi\models\Credential $instance */ |
|
176
|
|
|
$instance = Container::get('gplcart\\modules\\gapi\\models\\Credential'); |
|
177
|
|
|
return $instance; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Returns the API model instance |
|
182
|
|
|
* @return \gplcart\modules\gapi\models\Api |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function getApiModel() |
|
185
|
|
|
{ |
|
186
|
|
|
/** @var \gplcart\modules\gapi\models\Api $instance */ |
|
187
|
|
|
$instance = Container::get('gplcart\\modules\\gapi\\models\\Api'); |
|
188
|
|
|
return $instance; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Returns an array of database scheme |
|
193
|
|
|
* @return array |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function getDbScheme() |
|
196
|
|
|
{ |
|
197
|
|
|
return array( |
|
198
|
|
|
'module_gapi_credential' => array( |
|
199
|
|
|
'fields' => array( |
|
200
|
|
|
'created' => array('type' => 'int', 'length' => 10, 'not_null' => true), |
|
201
|
|
|
'name' => array('type' => 'varchar', 'length' => 255, 'not_null' => true), |
|
202
|
|
|
'type' => array('type' => 'varchar', 'length' => 255, 'not_null' => true), |
|
203
|
|
|
'data' => array('type' => 'blob', 'not_null' => true, 'serialize' => true), |
|
204
|
|
|
'modified' => array('type' => 'int', 'length' => 10, 'not_null' => true, 'default' => 0), |
|
205
|
|
|
'credential_id' => array('type' => 'int', 'length' => 10, 'auto_increment' => true, 'primary' => true) |
|
206
|
|
|
) |
|
207
|
|
|
) |
|
208
|
|
|
); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|