forked from magento/magento2
/
Layout.php
1442 lines (1311 loc) · 43.4 KB
/
Layout.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
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Core
* @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Layout model
*
* @category Mage
* @package Mage_Core
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Mage_Core_Model_Layout extends Varien_Simplexml_Config
{
/**#@+
* Supported layout directives
*/
const TYPE_BLOCK = 'block';
const TYPE_CONTAINER = 'container';
const TYPE_ACTION = 'action';
const TYPE_REFERENCE = 'reference';
const TYPE_REMOVE = 'remove';
const TYPE_MOVE = 'move';
/**#@-*/
/**#@+
* Names of container options in layout
*/
const CONTAINER_OPT_HTML_TAG = 'htmlTag';
const CONTAINER_OPT_HTML_CLASS = 'htmlClass';
const CONTAINER_OPT_HTML_ID = 'htmlId';
const CONTAINER_OPT_LABEL = 'label';
/**#@-*/
/**
* Scheduled structure array index for name
*/
const SCHEDULED_STRUCTURE_INDEX_NAME = 0;
/**
* Scheduled structure array index for alias
*/
const SCHEDULED_STRUCTURE_INDEX_ALIAS = 1;
/**
* Scheduled structure array index for parent element name
*/
const SCHEDULED_STRUCTURE_INDEX_PARENT_NAME = 2;
/**
* Scheduled structure array index for sibling element name
*/
const SCHEDULED_STRUCTURE_INDEX_SIBLING_NAME = 3;
/**
* Scheduled structure array index for is after parameter
*/
const SCHEDULED_STRUCTURE_INDEX_IS_AFTER = 4;
/**
* Scheduled structure array index for layout element object
*/
const SCHEDULED_STRUCTURE_INDEX_LAYOUT_ELEMENT = 5;
/**
* Layout Update module
*
* @var Mage_Core_Model_Layout_Update
*/
protected $_update;
/**
* Blocks registry
*
* @var array
*/
protected $_blocks = array();
/**
* Cache of elements to output during rendering
*
* @var array
*/
protected $_output = array();
/**
* Layout area (f.e. admin, frontend)
*
* @var string
*/
protected $_area;
/**
* Helper blocks cache for this layout
*
* @var array
*/
protected $_helpers = array();
/**
* Flag to have blocks' output go directly to browser as oppose to return result
*
* @var boolean
*/
protected $_directOutput = false;
/**
* A variable for transporting output into observer during rendering
*
* @var Varien_Object
*/
protected $_renderingOutput = null;
/**
* Cache of generated elements' HTML
*
* @var array
*/
protected $_renderElementCache = array();
/**
* Layout structure model
*
* @var Magento_Data_Structure
*/
protected $_structure;
/**
* An increment to generate names
*
* @var int
*/
protected $_nameIncrement = 0;
/**
* Information about structural elements, scheduled for creation
*
* @var array
*/
protected $_scheduledStructure = array();
/**
* Materialized paths for overlapping workaround of scheduled structural elements
*
* @var array
*/
protected $_scheduledPaths = array();
/**
* Full information about elements to be populated in the layout structure after generating structure
*
* @var array
*/
protected $_scheduledElements = array();
/**
* Scheduled structure elements moves
*
* @var array
*/
protected $_scheduledMoves = array();
/**
* Scheduled structure elements removes
*
* @var array
*/
protected $_scheduledRemoves = array();
/**
* Class constructor
*
* @param array $arguments
* @throws InvalidArgumentException
*/
public function __construct(array $arguments = array())
{
$this->_area = isset($arguments['area']) ? $arguments['area'] : Mage_Core_Model_Design_Package::DEFAULT_AREA;
if (isset($arguments['structure'])) {
if ($arguments['structure'] instanceof Magento_Data_Structure) {
$this->_structure = $arguments['structure'];
} else {
throw new InvalidArgumentException('Expected instance of Magento_Data_Structure.');
}
} else {
$this->_structure = Mage::getModel('Magento_Data_Structure');
}
$this->_elementClass = Mage::getConfig()->getModelClassName('Mage_Core_Model_Layout_Element');
$this->setXml(simplexml_load_string('<layout/>', $this->_elementClass));
$this->_renderingOutput = new Varien_Object;
}
/**
* Cleanup circular references between layout & blocks
*
* Destructor should be called explicitly in order to work around the PHP bug
* https://bugs.php.net/bug.php?id=62468
*/
public function __destruct()
{
$this->_blocks = array();
}
/**
* Retrieve the layout update instance
*
* @return Mage_Core_Model_Layout_Update
*/
public function getUpdate()
{
if (!$this->_update) {
$this->_update = Mage::getModel('Mage_Core_Model_Layout_Update', array('area' => $this->getArea()));
}
return $this->_update;
}
/**
* Retrieve layout area
*
* @return string
*/
public function getArea()
{
return $this->_area;
}
/**
* Declaring layout direct output flag
*
* @param bool $flag
* @return Mage_Core_Model_Layout
*/
public function setDirectOutput($flag)
{
$this->_directOutput = $flag;
return $this;
}
/**
* Retrieve direct output flag
*
* @return bool
*/
public function isDirectOutput()
{
return $this->_directOutput;
}
/**
* Layout xml generation
*
* @return Mage_Core_Model_Layout
*/
public function generateXml()
{
$xml = $this->getUpdate()->asSimplexml();
$this->setXml($xml);
$this->_structure->importElements(array());
return $this;
}
/**
* Create structure of elements from the loaded XML configuration
*/
public function generateElements()
{
Magento_Profiler::start(__CLASS__ . '::' . __METHOD__);
Magento_Profiler::start('build_structure');
$this->_scheduledStructure = array();
$this->_scheduledPaths = array();
$this->_scheduledElements = array();
$this->_readStructure($this->getNode());
while (!empty($this->_scheduledStructure)) {
reset($this->_scheduledStructure);
$this->_scheduleElement(key($this->_scheduledStructure));
};
$this->_scheduledPaths = array();
$moveList = array_keys(array_intersect_key($this->_scheduledElements, $this->_scheduledMoves));
foreach ($moveList as $elementToMove) {
$this->_moveElementInStructure($elementToMove);
}
$removeList = array_keys(array_intersect_key($this->_scheduledElements, $this->_scheduledRemoves));
foreach ($removeList as $elementToRemove) {
$this->_removeElement($elementToRemove);
}
Magento_Profiler::stop('build_structure');
Magento_Profiler::start('generate_elements');
while (!empty($this->_scheduledElements)) {
list($type, $node) = reset($this->_scheduledElements);
$elementName = key($this->_scheduledElements);
if (isset($node['output'])) {
$this->addOutputElement($elementName);
}
if ($type == self::TYPE_BLOCK) {
$this->_generateBlock($elementName);
} else {
$this->_generateContainer($elementName, (string)$node[self::CONTAINER_OPT_LABEL], array(
self::CONTAINER_OPT_HTML_TAG => (string)$node[self::CONTAINER_OPT_HTML_TAG],
self::CONTAINER_OPT_HTML_ID => (string)$node[self::CONTAINER_OPT_HTML_ID],
self::CONTAINER_OPT_HTML_CLASS => (string)$node[self::CONTAINER_OPT_HTML_CLASS]
));
unset($this->_scheduledElements[$elementName]);
}
}
Magento_Profiler::stop('generate_elements');
Magento_Profiler::stop(__CLASS__ . '::' . __METHOD__);
}
/**
* Remove scheduled element
*
* @param string $elementName
* @param bool $isChild
* @return Mage_Core_Model_Layout
*/
protected function _removeElement($elementName, $isChild = false)
{
$elementsToRemove = array_keys($this->_structure->getChildren($elementName));
unset($this->_scheduledElements[$elementName]);
foreach ($elementsToRemove as $element) {
$this->_removeElement($element, true);
}
if (!$isChild) {
$this->_structure->unsetElement($elementName);
unset($this->_scheduledRemoves[$elementName]);
}
return $this;
}
/**
* Move element in scheduled structure
*
* @param string $element
* @return Mage_Core_Model_Layout
*/
protected function _moveElementInStructure($element)
{
list ($destination, $siblingName, $isAfter, $alias) = $this->_scheduledMoves[$element];
if (!$alias && false === $this->_structure->getChildId($destination, $this->getElementAlias($element))) {
$alias = $this->getElementAlias($element);
}
$this->_structure->unsetChild($element, $alias)->setAsChild($element, $destination, $alias);
$this->reorderChild($destination, $element, $siblingName, $isAfter);
return $this;
}
/**
* Traverse through all elements of specified XML-node and schedule structural elements of it
*
* @param Mage_Core_Model_Layout_Element $parent
*/
protected function _readStructure($parent)
{
/** @var Mage_Core_Model_Layout_Element $node */
foreach ($parent as $node) {
switch ($node->getName()) {
case self::TYPE_CONTAINER:
case self::TYPE_BLOCK:
$this->_scheduleStructure($node, $parent);
$this->_readStructure($node);
break;
case self::TYPE_REFERENCE:
$this->_readStructure($node);
break;
case self::TYPE_ACTION:
$referenceName = $parent->getAttribute('name');
$this->_scheduledStructure[$referenceName]['actions'][] = array($node, $parent);
break;
case self::TYPE_MOVE:
$this->_scheduleMove($node);
break;
case self::TYPE_REMOVE:
$this->_scheduledRemoves[(string)$node->getAttribute('name')] = 1;
break;
default:
break;
}
}
}
/**
* Schedule structural changes for move directive
*
* @param Mage_Core_Model_Layout_Element $node
* @throws Magento_Exception
* @return Mage_Core_Model_Layout
*/
protected function _scheduleMove($node)
{
$elementName = (string)$node->getAttribute('element');
$destination = (string)$node->getAttribute('destination');
$alias = (string)$node->getAttribute('as') ?: '';
if ($elementName && $destination) {
list($siblingName, $isAfter) = $this->_beforeAfterToSibling($node);
$this->_scheduledMoves[$elementName] = array($destination, $siblingName, $isAfter, $alias);
} else {
throw new Magento_Exception('Element name and destination must be specified.');
}
return $this;
}
/**
* Populate queue for generating structural elements
*
* @param Mage_Core_Model_Layout_Element $node
* @param Mage_Core_Model_Layout_Element $parent
* @see _scheduleElement() where the _scheduledStructure is used
*/
protected function _scheduleStructure($node, $parent)
{
if ((string)$node->getAttribute('name')) {
$name = (string)$node->getAttribute('name');
} else {
$name = $this->_generateAnonymousName();
$node->addAttribute('name', $name);
}
$path = $name;
// type, alias, parentName, siblingName, isAfter, node
$row = array(
self::SCHEDULED_STRUCTURE_INDEX_NAME => $node->getName(),
self::SCHEDULED_STRUCTURE_INDEX_ALIAS => '',
self::SCHEDULED_STRUCTURE_INDEX_PARENT_NAME => '',
self::SCHEDULED_STRUCTURE_INDEX_SIBLING_NAME => null,
self::SCHEDULED_STRUCTURE_INDEX_IS_AFTER => true,
self::SCHEDULED_STRUCTURE_INDEX_LAYOUT_ELEMENT => $node
);
$parentName = $parent->getElementName();
if ($parentName) {
$row[self::SCHEDULED_STRUCTURE_INDEX_ALIAS] = (string)$node->getAttribute('as');
$row[self::SCHEDULED_STRUCTURE_INDEX_PARENT_NAME] = $parentName;
list(
$row[self::SCHEDULED_STRUCTURE_INDEX_SIBLING_NAME],
$row[self::SCHEDULED_STRUCTURE_INDEX_IS_AFTER]
) = $this->_beforeAfterToSibling($node);
// materialized path for referencing nodes in the plain array of _scheduledStructure
if (isset($this->_scheduledPaths[$parentName])) {
$path = $this->_scheduledPaths[$parentName] . '/' . $path;
}
}
$this->_overrideElementWorkaround($name, $path);
$this->_scheduledPaths[$name] = $path;
if (isset($this->_scheduledStructure[$name])) {
$this->_scheduledStructure[$name] = $row + $this->_scheduledStructure[$name]; // union of arrays
} else {
$this->_scheduledStructure[$name] = $row;
}
}
/**
* Analyze "before" and "after" information in the node and return sibling name and whether "after" or "before"
*
* @param Mage_Core_Model_Layout_Element $node
* @return array
*/
protected function _beforeAfterToSibling($node)
{
$result = array(null, true);
if (isset($node['after'])) {
$result[0] = (string)$node['after'];
} elseif (isset($node['before'])) {
$result[0] = (string)$node['before'];
$result[1] = false;
}
return $result;
}
/**
* Destroy previous element with same name and all its children, if new element overrides it
*
* This is a workaround to handle situation, when an element emerges with name of element that already exists.
* In this case we destroy entire structure of the former element and replace with the new one.
*
* @param string $name
* @param string $path
*/
protected function _overrideElementWorkaround($name, $path)
{
if (isset($this->_scheduledStructure[$name])) {
foreach ($this->_scheduledPaths as $potentialChild => $childPath) {
if (0 === strpos($childPath, "{$path}/")) {
unset($this->_scheduledPaths[$potentialChild], $this->_scheduledStructure[$potentialChild]);
}
}
}
}
/**
* Process queue of structural elements and actually add them to structure, and schedule elements for generation
*
* The catch is to populate parents first, if they are not in the structure yet.
* Since layout updates could come in arbitrary order, a case is possible where an element is declared in reference,
* while referenced element itself is not declared yet.
*
* @param string $key in _scheduledStructure represent element name
*/
protected function _scheduleElement($key)
{
$row = $this->_scheduledStructure[$key];
if (!isset($row[self::SCHEDULED_STRUCTURE_INDEX_LAYOUT_ELEMENT])) {
Mage::log("Broken reference: missing declaration of the element '{$key}'.", Zend_Log::CRIT);
unset($this->_scheduledStructure[$key], $this->_scheduledPaths[$key]);
return;
}
list($type, $alias, $parentName, $siblingName, $isAfter, $node) = $row;
$name = $this->_createStructuralElement($key, $type);
if ($parentName) {
// recursively populate parent first
if (isset($this->_scheduledStructure[$parentName])) {
$this->_scheduleElement($parentName, $this->_scheduledStructure[$parentName]);
}
if ($this->_structure->hasElement($parentName)) {
$this->_structure->setAsChild($name, $parentName, $alias);
} else {
Mage::log("Broken reference: the '{$name}' element cannot be added as child to '{$parentName}, "
. 'because the latter doesn\'t exist', Zend_Log::CRIT
);
}
}
unset($this->_scheduledStructure[$key]);
$this->_scheduledElements[$name] = array($type, $node, isset($row['actions']) ? $row['actions'] : array());
/**
* Some elements provide info "after" or "before" which sibling they are supposed to go
* Make sure to populate these siblings as well and order them correctly
*/
if ($siblingName) {
if (isset($this->_scheduledStructure[$siblingName])) {
$this->_scheduleElement($siblingName);
}
$this->reorderChild($parentName, $name, $siblingName, $isAfter);
}
}
/**
* Register an element in structure
*
* Will assign an "anonymous" name to the element, if provided with an empty name
*
* @param string $name
* @param string $type
* @return string
*/
protected function _createStructuralElement($name, $type)
{
if (empty($name)) {
$name = $this->_generateAnonymousName();
}
$this->_structure->createElement($name, array('type' => $type));
return $name;
}
/**
* Generate anonymous element name for structure
*
* @return string
*/
protected function _generateAnonymousName()
{
return 'ANONYMOUS_' . $this->_nameIncrement++;
}
/**
* Creates block object based on xml node data and add it to the layout
*
* @param string $elementName
* @return Mage_Core_Block_Abstract
* @throws Magento_Exception
*/
protected function _generateBlock($elementName)
{
list($type, $node, $actions) = $this->_scheduledElements[$elementName];
if ($type !== self::TYPE_BLOCK) {
throw new Magento_Exception("Unexpected element type specified for generating block: {$type}.");
}
// create block
if (!empty($node['class'])) {
$className = (string)$node['class'];
} else {
$className = (string)$node['type'];
}
$block = $this->_createBlock($className, $elementName);
if (!empty($node['template'])) {
$block->setTemplate((string)$node['template']);
}
unset($this->_scheduledElements[$elementName]);
// execute block methods
foreach ($actions as $action) {
list($actionNode, $parent) = $action;
$this->_generateAction($actionNode, $parent);
}
return $block;
}
/**
* Set container-specific data to structure element
*
* @param string $name
* @param string $label
* @param array $options
* @throws Magento_Exception if any of arguments are invalid
*/
protected function _generateContainer($name, $label, array $options)
{
if (empty($label)) {
throw new Magento_Exception('Container requires a label.');
}
$this->_structure->setAttribute($name, self::CONTAINER_OPT_LABEL, $label);
unset($options[self::CONTAINER_OPT_LABEL]);
unset($options['type']);
if (empty($options[self::CONTAINER_OPT_HTML_TAG])
&& (!empty($options[self::CONTAINER_OPT_HTML_ID]) || !empty($options[self::CONTAINER_OPT_HTML_CLASS]))
) {
throw new Magento_Exception('HTML ID or class will not have effect, if HTML tag is not specified.');
}
foreach ($options as $key => $value) {
$this->_structure->setAttribute($name, $key, $value);
}
}
/**
* Run action defined in layout update
*
* @param Mage_Core_Model_Layout_Element $node
* @param Mage_Core_Model_Layout_Element $parent
*/
protected function _generateAction($node, $parent)
{
$configPath = $node->getAttribute('ifconfig');
if ($configPath && !Mage::getStoreConfigFlag($configPath)) {
return;
}
$method = $node->getAttribute('method');
$parentName = $node->getAttribute('block');
if (empty($parentName)) {
$parentName = $parent->getElementName();
}
$profilerKey = 'BLOCK_ACTION:' . $parentName . '>' . $method;
Magento_Profiler::start($profilerKey);
$block = $this->getBlock($parentName);
if (!empty($block)) {
$args = $this->_extractArgs($node);
$this->_translateLayoutNode($node, $args);
call_user_func_array(array($block, $method), $args);
}
Magento_Profiler::stop($profilerKey);
}
/**
* Get child block if exists
*
* @param string $parentName
* @param string $alias
* @return bool|Mage_Core_Block_Abstract
*/
public function getChildBlock($parentName, $alias)
{
$name = $this->_structure->getChildId($parentName, $alias);
if ($this->isBlock($name)) {
return $this->getBlock($name);
}
return false;
}
/**
* Set child element into layout structure
*
* @param string $parentName
* @param string $elementName
* @param string $alias
* @return Mage_Core_Model_Layout
*/
public function setChild($parentName, $elementName, $alias)
{
$this->_structure->setAsChild($elementName, $parentName, $alias);
return $this;
}
/**
* Reorder a child of a specified element
*
* If $offsetOrSibling is null, it will put the element to the end
* If $offsetOrSibling is numeric (integer) value, it will put the element after/before specified position
* Otherwise -- after/before specified sibling
*
* @param string $parentName
* @param string $childName
* @param string|int|null $offsetOrSibling
* @param bool $after
*/
public function reorderChild($parentName, $childName, $offsetOrSibling, $after = true)
{
if (is_numeric($offsetOrSibling)) {
$offset = (int)abs($offsetOrSibling) * ($after ? 1 : -1);
$this->_structure->reorderChild($parentName, $childName, $offset);
} elseif (null === $offsetOrSibling) {
$this->_structure->reorderChild($parentName, $childName, null);
} else {
$children = $this->getChildNames($parentName);
$sibling = $this->_filterSearchMinus($offsetOrSibling, $children, $after);
if ($childName !== $sibling) {
$siblingParentName = $this->_structure->getParentId($sibling);
if ($parentName !== $siblingParentName) {
Mage::log("Broken reference: the '{$childName}' tries to reorder itself towards '{$sibling}', "
. "but their parents are different: '{$parentName}' and '{$siblingParentName}' respectively.",
Zend_Log::CRIT
);
return;
}
$this->_structure->reorderToSibling($parentName, $childName, $sibling, $after ? 1 : -1);
}
}
}
/**
* Search for an array element using needle, but needle may be '-', which means "first" or "last" element
*
* Returns first or last element in the haystack, or the $needle argument
*
* @param string $needle
* @param array $haystack
* @param bool $isLast
* @return string
*/
protected function _filterSearchMinus($needle, array $haystack, $isLast)
{
if ('-' === $needle) {
if ($isLast) {
return array_pop($haystack);
}
return array_shift($haystack);
}
return $needle;
}
/**
* Remove child element from parent
*
* @param string $parentName
* @param string $alias
* @return Mage_Core_Model_Layout
*/
public function unsetChild($parentName, $alias)
{
$this->_structure->unsetChild($parentName, $alias);
return $this;
}
/**
* Get list of child names
*
* @param string $parentName
* @return array
*/
public function getChildNames($parentName)
{
return array_keys($this->_structure->getChildren($parentName));
}
/**
* Get list of child blocks
*
* Returns associative array of <alias> => <block instance>
*
* @param string $parentName
* @return array
*/
public function getChildBlocks($parentName)
{
$blocks = array();
foreach ($this->_structure->getChildren($parentName) as $childName => $alias) {
$block = $this->getBlock($childName);
if ($block) {
$blocks[$alias] = $block;
}
}
return $blocks;
}
/**
* Get child name by alias
*
* @param string $parentName
* @param string $alias
* @return bool|string
*/
public function getChildName($parentName, $alias)
{
return $this->_structure->getChildId($parentName, $alias);
}
/**
* Find an element in layout, render it and return string with its output
*
* @param string $name
* @param bool $useCache
* @return string
*/
public function renderElement($name, $useCache = true)
{
if (!isset($this->_renderElementCache[$name]) || !$useCache) {
if ($this->isBlock($name)) {
$result = $this->_renderBlock($name);
} else {
$result = $this->_renderContainer($name);
}
$this->_renderElementCache[$name] = $result;
}
$this->_renderingOutput->setData('output', $this->_renderElementCache[$name]);
Mage::dispatchEvent('core_layout_render_element', array(
'element_name' => $name,
'layout' => $this,
'transport' => $this->_renderingOutput,
));
return $this->_renderingOutput->getData('output');
}
/**
* Gets HTML of block element
*
* @param string $name
* @return string
* @throws Magento_Exception
*/
protected function _renderBlock($name)
{
$block = $this->getBlock($name);
return $block ? $block->toHtml() : '';
}
/**
* Gets HTML of container element
*
* @param string $name
* @return string
*/
protected function _renderContainer($name)
{
$html = '';
$children = $this->getChildNames($name);
foreach ($children as $child) {
$html .= $this->renderElement($child);
}
if ($html == '' || !$this->_structure->getAttribute($name, self::CONTAINER_OPT_HTML_TAG)) {
return $html;
}
$htmlId = $this->_structure->getAttribute($name, self::CONTAINER_OPT_HTML_ID);
if ($htmlId) {
$htmlId = ' id="' . $htmlId . '"';
}
$htmlClass = $this->_structure->getAttribute($name, self::CONTAINER_OPT_HTML_CLASS);
if ($htmlClass) {
$htmlClass = ' class="'. $htmlClass . '"';
}
$htmlTag = $this->_structure->getAttribute($name, self::CONTAINER_OPT_HTML_TAG);
$html = sprintf('<%1$s%2$s%3$s>%4$s</%1$s>', $htmlTag, $htmlId, $htmlClass, $html);
return $html;
}
/**
* Add element to parent group
*
* @param string $blockName
* @param string $parentGroupName
* @return bool
*/
public function addToParentGroup($blockName, $parentGroupName)
{
return $this->_structure->addToParentGroup($blockName, $parentGroupName);
}
/**
* Get element names for specified group
*
* @param string $blockName
* @param string $groupName
* @return array
*/
public function getGroupChildNames($blockName, $groupName)
{
return $this->_structure->getGroupChildNames($blockName, $groupName);
}
/**
* Update args according to its type
*
* @param Mage_Core_Model_Layout_Element $node
* @return array
*/
protected function _extractArgs($node)
{
$args = (array)$node->children();
unset($args['@attributes']);
foreach ($args as $key => $arg) {
if (($arg instanceof Mage_Core_Model_Layout_Element)) {
if (isset($arg['helper'])) {
$args[$key] = $this->_getArgsByHelper($arg);
} else {
/**
* if there is no helper we hope that this is assoc array
*/
$arr = $this->_getArgsFromAssoc($arg);
if (!empty($arr)) {
$args[$key] = $arr;
}
}
}
}
if (isset($node['json'])) {
$json = explode(' ', (string)$node['json']);
foreach ($json as $arg) {
$args[$arg] = Mage::helper('Mage_Core_Helper_Data')->jsonDecode($args[$arg]);
}
}
return $args;
}
/**
* Gets arguments using helper method
*
* @param Mage_Core_Model_Layout_Element $arg
* @return mixed
*/
protected function _getArgsByHelper(Mage_Core_Model_Layout_Element $arg)
{
$helper = (string)$arg['helper'];
list($helperName, $helperMethod) = explode('::', $helper);
$arg = $arg->asArray();
unset($arg['@']);
return call_user_func_array(array(Mage::helper($helperName), $helperMethod), $arg);
}
/**
* Converts input array to arguments array
*
* @param array $array
* @return array
*/
protected function _getArgsFromAssoc($array)
{
$arr = array();
foreach ($array as $key => $value) {