~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/framework/zii/widgets/grid/CDataColumn.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CDataColumn class file.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 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 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
 * @version $Id: CDataColumn.php 2799 2011-01-01 19:31:13Z qiang.xue $
 
25
 * @package zii.widgets.grid
 
26
 * @since 1.1
 
27
 */
 
28
class CDataColumn extends CGridColumn
 
29
{
 
30
        /**
 
31
         * @var string the attribute name of the data model. The corresponding attribute value will be rendered
 
32
         * in each data cell. If {@link value} is specified, this property will be ignored
 
33
         * unless the column needs to be sortable.
 
34
         * @see value
 
35
         * @see sortable
 
36
         */
 
37
        public $name;
 
38
        /**
 
39
         * @var string a PHP expression that will be evaluated for every data cell and whose result will be rendered
 
40
         * as the content of the data cells. In this expression, the variable
 
41
         * <code>$row</code> the row number (zero-based); <code>$data</code> the data model for the row;
 
42
         * and <code>$this</code> the column object.
 
43
         */
 
44
        public $value;
 
45
        /**
 
46
         * @var string the type of the attribute value. This determines how the attribute value is formatted for display.
 
47
         * Valid values include those recognizable by {@link CGridView::formatter}, such as: raw, text, ntext, html, date, time,
 
48
         * datetime, boolean, number, email, image, url. For more details, please refer to {@link CFormatter}.
 
49
         * Defaults to 'text' which means the attribute value will be HTML-encoded.
 
50
         */
 
51
        public $type='text';
 
52
        /**
 
53
         * @var boolean whether the column is sortable. If so, the header cell will contain a link that may trigger the sorting.
 
54
         * Defaults to true. Note that if {@link name} is not set, or if {@link name} is not allowed by {@link CSort},
 
55
         * this property will be treated as false.
 
56
         * @see name
 
57
         */
 
58
        public $sortable=true;
 
59
        /**
 
60
         * @var mixed the HTML code representing a filter input (e.g. a text field, a dropdown list)
 
61
         * that is used for this data column. This property is effective only when
 
62
         * {@link CGridView::enableFiltering} is set true.
 
63
         * If this property is not set, a text field will be generated as the filter input;
 
64
         * If this property is an array, a dropdown list will be generated that uses this property value as
 
65
         * the list options.
 
66
         * @since 1.1.1
 
67
         */
 
68
        public $filter;
 
69
 
 
70
        /**
 
71
         * Initializes the column.
 
72
         */
 
73
        public function init()
 
74
        {
 
75
                parent::init();
 
76
                if($this->name===null)
 
77
                        $this->sortable=false;
 
78
                if($this->name===null && $this->value===null)
 
79
                        throw new CException(Yii::t('zii','Either "name" or "value" must be specified for CDataColumn.'));
 
80
        }
 
81
 
 
82
        /**
 
83
         * Renders the filter cell content.
 
84
         * This method will render the {@link filter} as is if it is a string.
 
85
         * If {@link filter} is an array, it is assumed to be a list of options, and a dropdown selector will be rendered.
 
86
         * Otherwise if {@link filter} is not false, a text field is rendered.
 
87
         * @since 1.1.1
 
88
         */
 
89
        protected function renderFilterCellContent()
 
90
        {
 
91
                if($this->filter!==false && $this->grid->filter!==null && $this->name!==null && strpos($this->name,'.')===false)
 
92
                {
 
93
                        if(is_array($this->filter))
 
94
                                echo CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id'=>false,'prompt'=>''));
 
95
                        else if($this->filter===null)
 
96
                                echo CHtml::activeTextField($this->grid->filter, $this->name, array('id'=>false));
 
97
                        else
 
98
                                echo $this->filter;
 
99
                }
 
100
                else
 
101
                        parent::renderFilterCellContent();
 
102
        }
 
103
 
 
104
        /**
 
105
         * Renders the header cell content.
 
106
         * This method will render a link that can trigger the sorting if the column is sortable.
 
107
         */
 
108
        protected function renderHeaderCellContent()
 
109
        {
 
110
                if($this->grid->enableSorting && $this->sortable && $this->name!==null)
 
111
                        echo $this->grid->dataProvider->getSort()->link($this->name,$this->header);
 
112
                else if($this->name!==null && $this->header===null)
 
113
                {
 
114
                        if($this->grid->dataProvider instanceof CActiveDataProvider)
 
115
                                echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));
 
116
                        else
 
117
                                echo CHtml::encode($this->name);
 
118
                }
 
119
                else
 
120
                        parent::renderHeaderCellContent();
 
121
        }
 
122
 
 
123
        /**
 
124
         * Renders the data cell content.
 
125
         * This method evaluates {@link value} or {@link name} and renders the result.
 
126
         * @param integer $row the row number (zero-based)
 
127
         * @param mixed $data the data associated with the row
 
128
         */
 
129
        protected function renderDataCellContent($row,$data)
 
130
        {
 
131
                if($this->value!==null)
 
132
                        $value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
 
133
                else if($this->name!==null)
 
134
                        $value=CHtml::value($data,$this->name);
 
135
                echo $value===null ? $this->grid->nullDisplay : $this->grid->getFormatter()->format($value,$this->type);
 
136
        }
 
137
}