From a6e96bf79950ee110e9229ce9ec7ecb9e50a0f92 Mon Sep 17 00:00:00 2001 From: kiat ng Date: Fri, 18 Dec 2020 22:29:21 +0800 Subject: [PATCH 1/6] Fixed admin tabs order not working properly. --- .../core/Mage/Adminhtml/Block/Widget/Tabs.php | 107 +++++++++++++++--- 1 file changed, 90 insertions(+), 17 deletions(-) diff --git a/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php b/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php index f78bbde04dc..0d3f361c03a 100644 --- a/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php +++ b/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php @@ -137,8 +137,9 @@ public function addTab($tabId, $tab) $this->_tabs[$tabId]->setId($tabId); $this->_tabs[$tabId]->setTabId($tabId); - if (is_null($this->_activeTab)) $this->_activeTab = $tabId; - if (true === $this->_tabs[$tabId]->getActive()) $this->setActiveTab($tabId); + if (true === $this->_tabs[$tabId]->getActive()) { + $this->setActiveTab($tabId); + } return $this; } @@ -189,32 +190,104 @@ protected function _setActiveTab($tabId) protected function _beforeToHtml() { + $this->_tabs = $this->_reorderTabs(); + if ($activeTab = $this->getRequest()->getParam('active_tab')) { $this->setActiveTab($activeTab); } elseif ($activeTabId = Mage::getSingleton('admin/session')->getActiveTabId()) { $this->_setActiveTab($activeTabId); } - $_new = array(); - foreach( $this->_tabs as $key => $tab ) { - foreach( $this->_tabs as $k => $t ) { - if( $t->getAfter() == $key ) { - $_new[$key] = $tab; - $_new[$k] = $t; - } else { - if( !$tab->getAfter() || !in_array($tab->getAfter(), array_keys($this->_tabs)) ) { - $_new[$key] = $tab; - } - } - } + if ($this->_activeTab === null && !empty($this->_tabs)) { + $this->_activeTab = (reset($this->_tabs))->getId(); } - $this->_tabs = $_new; - unset($_new); - $this->assign('tabs', $this->_tabs); return parent::_beforeToHtml(); } + + /** + * Reorder the tabs. + * + * @return array + */ + private function _reorderTabs() + { + $orderByIdentity = []; + $orderByPosition = []; + $position = 100; + + /** + * Set the initial positions for each tab. + * + * @var string $key + * @var TabInterface $tab + */ + foreach ($this->_tabs as $key => $tab) { + $tab->setPosition($position); + + $orderByIdentity[$key] = $tab; + $orderByPosition[$position] = $tab; + + $position += 100; + } + + return $this->_applyTabsCorrectOrder($orderByPosition, $orderByIdentity); + } + + /** + * @param array $orderByPosition + * @param array $orderByIdentity + * @return array + */ + private function _applyTabsCorrectOrder(array $orderByPosition, array $orderByIdentity) + { + $positionFactor = 1; + + /** + * Rearrange the positions by using the after tag for each tab. + * + * @var integer $position + * @var Varien_Object $tab + */ + foreach ($orderByPosition as $position => $tab) { + if (!$tab->getAfter() || !array_key_exists($tab->getAfter(), $orderByIdentity)) { + $positionFactor = 1; + continue; + } + + $grandPosition = $orderByIdentity[$tab->getAfter()]->getPosition(); + $newPosition = $grandPosition + $positionFactor; + + unset($orderByPosition[$position]); + $orderByPosition[$newPosition] = $tab; + $tab->setPosition($newPosition); + + $positionFactor++; + } + + return $this->_finalTabsSortOrder($orderByPosition); + } + + /** + * Apply the last sort order to tabs. + * + * @param array $orderByPosition + * @return array + */ + private function _finalTabsSortOrder(array $orderByPosition) + { + ksort($orderByPosition); + + $ordered = []; + + /** @var Varien_Object $tab */ + foreach ($orderByPosition as $tab) { + $ordered[$tab->getId()] = $tab; + } + + return $ordered; + } public function getJsObjectName() { From bd07ba2c79e6876fef9858281a24fcefb50ebd54 Mon Sep 17 00:00:00 2001 From: kiat ng Date: Sat, 19 Dec 2020 13:44:50 +0800 Subject: [PATCH 2/6] Improve tabs sorting. --- .../core/Mage/Adminhtml/Block/Widget/Tabs.php | 138 +++++++----------- 1 file changed, 50 insertions(+), 88 deletions(-) diff --git a/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php b/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php index 0d3f361c03a..dbe464052b7 100644 --- a/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php +++ b/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php @@ -38,7 +38,28 @@ class Mage_Adminhtml_Block_Widget_Tabs extends Mage_Adminhtml_Block_Widget * * @var array */ - protected $_tabs = array(); + protected $_tabs = []; + + /** + * For sorting tabs. + * + * @var array + */ + protected $_afterTabIds = []; + + /** + * For sorting tabs. + * + * @var array + */ + protected $_tabPositions = []; + + /** + * For sorting tabs. + * + * @var int + */ + protected $_tabPosition = 100; /** * Active tab key @@ -79,21 +100,21 @@ public function setDestElementId($elementId) * Add new tab after another * * @param string $tabId new tab Id - * @param array|Varien_Object $tab + * @param string|array|Varien_Object $tab * @param string $afterTabId * @return Mage_Adminhtml_Block_Widget_Tabs */ public function addTabAfter($tabId, $tab, $afterTabId) { $this->addTab($tabId, $tab); - $this->_tabs[$tabId]->setAfter($afterTabId); + $this->_afterTabIds[$tabId] = $afterTabId; } /** * Add new tab * * @param string $tabId - * @param array|Varien_Object $tab + * @param string|array|Varien_Object $tab * @return Mage_Adminhtml_Block_Widget_Tabs */ public function addTab($tabId, $tab) @@ -141,6 +162,13 @@ public function addTab($tabId, $tab) $this->setActiveTab($tabId); } + // For sorting tabs. + $this->_tabPositions[$tabId] = $this->_tabPosition; + $this->_tabPosition += 100; + if ($this->_tabs[$tabId]->getAfter()) { + $this->_afterTabIds[$tabId] = $this->_tabs[$tabId]->getAfter(); + } + return $this; } @@ -161,11 +189,6 @@ public function setActiveTab($tabId) if (isset($this->_tabs[$tabId]) && $this->canShowTab($this->_tabs[$tabId]) && !$this->getTabIsHidden($this->_tabs[$tabId])) { $this->_activeTab = $tabId; - if (!(is_null($this->_activeTab)) && ($tabId !== $this->_activeTab)) { - foreach ($this->_tabs as $id => $tab) { - $tab->setActive($id === $tabId); - } - } } return $this; } @@ -178,20 +201,12 @@ public function setActiveTab($tabId) */ protected function _setActiveTab($tabId) { - foreach ($this->_tabs as $id => $tab) { - if ($this->getTabId($tab) == $tabId) { - $this->_activeTab = $id; - $tab->setActive(true); - return $this; - } - } + $this->_activeTab = $tabId; return $this; } protected function _beforeToHtml() { - $this->_tabs = $this->_reorderTabs(); - if ($activeTab = $this->getRequest()->getParam('active_tab')) { $this->setActiveTab($activeTab); } elseif ($activeTabId = Mage::getSingleton('admin/session')->getActiveTabId()) { @@ -202,88 +217,35 @@ protected function _beforeToHtml() $this->_activeTab = (reset($this->_tabs))->getId(); } + if (!empty($this->_afterTabIds)) { + $this->_tabs = $this->_reorderTabs(); + } + $this->assign('tabs', $this->_tabs); return parent::_beforeToHtml(); } - /** - * Reorder the tabs. - * - * @return array - */ - private function _reorderTabs() - { - $orderByIdentity = []; - $orderByPosition = []; - $position = 100; - - /** - * Set the initial positions for each tab. - * - * @var string $key - * @var TabInterface $tab - */ - foreach ($this->_tabs as $key => $tab) { - $tab->setPosition($position); - - $orderByIdentity[$key] = $tab; - $orderByPosition[$position] = $tab; - - $position += 100; - } - - return $this->_applyTabsCorrectOrder($orderByPosition, $orderByIdentity); - } - - /** - * @param array $orderByPosition - * @param array $orderByIdentity - * @return array - */ - private function _applyTabsCorrectOrder(array $orderByPosition, array $orderByIdentity) + protected function _reorderTabs() { $positionFactor = 1; - /** - * Rearrange the positions by using the after tag for each tab. - * - * @var integer $position - * @var Varien_Object $tab - */ - foreach ($orderByPosition as $position => $tab) { - if (!$tab->getAfter() || !array_key_exists($tab->getAfter(), $orderByIdentity)) { - $positionFactor = 1; - continue; + // Set new position based on $afterTabId. + foreach ($this->_afterTabIds as $tabId => $afterTabId) { + if (array_key_exists($afterTabId, $this->_tabs)) { + $this->_tabPositions[$tabId] = $this->_tabPositions[$afterTabId] + $positionFactor;; + $positionFactor++; } - - $grandPosition = $orderByIdentity[$tab->getAfter()]->getPosition(); - $newPosition = $grandPosition + $positionFactor; - - unset($orderByPosition[$position]); - $orderByPosition[$newPosition] = $tab; - $tab->setPosition($newPosition); - - $positionFactor++; } - return $this->_finalTabsSortOrder($orderByPosition); - } - - /** - * Apply the last sort order to tabs. - * - * @param array $orderByPosition - * @return array - */ - private function _finalTabsSortOrder(array $orderByPosition) - { - ksort($orderByPosition); + asort($this->_tabPositions); $ordered = []; - - /** @var Varien_Object $tab */ - foreach ($orderByPosition as $tab) { - $ordered[$tab->getId()] = $tab; + foreach ($this->_tabPositions as $tabId => $position) { + if (isset($this->_tabs[$tabId])) { + $tab = $this->_tabs[$tabId]; + $tab->setActive($tab->getTabId() == $this->_activeTab); + $ordered[$tabId] = $tab; + } } return $ordered; From d2ec9b9ba5bb3902aa832df77e66fc2c86974a80 Mon Sep 17 00:00:00 2001 From: kiat ng Date: Mon, 21 Dec 2020 11:13:22 +0800 Subject: [PATCH 3/6] Fixed bug when tabs with the same root are added non-sequentially. --- .../core/Mage/Adminhtml/Block/Widget/Tabs.php | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php b/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php index dbe464052b7..3d3c4188049 100644 --- a/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php +++ b/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php @@ -207,6 +207,8 @@ protected function _setActiveTab($tabId) protected function _beforeToHtml() { + Mage::dispatchEvent('adminhtml_widget_tabs_html_before', array('block' => $this)); + if ($activeTab = $this->getRequest()->getParam('active_tab')) { $this->setActiveTab($activeTab); } elseif ($activeTabId = Mage::getSingleton('admin/session')->getActiveTabId()) { @@ -224,16 +226,33 @@ protected function _beforeToHtml() $this->assign('tabs', $this->_tabs); return parent::_beforeToHtml(); } - - protected function _reorderTabs() + + /** + * Find the root parent Tab ID recursively. + * + * @param string $currentAfterTabId + * @param int $degree Degrees of separation between child and root parent. + * @return string The parent tab ID. + */ + protected function _getRootParentTabId($currentAfterTabId, &$degree) { - $positionFactor = 1; + if (array_key_exists($currentAfterTabId, $this->_afterTabIds)) { + $degree++; + return $this->_getRootParentTabId($this->_afterTabIds[$currentAfterTabId], $degree); + } else { + return $currentAfterTabId; + } + } + protected function _reorderTabs() + { // Set new position based on $afterTabId. foreach ($this->_afterTabIds as $tabId => $afterTabId) { if (array_key_exists($afterTabId, $this->_tabs)) { - $this->_tabPositions[$tabId] = $this->_tabPositions[$afterTabId] + $positionFactor;; - $positionFactor++; + $degree = 1; // Initialize to 1 degree of separation. + $parentAfterTabId = $this->_getRootParentTabId($afterTabId, $degree); + $this->_tabPositions[$tabId] = $this->_tabPositions[$parentAfterTabId] + $degree; + $degree++; } } @@ -243,7 +262,6 @@ protected function _reorderTabs() foreach ($this->_tabPositions as $tabId => $position) { if (isset($this->_tabs[$tabId])) { $tab = $this->_tabs[$tabId]; - $tab->setActive($tab->getTabId() == $this->_activeTab); $ordered[$tabId] = $tab; } } From 5452e9db3bfd55c2149fedfa990a775a9143e00d Mon Sep 17 00:00:00 2001 From: Ng Kiat Siong Date: Mon, 21 Dec 2020 11:16:20 +0800 Subject: [PATCH 4/6] Update Tabs.php --- app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php b/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php index 3d3c4188049..8fcd514690e 100644 --- a/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php +++ b/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php @@ -207,8 +207,6 @@ protected function _setActiveTab($tabId) protected function _beforeToHtml() { - Mage::dispatchEvent('adminhtml_widget_tabs_html_before', array('block' => $this)); - if ($activeTab = $this->getRequest()->getParam('active_tab')) { $this->setActiveTab($activeTab); } elseif ($activeTabId = Mage::getSingleton('admin/session')->getActiveTabId()) { From c11d8b9a5fad13b5f90e1d93079244eed0a7c543 Mon Sep 17 00:00:00 2001 From: kiat ng Date: Tue, 5 Jan 2021 14:01:09 +0800 Subject: [PATCH 5/6] Revert function _setActiveTab() to original, necessary for horizontal tabs in Categories page. --- app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php b/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php index 8fcd514690e..b62db6f79aa 100644 --- a/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php +++ b/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php @@ -201,7 +201,13 @@ public function setActiveTab($tabId) */ protected function _setActiveTab($tabId) { - $this->_activeTab = $tabId; + foreach ($this->_tabs as $id => $tab) { + if ($this->getTabId($tab) == $tabId) { + $this->_activeTab = $id; + $tab->setActive(true); + return $this; + } + } return $this; } From 346ac5f2ef9bd3faa709e758f131f9bd0dc08a6d Mon Sep 17 00:00:00 2001 From: kiat ng Date: Mon, 19 Jul 2021 08:59:47 +0800 Subject: [PATCH 6/6] Trim trailing white spaces. --- app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php b/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php index b62db6f79aa..99b2a0d6fc1 100644 --- a/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php +++ b/app/code/core/Mage/Adminhtml/Block/Widget/Tabs.php @@ -42,21 +42,21 @@ class Mage_Adminhtml_Block_Widget_Tabs extends Mage_Adminhtml_Block_Widget /** * For sorting tabs. - * + * * @var array */ protected $_afterTabIds = []; /** * For sorting tabs. - * + * * @var array */ protected $_tabPositions = []; /** * For sorting tabs. - * + * * @var int */ protected $_tabPosition = 100; @@ -233,7 +233,7 @@ protected function _beforeToHtml() /** * Find the root parent Tab ID recursively. - * + * * @param string $currentAfterTabId * @param int $degree Degrees of separation between child and root parent. * @return string The parent tab ID.