Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TTemplateControl 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: TTemplateControl.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI
11
 */
12
 
13
/**
14
 * Includes TCompositeControl class
15
 */
16
Prado::using('System.Web.UI.TCompositeControl');
17
 
18
/**
19
 * TTemplateControl class.
20
 * TTemplateControl is the base class for all controls that use templates.
21
 * By default, a control template is assumed to be in a file under the same
22
 * directory with the control class file. They have the same file name and
23
 * different extension name. For template file, the extension name is ".tpl".
24
 *
25
 * @author Qiang Xue <qiang.xue@gmail.com>
26
 * @version $Id: TTemplateControl.php 2541 2008-10-21 15:05:13Z qiang.xue $
27
 * @package System.Web.UI
28
 * @since 3.0
29
 */
30
class TTemplateControl extends TCompositeControl
31
{
32
	/**
33
	 * template file extension.
34
	 */
35
	const EXT_TEMPLATE='.tpl';
36
 
37
	/**
38
	 * @var ITemplate the parsed template structure shared by the same control class
39
	 */
40
	private static $_template=array();
41
	/**
42
	 * @var ITemplate the parsed template structure specific for this control instance
43
	 */
44
	private $_localTemplate=null;
45
	/**
46
	 * @var TTemplateControl the master control if any
47
	 */
48
	private $_master=null;
49
	/**
50
	 * @var string master control class name
51
	 */
52
	private $_masterClass='';
53
	/**
54
	 * @var array list of TContent controls
55
	 */
56
	private $_contents=array();
57
	/**
58
	 * @var array list of TContentPlaceHolder controls
59
	 */
60
	private $_placeholders=array();
61
 
62
	/**
63
	 * Returns the template object associated with this control object.
64
	 * @return ITemplate|null the parsed template, null if none
65
	 */
66
	public function getTemplate()
67
	{
68
		if($this->_localTemplate===null)
69
		{
70
			$class=get_class($this);
71
			if(!isset(self::$_template[$class]))
72
				self::$_template[$class]=$this->loadTemplate();
73
			return self::$_template[$class];
74
		}
75
		else
76
			return $this->_localTemplate;
77
	}
78
 
79
	/**
80
	 * Sets the parsed template.
81
	 * Note, the template will be applied to the whole control class.
82
	 * This method should only be used by framework and control developers.
83
	 * @param ITemplate the parsed template
84
	 */
85
	public function setTemplate($value)
86
	{
87
		$this->_localTemplate=$value;
88
	}
89
 
90
	/**
91
	 * @return boolean whether this control is a source template control.
92
	 * A source template control loads its template from external storage,
93
	 * such as file, db, rather than from within another template.
94
	 */
95
	public function getIsSourceTemplateControl()
96
	{
97
		if(($template=$this->getTemplate())!==null)
98
			return $template->getIsSourceTemplate();
99
		else
100
			return false;
101
	}
102
 
103
	/**
104
	 * @return string the directory containing the template. Empty if no template available.
105
	 */
106
	public function getTemplateDirectory()
107
	{
108
		if(($template=$this->getTemplate())!==null)
109
			return $template->getContextPath();
110
		else
111
			return '';
112
	}
113
 
114
	/**
115
	 * Loads the template associated with this control class.
116
	 * @return ITemplate the parsed template structure
117
	 */
118
	protected function loadTemplate()
119
	{
120
		Prado::trace("Loading template ".get_class($this),'System.Web.UI.TTemplateControl');
121
		$template=$this->getService()->getTemplateManager()->getTemplateByClassName(get_class($this));
122
		return $template;
123
	}
124
 
125
	/**
126
	 * Creates child controls.
127
	 * This method is overridden to load and instantiate control template.
128
	 * This method should only be used by framework and control developers.
129
	 */
130
	public function createChildControls()
131
	{
132
		if($tpl=$this->getTemplate())
133
		{
134
			foreach($tpl->getDirective() as $name=>$value)
135
			{
136
				if(is_string($value))
137
					$this->setSubProperty($name,$value);
138
				else
139
					throw new TConfigurationException('templatecontrol_directive_invalid',get_class($this),$name);
140
			}
141
			$tpl->instantiateIn($this);
142
		}
143
	}
144
 
145
	/**
146
	 * Registers a content control.
147
	 * @param string ID of the content
148
	 * @param TContent
149
	 */
150
	public function registerContent($id,TContent $object)
151
	{
152
		if(isset($this->_contents[$id]))
153
			throw new TConfigurationException('templatecontrol_contentid_duplicated',$id);
154
		else
155
			$this->_contents[$id]=$object;
156
	}
157
 
158
	/**
159
	 * Registers a content placeholder to this template control.
160
	 * This method should only be used by framework and control developers.
161
	 * @param string placeholder ID
162
	 * @param TContentPlaceHolder placeholder control
163
	 */
164
	public function registerContentPlaceHolder($id,TContentPlaceHolder $object)
165
	{
166
		if(isset($this->_placeholders[$id]))
167
			throw new TConfigurationException('templatecontrol_placeholderid_duplicated',$id);
168
		else
169
			$this->_placeholders[$id]=$object;
170
	}
171
 
172
	/**
173
	 * @return string master class name (in namespace form)
174
	 */
175
	public function getMasterClass()
176
	{
177
		return $this->_masterClass;
178
	}
179
 
180
	/**
181
	 * @param string  master control class name (in namespace form)
182
	 */
183
	public function setMasterClass($value)
184
	{
185
		$this->_masterClass=$value;
186
	}
187
 
188
	/**
189
	 * @return TTemplateControl|null master control associated with this control, null if none
190
	 */
191
	public function getMaster()
192
	{
193
		return $this->_master;
194
	}
195
 
196
	/**
197
	 * Injects all content controls (and their children) to the corresponding content placeholders.
198
	 * This method should only be used by framework and control developers.
199
	 * @param string ID of the content control
200
	 * @param TContent the content to be injected
201
	 */
202
	public function injectContent($id,$content)
203
	{
204
		if(isset($this->_placeholders[$id]))
205
		{
206
			$placeholder=$this->_placeholders[$id];
207
			$controls=$placeholder->getParent()->getControls();
208
			$loc=$controls->remove($placeholder);
209
			$controls->insertAt($loc,$content);
210
		}
211
		else
212
			throw new TConfigurationException('templatecontrol_placeholder_inexistent',$id);
213
	}
214
 
215
	/**
216
	 * Performs the OnInit step for the control and all its child controls.
217
	 * This method overrides the parent implementation
218
	 * by ensuring child controls are created first,
219
	 * and if master class is set, master will be applied.
220
	 * Only framework developers should use this method.
221
	 * @param TControl the naming container control
222
	 */
223
	protected function initRecursive($namingContainer=null)
224
	{
225
		$this->ensureChildControls();
226
		if($this->_masterClass!=='')
227
		{
228
			$master=Prado::createComponent($this->_masterClass);
229
			if(!($master instanceof TTemplateControl))
230
				throw new TInvalidDataValueException('templatecontrol_mastercontrol_invalid');
231
			$this->_master=$master;
232
			$this->getControls()->clear();
233
			$this->getControls()->add($master);
234
			$master->ensureChildControls();
235
			foreach($this->_contents as $id=>$content)
236
				$master->injectContent($id,$content);
237
		}
238
		else if(!empty($this->_contents))
239
			throw new TConfigurationException('templatecontrol_mastercontrol_required',get_class($this));
240
		parent::initRecursive($namingContainer);
241
	}
242
}
243