1: <?php
2: /**
3: * CLinkColumn 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: * CLinkColumn represents a grid view column that renders a hyperlink in each of its data cells.
15: *
16: * The {@link label} and {@link url} properties determine how each hyperlink will be rendered.
17: * The {@link labelExpression}, {@link urlExpression} properties may be used instead if they are available.
18: * In addition, if {@link imageUrl} is set, an image link will be rendered.
19: *
20: * @author Qiang Xue <qiang.xue@gmail.com>
21: * @package zii.widgets.grid
22: * @since 1.1
23: */
24: class CLinkColumn extends CGridColumn
25: {
26: /**
27: * @var string the label to the hyperlinks in the data cells. Note that the label will not
28: * be HTML-encoded when rendering. This property is ignored if {@link labelExpression} is set.
29: * @see labelExpression
30: */
31: public $label='Link';
32: /**
33: * @var string a PHP expression that will be evaluated for every data cell and whose result will be rendered
34: * as the label of the hyperlink of the data cell.
35: * In this expression, you can use the following variables:
36: * <ul>
37: * <li><code>$row</code> the row number (zero-based).</li>
38: * <li><code>$data</code> the data model for the row.</li>
39: * <li><code>$this</code> the column object.</li>
40: * </ul>
41: * The PHP expression will be evaluated using {@link evaluateExpression}.
42: *
43: * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
44: * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
45: */
46: public $labelExpression;
47: /**
48: * @var string the URL to the image. If this is set, an image link will be rendered.
49: */
50: public $imageUrl;
51: /**
52: * @var string the URL of the hyperlinks in the data cells.
53: * This property is ignored if {@link urlExpression} is set.
54: * @see urlExpression
55: */
56: public $url='javascript:void(0)';
57: /**
58: * @var string a PHP expression that will be evaluated for every data cell and whose result will be rendered
59: * as the URL of the hyperlink of the data cells.
60: * In this expression, you can use the following variables:
61: * <ul>
62: * <li><code>$row</code> the row number (zero-based).</li>
63: * <li><code>$data</code> the data model for the row.</li>
64: * <li><code>$this</code> the column object.</li>
65: * </ul>
66: * The PHP expression will be evaluated using {@link evaluateExpression}.
67: *
68: * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
69: * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
70: */
71: public $urlExpression;
72: /**
73: * @var array the HTML options for the data cell tags.
74: */
75: public $htmlOptions=array('class'=>'link-column');
76: /**
77: * @var array the HTML options for the header cell tag.
78: */
79: public $headerHtmlOptions=array('class'=>'link-column');
80: /**
81: * @var array the HTML options for the footer cell tag.
82: */
83: public $footerHtmlOptions=array('class'=>'link-column');
84: /**
85: * @var array the HTML options for the hyperlinks
86: */
87: public $linkHtmlOptions=array();
88:
89: /**
90: * Returns the data cell content.
91: * This method renders a hyperlink in the data cell.
92: * @param integer $row the row number (zero-based)
93: * @return string the data cell content.
94: * @since 1.1.16
95: */
96: public function getDataCellContent($row)
97: {
98: $data=$this->grid->dataProvider->data[$row];
99: if($this->urlExpression!==null)
100: $url=$this->evaluateExpression($this->urlExpression,array('data'=>$data,'row'=>$row));
101: else
102: $url=$this->url;
103: if($this->labelExpression!==null)
104: $label=$this->evaluateExpression($this->labelExpression,array('data'=>$data,'row'=>$row));
105: else
106: $label=$this->label;
107: $options=$this->linkHtmlOptions;
108: if(is_string($this->imageUrl))
109: return CHtml::link(CHtml::image($this->imageUrl,$label),$url,$options);
110: else
111: return CHtml::link($label,$url,$options);
112: }
113: }
114: