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