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