Passed
Push — master ( 06f7cb...c3c92a )
by Fabio
05:08
created

TControl::getPluginModule()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 0
dl 0
loc 19
ccs 0
cts 4
cp 0
crap 30
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * TControl, TControlCollection, TEventParameter and INamingContainer class file
4
 *
5
 * @author Qiang Xue <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado\Web\UI
9
 */
10
11
namespace Prado\Web\UI;
12
13
use Exception;
14
use Prado\Exceptions\TInvalidDataValueException;
15
use Prado\Exceptions\TInvalidOperationException;
16
use Prado\Prado;
17
use Prado\TPropertyValue;
18
use Prado\Web\UI\ActiveControls\IActiveControl;
19
use Prado\Collections\TAttributeCollection;
20
use ReflectionClass;
21
22
/**
23
 * TControl class
24
 *
25
 * TControl is the base class for all components on a page hierarchy.
26
 * It implements the following features for UI-related functionalities:
27
 * - databinding feature
28
 * - parent and child relationship
29
 * - naming container and containee relationship
30
 * - viewstate and controlstate features
31
 * - rendering scheme
32
 * - control lifecycles
33
 *
34
 * A property can be data-bound with an expression. By calling {@link dataBind},
35
 * expressions bound to properties will be evaluated and the results will be
36
 * set to the corresponding properties.
37
 *
38
 * Parent and child relationship determines how the presentation of controls are
39
 * enclosed within each other. A parent will determine where to place
40
 * the presentation of its child controls. For example, a TPanel will enclose
41
 * all its child controls' presentation within a div html tag. A control's parent
42
 * can be obtained via {@link getParent Parent} property, and its
43
 * {@link getControls Controls} property returns a list of the control's children,
44
 * including controls and static texts. The property can be manipulated
45
 * like an array for adding or removing a child (see {@link TList} for more details).
46
 *
47
 * A naming container control implements INamingContainer and ensures that
48
 * its containee controls can be differentiated by their ID property values.
49
 * Naming container and containee realtionship specifies a protocol to uniquely
50
 * identify an arbitrary control on a page hierarchy by an ID path (concatenation
51
 * of all naming containers' IDs and the target control's ID).
52
 *
53
 * Viewstate and controlstate are two approaches to preserve state across
54
 * page postback requests. ViewState is mainly related with UI specific state
55
 * and can be disabled if not needed. ControlState represents crucial logic state
56
 * and cannot be disabled.
57
 *
58
 * A control is rendered via its {@link render()} method (the method is invoked
59
 * by the framework.) Descendant control classes may override this method for
60
 * customized rendering. By default, {@link render()} invokes {@link renderChildren()}
61
 * which is responsible for rendering of children of the control.
62
 * Control's {@link getVisible Visible} property governs whether the control
63
 * should be rendered or not.
64
 *
65
 * Each control on a page will undergo a series of lifecycles, including
66
 * control construction, Init, Load, PreRender, Render, and OnUnload.
67
 * They work together with page lifecycles to process a page request.
68
 *
69
 * @author Qiang Xue <[email protected]>
70
 * @package Prado\Web\UI
71
 * @since 3.0
72
 */
73
class TControl extends \Prado\TApplicationComponent implements IRenderable, IBindable
74
{
75
	/**
76
	 * format of control ID
77
	 */
78
	public const ID_FORMAT = '/^[a-zA-Z_]\\w*$/';
79
	/**
80
	 * separator char between IDs in a UniqueID
81
	 */
82
	public const ID_SEPARATOR = '$';
83
	/**
84
	 * separator char between IDs in a ClientID
85
	 */
86
	public const CLIENT_ID_SEPARATOR = '_';
87
	/**
88
	 * prefix to an ID automatically generated
89
	 */
90
	public const AUTOMATIC_ID_PREFIX = 'ctl';
91
92
	/**
93
	 * the stage of lifecycles that the control is currently at
94
	 */
95
	public const CS_CONSTRUCTED = 0;
96
	public const CS_CHILD_INITIALIZED = 1;
97
	public const CS_INITIALIZED = 2;
98
	public const CS_STATE_LOADED = 3;
99
	public const CS_LOADED = 4;
100
	public const CS_PRERENDERED = 5;
101
102
	/**
103
	 * State bits.
104
	 */
105
	public const IS_ID_SET = 0x01;
106
	public const IS_DISABLE_VIEWSTATE = 0x02;
107
	public const IS_SKIN_APPLIED = 0x04;
108
	public const IS_STYLESHEET_APPLIED = 0x08;
109
	public const IS_DISABLE_THEMING = 0x10;
110
	public const IS_CHILD_CREATED = 0x20;
111
	public const IS_CREATING_CHILD = 0x40;
112
113
	/**
114
	 * Indexes for the rare fields.
115
	 * In order to save memory, rare fields will only be created if they are needed.
116
	 */
117
	public const RF_CONTROLS = 0;			// child controls
118
	public const RF_CHILD_STATE = 1;			// child state field
119
	public const RF_NAMED_CONTROLS = 2;		// list of controls whose namingcontainer is this control
120
	public const RF_NAMED_CONTROLS_ID = 3;	// counter for automatic id
121
	public const RF_SKIN_ID = 4;				// skin ID
122
	public const RF_DATA_BINDINGS = 5;		// data bindings
123
	public const RF_EVENTS = 6;				// event handlers
124
	public const RF_CONTROLSTATE = 7;		// controlstate
125
	public const RF_NAMED_OBJECTS = 8;		// controls declared with ID on template
126
	public const RF_ADAPTER = 9;				// adapter
127
	public const RF_AUTO_BINDINGS = 10;		// auto data bindings
128
129
	/**
130
	 * @var string control ID
131
	 */
132
	private $_id = '';
133
	/**
134
	 * @var string control unique ID
135
	 */
136
	private $_uid;
137
	/**
138
	 * @var \Prado\Web\UI\TControl parent of the control
139
	 */
140
	private $_parent;
141
	/**
142
	 * @var TPage page that the control resides in
143
	 */
144
	private $_page;
145
	/**
146
	 * @var TPluginModule that the control share in their path
0 ignored issues
show
Bug introduced by
The type Prado\Web\UI\TPluginModule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
147
	 */
148
	private $_pluginmodule;
149
	/**
150
	 * @var \Prado\Web\UI\TControl naming container of the control
151
	 */
152
	private $_namingContainer;
153
	/**
154
	 * @var TTemplateControl control whose template contains the control
155
	 */
156
	private $_tplControl;
157
	/**
158
	 * @var array viewstate data
159
	 */
160
	private $_viewState = [];
161
	/**
162
	 * @var array temporary state (usually set in template)
163
	 */
164
	private $_tempState = [];
165
	/**
166
	 * @var bool whether we should keep state in viewstate
167
	 */
168
	private $_trackViewState = true;
169
	/**
170
	 * @var int the current stage of the control lifecycles
171
	 */
172
	private $_stage = 0;
173
	/**
174
	 * @var int representation of the state bits
175
	 */
176
	private $_flags = 0;
177
	/**
178
	 * @var array a collection of rare control data
179
	 */
180
	private $_rf = [];
181
182
	/**
183
	 * TControl need not auto listen to global events because class
184
	 * behaviors are not typically added in the middle of running a page
185
	 * and the overhead of so many TControls listening and unlistening.
186
	 *
187
	 * @return bool returns false
188
	 */
189
	public function getAutoGlobalListen()
190
	{
191
		return false;
192
	}
193
194
	/**
195
	 * Returns a property value by name or a control by ID.
196
	 * This overrides the parent implementation by allowing accessing
197
	 * a control via its ID using the following syntax,
198
	 * <code>
199
	 * $menuBar=$this->menuBar;
200
	 * </code>
201
	 * Note, the control must be configured in the template
202
	 * with explicit ID. If the name matches both a property and a control ID,
203
	 * the control ID will take the precedence.
204
	 *
205
	 * @param string $name the property name or control ID
206
	 * @throws TInvalidOperationException if the property is not defined.
207
	 * @return mixed the property value or the target control
208
	 * @see registerObject
209
	 */
210
	public function __get($name)
211
	{
212
		if (isset($this->_rf[self::RF_NAMED_OBJECTS][$name])) {
213
			return $this->_rf[self::RF_NAMED_OBJECTS][$name];
214
		} else {
215
			return parent::__get($name);
216
		}
217
	}
218
219
	/**
220
	 * Checks for the existance of a property value by name or a control by ID.
221
	 * This overrides the parent implementation by allowing checking for the
222
	 * existance of a control via its ID using the following syntax,
223
	 * <code>
224
	 * $menuBarExists = isset($this->menuBar);
225
	 * </code>
226
	 * Do not call this method. This is a PHP magic method that we override
227
	 * to allow using isset() to detect if a component property is set or not.
228
	 * Note, the control must be configured in the template
229
	 * with explicit ID. If the name matches both a property and a control ID,
230
	 * the control ID will take the precedence.
231
	 *
232
	 * @param string $name the property name or control ID
233
	 * @return bool wether the control or property exists
234
	 * @see __get
235
	 */
236
	public function __isset($name)
237
	{
238
		if (isset($this->_rf[self::RF_NAMED_OBJECTS][$name])) {
239 1
			return true;
240
		} else {
241 1
			return parent::__isset($name);
242
		}
243
	}
244
245
	/**
246
	 * @return bool whether there is an adapter for this control
247 1
	 */
248
	public function getHasAdapter()
249 1
	{
250 1
		return isset($this->_rf[self::RF_ADAPTER]);
251
	}
252
253
	/**
254
	 * @return TControlAdapter control adapter. Null if not exists.
255
	 */
256
	public function getAdapter()
257
	{
258
		return $this->_rf[self::RF_ADAPTER] ?? null;
259
	}
260
261
	/**
262
	 * @param TControlAdapter $adapter control adapter
263
	 */
264
	public function setAdapter(TControlAdapter $adapter)
265
	{
266
		$this->_rf[self::RF_ADAPTER] = $adapter;
267
	}
268
269
	/**
270
	 * @return \Prado\Web\UI\TControl the parent of this control
271
	 */
272
	public function getParent()
273
	{
274
		return $this->_parent;
275
	}
276
277
	/**
278
	 * @return \Prado\Web\UI\TControl the naming container of this control
279
	 */
280
	public function getNamingContainer()
281
	{
282
		if (!$this->_namingContainer && $this->_parent) {
283
			if ($this->_parent instanceof INamingContainer) {
284
				$this->_namingContainer = $this->_parent;
285
			} else {
286
				$this->_namingContainer = $this->_parent->getNamingContainer();
287
			}
288
		}
289
		return $this->_namingContainer;
290
	}
291
292
	/**
293
	 * @return TPage the page that contains this control
294
	 */
295
	public function getPage()
296
	{
297
		if (!$this->_page) {
298
			if ($this->_parent) {
299
				$this->_page = $this->_parent->getPage();
300
			} elseif ($this->_tplControl) {
301
				$this->_page = $this->_tplControl->getPage();
302
			}
303
		}
304
		return $this->_page;
305
	}
306
307
	/**
308
	 * Sets the page for a control.
309
	 * Only framework developers should use this method.
310
	 * @param TPage $page the page that contains this control
311
	 */
312
	public function setPage($page)
313
	{
314
		$this->_page = $page;
315
	}
316
317
	/**
318
	 * Sets the control whose template contains this control.
319
	 * Only framework developers should use this method.
320
	 * @param TTemplateControl $control the control whose template contains this control
321
	 */
322
	public function setTemplateControl($control)
323
	{
324
		$this->_tplControl = $control;
325
	}
326
327
	/**
328
	 * @return TTemplateControl the control whose template contains this control
329
	 */
330
	public function getTemplateControl()
331
	{
332
		if (!$this->_tplControl && $this->_parent) {
333
			$this->_tplControl = $this->_parent->getTemplateControl();
334
		}
335
		return $this->_tplControl;
336
	}
337
338
	/**
339
	 * @return TTemplateControl the control whose template is loaded from
340
	 * some external storage, such as file, db, and whose template ultimately
341
	 * contains this control.
342
	 */
343
	public function getSourceTemplateControl()
344 1
	{
345
		$control = $this;
346 1
		while (($control instanceof TControl) && ($control = $control->getTemplateControl()) !== null) {
347
			if (($control instanceof TTemplateControl) && $control->getIsSourceTemplateControl()) {
348
				return $control;
349
			}
350
		}
351
		return $this->getPage();
352
	}
353
354
	/**
355
	 * Returns the module associated with the class path of the control.  This is for Composer
356
	 * extensions adding their own Controls to access their associated Module.
357
	 * @return null|Prado\Util\IPluginModule the module associated with this TControl
0 ignored issues
show
Bug introduced by
The type Prado\Prado\Util\IPluginModule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
358
	 * @since 4.2.0
359
	 */
360
	public function getPluginModule()
361
	{
362
		if ($this->_pluginmodule === false) {
0 ignored issues
show
introduced by
The condition $this->_pluginmodule === false is always false.
Loading history...
363
			$this->_pluginmodule = null;
364
			
365
			$reflect = new ReflectionClass(get_class($this));
366
			$folder = $reflect->getFileName();
367
			
368
			foreach ($this->getApplication()->getModulesByType('Prado\\Util\\IPluginModule') as $id => $module) {
369
				if (!$module) {
370
					continue;
371
				}
372
				if (stripos($folder, $module->getPluginPath()) !== false) {
373
					$this->_pluginmodule = $module;
374
					break;
375
				}
376
			}
377
		}
378
		return $this->_pluginmodule;
379
	}
380
381
	/**
382
	 * Gets the lifecycle step the control is currently at.
383
	 * This method should only be used by control developers.
384
	 * @return int the lifecycle step the control is currently at.
385
	 * The value can be CS_CONSTRUCTED, CS_CHILD_INITIALIZED, CS_INITIALIZED,
386
	 * CS_STATE_LOADED, CS_LOADED, CS_PRERENDERED.
387
	 */
388
	protected function getControlStage()
389
	{
390
		return $this->_stage;
391
	}
392
393
	/**
394
	 * Sets the lifecycle step the control is currently at.
395
	 * This method should only be used by control developers.
396
	 * @param int $value the lifecycle step the control is currently at.
397
	 * Valid values include CS_CONSTRUCTED, CS_CHILD_INITIALIZED, CS_INITIALIZED,
398
	 * CS_STATE_LOADED, CS_LOADED, CS_PRERENDERED.
399
	 */
400
	protected function setControlStage($value)
401
	{
402
		$this->_stage = $value;
403
	}
404
405
	/**
406
	 * Returns the id of the control.
407
	 * Control ID can be either manually set or automatically generated.
408
	 * If $hideAutoID is true, automatically generated ID will be returned as an empty string.
409
	 * @param bool $hideAutoID whether to hide automatically generated ID
410
	 * @return string the ID of the control
411
	 */
412
	public function getID($hideAutoID = true)
413
	{
414
		if ($hideAutoID) {
415
			return ($this->_flags & self::IS_ID_SET) ? $this->_id : '';
416
		} else {
417
			return $this->_id;
418
		}
419
	}
420
421
	/**
422
	 * @param string $id the new control ID. The value must consist of word characters [a-zA-Z0-9_] only
423
	 * @throws TInvalidDataValueException if ID is in a bad format
424
	 */
425
	public function setID($id)
426
	{
427
		if (!preg_match(self::ID_FORMAT, $id)) {
428
			throw new TInvalidDataValueException('control_id_invalid', get_class($this), $id);
429
		}
430
		$this->_id = $id;
431
		$this->_flags |= self::IS_ID_SET;
432
		$this->clearCachedUniqueID($this instanceof INamingContainer);
433
		if ($this->_namingContainer) {
434
			$this->_namingContainer->clearNameTable();
435
		}
436
	}
437
438
	/**
439
	 * Returns a unique ID that identifies the control in the page hierarchy.
440
	 * A unique ID is the contenation of all naming container controls' IDs and the control ID.
441
	 * These IDs are separated by '$' character.
442
	 * Control users should not rely on the specific format of UniqueID, however.
443
	 * @return string a unique ID that identifies the control in the page hierarchy
444
	 */
445
	public function getUniqueID()
446
	{
447
		if ($this->_uid === '' || $this->_uid === null) {	// need to build the UniqueID
448
			$this->_uid = '';  // set to not-null, so that clearCachedUniqueID() may take action
449
			if ($namingContainer = $this->getNamingContainer()) {
450
				if ($this->getPage() === $namingContainer) {
451
					return ($this->_uid = $this->_id);
452
				} elseif (($prefix = $namingContainer->getUniqueID()) === '') {
453
					return $this->_id;
454
				} else {
455
					return ($this->_uid = $prefix . self::ID_SEPARATOR . $this->_id);
456
				}
457
			} else {	// no naming container
458
				return $this->_id;
459
			}
460
		} else {
461
			return $this->_uid;
462
		}
463
	}
464
465
	/**
466
	 * Sets input focus to this control.
467
	 */
468
	public function focus()
469
	{
470
		$this->getPage()->setFocus($this);
471
	}
472
473
	/**
474
	 * Returns the client ID of the control.
475
	 * The client ID can be used to uniquely identify
476
	 * the control in client-side scripts (such as JavaScript).
477
	 * Do not rely on the explicit format of the return ID.
478
	 * @return string the client ID of the control
479
	 */
480
	public function getClientID()
481
	{
482
		return strtr($this->getUniqueID(), self::ID_SEPARATOR, self::CLIENT_ID_SEPARATOR);
483
	}
484
485
	/**
486
	 * Converts a unique ID to a client ID.
487
	 * @param string $uniqueID the unique ID of a control
488
	 * @return string the client ID of the control
489
	 */
490
	public static function convertUniqueIdToClientId($uniqueID)
491
	{
492
		return strtr($uniqueID, self::ID_SEPARATOR, self::CLIENT_ID_SEPARATOR);
493
	}
494
495
	/**
496
	 * @return string the skin ID of this control, '' if not set
497
	 */
498
	public function getSkinID()
499
	{
500
		return $this->_rf[self::RF_SKIN_ID] ?? '';
501
	}
502
503
	/**
504
	 * @param string $value the skin ID of this control
505
	 * @throws TInvalidOperationException if the SkinID is set in a stage later than PreInit, or if the skin is applied already.
506
	 */
507
	public function setSkinID($value)
508
	{
509
		if (($this->_flags & self::IS_SKIN_APPLIED) || $this->_stage >= self::CS_CHILD_INITIALIZED) {
510
			throw new TInvalidOperationException('control_skinid_unchangeable', get_class($this));
511
		} else {
512
			$this->_rf[self::RF_SKIN_ID] = $value;
513
		}
514
	}
515
516
	/**
517
	 * @return bool if a skin is applied.
518
	 */
519
	public function getIsSkinApplied()
520
	{
521
		return ($this->_flags & self::IS_SKIN_APPLIED);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_flags & self::IS_SKIN_APPLIED returns the type integer which is incompatible with the documented return type boolean.
Loading history...
522
	}
523
524
	/**
525
	 * @return bool whether theming is enabled for this control.
526
	 * The theming is enabled if the control and all its parents have it enabled.
527
	 */
528
	public function getEnableTheming()
529
	{
530
		if ($this->_flags & self::IS_DISABLE_THEMING) {
531
			return false;
532
		} else {
533
			return $this->_parent ? $this->_parent->getEnableTheming() : true;
534
		}
535
	}
536
537
	/**
538
	 * @param bool $value whether to enable theming
539
	 * @throws TInvalidOperationException if this method is invoked after OnPreInit
540
	 */
541
	public function setEnableTheming($value)
542
	{
543
		if ($this->_stage >= self::CS_CHILD_INITIALIZED) {
544
			throw new TInvalidOperationException('control_enabletheming_unchangeable', get_class($this), $this->getUniqueID());
545
		} elseif (TPropertyValue::ensureBoolean($value)) {
546
			$this->_flags &= ~self::IS_DISABLE_THEMING;
547
		} else {
548
			$this->_flags |= self::IS_DISABLE_THEMING;
549
		}
550
	}
551
552
	/**
553
	 * Returns custom data associated with this control.
554
	 * A control may be associated with some custom data for various purposes.
555
	 * For example, a button may be associated with a string to identify itself
556
	 * in a generic OnClick event handler.
557
	 * @return mixed custom data associated with this control. Defaults to null.
558
	 */
559
	public function getCustomData()
560
	{
561
		return $this->getViewState('CustomData', null);
562
	}
563
564
	/**
565
	 * Associates custom data with this control.
566
	 * Note, the custom data must be serializable and unserializable.
567
	 * @param mixed $value custom data
568
	 */
569
	public function setCustomData($value)
570
	{
571
		$this->setViewState('CustomData', $value, null);
572
	}
573
574
	/**
575
	 * @return bool whether the control has child controls
576
	 */
577
	public function getHasControls()
578
	{
579
		return isset($this->_rf[self::RF_CONTROLS]) && $this->_rf[self::RF_CONTROLS]->getCount() > 0;
580
	}
581
582
	/**
583
	 * @return TControlCollection the child control collection
584
	 */
585
	public function getControls()
586
	{
587
		if (!isset($this->_rf[self::RF_CONTROLS])) {
588
			$this->_rf[self::RF_CONTROLS] = $this->createControlCollection();
589
		}
590
		return $this->_rf[self::RF_CONTROLS];
591
	}
592
593
	/**
594
	 * Creates a control collection object that is to be used to hold child controls
595
	 * @return TControlCollection control collection
596
	 * @see getControls
597
	 */
598
	protected function createControlCollection()
599
	{
600
		return $this->getAllowChildControls() ? new TControlCollection($this) : new TEmptyControlCollection($this);
601
	}
602
603
	/**
604
	 * Checks if a control is visible.
605
	 * If parent check is required, then a control is visible only if the control
606
	 * and all its ancestors are visible.
607
	 * @param bool $checkParents whether the parents should also be checked if visible
608
	 * @return bool whether the control is visible (default=true).
609
	 */
610
	public function getVisible($checkParents = true)
611
	{
612
		if ($checkParents) {
613
			for ($control = $this; $control; $control = $control->_parent) {
614
				if (!$control->getVisible(false)) {
615
					return false;
616
				}
617
			}
618
			return true;
619
		} else {
620
			return $this->getViewState('Visible', true);
621
		}
622
	}
623
624
	/**
625
	 * @param bool $value whether the control is visible
626
	 */
627
	public function setVisible($value)
628
	{
629
		$this->setViewState('Visible', TPropertyValue::ensureBoolean($value), true);
630
	}
631
632
	/**
633
	 * Returns a value indicating whether the control is enabled.
634
	 * A control is enabled if it allows client user interaction.
635
	 * If $checkParents is true, all parent controls will be checked,
636
	 * and unless they are all enabled, false will be returned.
637
	 * The property Enabled is mainly used for {@link TWebControl}
638
	 * derived controls.
639
	 * @param bool $checkParents whether the parents should also be checked enabled
640
	 * @return bool whether the control is enabled.
641
	 */
642
	public function getEnabled($checkParents = false)
643
	{
644
		if ($checkParents) {
645
			for ($control = $this; $control; $control = $control->_parent) {
646
				if (!$control->getViewState('Enabled', true)) {
647
					return false;
648
				}
649
			}
650
			return true;
651
		} else {
652
			return $this->getViewState('Enabled', true);
653
		}
654
	}
655
656
	/**
657
	 * @param bool $value whether the control is to be enabled.
658
	 */
659
	public function setEnabled($value)
660
	{
661
		$this->setViewState('Enabled', TPropertyValue::ensureBoolean($value), true);
662
	}
663
664
	/**
665
	 * @return bool whether the control has custom attributes
666
	 */
667
	public function getHasAttributes()
668
	{
669
		if ($attributes = $this->getViewState('Attributes', null)) {
670
			return $attributes->getCount() > 0;
671
		} else {
672
			return false;
673
		}
674
	}
675
676
	/**
677
	 * Returns the list of custom attributes.
678
	 * Custom attributes are name-value pairs that may be rendered
679
	 * as HTML tags' attributes.
680
	 * @return TAttributeCollection the list of custom attributes
681
	 */
682
	public function getAttributes()
683
	{
684
		if ($attributes = $this->getViewState('Attributes', null)) {
685
			return $attributes;
686
		} else {
687
			$attributes = new TAttributeCollection;
688
			$this->setViewState('Attributes', $attributes, null);
689
			return $attributes;
690
		}
691
	}
692
693
	/**
694
	 * @param mixed $name
695
	 * @return bool whether the named attribute exists
696
	 */
697
	public function hasAttribute($name)
698
	{
699
		if ($attributes = $this->getViewState('Attributes', null)) {
700
			return $attributes->contains($name);
701
		} else {
702
			return false;
703
		}
704
	}
705
706
	/**
707
	 * @param mixed $name
708
	 * @return string attribute value, null if attribute does not exist
709
	 */
710
	public function getAttribute($name)
711
	{
712
		if ($attributes = $this->getViewState('Attributes', null)) {
713
			return $attributes->itemAt($name);
714
		} else {
715
			return null;
716
		}
717
	}
718
719
	/**
720
	 * Sets a custom control attribute.
721
	 * @param string $name attribute name
722
	 * @param string $value value of the attribute
723
	 */
724
	public function setAttribute($name, $value)
725
	{
726
		$this->getAttributes()->add($name, $value);
727
	}
728
729
	/**
730
	 * Removes the named attribute.
731
	 * @param string $name the name of the attribute to be removed.
732
	 * @return string attribute value removed, null if attribute does not exist.
733
	 */
734
	public function removeAttribute($name)
735
	{
736
		if ($attributes = $this->getViewState('Attributes', null)) {
737
			return $attributes->remove($name);
738
		} else {
739
			return null;
740
		}
741
	}
742
743
	/**
744
	 * @param mixed $checkParents
745
	 * @return bool whether viewstate is enabled
746
	 */
747
	public function getEnableViewState($checkParents = false)
748
	{
749
		if ($checkParents) {
750
			for ($control = $this; $control !== null; $control = $control->getParent()) {
751
				if ($control->_flags & self::IS_DISABLE_VIEWSTATE) {
752
					return false;
753
				}
754
			}
755
			return true;
756
		} else {
757
			return !($this->_flags & self::IS_DISABLE_VIEWSTATE);
758
		}
759
	}
760
761
	/**
762
	 * @param bool $value set whether to enable viewstate
763
	 */
764
	public function setEnableViewState($value)
765
	{
766
		if (TPropertyValue::ensureBoolean($value)) {
767
			$this->_flags &= ~self::IS_DISABLE_VIEWSTATE;
768
		} else {
769
			$this->_flags |= self::IS_DISABLE_VIEWSTATE;
770
		}
771
	}
772
773
	/**
774
	 * Returns a controlstate value.
775
	 *
776
	 * This function is mainly used in defining getter functions for control properties
777
	 * that must be kept in controlstate.
778
	 * @param string $key the name of the controlstate value to be returned
779
	 * @param mixed $defaultValue the default value. If $key is not found in controlstate, $defaultValue will be returned
780
	 * @return mixed the controlstate value corresponding to $key
781
	 */
782
	protected function getControlState($key, $defaultValue = null)
783
	{
784
		return $this->_rf[self::RF_CONTROLSTATE][$key] ?? $defaultValue;
785
	}
786
787
	/**
788
	 * Sets a controlstate value.
789
	 *
790
	 * This function is very useful in defining setter functions for control properties
791 14
	 * that must be kept in controlstate.
792
	 * Make sure that the controlstate value must be serializable and unserializable.
793 14
	 * @param string $key the name of the controlstate value
794 12
	 * @param mixed $value the controlstate value to be set
795 7
	 * @param null|mixed $defaultValue default value. If $value===$defaultValue, the item will be cleared from controlstate
796
	 */
797
	protected function setControlState($key, $value, $defaultValue = null)
798
	{
799
		if ($value === $defaultValue) {
800
			unset($this->_rf[self::RF_CONTROLSTATE][$key]);
801 7
		} else {
802
			$this->_rf[self::RF_CONTROLSTATE][$key] = $value;
803
		}
804
	}
805
806
	/**
807
	 * Clears a controlstate value.
808
	 * @param string $key the name of the controlstate value to be cleared
809
	 */
810
	protected function clearControlState($key)
811
	{
812
		unset($this->_rf[self::RF_CONTROLSTATE][$key]);
813
	}
814
815 13
	/**
816
	 * Sets a value indicating whether we should keep data in viewstate.
817 13
	 * When it is false, data saved via setViewState() will not be persisted.
818 13
	 * By default, it is true, meaning data will be persisted across postbacks.
819 13
	 * @param bool $enabled whether data should be persisted
820
	 */
821
	public function trackViewState($enabled)
822
	{
823
		$this->_trackViewState = TPropertyValue::ensureBoolean($enabled);
824
	}
825
826
	/**
827
	 * Returns a viewstate value.
828 13
	 *
829
	 * This function is very useful in defining getter functions for component properties
830
	 * that must be kept in viewstate.
831
	 * @param string $key the name of the viewstate value to be returned
832
	 * @param mixed $defaultValue the default value. If $key is not found in viewstate, $defaultValue will be returned
833
	 * @return mixed the viewstate value corresponding to $key
834 1
	 */
835
	public function getViewState($key, $defaultValue = null)
836 1
	{
837 1
		if (isset($this->_viewState[$key])) {
838 1
			return $this->_viewState[$key] !== null ? $this->_viewState[$key] : $defaultValue;
839
		} elseif (isset($this->_tempState[$key])) {
840
			if (is_object($this->_tempState[$key]) && $this->_trackViewState) {
841
				$this->_viewState[$key] = $this->_tempState[$key];
842
			}
843
			return $this->_tempState[$key];
844
		} else {
845
			return $defaultValue;
846
		}
847
	}
848
849
	/**
850
	 * Sets a viewstate value.
851
	 *
852
	 * This function is very useful in defining setter functions for control properties
853
	 * that must be kept in viewstate.
854
	 * Make sure that the viewstate value must be serializable and unserializable.
855
	 * @param string $key the name of the viewstate value
856
	 * @param mixed $value the viewstate value to be set
857
	 * @param null|mixed $defaultValue default value. If $value===$defaultValue, the item will be cleared from the viewstate.
858
	 */
859
	public function setViewState($key, $value, $defaultValue = null)
860
	{
861
		if ($this->_trackViewState) {
862
			unset($this->_tempState[$key]);
863
			$this->_viewState[$key] = $value;
864
		} else {
865
			unset($this->_viewState[$key]);
866
			if ($value === $defaultValue) {
867
				unset($this->_tempState[$key]);
868
			} else {
869
				$this->_tempState[$key] = $value;
870
			}
871
		}
872
	}
873
874
	/**
875
	 * Clears a viewstate value.
876
	 * @param string $key the name of the viewstate value to be cleared
877
	 */
878
	public function clearViewState($key)
879
	{
880
		unset($this->_viewState[$key]);
881
		unset($this->_tempState[$key]);
882
	}
883
884
	/**
885
	 * Sets up the binding between a property (or property path) and an expression.
886 1
	 * The context of the expression is the template control (or the control itself if it is a page).
887
	 * @param string $name the property name, or property path
888 1
	 * @param string $expression the expression
889 1
	 */
890
	public function bindProperty($name, $expression)
891
	{
892
		$this->_rf[self::RF_DATA_BINDINGS][$name] = $expression;
893
	}
894
895
	/**
896
	 * Breaks the binding between a property (or property path) and an expression.
897 1
	 * @param string $name the property name (or property path)
898
	 */
899
	public function unbindProperty($name)
900
	{
901
		unset($this->_rf[self::RF_DATA_BINDINGS][$name]);
902
	}
903
904
	/**
905
	 * Sets up the binding between a property (or property path) and an expression.
906
	 * Unlike regular databinding, the expression bound by this method
907
	 * is automatically evaluated during {@link prerenderRecursive()}.
908
	 * The context of the expression is the template control (or the control itself if it is a page).
909
	 * @param string $name the property name, or property path
910
	 * @param string $expression the expression
911
	 */
912
	public function autoBindProperty($name, $expression)
913
	{
914
		$this->_rf[self::RF_AUTO_BINDINGS][$name] = $expression;
915
	}
916
917
	/**
918
	 * Performs the databinding for this control.
919
	 */
920
	public function dataBind()
921
	{
922
		$this->dataBindProperties();
923
		$this->onDataBinding(null);
924
		$this->dataBindChildren();
925
	}
926
927
	/**
928
	 * Databinding properties of the control.
929
	 */
930
	protected function dataBindProperties()
931
	{
932
		Prado::trace("Data bind properties", 'Prado\Web\UI\TControl');
933
		if (isset($this->_rf[self::RF_DATA_BINDINGS])) {
934
			if (($context = $this->getTemplateControl()) === null) {
935
				$context = $this;
936
			}
937
			foreach ($this->_rf[self::RF_DATA_BINDINGS] as $property => $expression) {
938
				$this->setSubProperty($property, $context->evaluateExpression($expression));
939
			}
940
		}
941
	}
942
943
	/**
944
	 * Auto databinding properties of the control.
945
	 */
946
	protected function autoDataBindProperties()
947
	{
948
		if (isset($this->_rf[self::RF_AUTO_BINDINGS])) {
949
			if (($context = $this->getTemplateControl()) === null) {
950
				$context = $this;
951
			}
952
			foreach ($this->_rf[self::RF_AUTO_BINDINGS] as $property => $expression) {
953
				$this->setSubProperty($property, $context->evaluateExpression($expression));
954
			}
955
		}
956
	}
957
958
	/**
959
	 * Databinding child controls.
960
	 */
961
	protected function dataBindChildren()
962
	{
963
		Prado::trace("dataBindChildren()", 'Prado\Web\UI\TControl');
964
		if (isset($this->_rf[self::RF_CONTROLS])) {
965
			foreach ($this->_rf[self::RF_CONTROLS] as $control) {
966
				if ($control instanceof IBindable) {
967
					$control->dataBind();
968
				}
969
			}
970
		}
971
	}
972
973
	/**
974
	 * @return bool whether child controls have been created
975
	 */
976
	final protected function getChildControlsCreated()
977
	{
978
		return ($this->_flags & self::IS_CHILD_CREATED) !== 0;
979
	}
980
981
	/**
982
	 * Sets a value indicating whether child controls are created.
983
	 * If false, any existing child controls will be cleared up.
984
	 * @param bool $value whether child controls are created
985
	 */
986
	final protected function setChildControlsCreated($value)
987
	{
988
		if ($value) {
989
			$this->_flags |= self::IS_CHILD_CREATED;
990
		} else {
991
			if ($this->getHasControls() && ($this->_flags & self::IS_CHILD_CREATED)) {
992
				$this->getControls()->clear();
993
			}
994
			$this->_flags &= ~self::IS_CHILD_CREATED;
995
		}
996
	}
997
998
	/**
999
	 * Ensures child controls are created.
1000
	 * If child controls are not created yet, this method will invoke
1001
	 * {@link createChildControls} to create them.
1002
	 */
1003
	public function ensureChildControls()
1004
	{
1005
		if (!($this->_flags & self::IS_CHILD_CREATED) && !($this->_flags & self::IS_CREATING_CHILD)) {
1006
			try {
1007
				$this->_flags |= self::IS_CREATING_CHILD;
1008
				if (isset($this->_rf[self::RF_ADAPTER])) {
1009
					$this->_rf[self::RF_ADAPTER]->createChildControls();
1010
				} else {
1011
					$this->createChildControls();
1012
				}
1013
				$this->_flags &= ~self::IS_CREATING_CHILD;
1014
				$this->_flags |= self::IS_CHILD_CREATED;
1015
			} catch (Exception $e) {
1016
				$this->_flags &= ~self::IS_CREATING_CHILD;
1017
				$this->_flags |= self::IS_CHILD_CREATED;
1018
				throw $e;
1019
			}
1020
		}
1021
	}
1022
1023
	/**
1024
	 * Creates child controls.
1025
	 * This method can be overriden for controls who want to have their controls.
1026
	 * Do not call this method directly. Instead, call {@link ensureChildControls}
1027
	 * to ensure child controls are created only once.
1028
	 */
1029
	public function createChildControls()
1030
	{
1031
	}
1032
1033
	/**
1034
	 * Finds a control by ID path within the current naming container.
1035
	 * The current naming container is either the control itself
1036
	 * if it implements {@link INamingContainer} or the control's naming container.
1037
	 * The ID path is an ID sequence separated by {@link TControl::ID_SEPARATOR}.
1038
	 * For example, 'Repeater1.Item1.Button1' looks for a control with ID 'Button1'
1039
	 * whose naming container is 'Item1' whose naming container is 'Repeater1'.
1040
	 * @param string $id ID of the control to be looked up
1041
	 * @throws TInvalidDataValueException if a control's ID is found not unique within its naming container.
1042
	 * @return null|mixed the control found, null if not found
1043
	 */
1044
	public function findControl($id)
1045
	{
1046
		$id = strtr($id, '.', self::ID_SEPARATOR);
1047
		$container = ($this instanceof INamingContainer) ? $this : $this->getNamingContainer();
1048
		if (!$container || !$container->getHasControls()) {
1049
			return null;
1050
		}
1051
		if (!isset($container->_rf[self::RF_NAMED_CONTROLS])) {
1052
			$container->_rf[self::RF_NAMED_CONTROLS] = [];
1053
			$container->fillNameTable($container, $container->_rf[self::RF_CONTROLS]);
1054
		}
1055
		if (($pos = strpos($id, self::ID_SEPARATOR)) === false) {
1056
			return $container->_rf[self::RF_NAMED_CONTROLS][$id] ?? null;
1057
		} else {
1058
			$cid = substr($id, 0, $pos);
1059
			$sid = substr($id, $pos + 1);
1060
			if (isset($container->_rf[self::RF_NAMED_CONTROLS][$cid])) {
1061
				return $container->_rf[self::RF_NAMED_CONTROLS][$cid]->findControl($sid);
1062
			} else {
1063
				return null;
1064
			}
1065
		}
1066
	}
1067
1068
	/**
1069
	 * Finds all child and grand-child controls that are of the specified type.
1070
	 * @param string $type the class name
1071
	 * @param bool $strict whether the type comparison is strict or not. If false, controls of the parent classes of the specified class will also be returned.
1072
	 * @return array list of controls found
1073
	 */
1074
	public function findControlsByType($type, $strict = true)
1075
	{
1076
		$controls = [];
1077
		if ($this->getHasControls()) {
1078
			foreach ($this->_rf[self::RF_CONTROLS] as $control) {
1079
				if (is_object($control) && (get_class($control) === $type || (!$strict && ($control instanceof $type)))) {
1080
					$controls[] = $control;
1081
				}
1082
				if (($control instanceof TControl) && $control->getHasControls()) {
1083
					$controls = array_merge($controls, $control->findControlsByType($type, $strict));
1084
				}
1085
			}
1086
		}
1087
		return $controls;
1088
	}
1089
1090
	/**
1091
	 * Finds all child and grand-child controls with the specified ID.
1092
	 * Note, this method is different from {@link findControl} in that
1093
	 * it searches through all controls that have this control as the ancestor
1094
	 * while {@link findcontrol} only searches through controls that have this
1095
	 * control as the direct naming container.
1096
	 * @param string $id the ID being looked for
1097
	 * @return array list of controls found
1098
	 */
1099
	public function findControlsByID($id)
1100
	{
1101
		$controls = [];
1102
		if ($this->getHasControls()) {
1103
			foreach ($this->_rf[self::RF_CONTROLS] as $control) {
1104
				if ($control instanceof TControl) {
1105
					if ($control->_id === $id) {
1106
						$controls[] = $control;
1107
					}
1108
					$controls = array_merge($controls, $control->findControlsByID($id));
1109
				}
1110
			}
1111
		}
1112
		return $controls;
1113
	}
1114
1115
	/**
1116
	 * Resets the control as a naming container.
1117
	 * Only framework developers should use this method.
1118
	 */
1119
	public function clearNamingContainer()
1120 1
	{
1121
		unset($this->_rf[self::RF_NAMED_CONTROLS_ID]);
1122 1
		$this->clearNameTable();
1123
	}
1124
1125
	/**
1126
	 * Registers an object by a name.
1127
	 * A registered object can be accessed like a public member variable.
1128
	 * This method should only be used by framework and control developers.
1129
	 * @param string $name name of the object
1130
	 * @param object $object object to be declared
1131
	 * @see __get
1132
	 */
1133
	public function registerObject($name, $object)
1134
	{
1135
		if (isset($this->_rf[self::RF_NAMED_OBJECTS][$name])) {
1136
			throw new TInvalidOperationException('control_object_reregistered', $name);
1137
		}
1138
		$this->_rf[self::RF_NAMED_OBJECTS][$name] = $object;
1139
	}
1140
1141
	/**
1142
	 * Unregisters an object by name.
1143
	 * @param string $name name of the object
1144
	 * @see registerObject
1145
	 */
1146
	public function unregisterObject($name)
1147
	{
1148
		unset($this->_rf[self::RF_NAMED_OBJECTS][$name]);
1149
	}
1150
1151
	/**
1152
	 * @see registerObject
1153
	 * @param mixed $name
1154
	 * @return bool whether an object has been registered with the name
1155
	 */
1156
	public function isObjectRegistered($name)
1157
	{
1158
		return isset($this->_rf[self::RF_NAMED_OBJECTS][$name]);
1159
	}
1160
1161
	/**
1162
	 * @return bool true if the child control has been initialized.
1163
	 */
1164
	public function getHasChildInitialized()
1165
	{
1166
		return $this->getControlStage() >= self::CS_CHILD_INITIALIZED;
1167
	}
1168
1169
	/**
1170
	 * @return bool true if the onInit event has raised.
1171
	 */
1172
	public function getHasInitialized()
1173
	{
1174
		return $this->getControlStage() >= self::CS_INITIALIZED;
1175
	}
1176
1177
	/**
1178
	 * @return bool true if the control has loaded post data.
1179
	 */
1180
	public function getHasLoadedPostData()
1181
	{
1182
		return $this->getControlStage() >= self::CS_STATE_LOADED;
1183
	}
1184
1185
	/**
1186
	 * @return bool true if the onLoad event has raised.
1187
	 */
1188
	public function getHasLoaded()
1189
	{
1190
		return $this->getControlStage() >= self::CS_LOADED;
1191
	}
1192
1193
	/**
1194
	 * @return bool true if onPreRender event has raised.
1195
	 */
1196
	public function getHasPreRendered()
1197
	{
1198
		return $this->getControlStage() >= self::CS_PRERENDERED;
1199
	}
1200
1201
	/**
1202
	 * Returns the named registered object.
1203
	 * A component with explicit ID on a template will be registered to
1204
	 * the template owner. This method allows you to obtain this component
1205
	 * with the ID.
1206
	 * @param mixed $name
1207
	 * @return mixed the named registered object. Null if object is not found.
1208
	 */
1209
	public function getRegisteredObject($name)
1210
	{
1211
		return $this->_rf[self::RF_NAMED_OBJECTS][$name] ?? null;
1212
	}
1213
1214
	/**
1215
	 * @return bool whether body contents are allowed for this control. Defaults to true.
1216
	 */
1217
	public function getAllowChildControls()
1218
	{
1219
		return true;
1220
	}
1221
1222
	/**
1223
	 * Adds the object instantiated on a template to the child control collection.
1224
	 * This method overrides the parent implementation.
1225
	 * Only framework developers and control developers should use this method.
1226
	 * @param \Prado\TComponent|string $object text string or component parsed and instantiated in template
1227
	 * @see createdOnTemplate
1228
	 */
1229
	public function addParsedObject($object)
1230
	{
1231
		$this->getControls()->add($object);
1232
	}
1233
1234
	/**
1235
	 * Clears up the child state data.
1236
	 * After a control loads its state, those state that do not belong to
1237
	 * any existing child controls are stored as child state.
1238
	 * This method will remove these state.
1239
	 * Only frameworker developers and control developers should use this method.
1240
	 */
1241
	final protected function clearChildState()
1242
	{
1243
		unset($this->_rf[self::RF_CHILD_STATE]);
1244
	}
1245
1246
	/**
1247
	 * @param \Prado\Web\UI\TControl $ancestor the potential ancestor control
1248
	 * @return bool if the control is a descendent (parent, parent of parent, etc.)
1249
	 * of the specified control
1250
	 */
1251
	final protected function isDescendentOf($ancestor)
1252
	{
1253
		$control = $this;
1254
		while ($control !== $ancestor && $control->_parent) {
1255
			$control = $control->_parent;
1256
		}
1257
		return $control === $ancestor;
1258
	}
1259
1260
	/**
1261
	 * Adds a control into the child collection of the control.
1262
	 * Control lifecycles will be caught up during the addition.
1263
	 * Only framework developers should use this method.
1264
	 * @param \Prado\Web\UI\TControl $control the new child control
1265
	 */
1266
	public function addedControl($control)
1267
	{
1268
		if ($control->_parent) {
1269
			$control->_parent->getControls()->remove($control);
1270
		}
1271
		$control->_parent = $this;
1272
		$control->_page = $this->getPage();
1273
		$namingContainer = ($this instanceof INamingContainer) ? $this : $this->_namingContainer;
1274
		if ($namingContainer) {
1275
			$control->_namingContainer = $namingContainer;
1276
			if ($control->_id === '') {
1277
				$control->generateAutomaticID();
1278
			} else {
1279
				$namingContainer->clearNameTable();
1280
			}
1281
			$control->clearCachedUniqueID($control instanceof INamingContainer);
1282
		}
1283
1284
		if ($this->_stage >= self::CS_CHILD_INITIALIZED) {
1285
			$control->initRecursive($namingContainer);
1286
			if ($this->_stage >= self::CS_STATE_LOADED) {
1287
				if (isset($this->_rf[self::RF_CHILD_STATE][$control->_id])) {
1288
					$state = $this->_rf[self::RF_CHILD_STATE][$control->_id];
1289
					unset($this->_rf[self::RF_CHILD_STATE][$control->_id]);
1290
				} else {
1291
					$state = null;
1292
				}
1293
				$control->loadStateRecursive($state, !($this->_flags & self::IS_DISABLE_VIEWSTATE));
1294
				if ($this->_stage >= self::CS_LOADED) {
1295
					$control->loadRecursive();
1296
					if ($this->_stage >= self::CS_PRERENDERED) {
1297
						$control->preRenderRecursive();
1298
					}
1299
				}
1300
			}
1301
		}
1302
	}
1303
1304
	/**
1305
	 * Removes a control from the child collection of the control.
1306
	 * Only framework developers should use this method.
1307
	 * @param \Prado\Web\UI\TControl $control the child control removed
1308
	 */
1309
	public function removedControl($control)
1310
	{
1311
		if ($this->_namingContainer) {
1312
			$this->_namingContainer->clearNameTable();
1313
		}
1314
		$control->unloadRecursive();
1315
		$control->_parent = null;
1316
		$control->_page = null;
1317
		$control->_namingContainer = null;
1318
		$control->_tplControl = null;
1319
		//$control->_stage=self::CS_CONSTRUCTED;
1320
		if (!($control->_flags & self::IS_ID_SET)) {
1321
			$control->_id = '';
1322
		} else {
1323
			unset($this->_rf[self::RF_NAMED_OBJECTS][$control->_id]);
1324
		}
1325
		$control->clearCachedUniqueID(true);
1326
	}
1327
1328
	/**
1329
	 * Performs the Init step for the control and all its child controls.
1330
	 * Only framework developers should use this method.
1331
	 * @param \Prado\Web\UI\TControl $namingContainer the naming container control
1332
	 */
1333
	protected function initRecursive($namingContainer = null)
1334
	{
1335
		$this->ensureChildControls();
1336
		if ($this->getHasControls()) {
1337
			if ($this instanceof INamingContainer) {
1338
				$namingContainer = $this;
1339
			}
1340
			$page = $this->getPage();
1341
			foreach ($this->_rf[self::RF_CONTROLS] as $control) {
1342
				if ($control instanceof TControl) {
1343
					$control->_namingContainer = $namingContainer;
1344
					$control->_page = $page;
1345
					if ($control->_id === '' && $namingContainer) {
1346
						$control->generateAutomaticID();
1347
					}
1348
					$control->initRecursive($namingContainer);
1349
				}
1350
			}
1351
		}
1352
		if ($this->_stage < self::CS_INITIALIZED) {
1353
			$this->_stage = self::CS_CHILD_INITIALIZED;
1354
			if (($page = $this->getPage()) && $this->getEnableTheming() && !($this->_flags & self::IS_SKIN_APPLIED)) {
1355
				$page->applyControlSkin($this);
1356
				$this->_flags |= self::IS_SKIN_APPLIED;
1357
			}
1358
			if (isset($this->_rf[self::RF_ADAPTER])) {
1359
				$this->_rf[self::RF_ADAPTER]->onInit(null);
1360
			} else {
1361
				$this->onInit(null);
1362
			}
1363
			$this->_stage = self::CS_INITIALIZED;
1364
		}
1365
	}
1366
1367
	/**
1368
	 * Performs the Load step for the control and all its child controls.
1369
	 * Only framework developers should use this method.
1370
	 */
1371
	protected function loadRecursive()
1372
	{
1373
		if ($this->_stage < self::CS_LOADED) {
1374
			if (isset($this->_rf[self::RF_ADAPTER])) {
1375
				$this->_rf[self::RF_ADAPTER]->onLoad(null);
1376
			} else {
1377
				$this->onLoad(null);
1378
			}
1379
		}
1380
		if ($this->getHasControls()) {
1381
			foreach ($this->_rf[self::RF_CONTROLS] as $control) {
1382
				if ($control instanceof TControl) {
1383
					$control->loadRecursive();
1384
				}
1385
			}
1386
		}
1387
		if ($this->_stage < self::CS_LOADED) {
1388
			$this->_stage = self::CS_LOADED;
1389
		}
1390
	}
1391
1392
	/**
1393
	 * Performs the PreRender step for the control and all its child controls.
1394
	 * Only framework developers should use this method.
1395
	 */
1396
	protected function preRenderRecursive()
1397
	{
1398
		$this->autoDataBindProperties();
1399
1400
		if ($this->getVisible(false)) {
1401
			if (isset($this->_rf[self::RF_ADAPTER])) {
1402
				$this->_rf[self::RF_ADAPTER]->onPreRender(null);
1403
			} else {
1404
				$this->onPreRender(null);
1405
			}
1406
			if ($this->getHasControls()) {
1407
				foreach ($this->_rf[self::RF_CONTROLS] as $control) {
1408
					if ($control instanceof TControl) {
1409
						$control->preRenderRecursive();
1410
					} elseif ($control instanceof TCompositeLiteral) {
1411
						$control->evaluateDynamicContent();
1412
					}
1413
				}
1414
			}
1415
		}
1416
		$this->_stage = self::CS_PRERENDERED;
1417
	}
1418
1419
	/**
1420
	 * Performs the Unload step for the control and all its child controls.
1421
	 * Only framework developers should use this method.
1422
	 */
1423
	protected function unloadRecursive()
1424
	{
1425
		if (!($this->_flags & self::IS_ID_SET)) {
1426
			$this->_id = '';
1427 1
		}
1428
		if ($this->getHasControls()) {
1429 1
			foreach ($this->_rf[self::RF_CONTROLS] as $control) {
1430 1
				if ($control instanceof TControl) {
1431 1
					$control->unloadRecursive();
1432
				}
1433
			}
1434
		}
1435
		if (isset($this->_rf[self::RF_ADAPTER])) {
1436
			$this->_rf[self::RF_ADAPTER]->onUnload(null);
1437
		} else {
1438
			$this->onUnload(null);
1439
		}
1440
	}
1441
1442
	/**
1443
	 * This method is invoked when the control enters 'OnInit' stage.
1444
	 * The method raises 'OnInit' event.
1445
	 * If you override this method, be sure to call the parent implementation
1446
	 * so that the event handlers can be invoked.
1447
	 * @param \Prado\TEventParameter $param event parameter to be passed to the event handlers
1448
	 */
1449
	public function onInit($param)
1450
	{
1451
		$this->raiseEvent('OnInit', $this, $param);
1452
	}
1453
1454
	/**
1455
	 * This method is invoked when the control enters 'OnLoad' stage.
1456
	 * The method raises 'OnLoad' event.
1457
	 * If you override this method, be sure to call the parent implementation
1458
	 * so that the event handlers can be invoked.
1459
	 * @param \Prado\TEventParameter $param event parameter to be passed to the event handlers
1460
	 */
1461
	public function onLoad($param)
1462
	{
1463
		$this->raiseEvent('OnLoad', $this, $param);
1464
	}
1465
1466
	/**
1467
	 * Raises 'OnDataBinding' event.
1468
	 * This method is invoked when {@link dataBind} is invoked.
1469
	 * @param \Prado\TEventParameter $param event parameter to be passed to the event handlers
1470
	 */
1471
	public function onDataBinding($param)
1472
	{
1473
		Prado::trace("onDataBinding()", 'Prado\Web\UI\TControl');
1474
		$this->raiseEvent('OnDataBinding', $this, $param);
1475
	}
1476
1477
1478
	/**
1479
	 * This method is invoked when the control enters 'OnUnload' stage.
1480
	 * The method raises 'OnUnload' event.
1481
	 * If you override this method, be sure to call the parent implementation
1482
	 * so that the event handlers can be invoked.
1483
	 * @param \Prado\TEventParameter $param event parameter to be passed to the event handlers
1484
	 */
1485
	public function onUnload($param)
1486
	{
1487
		$this->raiseEvent('OnUnload', $this, $param);
1488
	}
1489
1490
	/**
1491
	 * This method is invoked when the control enters 'OnPreRender' stage.
1492
	 * The method raises 'OnPreRender' event.
1493
	 * If you override this method, be sure to call the parent implementation
1494
	 * so that the event handlers can be invoked.
1495
	 * @param \Prado\TEventParameter $param event parameter to be passed to the event handlers
1496
	 */
1497
	public function onPreRender($param)
1498
	{
1499
		$this->raiseEvent('OnPreRender', $this, $param);
1500
	}
1501
1502
	/**
1503
	 * Invokes the parent's bubbleEvent method.
1504
	 * A control who wants to bubble an event must call this method in its onEvent method.
1505
	 * @param \Prado\Web\UI\TControl $sender sender of the event
1506
	 * @param \Prado\TEventParameter $param event parameter
1507
	 * @see bubbleEvent
1508
	 */
1509
	protected function raiseBubbleEvent($sender, $param)
1510
	{
1511
		$control = $this;
1512
		while ($control = $control->_parent) {
1513
			if ($control->bubbleEvent($sender, $param)) {
1514
				break;
1515
			}
1516
		}
1517
	}
1518
1519
	/**
1520
	 * This method responds to a bubbled event.
1521
	 * This method should be overriden to provide customized response to a bubbled event.
1522
	 * Check the type of event parameter to determine what event is bubbled currently.
1523
	 * @param \Prado\Web\UI\TControl $sender sender of the event
1524
	 * @param \Prado\TEventParameter $param event parameters
1525
	 * @return bool true if the event bubbling is handled and no more bubbling.
1526
	 * @see raiseBubbleEvent
1527
	 */
1528
	public function bubbleEvent($sender, $param)
1529
	{
1530
		return false;
1531
	}
1532
1533
	/**
1534
	 * Broadcasts an event.
1535
	 * The event will be sent to all controls on the current page hierarchy.
1536
	 * If a control defines the event, the event will be raised for the control.
1537
	 * If a control implements {@link IBroadcastEventReceiver}, its
1538
	 * {@link IBroadcastEventReceiver::broadcastEventReceived broadcastEventReceived()} method will
1539
	 * be invoked which gives the control a chance to respond to the event.
1540
	 * For example, when broadcasting event 'OnClick', all controls having 'OnClick'
1541
	 * event will have this event raised, and all controls implementing
1542
	 * {@link IBroadcastEventReceiver} will also have its
1543
	 * {@link IBroadcastEventReceiver::broadcastEventReceived broadcastEventReceived()}
1544
	 * invoked.
1545
	 * @param string $name name of the broadcast event
1546
	 * @param \Prado\Web\UI\TControl $sender sender of this event
1547
	 * @param \Prado\TEventParameter $param event parameter
1548
	 */
1549
	public function broadcastEvent($name, $sender, $param)
1550
	{
1551
		$rootControl = (($page = $this->getPage()) === null) ? $this : $page;
1552
		$rootControl->broadcastEventInternal($name, $sender, new TBroadcastEventParameter($name, $param));
1553
	}
1554
1555
	/**
1556
	 * Recursively broadcasts an event.
1557
	 * This method should only be used by framework developers.
1558
	 * @param string $name name of the broadcast event
1559
	 * @param \Prado\Web\UI\TControl $sender sender of the event
1560
	 * @param TBroadcastEventParameter $param event parameter
1561
	 */
1562
	private function broadcastEventInternal($name, $sender, $param)
1563
	{
1564
		if ($this->hasEvent($name)) {
1565
			$this->raiseEvent($name, $sender, $param->getParameter());
1566
		}
1567
		if ($this instanceof IBroadcastEventReceiver) {
1568
			$this->broadcastEventReceived($sender, $param);
0 ignored issues
show
Bug introduced by
The method broadcastEventReceived() does not exist on Prado\Web\UI\TControl. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1568
			$this->/** @scrutinizer ignore-call */ 
1569
          broadcastEventReceived($sender, $param);
Loading history...
1569
		}
1570
		if ($this->getHasControls()) {
1571
			foreach ($this->_rf[self::RF_CONTROLS] as $control) {
1572
				if ($control instanceof TControl) {
1573
					$control->broadcastEventInternal($name, $sender, $param);
1574
				}
1575
			}
1576
		}
1577
	}
1578
1579
	/**
1580
	 * Traverse the whole control hierarchy rooted at this control.
1581
	 * Callback function may be invoked for each control being visited.
1582
	 * A pre-callback is invoked before traversing child controls;
1583
	 * A post-callback is invoked after traversing child controls.
1584
	 * Callback functions can be global functions or class methods.
1585
	 * They must be of the following signature:
1586
	 * <code>
1587
	 * function callback_func($control,$param) {...}
1588
	 * </code>
1589
	 * where $control refers to the control being visited and $param
1590
	 * is the parameter that is passed originally when calling this traverse function.
1591
	 *
1592
	 * @param mixed $param parameter to be passed to callbacks for each control
1593
	 * @param null|callable $preCallback callback invoked before traversing child controls. If null, it is ignored.
1594
	 * @param null|callable $postCallback callback invoked after traversing child controls. If null, it is ignored.
1595
	 */
1596
	protected function traverseChildControls($param, $preCallback = null, $postCallback = null)
1597
	{
1598
		if ($preCallback !== null) {
1599
			call_user_func($preCallback, $this, $param);
1600
		}
1601
		if ($this->getHasControls()) {
1602
			foreach ($this->_rf[self::RF_CONTROLS] as $control) {
1603
				if ($control instanceof TControl) {
1604
					$control->traverseChildControls($param, $preCallback, $postCallback);
1605
				}
1606
			}
1607
		}
1608
		if ($postCallback !== null) {
1609
			call_user_func($postCallback, $this, $param);
1610
		}
1611
	}
1612
1613
	/**
1614
	 * Renders the control.
1615
	 * Only when the control is visible will the control be rendered.
1616
	 * @param \Prado\Web\UI\THtmlWriter $writer the writer used for the rendering purpose
1617
	 */
1618
	public function renderControl($writer)
1619
	{
1620
		if ($this instanceof IActiveControl || $this->getVisible(false)) {
1621
			if (isset($this->_rf[self::RF_ADAPTER])) {
1622
				$this->_rf[self::RF_ADAPTER]->render($writer);
1623
			} else {
1624
				$this->render($writer);
1625
			}
1626
		}
1627
	}
1628
1629
	/**
1630
	 * Renders the control.
1631
	 * This method is invoked by {@link renderControl} when the control is visible.
1632
	 * You can override this method to provide customized rendering of the control.
1633
	 * By default, the control simply renders all its child contents.
1634
	 * @param \Prado\Web\UI\THtmlWriter $writer the writer used for the rendering purpose
1635
	 */
1636
	public function render($writer)
1637
	{
1638
		$this->renderChildren($writer);
1639
	}
1640
1641
	/**
1642
	 * Renders the children of the control.
1643
	 * This method iterates through all child controls and static text strings
1644
	 * and renders them in order.
1645
	 * @param \Prado\Web\UI\THtmlWriter $writer the writer used for the rendering purpose
1646
	 */
1647
	public function renderChildren($writer)
1648
	{
1649
		if ($this->getHasControls()) {
1650
			foreach ($this->_rf[self::RF_CONTROLS] as $control) {
1651
				if (is_string($control)) {
1652
					$writer->write($control);
1653
				} elseif ($control instanceof TControl) {
1654
					$control->renderControl($writer);
1655
				} elseif ($control instanceof IRenderable) {
1656
					$control->render($writer);
1657
				}
1658
			}
1659
		}
1660
	}
1661
1662
	/**
1663
	 * This method is invoked when control state is to be saved.
1664
	 * You can override this method to do last step state saving.
1665
	 * Parent implementation must be invoked.
1666
	 */
1667
	public function saveState()
1668
	{
1669
	}
1670
1671
	/**
1672
	 * This method is invoked right after the control has loaded its state.
1673
	 * You can override this method to initialize data from the control state.
1674
	 * Parent implementation must be invoked.
1675
	 */
1676
	public function loadState()
1677
	{
1678
	}
1679
1680
	/**
1681
	 * Loads state (viewstate and controlstate) into a control and its children.
1682
	 * This method should only be used by framework developers.
1683
	 * @param array $state the collection of the state
1684
	 * @param bool $needViewState whether the viewstate should be loaded
1685
	 */
1686
	protected function loadStateRecursive(&$state, $needViewState = true)
1687
	{
1688
		if (is_array($state)) {
0 ignored issues
show
introduced by
The condition is_array($state) is always true.
Loading history...
1689
			// A null state means the stateful properties all take default values.
1690
			// So if the state is enabled, we have to assign the null value.
1691
			$needViewState = ($needViewState && !($this->_flags & self::IS_DISABLE_VIEWSTATE));
1692
			if (isset($state[1])) {
1693
				$this->_rf[self::RF_CONTROLSTATE] = &$state[1];
1694
				unset($state[1]);
1695
			} else {
1696
				unset($this->_rf[self::RF_CONTROLSTATE]);
1697
			}
1698
			if ($needViewState) {
1699
				if (isset($state[0])) {
1700
					$this->_viewState = &$state[0];
1701
				} else {
1702
					$this->_viewState = [];
1703
				}
1704
			}
1705
			unset($state[0]);
1706
			if ($this->getHasControls()) {
1707
				foreach ($this->_rf[self::RF_CONTROLS] as $control) {
1708
					if ($control instanceof TControl) {
1709
						if (isset($state[$control->_id])) {
1710
							$control->loadStateRecursive($state[$control->_id], $needViewState);
1711
							unset($state[$control->_id]);
1712
						}
1713
					}
1714
				}
1715
			}
1716
			if (!empty($state)) {
1717
				$this->_rf[self::RF_CHILD_STATE] = &$state;
1718
			}
1719
		}
1720
		$this->_stage = self::CS_STATE_LOADED;
1721
		if (isset($this->_rf[self::RF_ADAPTER])) {
1722
			$this->_rf[self::RF_ADAPTER]->loadState();
1723
		} else {
1724
			$this->loadState();
1725
		}
1726
	}
1727
1728
	/**
1729
	 * Saves all control state (viewstate and controlstate) as a collection.
1730
	 * This method should only be used by framework developers.
1731
	 * @param bool $needViewState whether the viewstate should be saved
1732
	 * @return array the collection of the control state (including its children's state).
1733
	 */
1734
	protected function &saveStateRecursive($needViewState = true)
1735
	{
1736
		if (isset($this->_rf[self::RF_ADAPTER])) {
1737
			$this->_rf[self::RF_ADAPTER]->saveState();
1738
		} else {
1739
			$this->saveState();
1740
		}
1741
		$needViewState = ($needViewState && !($this->_flags & self::IS_DISABLE_VIEWSTATE));
1742
		$state = [];
1743
		if ($this->getHasControls()) {
1744
			foreach ($this->_rf[self::RF_CONTROLS] as $control) {
1745
				if ($control instanceof TControl) {
1746
					if (count($tmp = &$control->saveStateRecursive($needViewState))) {
1747
						$state[$control->_id] = $tmp;
1748
					}
1749
				}
1750
			}
1751
		}
1752
		if ($needViewState && !empty($this->_viewState)) {
1753
			$state[0] = &$this->_viewState;
1754
		}
1755
		if (isset($this->_rf[self::RF_CONTROLSTATE])) {
1756
			$state[1] = &$this->_rf[self::RF_CONTROLSTATE];
1757
		}
1758
		return $state;
1759
	}
1760
1761
	/**
1762
	 * Applies a stylesheet skin to a control.
1763
	 * @param TPage $page the page containing the control
1764
	 * @throws TInvalidOperationException if the stylesheet skin is applied already
1765
	 */
1766
	public function applyStyleSheetSkin($page)
1767
	{
1768
		if ($page && !($this->_flags & self::IS_STYLESHEET_APPLIED)) {
1769
			$page->applyControlStyleSheet($this);
1770
			$this->_flags |= self::IS_STYLESHEET_APPLIED;
1771
		} elseif ($this->_flags & self::IS_STYLESHEET_APPLIED) {
1772
			throw new TInvalidOperationException('control_stylesheet_applied', get_class($this));
1773
		}
1774
	}
1775
1776
	/**
1777
	 * Clears the cached UniqueID.
1778
	 * If $recursive=true, all children's cached UniqueID will be cleared as well.
1779
	 * @param bool $recursive whether the clearing is recursive.
1780
	 */
1781
	private function clearCachedUniqueID($recursive)
1782
	{
1783
		if ($recursive && $this->_uid !== null && isset($this->_rf[self::RF_CONTROLS])) {
1784
			foreach ($this->_rf[self::RF_CONTROLS] as $control) {
1785
				if ($control instanceof TControl) {
1786
					$control->clearCachedUniqueID($recursive);
1787
				}
1788
			}
1789
		}
1790
		$this->_uid = null;
1791
	}
1792
1793
	/**
1794
	 * Generates an automatic ID for the control.
1795
	 */
1796
	private function generateAutomaticID()
1797
	{
1798
		$this->_flags &= ~self::IS_ID_SET;
1799
		if (!isset($this->_namingContainer->_rf[self::RF_NAMED_CONTROLS_ID])) {
1800
			$this->_namingContainer->_rf[self::RF_NAMED_CONTROLS_ID] = 0;
1801
		}
1802
		$id = $this->_namingContainer->_rf[self::RF_NAMED_CONTROLS_ID]++;
1803
		$this->_id = self::AUTOMATIC_ID_PREFIX . $id;
1804
		$this->_namingContainer->clearNameTable();
1805
	}
1806
1807
	/**
1808
	 * Clears the list of the controls whose IDs are managed by the specified naming container.
1809
	 */
1810
	private function clearNameTable()
1811
	{
1812
		unset($this->_rf[self::RF_NAMED_CONTROLS]);
1813
	}
1814
1815
	/**
1816
	 * Updates the list of the controls whose IDs are managed by the specified naming container.
1817
	 * @param \Prado\Web\UI\TControl $container the naming container
1818
	 * @param TControlCollection $controls list of controls
1819
	 * @throws TInvalidDataValueException if a control's ID is not unique within its naming container.
1820
	 */
1821
	private function fillNameTable($container, $controls)
1822
	{
1823
		foreach ($controls as $control) {
1824
			if ($control instanceof TControl) {
1825
				if ($control->_id !== '') {
1826
					if (isset($container->_rf[self::RF_NAMED_CONTROLS][$control->_id])) {
1827
						throw new TInvalidDataValueException('control_id_nonunique', get_class($control), $control->_id);
1828
					} else {
1829
						$container->_rf[self::RF_NAMED_CONTROLS][$control->_id] = $control;
1830
					}
1831
				}
1832
				if (!($control instanceof INamingContainer) && $control->getHasControls()) {
1833
					$this->fillNameTable($container, $control->_rf[self::RF_CONTROLS]);
1834
				}
1835
			}
1836
		}
1837
	}
1838
}
1839