Build Menu Based on User Permissions


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.  * Get Menu
  3.  *
  4.  * Builds a menu based on what the current user has access to.
  5.  *
  6.  * @return string
  7.  */
  8. public function get_menu()
  9. {
  10. // Load the available paths from the database.
  11. $crm = url::is_admin() ? '1' : '0';
  12. $sql = 'SELECT p.id, p.parent_id, p.path, p.text
  13. FROM paths AS p,
  14. path_users AS u
  15. WHERE u.user_id='.$this->user->id.'
  16. AND u.path_id=p.id
  17. ORDER BY p.parent_id ASC, p.display ASC';
  18. $query = $this->db->query($sql);
  19.  
  20. // Loop through the results and build a new array.
  21. $pages = array();
  22. foreach ($query as $row)
  23. {
  24. $data = array(
  25. 'path' => $row->path,
  26. 'text' => $row->text
  27. );
  28. // Parent page?
  29. if ($row->parent_id == 0)
  30. {
  31. $pages[$row->id] = $data;
  32. }
  33. else
  34. {
  35. $pages[$row->parent_id]['children'][] = $data;
  36. }
  37. }
  38.  
  39. // Build the output
  40. $output = '';
  41. foreach ($pages as $page)
  42. {
  43. $output .= '<ul><li><a href="'.$page['path'].'">'.$page['text'].'</a>';
  44.  
  45. // Build children.
  46. $total = isset($page['children']) ? count($page['children']) : 0;
  47. if ($total > 0)
  48. {
  49. $output .= '<ul>';
  50. for ($i = 0; $i < $total; ++$i)
  51. {
  52. $output .= '<li><a href="'.$page['children'][$i]['path'].'">'.$page['children'][$i]['text'].'</a></li>';
  53. }
  54. $output .= '</ul>';
  55. }
  56.  
  57. $output .= '<li></ul>';
  58. }
  59.  
  60. return $output;
  61. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.