-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBooleanButtonColumn.php
executable file
·102 lines (83 loc) · 2.7 KB
/
BooleanButtonColumn.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
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* A column that displays an image that changes according to the value of the field, and has a link to toggle it on of off.
* The attribute is defaulted to "status"
*
* @author igoru
*/
class BooleanButtonColumn extends CLinkColumn {
public $name = 'status';
public $type = 'html';
/**
* A simple value; it won't be evaluated. Should be a boolean valid value.
* @var boolean
*/
public $value;
/**
* The button label. Defaults to {@link name}
* @var string
*/
public $label;
/**
* HTML options for the button tag
* @var array
*/
public $options;
/**
* Image for when status is true
* @var string
*/
public $trueImageUrl;
/**
* Image for when status is false
* @var string
*/
public $falseImageUrl;
public $toggleUrl = 'Yii::app()->controller->createUrl("toggle",array("id"=>$data->primaryKey))';
protected $baseScriptUrl;
/**
* Initializes the column.
*/
public function init()
{
parent::init();
if ($this->name === null)
throw new CException(Yii::t('zii','"name" must be specified for BooleanButtonColumn.'));
if ($this->label === null)
$this->label=$this->name;
if ($this->baseScriptUrl === null)
$this->baseScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('ext.assets.booleanButtonColumn'));
if ($this->trueImageUrl === null) $this->trueImageUrl = $this->baseScriptUrl.'/enabled.png';
if ($this->falseImageUrl === null) $this->falseImageUrl = $this->baseScriptUrl.'/disabled.png';
}
/**
* Renders the data cell content.
* This method evaluates {@link value} or {@link name} and renders the result.
* @param integer the row number (zero-based)
* @param mixed the data associated with the row
*/
protected function renderDataCellContent($row,$data)
{
if ($this->value !== null) $value = $this->value;
elseif ($this->name !== null) $value = CHtml::value($data,$this->name);
$image = ($value)? $this->trueImageUrl : $this->falseImageUrl;
$otherImage = ($value)? $this->falseImageUrl : $this->trueImageUrl;
$url = (isset($this->toggleUrl))? $this->evaluateExpression($this->toggleUrl, array('data' => $data, 'row' => $row)) : '#';
$options = (isset($this->options))? $this->options : array();
if (!isset($options['title'])) $options['title'] = $this->label;
$imgChange = array(
'onmouseover' => "this.src='$otherImage'",
'onmouseout' => "this.src='$image'",
);
if(isset($this->trueImageUrl) && is_string($this->trueImageUrl))
echo CHtml::link(CHtml::image($image, $this->label, $imgChange), $url, $options);
else
echo CHtml::link($this->label, $url, $options);
// echo $value;
}
}
?>