@@ -38,7 +38,28 @@ class Mage_Adminhtml_Block_Widget_Tabs extends Mage_Adminhtml_Block_Widget
3838 *
3939 * @var array
4040 */
41- protected $ _tabs = array ();
41+ protected $ _tabs = [];
42+
43+ /**
44+ * For sorting tabs.
45+ *
46+ * @var array
47+ */
48+ protected $ _afterTabIds = [];
49+
50+ /**
51+ * For sorting tabs.
52+ *
53+ * @var array
54+ */
55+ protected $ _tabPositions = [];
56+
57+ /**
58+ * For sorting tabs.
59+ *
60+ * @var int
61+ */
62+ protected $ _tabPosition = 100 ;
4263
4364 /**
4465 * Active tab key
@@ -79,21 +100,21 @@ public function setDestElementId($elementId)
79100 * Add new tab after another
80101 *
81102 * @param string $tabId new tab Id
82- * @param array|Varien_Object $tab
103+ * @param string| array|Varien_Object $tab
83104 * @param string $afterTabId
84105 * @return Mage_Adminhtml_Block_Widget_Tabs
85106 */
86107 public function addTabAfter ($ tabId , $ tab , $ afterTabId )
87108 {
88109 $ this ->addTab ($ tabId , $ tab );
89- $ this ->_tabs [$ tabId ]-> setAfter ( $ afterTabId) ;
110+ $ this ->_afterTabIds [$ tabId ] = $ afterTabId ;
90111 }
91112
92113 /**
93114 * Add new tab
94115 *
95116 * @param string $tabId
96- * @param array|Varien_Object $tab
117+ * @param string| array|Varien_Object $tab
97118 * @return Mage_Adminhtml_Block_Widget_Tabs
98119 */
99120 public function addTab ($ tabId , $ tab )
@@ -137,8 +158,16 @@ public function addTab($tabId, $tab)
137158 $ this ->_tabs [$ tabId ]->setId ($ tabId );
138159 $ this ->_tabs [$ tabId ]->setTabId ($ tabId );
139160
140- if (is_null ($ this ->_activeTab )) $ this ->_activeTab = $ tabId ;
141- if (true === $ this ->_tabs [$ tabId ]->getActive ()) $ this ->setActiveTab ($ tabId );
161+ if (true === $ this ->_tabs [$ tabId ]->getActive ()) {
162+ $ this ->setActiveTab ($ tabId );
163+ }
164+
165+ // For sorting tabs.
166+ $ this ->_tabPositions [$ tabId ] = $ this ->_tabPosition ;
167+ $ this ->_tabPosition += 100 ;
168+ if ($ this ->_tabs [$ tabId ]->getAfter ()) {
169+ $ this ->_afterTabIds [$ tabId ] = $ this ->_tabs [$ tabId ]->getAfter ();
170+ }
142171
143172 return $ this ;
144173 }
@@ -160,11 +189,6 @@ public function setActiveTab($tabId)
160189 if (isset ($ this ->_tabs [$ tabId ]) && $ this ->canShowTab ($ this ->_tabs [$ tabId ])
161190 && !$ this ->getTabIsHidden ($ this ->_tabs [$ tabId ])) {
162191 $ this ->_activeTab = $ tabId ;
163- if (!(is_null ($ this ->_activeTab )) && ($ tabId !== $ this ->_activeTab )) {
164- foreach ($ this ->_tabs as $ id => $ tab ) {
165- $ tab ->setActive ($ id === $ tabId );
166- }
167- }
168192 }
169193 return $ this ;
170194 }
@@ -196,27 +220,60 @@ protected function _beforeToHtml()
196220 $ this ->_setActiveTab ($ activeTabId );
197221 }
198222
199- $ _new = array ();
200- foreach ( $ this ->_tabs as $ key => $ tab ) {
201- foreach ( $ this ->_tabs as $ k => $ t ) {
202- if ( $ t ->getAfter () == $ key ) {
203- $ _new [$ key ] = $ tab ;
204- $ _new [$ k ] = $ t ;
205- } else {
206- if ( !$ tab ->getAfter () || !in_array ($ tab ->getAfter (), array_keys ($ this ->_tabs )) ) {
207- $ _new [$ key ] = $ tab ;
208- }
209- }
210- }
223+ if ($ this ->_activeTab === null && !empty ($ this ->_tabs )) {
224+ $ this ->_activeTab = (reset ($ this ->_tabs ))->getId ();
211225 }
212226
213- $ this ->_tabs = $ _new ;
214- unset($ _new );
227+ if (!empty ($ this ->_afterTabIds )) {
228+ $ this ->_tabs = $ this ->_reorderTabs ();
229+ }
215230
216231 $ this ->assign ('tabs ' , $ this ->_tabs );
217232 return parent ::_beforeToHtml ();
218233 }
219234
235+ /**
236+ * Find the root parent Tab ID recursively.
237+ *
238+ * @param string $currentAfterTabId
239+ * @param int $degree Degrees of separation between child and root parent.
240+ * @return string The parent tab ID.
241+ */
242+ protected function _getRootParentTabId ($ currentAfterTabId , &$ degree )
243+ {
244+ if (array_key_exists ($ currentAfterTabId , $ this ->_afterTabIds )) {
245+ $ degree ++;
246+ return $ this ->_getRootParentTabId ($ this ->_afterTabIds [$ currentAfterTabId ], $ degree );
247+ } else {
248+ return $ currentAfterTabId ;
249+ }
250+ }
251+
252+ protected function _reorderTabs ()
253+ {
254+ // Set new position based on $afterTabId.
255+ foreach ($ this ->_afterTabIds as $ tabId => $ afterTabId ) {
256+ if (array_key_exists ($ afterTabId , $ this ->_tabs )) {
257+ $ degree = 1 ; // Initialize to 1 degree of separation.
258+ $ parentAfterTabId = $ this ->_getRootParentTabId ($ afterTabId , $ degree );
259+ $ this ->_tabPositions [$ tabId ] = $ this ->_tabPositions [$ parentAfterTabId ] + $ degree ;
260+ $ degree ++;
261+ }
262+ }
263+
264+ asort ($ this ->_tabPositions );
265+
266+ $ ordered = [];
267+ foreach ($ this ->_tabPositions as $ tabId => $ position ) {
268+ if (isset ($ this ->_tabs [$ tabId ])) {
269+ $ tab = $ this ->_tabs [$ tabId ];
270+ $ ordered [$ tabId ] = $ tab ;
271+ }
272+ }
273+
274+ return $ ordered ;
275+ }
276+
220277 public function getJsObjectName ()
221278 {
222279 return $ this ->getId () . 'JsTabs ' ;
0 commit comments