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