@@ -53,11 +53,13 @@ protected function _construct()
53
53
*
54
54
* @param string $name
55
55
* @param string $params
56
+ * @param string $referenceName
57
+ * @param bool $before
56
58
* @return $this
57
59
*/
58
- public function addCss ($ name , $ params = "" )
60
+ public function addCss ($ name , $ params = "" , $ referenceName = " * " , $ before = null )
59
61
{
60
- $ this ->addItem ('skin_css ' , $ name , $ params );
62
+ $ this ->addItem ('skin_css ' , $ name , $ params, null , null , $ referenceName , $ before );
61
63
return $ this ;
62
64
}
63
65
@@ -66,11 +68,13 @@ public function addCss($name, $params = "")
66
68
*
67
69
* @param string $name
68
70
* @param string $params
71
+ * @param string $referenceName
72
+ * @param bool $before
69
73
* @return $this
70
74
*/
71
- public function addJs ($ name , $ params = "" )
75
+ public function addJs ($ name , $ params = "" , $ referenceName = " * " , $ before = null )
72
76
{
73
- $ this ->addItem ('js ' , $ name , $ params );
77
+ $ this ->addItem ('js ' , $ name , $ params, null , null , $ referenceName , $ before );
74
78
return $ this ;
75
79
}
76
80
@@ -79,11 +83,13 @@ public function addJs($name, $params = "")
79
83
*
80
84
* @param string $name
81
85
* @param string $params
86
+ * @param string $referenceName
87
+ * @param bool $before
82
88
* @return $this
83
89
*/
84
- public function addCssIe ($ name , $ params = "" )
90
+ public function addCssIe ($ name , $ params = "" , $ referenceName = " * " , $ before = null )
85
91
{
86
- $ this ->addItem ('skin_css ' , $ name , $ params , 'IE ' );
92
+ $ this ->addItem ('skin_css ' , $ name , $ params , 'IE ' , null , $ referenceName , $ before );
87
93
return $ this ;
88
94
}
89
95
@@ -92,11 +98,13 @@ public function addCssIe($name, $params = "")
92
98
*
93
99
* @param string $name
94
100
* @param string $params
101
+ * @param string $referenceName
102
+ * @param bool $before
95
103
* @return $this
96
104
*/
97
- public function addJsIe ($ name , $ params = "" )
105
+ public function addJsIe ($ name , $ params = "" , $ referenceName = " * " , $ before = null )
98
106
{
99
- $ this ->addItem ('js ' , $ name , $ params , 'IE ' );
107
+ $ this ->addItem ('js ' , $ name , $ params , 'IE ' , null , $ referenceName , $ before );
100
108
return $ this ;
101
109
}
102
110
@@ -128,20 +136,41 @@ public function addLinkRel($rel, $href)
128
136
* @param string $params
129
137
* @param string $if
130
138
* @param string $cond
139
+ * @param string $referenceName name of the item to insert the element before. If name is not found, insert at the end, * has special meaning (before all / before all)
140
+ * @param bool $before If true insert before the $referenceName instead of after
131
141
* @return $this
132
142
*/
133
- public function addItem ($ type , $ name , $ params = null , $ if = null , $ cond = null )
143
+ public function addItem ($ type , $ name , $ params = null , $ if = null , $ cond = null , $ referenceName = " * " , $ before = false )
134
144
{
135
- if ($ type ==='skin_css ' && empty ($ params )) {
145
+ // allow skipping of parameters in the layout XML files via empty-string
146
+ if ($ params === '' ) {
147
+ $ params = null ;
148
+ }
149
+ if ($ if === '' ) {
150
+ $ if = null ;
151
+ }
152
+ if ($ cond === '' ) {
153
+ $ cond = null ;
154
+ }
155
+
156
+ if ($ type === 'skin_css ' && empty ($ params )) {
136
157
$ params = 'media="all" ' ;
137
158
}
138
- $ this ->_data ['items ' ][$ type. '/ ' . $ name ] = array (
139
- 'type ' => $ type ,
140
- 'name ' => $ name ,
159
+ $ this ->_data ['items ' ][$ type . '/ ' . $ name ] = array (
160
+ 'type ' => $ type ,
161
+ 'name ' => $ name ,
141
162
'params ' => $ params ,
142
- 'if ' => $ if ,
143
- 'cond ' => $ cond ,
163
+ 'if ' => $ if ,
164
+ 'cond ' => $ cond ,
144
165
);
166
+
167
+ // that is the standard behaviour
168
+ if ($ referenceName === '* ' && $ before === false ) {
169
+ return $ this ;
170
+ }
171
+
172
+ $ this ->_sortItems ($ referenceName , $ before , $ type );
173
+
145
174
return $ this ;
146
175
}
147
176
@@ -536,4 +565,43 @@ protected function _isFile($filename)
536
565
}
537
566
return is_file ($ filename );
538
567
}
568
+
569
+ /**
570
+ * @param string $referenceName
571
+ * @param string $before
572
+ * @param string $type
573
+ */
574
+ protected function _sortItems ($ referenceName , $ before , $ type )
575
+ {
576
+ $ items = $ this ->_data ['items ' ];
577
+
578
+ // get newly inserted item so we do not have to reproduce the functionality of the parent
579
+ end ($ items );
580
+ $ newKey = key ($ items );
581
+ $ newVal = array_pop ($ items );
582
+
583
+ $ newItems = array ();
584
+
585
+ if ($ referenceName === '* ' && $ before === true ) {
586
+ $ newItems [$ newKey ] = $ newVal ;
587
+ }
588
+
589
+ $ referenceName = $ type . '/ ' . $ referenceName ;
590
+ foreach ($ items as $ key => $ value ) {
591
+ if ($ key === $ referenceName && $ before === true ) {
592
+ $ newItems [$ newKey ] = $ newVal ;
593
+ }
594
+
595
+ $ newItems [$ key ] = $ value ;
596
+
597
+ if ($ key === $ referenceName && $ before === false ) {
598
+ $ newItems [$ newKey ] = $ newVal ;
599
+ }
600
+ }
601
+
602
+ // replace items only if the reference was found (otherwise insert as last item)
603
+ if (isset ($ newItems [$ newKey ])) {
604
+ $ this ->_data ['items ' ] = $ newItems ;
605
+ }
606
+ }
539
607
}
0 commit comments