summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TTemplateColumn.php
blob: ec4f191dfb388db7ed54ff135d2c2bdb84b73f99 (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
<?php
/**
 * TTemplateColumn class file
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Revision: $  $Date: $
 * @package System.Web.UI.WebControls
 */

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

/**
 * TTemplateColumn class
 *
 * TTemplateColumn customizes the layout of controls in the column with templates.
 * In particular, you can specify <b>ItemTemplate</b>, <b>EditItemTemplate</b>
 * <b>HeaderTemplate</b> and <b>FooterTemplate</b> to customize specific
 * type of cells in the column.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @version $Revision: $  $Date: $
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TTemplateColumn extends TDataGridColumn
{
	/**
	 * Number of seconds that a cached template will expire after
	 */
	const CACHE_EXPIRY=18000;
	/**
	 * Various item templates
	 * @var string
	 */
	private $_itemTemplate='';
	private $_editItemTemplate='';
	private $_headerTemplate='';
	private $_footerTemplate='';
	private static $_templates=array();

	/**
	 * @return string the edit item template string
	 */
	public function getEditItemTemplate()
	{
		return $this->_editItemTemplate;
	}

	/**
	 * Sets the edit item template string
	 * @param string the edit item template
	 */
	public function setEditItemTemplate($value)
	{
		$this->_editItemTemplate=$value;
	}

	/**
	 * @return string the template string for the item
	 */
	public function getItemTemplate()
	{
		return $this->_itemTemplate;
	}

	/**
	 * Sets the template string for the item
	 * @param string the item template
	 */
	public function setItemTemplate($value)
	{
		$this->_itemTemplate=$value;
	}

	/**
	 * @return string the header template string
	 */
	public function getHeaderTemplate()
	{
		return $this->_headerTemplate;
	}

	/**
	 * Sets the header template.
	 * The template will be parsed immediately.
	 * @param string the header template
	 */
	public function setHeaderTemplate($value)
	{
		$this->_headerTemplate=$value;
	}

	/**
	 * @return string the footer template string
	 */
	public function getFooterTemplate()
	{
		return $this->_footerTemplate;
	}

	/**
	 * Sets the footer template.
	 * The template will be parsed immediately.
	 * @param string the footer template
	 */
	public function setFooterTemplate($value)
	{
		$this->_footerTemplate=$value;
	}

	/**
	 * Initializes the specified cell to its initial values.
	 * This method overrides the parent implementation.
	 * It initializes the cell based on different templates
	 * (ItemTemplate, EditItemTemplate, HeaderTemplate, FooterTemplate).
	 * @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)
	{
		parent::initializeCell($cell,$columnIndex,$itemType);
		$tplContent='';
		switch($itemType)
		{
			case 'Header':
				$tplContent=$this->_headerTemplate;
				break;
			case 'Footer':
				$tplContent=$this->_footerTemplate;
				break;
			case 'Item':
			case 'AlternatingItem':
			case 'SelectedItem':
				$tplContent=$this->_itemTemplate;
				break;
			case 'EditItem':
				$tplContent=$this->_editItemTemplate===''?$this->_itemTemplate:$this->_editItemTemplate;
				break;
		}
		if($tplContent!=='')
		{
			$cell->setText('');
			$cell->getControls()->clear();
			$this->createTemplate($tplContent)->instantiateIn($cell);
		}
	}

	/**
	 * Parses item template.
	 * This method uses caching technique to accelerate template parsing.
	 * @param string template string
	 * @return ITemplate parsed template object
	 */
	protected function createTemplate($str)
	{
		$key=md5($str);
		if(isset(self::$_templates[$key]))
			return self::$_templates[$key];
		else
		{
			$contextPath=$this->getOwner()->getTemplateControl()->getTemplate()->getContextPath();
			if(($cache=$this->getApplication()->getCache())!==null)
			{
				if(($template=$cache->get($key))===null)
				{
					$template=new TTemplate($str,$contextPath);
					$cache->set($key,$template,self::CACHE_EXPIRY);
				}
			}
			else
				$template=new TTemplate($str,$contextPath);
			self::$_templates[$key]=$template;
			return $template;
		}
	}
}

?>