1: <?php
2: /**
3: * CDataColumn 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: * CDataColumn represents a grid view column that is associated with a data attribute or PHP expression.
15: *
16: * Either {@link name} or {@link value} should be specified. The former specifies
17: * a data attribute name, while the latter a PHP expression whose value should be rendered instead.
18: *
19: * The property {@link sortable} determines whether the grid view can be sorted according to this column.
20: * Note that the {@link name} should always be set if the column needs to be sortable. The {@link name}
21: * value will be used by {@link CSort} to render a clickable link in the header cell to trigger the sorting.
22: *
23: * @author Qiang Xue <qiang.xue@gmail.com>
24: * @package zii.widgets.grid
25: * @since 1.1
26: */
27: class CDataColumn extends CGridColumn
28: {
29: /**
30: * @var string the attribute name of the data model. Used for column sorting, filtering and to render the corresponding
31: * attribute value in each data cell. If {@link value} is specified it will be used to rendered the data cell instead of the attribute value.
32: * @see value
33: * @see sortable
34: */
35: public $name;
36: /**
37: * @var string a PHP expression that will be evaluated for every data cell using {@link evaluateExpression} and whose result will be rendered
38: * as the content of the data cell.
39: * In this expression, you can use the following variables:
40: * <ul>
41: * <li><code>$row</code> the row number (zero-based).</li>
42: * <li><code>$data</code> the data model for the row.</li>
43: * <li><code>$this</code> the column object.</li>
44: * </ul>
45: * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
46: * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
47: */
48: public $value;
49: /**
50: * @var string the type of the attribute value. This determines how the attribute value is formatted for display.
51: * Valid values include those recognizable by {@link CGridView::formatter}, such as: raw, text, ntext, html, date, time,
52: * datetime, boolean, number, email, image, url. For more details, please refer to {@link CFormatter}.
53: * Defaults to 'text' which means the attribute value will be HTML-encoded.
54: */
55: public $type='text';
56: /**
57: * @var boolean whether the column is sortable. If so, the header cell will contain a link that may trigger the sorting.
58: * Defaults to true. Note that if {@link name} is not set, or if {@link name} is not allowed by {@link CSort},
59: * this property will be treated as false.
60: * @see name
61: */
62: public $sortable=true;
63: /**
64: * @var mixed the HTML code representing a filter input (eg a text field, a dropdown list)
65: * that is used for this data column. This property is effective only when
66: * {@link CGridView::filter} is set.
67: * If this property is not set, a text field will be generated as the filter input;
68: * If this property is an array, a dropdown list will be generated that uses this property value as
69: * the list options.
70: * If you don't want a filter for this data column, set this value to false.
71: * @since 1.1.1
72: */
73: public $filter;
74:
75: /**
76: * Initializes the column.
77: */
78: public function init()
79: {
80: parent::init();
81: if($this->name===null)
82: $this->sortable=false;
83: if($this->name===null && $this->value===null)
84: throw new CException(Yii::t('zii','Either "name" or "value" must be specified for CDataColumn.'));
85: }
86:
87: /**
88: * Returns the filter cell content.
89: * This method will return the {@link filter} as is if it is a string.
90: * If {@link filter} is an array, it is assumed to be a list of options, and a dropdown selector will be rendered.
91: * Otherwise if {@link filter} is not false, a text field is rendered.
92: * @return string the filter cell content
93: * @since 1.1.16
94: */
95: public function getFilterCellContent()
96: {
97: if(is_string($this->filter))
98: return $this->filter;
99: elseif($this->filter!==false && $this->grid->filter!==null && $this->name!==null && strpos($this->name,'.')===false)
100: {
101: if(is_array($this->filter))
102: return CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id'=>false,'prompt'=>''));
103: elseif($this->filter===null)
104: return CHtml::activeTextField($this->grid->filter, $this->name, array('id'=>false));
105: }
106: else
107: return parent::getFilterCellContent();
108: }
109:
110: /**
111: * Returns the header cell content.
112: * This method will render a link that can trigger the sorting if the column is sortable.
113: * @return string the header cell content.
114: * @since 1.1.16
115: */
116: public function getHeaderCellContent()
117: {
118: if($this->grid->enableSorting && $this->sortable && $this->name!==null)
119: return $this->grid->dataProvider->getSort()->link($this->name,$this->header,array('class'=>'sort-link'));
120: elseif($this->name!==null && $this->header===null)
121: {
122: if($this->grid->dataProvider instanceof CActiveDataProvider)
123: return CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));
124: else
125: return CHtml::encode($this->name);
126: }
127: else
128: return parent::getHeaderCellContent();
129: }
130:
131: /**
132: * Returns the data cell content.
133: * This method evaluates {@link value} or {@link name} and renders the result.
134: * @param integer $row the row number (zero-based)
135: * @return string the data cell content.
136: * @since 1.1.16
137: */
138: public function getDataCellContent($row)
139: {
140: $data=$this->grid->dataProvider->data[$row];
141: if($this->value!==null)
142: $value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
143: elseif($this->name!==null)
144: $value=CHtml::value($data,$this->name);
145: return $value===null ? $this->grid->nullDisplay : $this->grid->getFormatter()->format($value,$this->type);
146: }
147: }
148: