summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TWebControlDecorator.php
blob: d08d9c8271b6863157886dec02b558e381c59003 (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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/**
 * TWebControlDecorator class file.
 *
 * @author Brad Anderson <javalizard@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package System.Web.UI.WebControls
 */


/**
 * TWebControlDecorator class
 *
 * A TWebControlDecorator can be applied to a {@link TWebControl} to customize its rendering.
 * TWebControlDecorator can add custom html code before and after both the open and close
 * tag of a {@link TWebControl}.
 * The html code can be an user-defined text or an external template file that will be
 * instantiated and rendered in place.
 *
 * This is an easy way to have your look and feel depend upon the theme instead of writing
 * specific html in your templates to achieve your website desires.
 * Here is an example of how to code your theme skin:
 * <code>
 * <com:THeader3>
 *	<prop:Decorator.PreTagText>
 * 			<!-- Surround the control with a div and apply a css class to it -->
 *		<div class="imported-theme-h3-container">
 *	</prop:Decorator.PreTagText>
 *	<prop:Decorator.PostTagText>
 * 			<!-- Properly close the tag -->
 *		</div>
 *	</prop:Decorator.PostTagText>
 * </com:THeader3>
 * </code>
 *
 * The order of the inclusion of the decoration into the page goes like this:
 * * PreTagTemplate
 * * PreTagText
 * * TWebControl Open Tag Rendered
 * * PreContentsText
 * * PreContentsTemplate
 * * TWebControl Children Rendered
 * * PostContentsTemplate
 * * PostContentsText
 * * TWebControl CloseTag Rendered
 * * PostTagText
 * * PostTagTemplate
 *
 *
 * @author Brad Anderson <javalizard@gmail.com>
 * @package System.Web.UI.WebControls
 * @since 3.2
 */

class TWebControlDecorator extends TComponent {

	/**
	 * @var boolean tells if there should only be decoration around the inner content
	 */
	private $_internalonly;

	/**
	 * @var boolean tells if the decoration uses state in its templates.  If there are no templates
	 * in the instance of the decoration this variable is unused.
	 */
	private $_usestate = false;

	/**
	 * @var TWebControl the control to decorate
	 */
	private $_control;

	/**
	 * @var TControl to tell the decorator where to place the outer controls
	 */
	private $_outercontrol;

	/**
	 * @var boolean This tells if the Templates have been
	 */
	private $_addedTemplateDecoration=false;


	/**
	 * @var string the text that goes before the open tag
	 */
	private $_pretagtext = '';
	/**
	 * @var string the text that goes after the open tag
	 */
	private $_precontentstext = '';
	/**
	 * @var string the text that goes before the close tag
	 */
	private $_postcontentstext = '';
	/**
	 * @var string the text that goes after the close tag
	 */
	private $_posttagtext = '';



	/**
	 * @var TTemplate the template that goes before the open tag
	 */
	private $_pretagtemplate;
	/**
	 * @var TTemplate the template that goes after the open tag
	 */
	private $_precontentstemplate;
	/**
	 * @var TTemplate the template that goes before the close tag
	 */
	private $_postcontentstemplate;
	/**
	 * @var TTemplate the template that goes after the close tag
	 */
	private $_posttagtemplate;

	/**
	 * Constructor.
	 * Initializes the control .
	 * @param TWebControl The control that is to be decorated.
	 * @param boolean whether decoration is just around the inner content
	 */
	public function __construct($control, $onlyinternal = false) {
		$this->_control = $control;
		$this->_internalonly = $onlyinternal;
	}

	/**
	 * @return boolean if the templates in this decoration need state.  This defaults to false
	 */
	public function getUseState()
	{
		return $this->_usestate;
	}

	/**
	 * @param boolean $value true to tell the decoration that the templates need state and should be
	 * placed in a control step before the state is saved.
	 */
	public function setUseState($value)
	{
		$this->_usestate = TPropertyValue::ensureBoolean($value);
	}

	/**
	 * @return string gets the text before the open tag in the TWebControl
	 */
	public function getPreTagText() {
		return $this->_pretagtext;
	}

	/**
	 * @param string sets the text before the open tag in the TWebControl
	 */
	public function setPreTagText($value) {
		if(!$this->_internalonly && !$this->_control->getIsSkinApplied())
			$this->_pretagtext = TPropertyValue::ensureString($value);
	}


	/**
	 * @return string the text after the open tag in the TWebControl
	 */
	public function getPreContentsText() {
		return $this->_precontentstext;
	}

	/**
	 * @param string sets the text after the open tag in the TWebControl
	 */
	public function setPreContentsText($value) {
		if(!$this->_control->getIsSkinApplied())
			$this->_precontentstext = TPropertyValue::ensureString($value);
	}


	/**
	 * @return string the text before the close tag in the TWebControl
	 */
	public function getPostContentsText() {
		return $this->_postcontentstext;
	}

	/**
	 * @param string sets the text before the close tag in the TWebControl
	 */
	public function setPostContentsText($value) {
		if(!$this->_control->getIsSkinApplied())
			$this->_postcontentstext = TPropertyValue::ensureString($value);
	}


	/**
	 * @return string the text before the close tag in the TWebControl
	 */
	public function getPostTagText() {
		return $this->_posttagtext;
	}

	/**
	 * @param string sets the text after the close tag in the TWebControl
	 */
	public function setPostTagText($value) {
		if(!$this->_internalonly && !$this->_control->getIsSkinApplied())
			$this->_posttagtext = TPropertyValue::ensureString($value);
	}


	/**
	 * @return TTemplate|null the template before the open tag in the TWebControl.  Defaults to null.
	 */
	public function getPreTagTemplate() {
		return $this->_pretagtemplate;
	}

	/**
	 * @param TTemplate sets the template before the open tag in the TWebControl
	 */
	public function setPreTagTemplate($value) {
		if(!$this->_internalonly && !$this->_control->getIsSkinApplied())
			$this->_pretagtemplate = $value;
	}


	/**
	 * @return TTemplate|null the template after the open tag in the TWebControl.  Defaults to null.
	 */
	public function getPreContentsTemplate() {
		return $this->_precontentstemplate;
	}

	/**
	 * @param TTemplate sets the template after the open tag in the TWebControl
	 */
	public function setPreContentsTemplate($value) {
		if(!$this->_control->getIsSkinApplied())
			$this->_precontentstemplate = $value;
	}


	/**
	 * @return TTemplate|null the template before the close tag in the TWebControl.  Defaults to null.
	 */
	public function getPostContentsTemplate() {
		return $this->_postcontentstemplate;
	}

	/**
	 * @param TTemplate sets the template before the close tag in the TWebControl
	 */
	public function setPostContentsTemplate($value) {
		if(!$this->_control->getIsSkinApplied())
			$this->_postcontentstemplate = $value;
	}


	/**
	 * @return TTemplate|null the template after the close tag in the TWebControl.  Defaults to null.
	 */
	public function getPostTagTemplate() {
		return $this->_posttagtemplate;
	}

	/**
	 * @param TTemplate sets the template before the close tag in the TWebControl
	 */
	public function setPostTagTemplate($value) {
		if(!$this->_internalonly && !$this->_control->getIsSkinApplied())
			$this->_posttagtemplate = $value;
	}

	/**
	 *	this is a framework call.  The Text decoration can't
	 * influence the object hierarchy because they are rendered into into the writer directly.
	 * This call attaches the ensureTemplateDecoration to the TPage onSaveStateComplete so
	 * these controls don't have page states.  This is as close to not influencing the page as possible.
	 */
	public function instantiate($outercontrol = null) {
		if($this->getPreTagTemplate() || $this->getPreContentsTemplate() ||
			$this->getPostContentsTemplate() || $this->getPostTagTemplate()) {

			$this->_outercontrol = $outercontrol;
			if($this->getUseState())
				$this->ensureTemplateDecoration();
			else
				$this->_control->getPage()->onSaveStateComplete[] = array($this, 'ensureTemplateDecoration');
		}
	}


	/**
	 *	This method places the templates around the open and close tag.  This takes a parameter which is
	 * to specify the control to get the outer template decoration.  If no outer control is specified
	 * @param TComponent this indicates the component or control to get the outer tag elements, just in case it's
	 * different than attached TWebControl.  If none is provided, the outer templates default to the attached
	 * control
	 * @return boolean returns true if the template decorations have been added
	 */
	public function ensureTemplateDecoration($sender=null, $param=null) {

		$control = $this->_control;
		$outercontrol = $this->_outercontrol;
		if($outercontrol === null)
			$outercontrol = $control;

		if($this->_addedTemplateDecoration)
			return $this->_addedTemplateDecoration;

		$this->_addedTemplateDecoration = true;

		if($this->getPreContentsTemplate())
		{
			$precontents = Prado::createComponent('TCompositeControl');
			$this->getPreContentsTemplate()->instantiateIn($precontents);
			$control->getControls()->insertAt(0, $precontents);
		}

		if($this->getPostContentsTemplate())
		{
			$postcontents = Prado::createComponent('TCompositeControl');
			$this->getPostContentsTemplate()->instantiateIn($postcontents);
			$control->getControls()->add($postcontents);
		}

		if(!$outercontrol->getParent())
			return $this->_addedTemplateDecoration;


		if($this->getPreTagTemplate())
		{
			$pretag = Prado::createComponent('TCompositeControl');
			$this->getPreTagTemplate()->instantiateIn($pretag);
			$outercontrol->getParent()->getControls()->insertBefore($outercontrol, $pretag);
		}

		if($this->getPostTagTemplate())
		{
			$posttag = Prado::createComponent('TCompositeControl');
			$this->getPostTagTemplate()->instantiateIn($posttag);
			$outercontrol->getParent()->getControls()->insertAfter($outercontrol, $posttag);
		}
		return true;
	}


	/**
	 * This method places the pre tag text into the {@link TTextWriter}
	 * @param {@link TTextWriter} the writer to which the text is written
	 */
	public function renderPreTagText($writer) {
		$writer->write($this->getPreTagText());
	}

	/**
	 * This method places the pre contents text into the {@link TTextWriter}
	 * @param {@link TTextWriter} the writer to which the text is written
	 */
	public function renderPreContentsText($writer) {
		$writer->write($this->getPreContentsText());
	}

	/**
	 * This method places the post contents text into the {@link TTextWriter}
	 * @param {@link TTextWriter} the writer to which the text is written
	 */
	public function renderPostContentsText($writer) {
		$writer->write($this->getPostContentsText());
	}

	/**
	 * This method places the post tag text into the {@link TTextWriter}
	 * @param {@link TTextWriter} the writer to which the text is written
	 */
	public function renderPostTagText($writer) {
		$writer->write($this->getPostTagText());
	}
}