-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTags.php
61 lines (52 loc) · 1.54 KB
/
Tags.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
<?php
namespace enigmatix\widgets;
use enigmatix\select2\Select2Asset;
use yii\helpers\Html;
use yii\widgets\InputWidget;
/**
* Class Tags
* @package frontend\widgets
* @author Joel Small
* @email [email protected]
*
* This class creates a widget that creates a list of tags in the form of a tag cloud.
*/
class Tags extends InputWidget
{
public $model;
public $attribute;
public $tags = [];
public $name;
public $pluginOptions;
public $url;
public $urlById;
public $value = [];
public $placeholder;
public $onChange;
public $disabled = false;
public function run()
{
$view = $this->getView();
Select2Asset::register($view);
if($this->value === ''){
$this->value = [];
}
if(!is_array($this->value))
{
$this->value[$this->value] = $this->value;
}
$tags = json_encode($this->tags);
echo Html::dropDownList($this->name, $this->value, $this->tags, ['class' =>'form-control','multiple' => true,'id' => $this->options['id']]);
$script = <<< SCRIPT
$("#{$this->options['id']}").select2({
placeholder: "$this->placeholder",
tags: $tags,
})
SCRIPT;
if($this->onChange){$script .= '.on("change", function(e){ $.ajax("'.$this->onChange.'&" + $("#'.$this->options['id'].'").serialize());})';}
if($this->disabled){$script .= '.prop("disabled", true)';}
$script .= ';';
$view = $this->getView();
$view->registerJs($script);
}
}