1: <?php
2: /**
3: * CCheckBoxColumn class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: Yii::import('zii.widgets.grid.CGridColumn');
12:
13: /**
14: * CCheckBoxColumn represents a grid view column of checkboxes.
15: *
16: * CCheckBoxColumn supports no checking (read-only), single check and multiple checking.
17: * The mode is determined according to {@link selectableRows}. When in multiple checking mode, the header cell will display
18: * an additional checkbox, clicking on which will check or uncheck all of the checkboxes in the data cells.
19: * The header cell can be customized by {@link headerTemplate}.
20: *
21: * Additionally selecting a checkbox can select a grid view row (depending on {@link CGridView::selectableRows} value) if
22: * {@link selectableRows} is null (default).
23: *
24: * By default, the checkboxes rendered in data cells will have the values that are the same as
25: * the key values of the data model. One may change this by setting either {@link name} or
26: * {@link value}.
27: *
28: * @author Qiang Xue <qiang.xue@gmail.com>
29: * @package zii.widgets.grid
30: * @since 1.1
31: */
32: class CCheckBoxColumn extends CGridColumn
33: {
34: /**
35: * @var string the attribute name of the data model. The corresponding attribute value will be rendered
36: * in each data cell as the checkbox value. Note that if {@link value} is specified, this property will be ignored.
37: * @see value
38: */
39: public $name;
40: /**
41: * @var string a PHP expression that will be evaluated for every data cell and whose result will be rendered
42: * in each data cell as the checkbox value. In this expression, you can use the following variables:
43: * <ul>
44: * <li><code>$row</code> the row number (zero-based)</li>
45: * <li><code>$data</code> the data model for the row</li>
46: * <li><code>$this</code> the column object</li>
47: * </ul>
48: * The PHP expression will be evaluated using {@link evaluateExpression}.
49: *
50: * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
51: * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
52: */
53: public $value;
54: /**
55: * @var string a PHP expression that will be evaluated for every data cell and whose result will
56: * determine if checkbox for each data cell is checked. In this expression, you can use the following variables:
57: * <ul>
58: * <li><code>$row</code> the row number (zero-based)</li>
59: * <li><code>$data</code> the data model for the row</li>
60: * <li><code>$this</code> the column object</li>
61: * </ul>
62: * The PHP expression will be evaluated using {@link evaluateExpression}.
63: *
64: * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
65: * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
66: * @since 1.1.4
67: */
68: public $checked;
69: /**
70: * @var string a PHP expression that will be evaluated for every data cell and whose result will
71: * determine if checkbox for each data cell is disabled. In this expression, you can use the following variables:
72: * <ul>
73: * <li><code>$row</code> the row number (zero-based)</li>
74: * <li><code>$data</code> the data model for the row</li>
75: * <li><code>$this</code> the column object</li>
76: * </ul>
77: * The PHP expression will be evaluated using {@link evaluateExpression}.
78: *
79: * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
80: * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
81: *
82: * Note that expression result will overwrite value set with <code>checkBoxHtmlOptions['disabled']</code>.
83: * @since 1.1.13
84: */
85: public $disabled;
86: /**
87: * @var array the HTML options for the data cell tags.
88: */
89: public $htmlOptions=array('class'=>'checkbox-column');
90: /**
91: * @var array the HTML options for the header cell tag.
92: */
93: public $headerHtmlOptions=array('class'=>'checkbox-column');
94: /**
95: * @var array the HTML options for the footer cell tag.
96: */
97: public $footerHtmlOptions=array('class'=>'checkbox-column');
98: /**
99: * @var array the HTML options for the checkboxes.
100: */
101: public $checkBoxHtmlOptions=array();
102: /**
103: * @var integer the number of rows that can be checked.
104: * Possible values:
105: * <ul>
106: * <li>0 - the state of the checkbox cannot be changed (read-only mode)</li>
107: * <li>1 - only one row can be checked. Checking a checkbox has nothing to do with selecting the row</li>
108: * <li>2 or more - multiple checkboxes can be checked. Checking a checkbox has nothing to do with selecting the row</li>
109: * <li>null - {@link CGridView::selectableRows} is used to control how many checkboxes can be checked.
110: * Checking a checkbox will also select the row.</li>
111: * </ul>
112: * You may also call the JavaScript function <code>$(gridID).yiiGridView('getChecked', columnID)</code>
113: * to retrieve the key values of the checked rows.
114: * @since 1.1.6
115: */
116: public $selectableRows=null;
117: /**
118: * @var string the template to be used to control the layout of the header cell.
119: * The token "{item}" is recognized and it will be replaced with a "check all" checkbox.
120: * By default if in multiple checking mode, the header cell will display an additional checkbox,
121: * clicking on which will check or uncheck all of the checkboxes in the data cells.
122: * See {@link selectableRows} for more details.
123: * @since 1.1.11
124: */
125: public $headerTemplate='{item}';
126:
127: /**
128: * Initializes the column.
129: * This method registers necessary client script for the checkbox column.
130: */
131: public function init()
132: {
133: if(isset($this->checkBoxHtmlOptions['name']))
134: $name=$this->checkBoxHtmlOptions['name'];
135: else
136: {
137: $name=$this->id;
138: if(substr($name,-2)!=='[]')
139: $name.='[]';
140: $this->checkBoxHtmlOptions['name']=$name;
141: }
142: $name=strtr($name,array('['=>"\\[",']'=>"\\]"));
143:
144: if($this->selectableRows===null)
145: {
146: if(isset($this->checkBoxHtmlOptions['class']))
147: $this->checkBoxHtmlOptions['class'].=' select-on-check';
148: else
149: $this->checkBoxHtmlOptions['class']='select-on-check';
150: return;
151: }
152:
153: $cball=$cbcode='';
154: if($this->selectableRows==0)
155: {
156: //.. read only
157: $cbcode="return false;";
158: }
159: elseif($this->selectableRows==1)
160: {
161: //.. only one can be checked, uncheck all other
162: $cbcode="jQuery(\"input:not(#\"+this.id+\")[name='$name']\").prop('checked',false);";
163: }
164: elseif(strpos($this->headerTemplate,'{item}')!==false)
165: {
166: //.. process check/uncheck all
167: $cball=<<<CBALL
168: jQuery(document).on('click','#{$this->id}_all',function() {
169: var checked=this.checked;
170: jQuery("input[name='$name']:enabled").each(function() {this.checked=checked;});
171: });
172:
173: CBALL;
174: $cbcode="jQuery('#{$this->id}_all').prop('checked', jQuery(\"input[name='$name']\").length==jQuery(\"input[name='$name']:checked\").length);";
175: }
176:
177: if($cbcode!=='')
178: {
179: $js=$cball;
180: $js.=<<<EOD
181: jQuery(document).on('click', "input[name='$name']", function() {
182: $cbcode
183: });
184: EOD;
185: Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->id,$js);
186: }
187: }
188:
189: /**
190: * Returns the header cell content.
191: * This method will render a checkbox in the header when {@link selectableRows} is greater than 1
192: * or in case {@link selectableRows} is null when {@link CGridView::selectableRows} is greater than 1.
193: * @return string the header cell content.
194: * @since 1.1.16
195: */
196: public function getHeaderCellContent()
197: {
198: if(trim($this->headerTemplate)==='')
199: return $this->grid->blankDisplay;
200:
201: if($this->selectableRows===null && $this->grid->selectableRows>1)
202: $item=CHtml::checkBox($this->id.'_all',false,array('class'=>'select-on-check-all'));
203: elseif($this->selectableRows>1)
204: $item=CHtml::checkBox($this->id.'_all',false);
205: else
206: $item=parent::getHeaderCellContent();
207:
208: return strtr($this->headerTemplate,array(
209: '{item}'=>$item,
210: ));
211: }
212:
213: /**
214: * Returns the data cell content.
215: * This method renders a checkbox in the data cell.
216: * @param integer $row the row number (zero-based)
217: * @return string the data cell content.
218: * @since 1.1.16
219: */
220: public function getDataCellContent($row)
221: {
222: $data=$this->grid->dataProvider->data[$row];
223: if($this->value!==null)
224: $value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
225: elseif($this->name!==null)
226: $value=CHtml::value($data,$this->name);
227: else
228: $value=$this->grid->dataProvider->keys[$row];
229:
230: $checked = false;
231: if($this->checked!==null)
232: $checked=$this->evaluateExpression($this->checked,array('data'=>$data,'row'=>$row));
233:
234: $options=$this->checkBoxHtmlOptions;
235: if($this->disabled!==null)
236: $options['disabled']=$this->evaluateExpression($this->disabled,array('data'=>$data,'row'=>$row));
237:
238: $name=$options['name'];
239: unset($options['name']);
240: $options['value']=$value;
241: $options['id']=$this->id.'_'.$row;
242: return CHtml::checkBox($name,$checked,$options);
243: }
244: }
245: