It is a simple widget embedding a dropdownlist to redirect on a selected value. I used this to allow the user to change the language.
Requirements ¶
Tested with yii 1.1
Usage ¶
Put the DropDownRedirect.php file in your components directory.
I'll show you the way I used this. In my main layout file, I wrote :
$this->widget('DropDownRedirect', array(
'data' => Yii::app()->user->avalaibleLanguages, // data od my dropdownlist
'url' => $this->createUrl($this->route, array_merge($_GET, array('lang' => '__value__'))), // the url (__value__ will be replaced by the selected value)
'select' => Yii::app()->user->language, //the preselected value
));
The dropdown is now placed. To handle the url redirection, I wrote in my controller :
public function init() {
if (isset($_GET['lang']))
Yii::app()->user->language = $_GET['lang'];
Yii::app()->user->applyPreferedLanguage();
Yii::app()->name = Yii::t('site', Yii::app()->name);
parent::init();
}
The user functions used above are here, in a personnal WebUser :
class WebUser extends CWebUser {
public function getLanguage() {
if ($this->getState('lang') == null) {
$lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
$this->setLanguage($lang);
}
return $this->getState('lang');
}
public function setLanguage($lang) {
$this->setState('lang', $lang);
}
public function applyPreferedLanguage() {
Yii::app()->language = $this->getLanguage();
}
public function getAvalaibleLanguages() {
return array('en' => Yii::t('site', 'english'), 'fr' => Yii::t('site', 'french'));
}
}
Resources ¶
...external resources for this extension...
Oups, I forgot availableLanguages function :)
This is in my WebUser class :
public function getAvalaibleLanguages() { return array('en' => Yii::t('site', 'english'), 'fr' => Yii::t('site', 'french')); }
Great work!
I've successfully integrated this with my application where I use it to allow my users to switch active project.
I my Project and Issue controller I have this function:
public function getProjects() { $Criteria = new CDbCriteria(); $Criteria->select = "id, name"; $results = Project::model()->findAll($Criteria); $project_list = array(); $project_list[''] = '<Switch Project>'; foreach($results as $result) { $project_list[$result->name] = $result->name; } return $project_list; }
I use this code to place the widget:
if (((Yii::app()->controller->id === 'project')||(Yii::app()->controller->id === 'issue')) && (isset($_GET['name']))) { $this->widget('DropDownRedirect', array( 'data' => Yii::app()->controller->getProjects(), 'url' => $this->createUrl($this->route, array_merge($_GET, array('name' => '__value__'))), 'select' => '<Switch Project>', //the preselected value 'htmlOptions' => array('class' => 'operations') )); }
Thanks a lot!
Correction..
Since we can't edit our comments, let me correct the criteria select statement:
$Criteria->select = "name";
'id' is not needed, of course.
My Project and my Issue controller makes use of the 'name' parameter to determine the active project.
Doesn't work in Yii 1.1.5
For some reason, this extension stopped working by Yii 1.1.5.
Would be great if someone could fix it. :)
This works
This version works in Yii 1.1.5:
/** * @brief This widget is a dropDown which can redirect to an url base on selected value via javascript */ class DropDownRedirect extends CWidget { public $name; // name attribute of the dropdownlist public $select; // selected value public $data; // data of the dropdownlist public $htmlOptions = array(); // options of the dropdownlist public $url; // url with the string $replacement somewhere, wich will be replaced by the current value public $replacement = '__value__'; // will be replaced by the value public function init() { if (! isset($this->name)) $this->name= $this->id; } public function run() { if (!isset($this->htmlOptions['id'])) $this->htmlOptions['id'] = $this->id; $script = 'window.location = "'.$this->url.'".replace("'.$this->replacement.'", $(this).val());'; if (!isset($this->htmlOptions['onChange'])) $this->htmlOptions['onChange'] = $script; echo CHtml::dropDownList($this->name, $this->select, $this->data, $this->htmlOptions); } }
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.