Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TTemplateColumn class file
4
 *
5
 * @author Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TTemplateColumn.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * TDataGridColumn class file
15
 */
16
Prado::using('System.Web.UI.WebControls.TDataGridColumn');
17
 
18
/**
19
 * TTemplateColumn class
20
 *
21
 * TTemplateColumn customizes the layout of controls in the column with templates.
22
 * In particular, you can specify {@link setItemTemplate ItemTemplate},
23
 * {@link setEditItemTemplate EditItemTemplate}, {@link setHeaderTemplate HeaderTemplate}
24
 * and {@link setFooterTemplate FooterTemplate} to customize specific
25
 * type of cells in the column.
26
 *
27
 * Since v3.1.0, TTemplateColumn has introduced two new properties {@link setItemRenderer ItemRenderer}
28
 * and {@link setEditItemRenderer EditItemRenderer} which can be used to specify
29
 * the layout of the datagrid cells in browsing and editing mode.
30
 * A renderer refers to a control class that is to be instantiated as a control.
31
 * For more details, see {@link TRepeater} and {@link TDataList}.
32
 *
33
 * When a renderer and a template are both defined for a type of item, the former
34
 * takes precedence.
35
 *
36
 * @author Qiang Xue <qiang.xue@gmail.com>
37
 * @version $Id: TTemplateColumn.php 2541 2008-10-21 15:05:13Z qiang.xue $
38
 * @package System.Web.UI.WebControls
39
 * @since 3.0
40
 */
41
class TTemplateColumn extends TDataGridColumn
42
{
43
	/**
44
	 * Various item templates
45
	 * @var string
46
	 */
47
	private $_itemTemplate=null;
48
	private $_editItemTemplate=null;
49
	private $_headerTemplate=null;
50
	private $_footerTemplate=null;
51
 
52
	/**
53
	 * @return string the class name for the item cell renderer. Defaults to empty, meaning not set.
54
	 * @since 3.1.0
55
	 */
56
	public function getItemRenderer()
57
	{
58
		return $this->getViewState('ItemRenderer','');
59
	}
60
 
61
	/**
62
	 * Sets the item cell renderer class.
63
	 *
64
	 * If not empty, the class will be used to instantiate as a child control in the item cells of the column.
65
	 *
66
	 * If the class implements {@link IDataRenderer}, the <b>Data</b> property
67
	 * will be set as the row of the data associated with the datagrid item that this cell resides in.
68
	 *
69
	 * @param string the renderer class name in namespace format.
70
	 * @since 3.1.0
71
	 */
72
	public function setItemRenderer($value)
73
	{
74
		$this->setViewState('ItemRenderer',$value,'');
75
	}
76
 
77
	/**
78
	 * @return string the class name for the edit item cell renderer. Defaults to empty, meaning not set.
79
	 * @since 3.1.0
80
	 */
81
	public function getEditItemRenderer()
82
	{
83
		return $this->getViewState('EditItemRenderer','');
84
	}
85
 
86
	/**
87
	 * Sets the edit item cell renderer class.
88
	 *
89
	 * If not empty, the class will be used to instantiate as a child control in the item cell that is in edit mode.
90
	 *
91
	 * If the class implements {@link IDataRenderer}, the <b>Data</b> property
92
	 * will be set as the row of the data associated with the datagrid item that this cell resides in.
93
	 *
94
	 * @param string the renderer class name in namespace format.
95
	 * @since 3.1.0
96
	 */
97
	public function setEditItemRenderer($value)
98
	{
99
		$this->setViewState('EditItemRenderer',$value,'');
100
	}
101
 
102
	/**
103
	 * @return ITemplate the edit item template
104
	 */
105
	public function getEditItemTemplate()
106
	{
107
		return $this->_editItemTemplate;
108
	}
109
 
110
	/**
111
	 * @param ITemplate the edit item template
112
	 * @throws TInvalidDataTypeException if the input is not an {@link ITemplate} or not null.
113
	 */
114
	public function setEditItemTemplate($value)
115
	{
116
		if($value instanceof ITemplate || $value===null)
117
			$this->_editItemTemplate=$value;
118
		else
119
			throw new TInvalidDataTypeException('templatecolumn_template_required','EditItemTemplate');
120
	}
121
 
122
	/**
123
	 * @return ITemplate the item template
124
	 */
125
	public function getItemTemplate()
126
	{
127
		return $this->_itemTemplate;
128
	}
129
 
130
	/**
131
	 * @param ITemplate the item template
132
	 * @throws TInvalidDataTypeException if the input is not an {@link ITemplate} or not null.
133
	 */
134
	public function setItemTemplate($value)
135
	{
136
		if($value instanceof ITemplate || $value===null)
137
			$this->_itemTemplate=$value;
138
		else
139
			throw new TInvalidDataTypeException('templatecolumn_template_required','ItemTemplate');
140
	}
141
 
142
	/**
143
	 * @return ITemplate the header template
144
	 */
145
	public function getHeaderTemplate()
146
	{
147
		return $this->_headerTemplate;
148
	}
149
 
150
	/**
151
	 * @param ITemplate the header template.
152
	 * @throws TInvalidDataTypeException if the input is not an {@link ITemplate} or not null.
153
	 */
154
	public function setHeaderTemplate($value)
155
	{
156
		if($value instanceof ITemplate || $value===null)
157
			$this->_headerTemplate=$value;
158
		else
159
			throw new TInvalidDataTypeException('templatecolumn_template_required','HeaderTemplate');
160
	}
161
 
162
	/**
163
	 * @return ITemplate the footer template
164
	 */
165
	public function getFooterTemplate()
166
	{
167
		return $this->_footerTemplate;
168
	}
169
 
170
	/**
171
	 * @param ITemplate the footer template
172
	 * @throws TInvalidDataTypeException if the input is not an {@link ITemplate} or not null.
173
	 */
174
	public function setFooterTemplate($value)
175
	{
176
		if($value instanceof ITemplate || $value===null)
177
			$this->_footerTemplate=$value;
178
		else
179
			throw new TInvalidDataTypeException('templatecolumn_template_required','FooterTemplate');
180
	}
181
 
182
	/**
183
	 * Initializes the specified cell to its initial values.
184
	 * This method overrides the parent implementation.
185
	 * It initializes the cell based on different templates
186
	 * (ItemTemplate, EditItemTemplate, HeaderTemplate, FooterTemplate).
187
	 * @param TTableCell the cell to be initialized.
188
	 * @param integer the index to the Columns property that the cell resides in.
189
	 * @param string the type of cell (Header,Footer,Item,AlternatingItem,EditItem,SelectedItem)
190
	 */
191
	public function initializeCell($cell,$columnIndex,$itemType)
192
	{
193
		if($itemType===TListItemType::Item || $itemType===TListItemType::AlternatingItem || $itemType===TListItemType::SelectedItem || $itemType===TListItemType::EditItem)
194
		{
195
			if($itemType===TListItemType::EditItem)
196
			{
197
				if(($classPath=$this->getEditItemRenderer())==='' && ($template=$this->_editItemTemplate)===null)
198
				{
199
					$classPath=$this->getItemRenderer();
200
					$template=$this->_itemTemplate;
201
				}
202
			}
203
			else
204
			{
205
				$template=$this->_itemTemplate;
206
				$classPath=$this->getItemRenderer();
207
			}
208
			if($classPath!=='')
209
			{
210
				$control=Prado::createComponent($classPath);
211
				$cell->getControls()->add($control);
212
				if($control instanceof IItemDataRenderer)
213
				{
214
					$control->setItemIndex($cell->getParent()->getItemIndex());
215
					$control->setItemType($itemType);
216
				}
217
				if($control instanceof IDataRenderer)
218
					$control->attachEventHandler('OnDataBinding',array($this,'dataBindColumn'));
219
			}
220
			else if($template!==null)
221
				$template->instantiateIn($cell);
222
			else if($itemType!==TListItemType::EditItem)
223
				$cell->setText('&nbsp;');
224
		}
225
		else if($itemType===TListItemType::Header)
226
		{
227
			if(($classPath=$this->getHeaderRenderer())!=='')
228
				$this->initializeHeaderCell($cell,$columnIndex);
229
			else if($this->_headerTemplate!==null)
230
				$this->_headerTemplate->instantiateIn($cell);
231
			else
232
				$this->initializeHeaderCell($cell,$columnIndex);
233
		}
234
		else if($itemType===TListItemType::Footer)
235
		{
236
			if(($classPath=$this->getFooterRenderer())!=='')
237
				$this->initializeFooterCell($cell,$columnIndex);
238
			else if($this->_footerTemplate!==null)
239
				$this->_footerTemplate->instantiateIn($cell);
240
			else
241
				$this->initializeFooterCell($cell,$columnIndex);
242
		}
243
	}
244
 
245
	/**
246
	 * Databinds a cell in the column.
247
	 * This method is invoked when datagrid performs databinding.
248
	 * It populates the content of the cell with the relevant data from data source.
249
	 */
250
	public function dataBindColumn($sender,$param)
251
	{
252
		$item=$sender->getNamingContainer();
253
		$sender->setData($item->getData());
254
	}
255
}
256