-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCtkNode.php
957 lines (887 loc) · 24.8 KB
/
CtkNode.php
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
<?php
/**
* Base node object for defining elements.
*
* PHP 5
*
* Cake Toolkit (http://caketoolkit.org)
* Copyright 2012, James Watts (http://github.com/jameswatts)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2012, James Watts (http://github.com/jameswatts)
* @link http://caketoolkit.org Cake Toolkit
* @package Ctk.Lib
* @since CakePHP(tm) v 2.2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('CakeValidationSet', 'Model/Validator');
App::uses('CtkBuildable', 'Ctk.Lib');
App::uses('CtkBindable', 'Ctk.Lib');
App::uses('CtkRenderable', 'Ctk.Lib');
App::uses('CtkObject', 'Ctk.Lib');
App::uses('CtkContent', 'Ctk.Lib');
App::uses('CtkFactory', 'Ctk.Lib');
App::uses('CtkEvent', 'Ctk.Lib');
App::uses('CtkHelperView', 'Ctk.View');
/**
* Abstract class representing a node object.
*
* @package Ctk.Lib
*/
abstract class CtkNode extends CtkObject implements CtkBuildable, CtkBindable, CtkRenderable {
/**
* The factory used to instanciate this object.
*
* @var CtkFactory The instance of the factory.
*/
protected $_factory = null;
/**
* The name of this object.
*
* @var string The name of the object.
*/
protected $_name = null;
/**
* The template to use for this object.
*
* @var string The name of the template.
*/
protected $_template = null;
/**
* The configuration parameters used by the template for this object.
*
* @var array The template configuration parameters.
*/
protected $_params = array();
/**
* The validation rules for configuration parameters, where the key is the parameter
* name and the value the rule as a string or an array.
*
* @var array The validation rules.
*/
protected $_validate = array();
/**
* The internal cache of validation sets used to validate parameters.
*
* @var array The validation sets.
*/
protected $_validation = array();
/**
* The errors logged from an invalid configuration parameter.
*
* @var array The validation errors.
*/
protected $_validationErrors = array();
/**
* The type of node this object represents.
*
* @var string The element type.
*/
protected $_nodeType = 'node';
/**
* The unique ID of the node.
*
* @var string The unique ID.
*/
protected $_nodeId = null;
/**
* The parent node for this object.
*
* @var CtkNode The parent node object.
*/
protected $_parentNode = null;
/**
* The child nodes on this object.
*
* @var array Collection of child node objects.
*/
protected $_childNodes = array();
/**
* The events registered on this object.
*
* @var array Collection of event types and objects.
*/
protected $_nodeEvents = array();
/**
* Determines if the node can have a parent.
*
* @var boolean Set to FALSE to block adding to other nodes.
*/
protected $_allowParent = true;
/**
* Limits the parent allowed for this node.
*
* @var array List of parents allowed by name, or NULL for no limit.
*/
protected $_limitParent = array();
/**
* Determines if the node accepts child nodes.
*
* @var boolean Set to FALSE to block adding child nodes.
*/
protected $_allowChildren = true;
/**
* Limits the children allowed on this node.
*
* @var array List of children allowed by name, or NULL for no limit.
*/
protected $_limitChildren = array();
/**
* Determines if the node accepts events.
*
* @var boolean Set to FALSE to block adding events.
*/
protected $_allowEvents = true;
/**
* Contructor
*
* Creates a new node with a unique ID and optional template configuration parameters.
*
* @param CtkFactory $factory The factory that created the node.
* @param array $params The optional configuration parameters for the template.
*/
final public function __construct(CtkFactory $factory, array $params = array()) {
parent::__construct();
$this->_factory = $factory;
$this->_name = str_replace($factory->getName(), '', get_class($this));
$this->_nodeId = uniqid('ID_');
$this->_inheritArrayProperties(array('_params', '_validate', '_limitParent', '_limitChildren'));
$this->setParams($params);
}
/**
* Returns a template configuration parameter.
*
* @param string $name Name of the configuration parameter.
* @return mixed
* @throws CakeException if the configuration parameter is undefined.
*/
final public function __get($name) {
if (array_key_exists($name, $this->_params)) {
return $this->_params[$name];
} else {
$factory = $this->getFactory();
if (property_exists($factory, $name) || array_key_exists($name, $factory->getHelpers())) {
return $factory->$name;
}
}
throw new CakeException(sprintf('Undefined parameter: %s', $name));
}
/**
* Set the value of a template configuration parameter. Accepts some special parameter names
* which match to internals of the object. These include:
*
* - "_type" string $value The node type to pass to setType().
* - "_id" string $value The unique ID to pass to setId().
* - "_parent" CtkBuildable $value The node to pass to setParent().
* - "_children" array $value An array of nodes or definitions to pass to addMany().
* - "_events" array $value An array of events, where keys are types and values CtkEvent objects.
*
* @param string $name Name of the configuration parameter.
* @param mixed $value Value of the configuration parameter.
* @throws CakeException if the value for "_events" is not an array or they specified value does not validate.
*/
final public function __set($name, $value = null) {
if ($name === '_type') {
$this->setType($value);
} else if ($name === '_id') {
$this->setId($value);
} else if ($name === '_parent') {
$this->setParent($value);
} else if ($name === '_children') {
$this->addMany($value);
} else if ($name === '_events') {
if (is_array($value)) {
foreach ($value as $type => $event) {
$this->bind($type, $event);
}
} else {
throw new CakeException('Value for "_events" parameter must be an array');
}
} else {
if (isset($this->_validate[(string) $name])) {
if (!$this->validateParam($name, $value)) {
throw new CakeException(sprintf('Value of "%s" parameter for %s is invalid, ' . implode(', ', $this->_validationErrors), $name, get_class($this)));
}
}
$this->_params[(string) $name] = $value;
}
}
/**
* Determines if a template configuration parameter has been defined.
*
* @param string $name Name of the configuration parameter.
* @return mixed
*/
final public function __isset($name) {
return (isset($this->_params[(string) $name]));
}
/**
* Requests an instance of an object on the common factory.
*
* @param string $name Name of object to create.
* @param array $arguments The method arguments.
* @return CtkBuildable
*/
final public function __call($name, $arguments) {
return $this->add(call_user_func_array(array($this->_factory, $name), $arguments));
}
/**
* Renders the node if called as a string.
*
* @return string
*/
final public function __toString() {
try {
if ($this->hasParent()) {
$parent = $this->getParent();
while ($parent->hasParent()) {
$parent = $parent->getParent();
}
return $parent->render();
}
return $this->render();
} catch(Exception $e) {
trigger_error($e->getMessage(), E_USER_ERROR);
}
}
/**
* Returns the name of the object.
*
* @return string
*/
final public function getName() {
return $this->_name;
}
/**
* Returns the factory which created the node.
*
* @return CtkFactory
*/
final public function getFactory() {
return $this->_factory;
}
/**
* Returns the name of the template for the node.
*
* @return string
*/
final public function getTemplate() {
return $this->_template;
}
/**
* Returns the configuration parameters for the template.
*
* @return array
*/
final public function getParams() {
return $this->_params;
}
/**
* Sets additional configuration parameters for the template.
*
* @param array $params The configuration paramaters.
* @return CtkBuildable
*/
final public function setParams(array $params = array()) {
foreach ($params as $name => $value) {
$this->__set((string) $name, $value);
}
return $this;
}
/**
* Validates the configuration parameter.
*
* @param string $name Name of the configuration parameter.
* @param mixed $value Value of the configuration parameter.
* @return boolean
*/
final public function validateParam($name, $value = null) {
if (isset($this->_validate[$name])) {
if (!isset($this->_validation[$name])) {
$this->_validation[$name] = new CakeValidationSet($name, $this->_validate[$name]);
}
$errors = $this->_validation[$name]->validate(array($name => $value));
if (count($errors) > 0) {
foreach ($errors as $error) {
$this->_validationErrors[] = str_replace('field', 'parameter', lcfirst($error));
}
return false;
}
}
return true;
}
/**
* Returns the node type.
*
* @return string
*/
final public function getType() {
return $this->_nodeType;
}
/**
* Sets the node type.
*
* @param string $type Node type.
* @return CtkBuildable
*/
final public function setType($type) {
$this->_nodeType = (string) $type;
return $this;
}
/**
* Gets the unique ID for this element.
*
* @return string
*/
final public function getId() {
return $this->_nodeId;
}
/**
* Sets the unique ID for the element.
*
* @param string $id The unique ID for this element.
* @return CtkBuildable
*/
final public function setId($id) {
$this->_nodeId = (string) $id;
return $this;
}
/**
* Determines if the node allows a parent node.
*
* @return boolean
*/
final public function allowsParent() {
return $this->_allowParent;
}
/**
* Determines if the node has a parent node.
*
* @return boolean
*/
final public function hasParent() {
return ($this->_parentNode !== null);
}
/**
* Returns the parent node of this node.
*
* @return CtkBuildable
*/
final public function getParent() {
return $this->_parentNode;
}
/**
* Sets the parent node for this node.
*
* @param CtkBuildable $node The parent node.
* @return CtkBuildable
* @throws CakeException if node is not allowed a parent or the parent node class is not allowed.
*/
final public function setParent(CtkBuildable $node = null) {
if (!$this->_allowParent) {
throw new CakeException(sprintf('Parent %s not allowed for %s', get_Class($node), get_Class($this)));
} else if (is_array($this->_limitParent) && count($this->_limitParent) > 0 && !in_array(get_class($node), $this->_limitParent)) {
throw new CakeException(sprintf('Invalid parent %s for %s, must be %s', get_class($node), get_class($this), implode(', ', $this->_limitParent)));
} else if ($this === $node) {
throw new CakeException('Child cannot be parent of itself');
} else {
$this->_parentNode = $node;
}
return $this;
}
/**
* Determines if the node is allowed children.
*
* @return boolean
*/
final public function allowsChildren() {
return $this->_allowChildren;
}
/**
* Determines if the node has child nodes.
*
* @return boolean
*/
final public function hasChildren() {
return (count($this->_childNodes) > 0);
}
/**
* Determines if a node is a child of this node.
*
* @return boolean
*/
final public function hasChild(CtkBuildable $node) {
if ($this !== $node) {
for ($i = 0; $i < count($this->_childNodes); $i++) {
if ($this->_childNodes[$i] === $node) {
return true;
}
}
}
return false;
}
/**
* Returns the child nodes of this node as an array.
*
* @return array
*/
final public function getChildren() {
return $this->_childNodes;
}
/**
* Returns the first child node of this node.
*
* @return CtkBuildable or null if no children exist
*/
final public function getFirst() {
if ($this->hasChildren()) {
return $this->_childNodes[0];
}
return null;
}
/**
* Returns the last child node of this node.
*
* @return CtkBuildable or null if no children exist
*/
final public function getLast() {
if ($this->hasChildren()) {
return $this->_childNodes[count($this->_childNodes)-1];
}
return null;
}
/**
* Returns the previous node before this node in the common parent.
*
* @return CtkBuildable or null if no node before this node
*/
final public function getPrevious() {
$children = $this->getParent()->getChildren();
$count = count($children);
if ($count > 1) {
for ($i = 0; $i < $count; $i++) {
if ($children[$i] === $this) {
return ($i < 1)? null : $children[$i-1];
}
}
}
return null;
}
/**
* Returns the next node after this node in the common parent.
*
* @return CtkBuildable or null if no node after this node
*/
final public function getNext() {
$children = $this->getParent()->getChildren();
$count = count($children);
if ($count > 1) {
for ($i = 0; $i < $count; $i++) {
if ($children[$i] === $this) {
return ($i == $count-1)? null : $children[$i+1];
}
}
}
return null;
}
/**
* Executes a callback function on each of the child nodes.
*
* @param callable $callback The callback function to use.
* @param array $data The optional array of data to be used by the callback function.
* @param boolean|int $deep Determines if applies to all children of children, or if an integer, defines the max depth.
* @return CtkBuildable
* @throws CakeException if the callback function is not callable.
*/
final public function each($callback, array $data = array(), $deep = false) {
if (!is_callable($callback)) {
throw new CakeException('Callback function must be callable');
}
if ($this->hasChildren()) {
$children = $this->getChildren();
$view = $this->_factory->getView();
for ($i = 0; $i < count($children); $i++) {
$break = call_user_func_array($callback, array($children[$i], $this, $view, $data, $i));
if (!is_null($break) && $break === true) {
break;
}
if ($deep && $children[$i]->hasChildren()) {
$children[$i]->each($callback, $data, (is_bool($deep))? true : $deep-1);
}
}
}
return $this;
}
/**
* Returns a duplicate of the node with a unique ID.
*
* @param array $params The optional configuration parameters to merge with exisitng values.
* @return CtkBuildable
*/
final public function copy(array $params = null) {
$node = clone $this;
$node->setId(uniqid('ID_'));
if (is_array($params)) {
$node->setParams($params);
}
$children = $node->getChildren();
$node->clearChildren();
for ($i = 0; $i < count($children); $i++) {
$node->add($children[$i]->copy());
}
return $node;
}
/**
* Adds a node to this node as a child.
*
* @param CtkBuildable $node Child node.
* @return CtkBuildable
* @throws CakeException if the child node class is not allowed or this node does not allow children.
*/
final public function add(CtkBuildable $node) {
if ($this->_allowChildren) {
if (!$this->hasChild($node)) {
if ($this === $node) {
throw new CakeException('Cannot add child to itself');
}
if (is_array($this->_limitChildren) && count($this->_limitChildren) > 0) {
if (in_array(get_class($node), $this->_limitChildren)) {
$node->setParent($this);
$this->_childNodes[] = $node;
} else {
throw new CakeException(sprintf('Invalid child %s for %s, must be %s', get_class($node), get_class($this), implode(', ', $this->_limitChildren)));
}
} else {
$node->setParent($this);
$this->_childNodes[] = $node;
}
}
return $node;
}
throw new CakeException(sprintf('Cannot add children to %s', get_class($this)));
}
/**
* Adds a node before the specified node.
*
* @param CtkBuildable $node Child node.
* @param CtkBuildable $before Node to add before.
* @return CtkBuildable
* @throws CakeException if the specified node is not a child or this node does not allow children.
*/
final public function addBefore(CtkBuildable $node, CtkBuildable $before) {
if ($this->_allowChildren) {
if ($this === $node || $this === $before) {
throw new CakeException('Cannot reference parent as a child');
}
if ($this->hasChild($node)) {
$this->removeChild($node);
}
for ($i = 0; $i < count($this->_childNodes); $i++) {
if ($this->_childNodes[$i] === $before) {
array_splice($this->_childNodes, $i-1, 0, $node);
return $node;
}
}
throw new CakeException(sprintf('Unknown child %s', get_class($before)));
}
throw new CakeException(sprintf('Cannot add children to %s', get_class($this)));
}
/**
* Adds a node after the specified node.
*
* @param CtkBuildable $node Child node.
* @param CtkBuildable $after Node to add after.
* @return CtkBuildable
* @throws CakeException if the specified node is not a child or this node does not allow children.
*/
final public function addAfter(CtkBuildable $node, CtkBuildable $after) {
if ($this->_allowChildren) {
if ($this === $node || $this === $after) {
throw new CakeException('Cannot reference parent as a child');
}
if ($this->hasChild($node)) {
$this->removeChild($node);
}
for ($i = 0; $i < count($this->_childNodes); $i++) {
if ($this->_childNodes[$i] === $after) {
array_splice($this->_childNodes, $i+1, 0, $node);
return $node;
}
}
throw new CakeException(sprintf('Unknown child %s', get_class($after)));
}
throw new CakeException(sprintf('Cannot add children to %s', get_class($this)));
}
/**
* Adds an array of nodes to this node as children.
*
* @param array $nodes The child nodes.
* @return CtkBuildable
* @throws CakeException if this node does not allow children.
*/
final public function addMany(array $nodes) {
if ($this->_allowChildren) {
foreach ($nodes as $name => &$node) {
if ($node instanceof CtkBuildable) {
$this->add($node);
} else if (is_string($node)) {
$this->$node();
} else if (is_array($node)) {
$this->add(call_user_func_array(array($this->_factory, $name), array($node)));
} else {
throw new CakeException(sprintf('Unknown child %s', (is_object($node))? get_class($node) : (string) $node));
}
}
return $this;
}
throw new CakeException(sprintf('Cannot add children to %s', get_class($this)));
}
/**
* Inherits the children of another node.
*
* @param CtkBuildable $node The node to inherit from.
* @param boolean $prepend Nodes should be added before existing children.
* @return CtkBuildable
* @throws CakeException if this node does not allow children.
*/
final public function addFrom(CtkBuildable $node, $prepend = false) {
if ($this->_allowChildren) {
$children = $node->getChildren();
$count = count($children);
if ($prepend && $count > 0) {
$first = $node->getFirst();
for ($i = $count-1; $i >= 0; $i--) {
$first = $this->addBefore($children[$i], $first);
}
} else {
for ($i = 0; $i < $count; $i++) {
$this->add($children[$i]);
}
}
return $this;
}
throw new CakeException(sprintf('Cannot add children to %s', get_class($this)));
}
/**
* Conditionally adds a node to this node as a child.
*
* @param boolean $condition The boolean value or expression.
* @param CtkBuildable $node Child node.
* @return CtkBuildable or null if condition is false
* @throws CakeException if this node does not allow children.
*/
final public function addIf($condition = false, CtkBuildable $node) {
if ($this->_allowChildren) {
return ((bool) $condition === true)? $this->add($node) : null;
}
throw new CakeException(sprintf('Cannot add children to %s', get_class($this)));
}
/**
* Adds a node to this node as a child while the callback function returns a node.
*
* @param callable $callback The callback function to return nodes.
* @param array $data The optional array of data to be used by the callback function.
* @return CtkBuildable
* @throws CakeException if the callback function is not callable or if this node does not allow children.
*/
final public function addWhile($callback, array $data = array()) {
if (!is_callable($callback)) {
throw new CakeException('Callback function must be callable');
}
if ($this->_allowChildren) {
$i = 0;
$view = $this->_factory->getView();
$node = call_user_func_array($callback, array($this, $view, $data, $i));
while (is_object($node) && $node instanceof CtkBuildable) {
$this->add($node);
$node = call_user_func_array($callback, array($this, $view, $data, ++$i));
}
return $this;
}
throw new CakeException(sprintf('Cannot add children to %s', get_class($this)));
}
/**
* Adds raw content to the children of this node.
*
* @param mixed $content The raw content to add.
* @return CtkBuildable
*/
final public function addContent($content = '') {
$this->_childNodes[] = new CtkContent($this->_factory->getView(), $content);
return $this;
}
/**
* Replaces the specified node with the given node.
*
* @param CtkBuildable $node Child node.
* @param CtkBuildable $replace Node to replace.
* @return CtkBuildable
* @throws CakeException if the specified node is not a child.
*/
final public function replaceChild(CtkBuildable $node, CtkBuildable $replace) {
if ($this === $node || $this === $replace) {
throw new CakeException('Cannot reference parent as a child');
}
if (!$this->hasChild($replace)) {
throw new CakeException(sprintf('Unknown child %s', get_class($replace)));
}
if ($this->hasChild($node)) {
$this->removeChild($node);
}
for ($i = 0; $i < count($this->_childNodes); $i++) {
if ($this->_childNodes[$i] === $replace) {
$replace->setParent(null);
$this->_childNodes[$i] = $node;
return $this;
}
}
}
/**
* Removes and returns a child node from this node.
*
* @param CtkBuildable $node Child node.
* @return CtkBuildable
* @throws CakeException if the specified node is not a child.
*/
final public function removeChild(CtkBuildable $node) {
if ($this === $node) {
throw new CakeException('Cannot reference parent as a child');
}
for ($i = 0; $i < count($this->_childNodes); $i++) {
if ($this->_childNodes[$i] === $node) {
$node->setParent(null);
array_splice($this->_childNodes, $i, 1);
return $node;
}
}
throw new CakeException(sprintf('Unknown child %s', get_class($node)));
}
/**
* Removes all children from this node.
*
* @return CtkBuildable
*/
final public function clearChildren() {
foreach ($this->_childNodes as &$node) {
$node->setParent(null);
}
$this->_childNodes = array();
return $this;
}
/**
* Determines if the node is allowed events.
*
* @return boolean
*/
final public function allowsEvents() {
return $this->_allowEvents;
}
/**
* Determines if any event types or a specific event type has been bound.
*
* @param string $type The event type.
* @return boolean
*/
final public function hasEvents($type = null) {
return (is_string($type))? isset($this->_nodeEvents[strtolower($type)]) : (bool) count($this->_nodeEvents);
}
/**
* Returns the events for a specific event type.
*
* @param string $type The optional event type to filter the registered events.
* @return array
*/
final public function getEvents($type = null) {
if (is_string($type)) {
if ($this->hasEvents($type)) {
return $this->_nodeEvents[strtolower($type)];
}
return array();
}
return $this->_nodeEvents;
}
/**
* Binds an event to the node.
*
* @param string $type The event type.
* @param CtkEvent $event The event object.
* @return CtkBuildable
* @throws CakeException if events are not allowed on this node.
*/
final public function bind($type, CtkEvent $event) {
if (!is_string($type)) {
throw new CakeException('Event type must be a string');
}
if ($this->_allowEvents) {
$this->_nodeEvents[strtolower($type)][] = $event;
return $this;
}
throw new CakeException('Cannot bind event to node');
}
/**
* Removes a previously bound event from the node.
*
* @param string $type The event type.
* @return CtkBuildable
*/
final public function removeEvents($type) {
unset($this->_nodeEvents[strtolower($type)]);
return $this;
}
/**
* Removes all events previously bound to the node.
*
* @param string $type The event type.
* @return CtkBuildable
*/
final public function clearEvents($type = null) {
if (is_string($type)) {
if ($this->hasEvents($type)) {
unset($this->_nodeEvents[strtolower($type)]);
}
} else {
$this->_nodeEvents = array();
}
return $this;
}
/**
* Parses the template for the node.
*
* @param string $path Path to the template.
* @return string
* @throws CakeException if template is not found.
*/
final public function template($path) {
if (!is_string($path)) {
throw new CakeException('Template path must be a string');
}
if (is_file($path) && is_readable($path)) {
ob_start();
require $path;
return ob_get_clean();
}
throw new CakeException(sprintf('Template not found: %s', $path));
}
/**
* Renders the node using the the view renderer.
*
* @return string
*/
final public function render() {
return $this->_factory->getView()->getRenderer()->render($this);
}
/**
* Renders the child nodes of this node.
*
* @return string
*/
final public function renderChildren() {
$content = '';
foreach ($this->_childNodes as $node) {
$content .= $node->render();
}
return $content;
}
}