Skip navigation.
Home

The evils of organically grown code...

Wonky Code

Thanks to Tit Petric for sending in this WTF. He's a courageous guy for submitting this because he wrote it himself. This code uses session variables to keep the last used menu item open when the user navigates to a page that isn't on the menu.

While not apparent, this WTF is a good example of what happens to code that grows to fulfill requirements that were not part of the original design. The original code was written to handle a single tier menu system. Then it grew to handle 2-tiers, 3-tiers, 4-tiers, etc. Right now the production code is sitting at 5-tiers *shudder*.

<?php
$current_link
= $menu->current_link();
foreach (
$menu->_menu as $id=>$val) {
       if ($menu->link_compare($val['link'],$current_link)) {
               $session->set($kv2."extra_id_root", $id);
               $session->set($kv2."extra_id_subroot",NULL);
               $session->set($kv2."extra_id_subsubroot",NULL);
               $session->set($kv2."extra_id_subsubsubroot", NULL);
               break;
       }
       $found_sm=false;
       foreach ($menu->_menu[$id]['item'] as $sid=>$sval) {
               $found_smx=false;
               if ($menu->link_compare($sval['link'],$current_link)) {
                       $session->set($kv2."extra_id_root", $id);
                       $session->set($kv2."extra_id_subroot", $sid);
                       $session->set($kv2."extra_id_subsubroot",NULL);
                       $session->set($kv2."extra_id_subsubsubroot", NULL);
                       $found_sm=true;
               }
               foreach ($menu->_menu[$id]['item'][$sid]['item'] as $ssid=>$ssval) {
                       if ($menu->link_compare($ssval['link'],$current_link)) {
                               $session->set($kv2."extra_id_root", $id);
                               $session->set($kv2."extra_id_subroot", $sid);
                               $session->set($kv2."extra_id_subsubroot", $ssid);
                               $session->set($kv2."extra_id_subsubsubroot", NULL);
                               $found_smx=true;
                       }
                       foreach ($menu->_menu[$id]['item'][$sid]['item'][$ssid]['item'] as $sssid=>$sssval) {
                               if ($menu->link_compare($sssval['link'],$current_link)) {
                                       $session->set($kv2."extra_id_root", $id);
                                       $session->set($kv2."extra_id_subroot", $sid);
                                       $session->set($kv2."extra_id_subsubroot", $ssid);
                                       $session->set($kv2."extra_id_subsubsubroot", $sssid);
                               }
                       }
                       if ($found_smx) {
                               break;
                       }
               }
               if ($found_sm) {
                       break;
               }
       }
}
?>

The obvious solution is use a recursive function. However, that is easier said than done because individual session variables are used to track the menus that should remain open. In the redesign I recommend putting all the open menu ID's into a one dimensional array. Something that looks like this:

<?php
// assuming that each menu has a unique ID...
$openMenus[$id] = true;
?>

Then the menu creation code can quickly and easily check that a particular menu ID is open and create whatever client side code is needed to display the menu. What do you think?