summaryrefslogtreecommitdiff
path: root/framework/Web/UI/TTemplateControlInheritable.php
blob: d22d6421b85fcfca554d255fa2065fa4f1743c08 (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
<?php

/**
 * TTemplateControlInheritable class file.
 *
 * @author Schlaue-Kids.net <info@schlaue-kids.net>
 * @link http://www.schlaue-kids.net/
 * @copyright Copyright &copy; 2010 Schlaue-Kids.net
 * @license http://www.pradosoft.com/license/
 * @package Prado\Web\UI
 */

namespace Prado\Web\UI;
use Prado\Exceptions\TConfigurationException;

/**
 * TTemplateControlInheritable class.
 * TTemplateControlInheritable is an extension to the base class for all controls that use templates.
 * By default, a control template is assumed to be in a file under the same
 * directory with the control class file. They have the same file name and
 * different extension name. For template file, the extension name is ".tpl".
 * If a TTemplateControlInheritable is inherited it uses the base class template unless the
 * inheriting control defines an own.
 *
 * @author Schlaue-Kids.net <info@schlaue-kids.net>
 * @author Kyle Caine <http://www.pradosoft.com/forum/index.php?action=profile;u=1752>
 * @package Prado\Web\UI
 * @since 3.1.8
 */
class TTemplateControlInheritable extends TTemplateControl
{
	// methods

	/**
	 * Creates child controls.
	 * This method is overridden to load and instantiate control template.
	 * This method should only be used by framework and control developers.
	 * Uses the controls template if available or the base class template otherwise.
	 *
	 * @return void
	 * @throws TConfigurationException if a template control directive is invalid
	 */	
	public function createChildControls()
	{
		if(null === ($_template = $this->getTemplate())) {
			return $this->doCreateChildControlsFor(get_class($this));
		}

		foreach($_template->getDirective() as $_name => $_value) {
			if(!is_string($_value)) {
				throw new TConfigurationException('templatecontrol_directive_invalid', get_class($this), $name);
			}
			
			$this->setSubProperty($_name, $_value);
		}

		$_template->instantiateIn($this);
	}

	/**
	 * This method creates the cild controls for the given class
	 *
	 * @param string $parentClass The class to generate the child controls for
	 * @return void
	 */
	public function doCreateChildControlsFor($parentClass)
	{
		if(false !== ($_parentClass = get_parent_class($parentClass)) && 'TTemplateControl' != $_parentClass) {
			$this->doCreateChildControlsFor($_parentClass);
		}

		$this->doTemplateForClass($parentClass);
	}

	/**
	 * This method creates the template object for the given class
	 *
	 * @param string $p_class The class to create the template from
	 * @return void
	 * @throws TConfigurationException if a template control directive is invalid
	 */
	public function doTemplateForClass($parentClass)
	{
		if(null !== ($_template = $this->getService()->getTemplateManager()->getTemplateByClassName($parentClass))) {
			foreach($_template->getDirective() as $_name => $_value) {
				if(!is_string($_value)) {
					throw new TConfigurationException('templatecontrol_directive_invalid', get_class(this), $_name);
				}
				
				$this->setSubProperty($_name, $_value);
			}

			$_template->instantiateIn($this);
		}
	}
	
	// getter/setter

	/**
	 * A source template control loads its template from external storage,
	 * such as file, db, rather than from within another template.
	 *
	 * @return boolean whether the current control is a source template control
	 */
	public function getIsSourceTemplateControl()
	{
		if(null !== ($_template = $this->getTemplate())) {
			return $_template->getIsSourceTemplate();
		}

		return ($_template = $this->getService()->getTemplateManager()->getTemplateByClassName(get_parent_class($this)))
			? $_template->getIsSourceTemplate()
			: false;
	}
}