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