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