1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Device |
5
|
|
|
* @author Iurii Makukh |
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\device; |
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
use gplcart\core\Module, |
14
|
|
|
gplcart\core\Library; |
15
|
|
|
use gplcart\core\helpers\Session as SessionHelper; |
16
|
|
|
use gplcart\core\exceptions\Dependency as DependencyException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Main class for Device module |
20
|
|
|
*/ |
21
|
|
|
class Main |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Module class instance |
26
|
|
|
* @var \gplcart\core\Module $module |
27
|
|
|
*/ |
28
|
|
|
protected $module; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Library class instance |
32
|
|
|
* @var \gplcart\core\Library $library |
33
|
|
|
*/ |
34
|
|
|
protected $library; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Session helper class instance |
38
|
|
|
* @var \gplcart\core\helpers\Session $session |
39
|
|
|
*/ |
40
|
|
|
protected $session; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Module $module |
44
|
|
|
* @param Library $library |
45
|
|
|
* @param SessionHelper $session |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Module $module, Library $library, SessionHelper $session) |
48
|
|
|
{ |
49
|
|
|
$this->module = $module; |
50
|
|
|
$this->library = $library; |
51
|
|
|
$this->session = $session; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Implements hook "library.list" |
56
|
|
|
* @param array $libraries |
57
|
|
|
*/ |
58
|
|
|
public function hookLibraryList(array &$libraries) |
59
|
|
|
{ |
60
|
|
|
$libraries['mobile_detect'] = array( |
61
|
|
|
'name' => /* @text */'Mobile Detect', |
62
|
|
|
'description' => /* @text */'A lightweight PHP class for detecting mobile devices', |
63
|
|
|
'url' => 'https://github.com/serbanghita/Mobile-Detect', |
64
|
|
|
'download' => 'https://github.com/serbanghita/Mobile-Detect/archive/2.8.25.zip', |
65
|
|
|
'type' => 'php', |
66
|
|
|
'version_source' => array( |
67
|
|
|
'file' => 'vendor/mobiledetect/mobiledetectlib/Mobile_Detect.php', |
68
|
|
|
'pattern' => '/.*VERSION.*(\\d+\\.+\\d+\\.+\\d+)/', |
69
|
|
|
'lines' => 100 |
70
|
|
|
), |
71
|
|
|
'module' => 'device', |
72
|
|
|
'files' => array( |
73
|
|
|
'vendor/mobiledetect/mobiledetectlib/Mobile_Detect.php' |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Implements hook "route.list" |
80
|
|
|
* @param array $routes |
81
|
|
|
*/ |
82
|
|
|
public function hookRouteList(array &$routes) |
83
|
|
|
{ |
84
|
|
|
$routes['admin/module/settings/device'] = array( |
85
|
|
|
'access' => 'module_edit', |
86
|
|
|
'handlers' => array( |
87
|
|
|
'controller' => array('gplcart\\modules\\device\\controllers\\Settings', 'editSettings') |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Implements hook "theme" |
94
|
|
|
* @param \gplcart\core\Controller $controller |
95
|
|
|
*/ |
96
|
|
|
public function hookTheme($controller) |
97
|
|
|
{ |
98
|
|
|
$this->switchTheme($controller); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Implements hook "module.enable.after" |
103
|
|
|
*/ |
104
|
|
|
public function hookModuleEnableAfter() |
105
|
|
|
{ |
106
|
|
|
$this->library->clearCache(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Implements hook "module.disable.after" |
111
|
|
|
*/ |
112
|
|
|
public function hookModuleDisableAfter() |
113
|
|
|
{ |
114
|
|
|
$this->library->clearCache(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Implements hook "module.install.after" |
119
|
|
|
*/ |
120
|
|
|
public function hookModuleInstallAfter() |
121
|
|
|
{ |
122
|
|
|
$this->library->clearCache(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Implements hook "module.uninstall.after" |
127
|
|
|
*/ |
128
|
|
|
public function hookModuleUninstallAfter() |
129
|
|
|
{ |
130
|
|
|
$this->library->clearCache(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns a device type |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function getDeviceType() |
138
|
|
|
{ |
139
|
|
|
$device = $this->session->get('device'); |
140
|
|
|
|
141
|
|
|
if (empty($device)) { |
142
|
|
|
|
143
|
|
|
try { |
144
|
|
|
$detector = $this->getLibrary(); |
145
|
|
|
} catch (Exception $ex) { |
146
|
|
|
return 'desktop'; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ($detector->isMobile()) { |
150
|
|
|
$device = 'mobile'; |
151
|
|
|
} else if ($detector->isTablet()) { |
152
|
|
|
$device = 'tablet'; |
153
|
|
|
} else { |
154
|
|
|
$device = 'desktop'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$this->session->set('device', $device); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $device; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns the mobile detector instance |
165
|
|
|
* @return \Mobile_Detect |
166
|
|
|
* @throws DependencyException |
167
|
|
|
*/ |
168
|
|
|
public function getLibrary() |
169
|
|
|
{ |
170
|
|
|
$this->library->load('mobile_detect'); |
171
|
|
|
|
172
|
|
|
if (class_exists('Mobile_Detect')) { |
173
|
|
|
return new \Mobile_Detect; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
throw new DependencyException('Class Mobile_Detect not forund'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Switch the current theme |
181
|
|
|
* @param \gplcart\core\Controller $controller |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
|
|
protected function switchTheme($controller) |
185
|
|
|
{ |
186
|
|
|
if ($controller->isInternalRoute()) { |
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$device = $this->getDeviceType(); |
191
|
|
|
$store_id = $controller->getStoreId(); |
192
|
|
|
$settings = $this->module->getSettings('device'); |
193
|
|
|
|
194
|
|
|
if ($controller->isBackend() || $device === 'desktop' || empty($settings['theme'][$store_id][$device])) { |
195
|
|
|
return false; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$theme = $settings['theme'][$store_id][$device]; |
199
|
|
|
|
200
|
|
|
if ($this->module->isEnabled($theme)) { |
201
|
|
|
$controller->setCurrentTheme($theme); |
202
|
|
|
return true; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|