1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-multiselect-widget project. |
5
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
6
|
|
|
* For the full copyright and license information, please view |
7
|
|
|
* the LICENSE file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace dosamigos\multiselect; |
11
|
|
|
|
12
|
|
|
use Yii; |
13
|
|
|
use yii\base\InvalidConfigException; |
14
|
|
|
use yii\base\InvalidParamException; |
15
|
|
|
use yii\helpers\Html; |
16
|
|
|
use yii\helpers\Json; |
17
|
|
|
use yii\widgets\InputWidget; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* MultiSelect renders a [David Stutz Multiselect widget](http://davidstutz.github.io/bootstrap-multiselect/) |
21
|
|
|
* |
22
|
|
|
* @see http://davidstutz.github.io/bootstrap-multiselect/ |
23
|
|
|
* @author Antonio Ramirez <[email protected]> |
24
|
|
|
* @link http://www.ramirezcobos.com/ |
25
|
|
|
* @link http://www.2amigos.us/ |
26
|
|
|
* @package dosamigos\widgets |
27
|
|
|
*/ |
28
|
|
|
class MultiSelect extends InputWidget |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var array data for generating the list options (value=>display) |
32
|
|
|
*/ |
33
|
|
|
public $data = []; |
34
|
|
|
/** |
35
|
|
|
* @var array the options for the Bootstrap Multiselect JS plugin. |
36
|
|
|
* Please refer to the Bootstrap Multiselect plugin Web page for possible options. |
37
|
|
|
* @see http://davidstutz.github.io/bootstrap-multiselect/#options |
38
|
|
|
*/ |
39
|
|
|
public $clientOptions = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Initializes the widget. |
43
|
|
|
* @throws InvalidConfigException |
44
|
|
|
*/ |
45
|
|
|
public function init() |
46
|
|
|
{ |
47
|
|
|
if (empty($this->data)) { |
48
|
|
|
throw new InvalidConfigException('"Multiselect::$data" attribute cannot be blank or an empty array.'); |
49
|
|
|
} |
50
|
|
|
parent::init(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
* @throws InvalidParamException |
56
|
|
|
*/ |
57
|
|
|
public function run() |
58
|
|
|
{ |
59
|
|
|
if ($this->hasModel()) { |
60
|
|
|
echo Html::activeDropDownList($this->model, $this->attribute, $this->data, $this->options); |
61
|
|
|
} else { |
62
|
|
|
echo Html::dropDownList($this->name, $this->value, $this->data, $this->options); |
63
|
|
|
} |
64
|
|
|
$this->registerPlugin(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Registers MultiSelect Bootstrap plugin and the related events |
69
|
|
|
* @throws InvalidParamException |
70
|
|
|
*/ |
71
|
|
|
protected function registerPlugin() |
72
|
|
|
{ |
73
|
|
|
$view = $this->getView(); |
74
|
|
|
|
75
|
|
|
MultiSelectAsset::register($view); |
76
|
|
|
|
77
|
|
|
$id = $this->options['id']; |
78
|
|
|
|
79
|
|
|
$options = $this->clientOptions !== false && !empty($this->clientOptions) |
80
|
|
|
? Json::encode($this->clientOptions) |
81
|
|
|
: ''; |
82
|
|
|
|
83
|
|
|
$js = "jQuery('#$id').multiselect($options);"; |
84
|
|
|
$view->registerJs($js); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|