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\Module, |
13
|
|
|
gplcart\core\Container; |
14
|
|
|
use gplcart\core\helpers\Url as UrlHelper, |
15
|
|
|
gplcart\core\helpers\Server as ServerHelper, |
16
|
|
|
gplcart\core\helpers\Session as SessionHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Main class for Language detector module |
20
|
|
|
*/ |
21
|
|
|
class LangDetect |
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
|
|
|
* @param Module $module |
50
|
|
|
* @param UrlHelper $url |
51
|
|
|
* @param ServerHelper $server |
52
|
|
|
* @param SessionHelper $session |
53
|
|
|
*/ |
54
|
|
|
public function __construct(Module $module, UrlHelper $url, ServerHelper $server, |
55
|
|
|
SessionHelper $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->setDetectedLanguage($langcode); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $langcode |
88
|
|
|
* @return null |
89
|
|
|
*/ |
90
|
|
|
protected function setDetectedLanguage($langcode) |
91
|
|
|
{ |
92
|
|
|
$settings = $this->module->getSettings('langdetect'); |
93
|
|
|
|
94
|
|
|
if (empty($settings['redirect'])) { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$saved = $this->session->get('langdetect'); |
99
|
|
|
|
100
|
|
|
if (isset($saved)) { |
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$detected_langcode = $this->server->httpLanguage(); |
105
|
|
|
if (!in_array($detected_langcode, $settings['redirect'])) { |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$language = $this->getLanguage()->get($detected_langcode); |
110
|
|
|
|
111
|
|
|
if (empty($language['status'])) { |
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->session->set('langdetect', $detected_langcode); |
116
|
|
|
|
117
|
|
|
if ($detected_langcode !== $langcode) { |
118
|
|
|
$redirect = $this->url->language($detected_langcode, $this->url->path()); |
119
|
|
|
$this->url->redirect($redirect, array(), true, true); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Language model class instance |
125
|
|
|
* @return \gplcart\core\models\Language |
126
|
|
|
*/ |
127
|
|
|
protected function getLanguage() |
128
|
|
|
{ |
129
|
|
|
return Container::get('gplcart\\core\\models\\Language'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|