Return to Snippet

Revision: 17949
at September 17, 2009 13:20 by awellis


Initial Code
/**
 * Get Menu
 * 
 * Builds a menu based on what the current user has access to.
 * 
 * @return string
 */
public function get_menu()
{
   // Load the available paths from the database.
   $crm = url::is_admin() ? '1' : '0';
   $sql = 'SELECT p.id, p.parent_id, p.path, p.text
             FROM paths AS p,
                  path_users AS u
            WHERE u.user_id='.$this->user->id.'
              AND u.path_id=p.id
            ORDER BY p.parent_id ASC, p.display ASC';
   $query = $this->db->query($sql);
   
   // Loop through the results and build a new array.
   $pages = array();
   foreach ($query as $row)
   {
      $data = array(
         'path' => $row->path,
         'text' => $row->text
      );
      // Parent page?
      if ($row->parent_id == 0)
      {
         $pages[$row->id] = $data;
      }
      else
      {
         $pages[$row->parent_id]['children'][] = $data;
      }
   }
   
   // Build the output
   $output = '';
   foreach ($pages as $page)
   {
      $output .= '<ul><li><a href="'.$page['path'].'">'.$page['text'].'</a>';
      
      // Build children.
      $total = isset($page['children']) ? count($page['children']) : 0;
      if ($total > 0)
      {
         $output .= '<ul>';
         for ($i = 0; $i < $total; ++$i)
         {
            $output .= '<li><a href="'.$page['children'][$i]['path'].'">'.$page['children'][$i]['text'].'</a></li>';
         }
         $output .= '</ul>';
      }
      
      $output .= '<li></ul>';
   }
   
   return $output;
}

Initial URL

                                

Initial Description

                                

Initial Title
Build Menu Based on User Permissions

Initial Tags

                                

Initial Language
PHP