summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Web/UI/WebControls/TButtonColumn.php
blob: 304c8faa3941e34b839cd684d179f42c4317354d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
 * TButtonColumn class file
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Web.UI.WebControls
 */

/**
 * TDataGridColumn class file
 */
Prado::using('System.Web.UI.WebControls.TDataGridColumn');
Prado::using('System.Web.UI.WebControls.TButton');
Prado::using('System.Web.UI.WebControls.TLinkButton');
Prado::using('System.Web.UI.WebControls.TImageButton');

/**
 * TButtonColumn class
 *
 * TButtonColumn contains a user-defined command button, such as Add or Remove,
 * that corresponds with each row in the column.
 *
 * The caption of the buttons in the column is determined by {@link setText Text}
 * and {@link setDataTextField DataTextField} properties. If both are present,
 * the latter takes precedence. The {@link setDataTextField DataTextField} property
 * refers to the name of the field in datasource whose value will be used as the button caption.
 * If {@link setDataTextFormatString DataTextFormatString} is not empty,
 * the value will be formatted before rendering.
 *
 * The buttons in the column can be set to display as hyperlinks, push buttons or images
 * by setting the {@link setButtonType ButtonType} property.
 * The {@link setCommandName CommandName} will assign its value to
 * all button's <b>CommandName</b> property. The datagrid will capture
 * the command event where you can write event handlers based on different command names.
 * The buttons' <b>CausesValidation</b> and <b>ValidationGroup</b> property values
 * are determined by the column's corresponding properties.
 *
 * The buttons in the column can be accessed by one of the following two methods:
 * <code>
 * $datagridItem->ButtonColumnID->Button
 * $datagridItem->ButtonColumnID->Controls[0]
 * </code>
 * The second method is possible because the button control created within the
 * datagrid cell is the first child.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TButtonColumn extends TDataGridColumn
{
	/**
	 * @return string the text caption of the button
	 */
	public function getText()
	{
		return $this->getViewState('Text','');
	}

	/**
	 * Sets the text caption of the button.
	 * @param string the text caption to be set
	 */
	public function setText($value)
	{
		$this->setViewState('Text',$value,'');
	}

	/**
	 * @return string the field name from the data source to bind to the button caption
	 */
	public function getDataTextField()
	{
		return $this->getViewState('DataTextField','');
	}

	/**
	 * @param string the field name from the data source to bind to the button caption
	 */
	public function setDataTextField($value)
	{
		$this->setViewState('DataTextField',$value,'');
	}

	/**
	 * @return string the formatting string used to control how the button caption will be displayed.
	 */
	public function getDataTextFormatString()
	{
		return $this->getViewState('DataTextFormatString','');
	}

	/**
	 * @param string the formatting string used to control how the button caption will be displayed.
	 */
	public function setDataTextFormatString($value)
	{
		$this->setViewState('DataTextFormatString',$value,'');
	}

	/**
	 * @return string the URL of the image file for image buttons
	 */
	public function getImageUrl()
	{
		return $this->getViewState('ImageUrl','');
	}

	/**
	 * @param string the URL of the image file for image buttons
	 */
	public function setImageUrl($value)
	{
		$this->setViewState('ImageUrl',$value,'');
	}

	/**
	 * @return string the field name from the data source to bind to the button image url
	 */
	public function getDataImageUrlField()
	{
		return $this->getViewState('DataImageUrlField','');
	}

	/**
	 * @param string the field name from the data source to bind to the button image url
	 */
	public function setDataImageUrlField($value)
	{
		$this->setViewState('DataImageUrlField',$value,'');
	}

	/**
	 * @return string the formatting string used to control how the button image url will be displayed.
	 */
	public function getDataImageUrlFormatString()
	{
		return $this->getViewState('DataImageUrlFormatString','');
	}

	/**
	 * @param string the formatting string used to control how the button image url will be displayed.
	 */
	public function setDataImageUrlFormatString($value)
	{
		$this->setViewState('DataImageUrlFormatString',$value,'');
	}

	/**
	 * @return TButtonColumnType the type of command button. Defaults to TButtonColumnType::LinkButton.
	 */
	public function getButtonType()
	{
		return $this->getViewState('ButtonType',TButtonColumnType::LinkButton);
	}

	/**
	 * @param TButtonColumnType the type of command button
	 */
	public function setButtonType($value)
	{
		$this->setViewState('ButtonType',TPropertyValue::ensureEnum($value,'TButtonColumnType'),TButtonColumnType::LinkButton);
	}

	/**
	 * @return string the command name associated with the <b>OnCommand</b> event.
	 */
	public function getCommandName()
	{
		return $this->getViewState('CommandName','');
	}

	/**
	 * Sets the command name associated with the <b>Command</b> event.
	 * @param string the text caption to be set
	 */
	public function setCommandName($value)
	{
		$this->setViewState('CommandName',$value,'');
	}

	/**
	 * @return boolean whether postback event trigger by this button will cause input validation, default is true
	 */
	public function getCausesValidation()
	{
		return $this->getViewState('CausesValidation',true);
	}

	/**
	 * @param boolean whether postback event trigger by this button will cause input validation
	 */
	public function setCausesValidation($value)
	{
		$this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true);
	}

	/**
	 * @return string the group of validators which the button causes validation upon postback
	 */
	public function getValidationGroup()
	{
		return $this->getViewState('ValidationGroup','');
	}

	/**
	 * @param string the group of validators which the button causes validation upon postback
	 */
	public function setValidationGroup($value)
	{
		$this->setViewState('ValidationGroup',$value,'');
	}

	/**
	 * Initializes the specified cell to its initial values.
	 * This method overrides the parent implementation.
	 * It creates a command button within the cell.
	 * @param TTableCell the cell to be initialized.
	 * @param integer the index to the Columns property that the cell resides in.
	 * @param string the type of cell (Header,Footer,Item,AlternatingItem,EditItem,SelectedItem)
	 */
	public function initializeCell($cell,$columnIndex,$itemType)
	{
		if($itemType===TListItemType::Item || $itemType===TListItemType::AlternatingItem || $itemType===TListItemType::SelectedItem || $itemType===TListItemType::EditItem)
		{
			$buttonType=$this->getButtonType();
			if($buttonType===TButtonColumnType::LinkButton)
				$button=new TLinkButton;
			else if($buttonType===TButtonColumnType::PushButton)
				$button=new TButton;
			else // image button
			{
				$button=new TImageButton;
				$button->setImageUrl($this->getImageUrl());
				$button->setToolTip($this->getText());
			}
			$button->setText($this->getText());
			$button->setCommandName($this->getCommandName());
			$button->setCausesValidation($this->getCausesValidation());
			$button->setValidationGroup($this->getValidationGroup());
			if($this->getDataTextField()!=='' || ($buttonType===TButtonColumnType::ImageButton && $this->getDataImageUrlField()!==''))
				$button->attachEventHandler('OnDataBinding',array($this,'dataBindColumn'));
			$cell->getControls()->add($button);
			$cell->registerObject('Button',$button);
		}
		else
			parent::initializeCell($cell,$columnIndex,$itemType);
	}

	/**
	 * Databinds a cell in the column.
	 * This method is invoked when datagrid performs databinding.
	 * It populates the content of the cell with the relevant data from data source.
	 */
	public function dataBindColumn($sender,$param)
	{
		if($sender instanceof IButtonControl)
		{
			if(($field=$this->getDataTextField())!=='')
			{
				$value=$this->getDataFieldValue($sender->getNamingContainer()->getData(),$field);
				$text=$this->formatDataValue($this->getDataTextFormatString(),$value);
				$sender->setText($text);
			}
			if(($sender instanceof TImageButton) && ($field=$this->getDataImageUrlField())!=='')
			{
				$value=$this->getDataFieldValue($sender->getNamingContainer()->getData(),$field);
				$url=$this->formatDataValue($this->getDataImageUrlFormatString(),$value);
				$sender->setImageUrl($url);
			}
		}
	}
}