-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMLUrlManager.php
126 lines (118 loc) · 4.14 KB
/
MLUrlManager.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
<?php
/**
* Created by JetBrains PhpStorm.
* User: misha
* Date: 22.05.12
* Time: 19:43
* To change this template use File | Settings | File Templates.
*/
class MLUrlManager extends CUrlManager
{
/**
* @var languages list
*/
public $languages = array();
private $_currentUrl = '';
/**
* Initializes the application component.
*/
public function init() {
if (!$this->languages)
$this->languages = array(Yii::app()->language);
$langReg = implode('|',$this->languages);
$newRules = array();
foreach ($this->rules as $reg => $rule) {
$newRules['<language:'.$langReg.'>/'.$reg] = $rule;
}
$newRules['<language:'.$langReg.'>'] = Yii::app()->defaultController;
$this->rules=$newRules;
parent::init();
}
/**
* Constructs a URL.
* @param string $route the controller and the action (e.g. article/read)
* @param array $params list of GET parameters (name=>value). Both the name and value will be URL-encoded.
* If the name is '#', the corresponding value will be treated as an anchor
* and will be appended at the end of the URL.
* @param string $ampersand the token separating name-value pairs in the URL. Defaults to '&'.
* @return string the constructed URL
*/
public function createUrl($route,$params=array(),$ampersand='&') {
if (!isset($params['language'])) {
$params['language'] = Yii::app()->language;
}
return parent::createUrl($route,$params,$ampersand);
}
/**
* Parses the user request.
* @param CHttpRequest $request the request application component
* @return string the route (controllerID/actionID) and perhaps GET parameters in path format.
*/
public function parseUrl($request) {
$this->_currentUrl = parent::parseUrl($request);
if (isset($_GET['language'])&&in_array($_GET['language'],$this->languages)) {
Yii::app()->language = $_GET['language'];
Yii::app()->user->setState('language',$_GET['language']);
} else {
if (Yii::app()->user->hasState('language'))
Yii::app()->language = Yii::app()->user->getState('language');
else if (isset(Yii::app()->request->cookies['language']))
Yii::app()->language = Yii::app()->request->cookies['language']->value;
}
return $this->_currentUrl;
}
/*
* returns the current link with the set language for CHtml::link()
* @param $language language string
* @return array
*/
public function changeLanguage($language) {
if ($this->_currentUrl)
$this->_currentUrl = '/'.$this->_currentUrl;
$newUrl = array($this->_currentUrl);
$newUrl = CMap::mergeArray($newUrl,$_GET);
if(in_array($language,$this->languages)) {
$newUrl['language'] = $language;
}
return $newUrl;
}
/*
* returns the current url with the set language
* @param $language language string
* @return string
*/
public function createLanguageUrl($language) {
if ($this->_currentUrl)
$this->_currentUrl = '/'.$this->_currentUrl;
$get = $_GET;
if(in_array($language,$this->languages)) {
$get['language'] = $language;
}
return Yii::app()->createUrl($this->_currentUrl,$get);
}
/*
* returns the current absolute url with the set language
* @param $language language string
* @return string
*/
public function createAbsoluteLanguageUrl($language) {
if ($this->_currentUrl)
$this->_currentUrl = '/'.$this->_currentUrl;
$get = $_GET;
if(in_array($language,$this->languages)) {
$get['language'] = $language;
}
return Yii::app()->createAbsoluteUrl($this->_currentUrl,$get);
}
/*
* returns languages list array
* @return array
*/
public function listLanguage() {
$list = array();
foreach ($this->languages as $language) {
$list[$language] = $this->changeLanguage($language);
}
return $list;
}
}