1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Language detector |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2015, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\langdetect; |
11
|
|
|
|
12
|
|
|
use gplcart\core\Container; |
13
|
|
|
use gplcart\core\helpers\Server; |
14
|
|
|
use gplcart\core\helpers\Session; |
15
|
|
|
use gplcart\core\helpers\Url; |
16
|
|
|
use gplcart\core\Module; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Main class for Language detector module |
20
|
|
|
*/ |
21
|
|
|
class Main |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* URL helper class instance |
26
|
|
|
* @var \gplcart\core\helpers\Url $url |
27
|
|
|
*/ |
28
|
|
|
protected $url; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Module class instance |
32
|
|
|
* @var \gplcart\core\Module $module |
33
|
|
|
*/ |
34
|
|
|
protected $module; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Session helper class instance |
38
|
|
|
* @var \gplcart\core\helpers\Session $session |
39
|
|
|
*/ |
40
|
|
|
protected $session; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Server helper class instance |
44
|
|
|
* @var \gplcart\core\helpers\Server $server |
45
|
|
|
*/ |
46
|
|
|
protected $server; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Main constructor. |
50
|
|
|
* @param Module $module |
51
|
|
|
* @param Url $url |
52
|
|
|
* @param Server $server |
53
|
|
|
* @param Session $session |
54
|
|
|
*/ |
55
|
|
|
public function __construct(Module $module, Url $url, Server $server, Session $session) |
56
|
|
|
{ |
57
|
|
|
$this->url = $url; |
58
|
|
|
$this->module = $module; |
59
|
|
|
$this->server = $server; |
60
|
|
|
$this->session = $session; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Implements hook "route.list" |
65
|
|
|
* @param array $routes |
66
|
|
|
*/ |
67
|
|
|
public function hookRouteList(array &$routes) |
68
|
|
|
{ |
69
|
|
|
$routes['admin/module/settings/langdetect'] = array( |
70
|
|
|
'access' => 'module_edit', |
71
|
|
|
'handlers' => array( |
72
|
|
|
'controller' => array('gplcart\\modules\\langdetect\\controllers\\Settings', 'editSettings') |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Implements hook "translation.set.before" |
79
|
|
|
* @param string $langcode |
80
|
|
|
*/ |
81
|
|
|
public function hookTranslationSetBefore($langcode) |
82
|
|
|
{ |
83
|
|
|
$this->setLanguage($langcode); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets detected language |
88
|
|
|
* @param $langcode |
89
|
|
|
* @return null |
90
|
|
|
*/ |
91
|
|
|
protected function setLanguage($langcode) |
92
|
|
|
{ |
93
|
|
|
$settings = $this->module->getSettings('langdetect'); |
94
|
|
|
|
95
|
|
|
if (empty($settings['redirect'])) { |
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$saved = $this->session->get('langdetect'); |
100
|
|
|
|
101
|
|
|
if (isset($saved)) { |
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$detected_langcode = $this->server->httpLanguage(); |
106
|
|
|
if (!in_array($detected_langcode, $settings['redirect'])) { |
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$language = $this->getLanguageModel()->get($detected_langcode); |
111
|
|
|
|
112
|
|
|
if (empty($language['status'])) { |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->session->set('langdetect', $detected_langcode); |
117
|
|
|
|
118
|
|
|
if ($detected_langcode !== $langcode) { |
119
|
|
|
$redirect = $this->url->language($detected_langcode, $this->url->path()); |
120
|
|
|
$this->url->redirect($redirect, array(), true, true); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Language model class instance |
128
|
|
|
* @return \gplcart\core\models\Language |
129
|
|
|
*/ |
130
|
|
|
protected function getLanguageModel() |
131
|
|
|
{ |
132
|
|
|
/** @var \gplcart\core\models\Language $instance */ |
133
|
|
|
$instance = Container::get('gplcart\\core\\models\\Language'); |
134
|
|
|
return $instance; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|