1: <?php
2: /**
3: * CGridColumn 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: /**
12: * CGridColumn is the base class for all grid view column classes.
13: *
14: * A CGridColumn object represents the specification for rendering the cells in
15: * a particular grid view column.
16: *
17: * In a column, there is one header cell, multiple data cells, and an optional footer cell.
18: * Child classes may override {@link renderHeaderCellContent}, {@link renderDataCellContent}
19: * and {@link renderFooterCellContent} to customize how these cells are rendered.
20: *
21: * @property boolean $hasFooter Whether this column has a footer cell.
22: * This is determined based on whether {@link footer} is set.
23: * @property string $filterCellContent The filter cell content.
24: * @property string $headerCellContent The header cell content.
25: * @property string $footerCellContent The footer cell content.
26: *
27: * @author Qiang Xue <qiang.xue@gmail.com>
28: * @package zii.widgets.grid
29: * @since 1.1
30: */
31: abstract class CGridColumn extends CComponent
32: {
33: /**
34: * @var string the ID of this column. This value should be unique among all grid view columns.
35: * If this is not set, it will be assigned one automatically.
36: */
37: public $id;
38: /**
39: * @var CGridView the grid view object that owns this column.
40: */
41: public $grid;
42: /**
43: * @var string the header cell text. Note that it will not be HTML-encoded.
44: */
45: public $header;
46: /**
47: * @var string the footer cell text. Note that it will not be HTML-encoded.
48: */
49: public $footer;
50: /**
51: * @var boolean whether this column is visible. Defaults to true.
52: */
53: public $visible=true;
54: /**
55: * @var string a PHP expression that is evaluated for every data cell and whose result
56: * is used as the CSS class name for the data cell. 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: */
67: public $cssClassExpression;
68: /**
69: * @var array the HTML options for the data cell tags.
70: */
71: public $htmlOptions=array();
72: /**
73: * @var array the HTML options for the filter cell tag.
74: */
75: public $filterHtmlOptions=array();
76: /**
77: * @var array the HTML options for the header cell tag.
78: */
79: public $headerHtmlOptions=array();
80: /**
81: * @var array the HTML options for the footer cell tag.
82: */
83: public $footerHtmlOptions=array();
84:
85: /**
86: * Constructor.
87: * @param CGridView $grid the grid view that owns this column.
88: */
89: public function __construct($grid)
90: {
91: $this->grid=$grid;
92: }
93:
94: /**
95: * Initializes the column.
96: * This method is invoked by the grid view when it initializes itself before rendering.
97: * You may override this method to prepare the column for rendering.
98: */
99: public function init()
100: {
101: }
102:
103: /**
104: * @return boolean whether this column has a footer cell.
105: * This is determined based on whether {@link footer} is set.
106: */
107: public function getHasFooter()
108: {
109: return $this->footer!==null;
110: }
111:
112: /**
113: * Renders the filter cell.
114: * @since 1.1.1
115: */
116: public function renderFilterCell()
117: {
118: echo CHtml::openTag('td',$this->filterHtmlOptions);
119: $this->renderFilterCellContent();
120: echo "</td>";
121: }
122:
123: /**
124: * Renders the header cell.
125: */
126: public function renderHeaderCell()
127: {
128: $this->headerHtmlOptions['id']=$this->id;
129: echo CHtml::openTag('th',$this->headerHtmlOptions);
130: $this->renderHeaderCellContent();
131: echo "</th>";
132: }
133:
134: /**
135: * Renders a data cell.
136: * @param integer $row the row number (zero-based)
137: */
138: public function renderDataCell($row)
139: {
140: $data=$this->grid->dataProvider->data[$row];
141: $options=$this->htmlOptions;
142: if($this->cssClassExpression!==null)
143: {
144: $class=$this->evaluateExpression($this->cssClassExpression,array('row'=>$row,'data'=>$data));
145: if(!empty($class))
146: {
147: if(isset($options['class']))
148: $options['class'].=' '.$class;
149: else
150: $options['class']=$class;
151: }
152: }
153: echo CHtml::openTag('td',$options);
154: $this->renderDataCellContent($row,$data);
155: echo '</td>';
156: }
157:
158: /**
159: * Renders the footer cell.
160: */
161: public function renderFooterCell()
162: {
163: echo CHtml::openTag('td',$this->footerHtmlOptions);
164: $this->renderFooterCellContent();
165: echo '</td>';
166: }
167:
168: /**
169: * Returns the header cell content.
170: * The default implementation simply returns {@link header}.
171: * This method may be overridden to customize the rendering of the header cell.
172: * @return string the header cell content.
173: * @since 1.1.16
174: */
175: public function getHeaderCellContent()
176: {
177: return trim($this->header)!=='' ? $this->header : $this->grid->blankDisplay;
178: }
179:
180: /**
181: * Renders the header cell content.
182: * @deprecated since 1.1.16. Use {@link getHeaderCellContent()} instead.
183: */
184: protected function renderHeaderCellContent()
185: {
186: echo $this->getHeaderCellContent();
187: }
188:
189: /**
190: * Returns the footer cell content.
191: * The default implementation simply returns {@link footer}.
192: * This method may be overridden to customize the rendering of the footer cell.
193: * @return string the footer cell content.
194: * @since 1.1.16
195: */
196: public function getFooterCellContent()
197: {
198: return trim($this->footer)!=='' ? $this->footer : $this->grid->blankDisplay;
199: }
200:
201: /**
202: * Renders the footer cell content.
203: * @deprecated since 1.1.16. Use {@link getFooterCellContent()} instead.
204: */
205: protected function renderFooterCellContent()
206: {
207: echo $this->getFooterCellContent();
208: }
209:
210: /**
211: * Returns the data cell content.
212: * This method SHOULD be overridden to customize the rendering of the data cell.
213: * @param integer $row the row number (zero-based)
214: * The data for this row is available via <code>$this->grid->dataProvider->data[$row];</code>
215: * @return string the data cell content.
216: * @since 1.1.16
217: */
218: public function getDataCellContent($row)
219: {
220: return $this->grid->blankDisplay;
221: }
222:
223: /**
224: * Renders the data cell content.
225: * @param integer $row the row number (zero-based)
226: * @param mixed $data the data associated with the row
227: * @deprecated since 1.1.16. Use {@link getDataCellContent()} instead.
228: */
229: protected function renderDataCellContent($row,$data)
230: {
231: echo $this->getDataCellContent($row);
232: }
233:
234: /**
235: * Returns the filter cell content.
236: * The default implementation simply returns an empty column.
237: * This method may be overridden to customize the rendering of the filter cell (if any).
238: * @return string the filter cell content.
239: * @since 1.1.16
240: */
241: public function getFilterCellContent()
242: {
243: return $this->grid->blankDisplay;
244: }
245:
246: /**
247: * Renders the filter cell content.
248: * @since 1.1.1
249: * @deprecated since 1.1.16. Use {@link getFilterCellContent()} instead.
250: */
251: protected function renderFilterCellContent()
252: {
253: echo $this->getFilterCellContent();
254: }
255: }
256: