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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
<?php
/**
* TTemplateControl class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.pradosoft.com/
* @copyright Copyright © 2005-2014 PradoSoft
* @license http://www.pradosoft.com/license/
* @package System.Web.UI
*/
/**
* Includes TCompositeControl class
*/
Prado::using('System.Web.UI.TCompositeControl');
/**
* TTemplateControl class.
* TTemplateControl is 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".
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @package System.Web.UI
* @since 3.0
*/
class TTemplateControl extends TCompositeControl
{
/**
* template file extension.
*/
const EXT_TEMPLATE='.tpl';
/**
* @var ITemplate the parsed template structure shared by the same control class
*/
private static $_template=array();
/**
* @var ITemplate the parsed template structure specific for this control instance
*/
private $_localTemplate=null;
/**
* @var TTemplateControl the master control if any
*/
private $_master=null;
/**
* @var string master control class name
*/
private $_masterClass='';
/**
* @var array list of TContent controls
*/
private $_contents=array();
/**
* @var array list of TContentPlaceHolder controls
*/
private $_placeholders=array();
/**
* Returns the template object associated with this control object.
* @return ITemplate|null the parsed template, null if none
*/
public function getTemplate()
{
if($this->_localTemplate===null)
{
$class=get_class($this);
if(!isset(self::$_template[$class]))
self::$_template[$class]=$this->loadTemplate();
return self::$_template[$class];
}
else
return $this->_localTemplate;
}
/**
* Sets the parsed template.
* Note, the template will be applied to the whole control class.
* This method should only be used by framework and control developers.
* @param ITemplate the parsed template
*/
public function setTemplate($value)
{
$this->_localTemplate=$value;
}
/**
* @return boolean whether this control is a source template control.
* A source template control loads its template from external storage,
* such as file, db, rather than from within another template.
*/
public function getIsSourceTemplateControl()
{
if(($template=$this->getTemplate())!==null)
return $template->getIsSourceTemplate();
else
return false;
}
/**
* @return string the directory containing the template. Empty if no template available.
*/
public function getTemplateDirectory()
{
if(($template=$this->getTemplate())!==null)
return $template->getContextPath();
else
return '';
}
/**
* Loads the template associated with this control class.
* @return ITemplate the parsed template structure
*/
protected function loadTemplate()
{
Prado::trace("Loading template ".get_class($this),'System.Web.UI.TTemplateControl');
$template=$this->getService()->getTemplateManager()->getTemplateByClassName(get_class($this));
return $template;
}
/**
* Creates child controls.
* This method is overridden to load and instantiate control template.
* This method should only be used by framework and control developers.
*/
public function createChildControls()
{
if($tpl=$this->getTemplate())
{
foreach($tpl->getDirective() as $name=>$value)
{
if(is_string($value))
$this->setSubProperty($name,$value);
else
throw new TConfigurationException('templatecontrol_directive_invalid',get_class($this),$name);
}
$tpl->instantiateIn($this);
}
}
/**
* Registers a content control.
* @param string ID of the content
* @param TContent
*/
public function registerContent($id,TContent $object)
{
if(isset($this->_contents[$id]))
throw new TConfigurationException('templatecontrol_contentid_duplicated',$id);
else
$this->_contents[$id]=$object;
}
/**
* Registers a content placeholder to this template control.
* This method should only be used by framework and control developers.
* @param string placeholder ID
* @param TContentPlaceHolder placeholder control
*/
public function registerContentPlaceHolder($id,TContentPlaceHolder $object)
{
if(isset($this->_placeholders[$id]))
throw new TConfigurationException('templatecontrol_placeholderid_duplicated',$id);
else
$this->_placeholders[$id]=$object;
}
/**
* @return string master class name (in namespace form)
*/
public function getMasterClass()
{
return $this->_masterClass;
}
/**
* @param string master control class name (in namespace form)
*/
public function setMasterClass($value)
{
$this->_masterClass=$value;
}
/**
* @return TTemplateControl|null master control associated with this control, null if none
*/
public function getMaster()
{
return $this->_master;
}
/**
* Injects all content controls (and their children) to the corresponding content placeholders.
* This method should only be used by framework and control developers.
* @param string ID of the content control
* @param TContent the content to be injected
*/
public function injectContent($id,$content)
{
if(isset($this->_placeholders[$id]))
{
$placeholder=$this->_placeholders[$id];
$controls=$placeholder->getParent()->getControls();
$loc=$controls->remove($placeholder);
$controls->insertAt($loc,$content);
}
else
throw new TConfigurationException('templatecontrol_placeholder_inexistent',$id);
}
/**
* Performs the OnInit step for the control and all its child controls.
* This method overrides the parent implementation
* by ensuring child controls are created first,
* and if master class is set, master will be applied.
* Only framework developers should use this method.
* @param TControl the naming container control
*/
protected function initRecursive($namingContainer=null)
{
$this->ensureChildControls();
if($this->_masterClass!=='')
{
$master=Prado::createComponent($this->_masterClass);
if(!($master instanceof TTemplateControl))
throw new TInvalidDataValueException('templatecontrol_mastercontrol_invalid');
$this->_master=$master;
$this->getControls()->clear();
$this->getControls()->add($master);
$master->ensureChildControls();
foreach($this->_contents as $id=>$content)
$master->injectContent($id,$content);
}
else if(!empty($this->_contents))
throw new TConfigurationException('templatecontrol_mastercontrol_required',get_class($this));
parent::initRecursive($namingContainer);
}
/**
* Function to update view controls with data in a given AR object.
* View controls and AR object need to have the same name in IDs and Attrs respectively.
* @param TActiveRecord $arObj
* @param Boolean $throwExceptions Wheter or not to throw exceptions
* @author Daniel Sampedro <darthdaniel85@gmail.com>
*/
public function tryToUpdateView($arObj, $throwExceptions = false)
{
$objAttrs = get_class_vars(get_class($arObj));
foreach (array_keys($objAttrs) as $key)
{
try
{
if ($key != "RELATIONS")
{
$control = $this->{$key};
if ($control instanceof TTextBox)
$control->Text = $arObj->{$key};
elseif ($control instanceof TCheckBox)
$control->Checked = (boolean) $arObj->{$key};
elseif ($control instanceof TDatePicker)
$control->Date = $arObj->{$key};
}
else
{
foreach ($objAttrs["RELATIONS"] as $relKey => $relValues)
{
$relControl = $this->{$relKey};
switch ($relValues[0])
{
case TActiveRecord::BELONGS_TO:
case TActiveRecord::HAS_ONE:
$relControl->Text = $arObj->{$relKey};
break;
case TActiveRecord::HAS_MANY:
if ($relControl instanceof TListControl)
{
$relControl->DataSource = $arObj->{$relKey};
$relControl->dataBind();
}
break;
}
}
break;
}
}
catch (Exception $ex)
{
if ($throwExceptions)
throw $ex;
}
}
}
/**
* Function to try to update an AR object with data in view controls.
* @param TActiveRecord $arObj
* @param Boolean $throwExceptions Wheter or not to throw exceptions
* @author Daniel Sampedro <darthdaniel85@gmail.com>
*/
public function tryToUpdateAR($arObj, $throwExceptions = false)
{
$objAttrs = get_class_vars(get_class($arObj));
foreach (array_keys($objAttrs) as $key)
{
try
{
if ($key == "RELATIONS")
break;
$control = $this->{$key};
if ($control instanceof TTextBox)
$arObj->{$key} = $control->Text;
elseif ($control instanceof TCheckBox)
$arObj->{$key} = $control->Checked;
elseif ($control instanceof TDatePicker)
$arObj->{$key} = $control->Date;
}
catch (Exception $ex)
{
if ($throwExceptions)
throw $ex;
}
}
}
}
|