You are here

public function FrxMenu::addMenuItems in Forena Reports 7.3

Same name and namespace in other branches
  1. 7.4 FrxMenu.inc \FrxMenu::addMenuItems()

Add menu items to the items array

Parameters

$items array of menu items.:

File

./FrxMenu.inc, line 138
FrxMenu.inc Drupal menu builder @author davidmetzler

Class

FrxMenu
@file FrxMenu.inc Drupal menu builder @author davidmetzler

Code

public function addMenuItems(&$items) {
  global $language;
  $result = db_query("SELECT report_name, path, title, cache FROM {forena_reports} where path<>'' AND language='en' ORDER BY path asc");
  $reports = array();
  foreach ($result as $row) {
    $access = TRUE;
    $cache = $row->cache;
    if ($cache) {
      $cache = unserialize($cache);

      // Load menu item defaults
      $menu = @$cache['menu'];
      $path = $menu['path'];
      $path_args = @$menu['args'];
      $type = @$menu['type'];
      $title = @$menu['title'] ? $menu['title'] : $row->title;
      if (module_exists('locale')) {
      }

      //Default type
      switch ($type) {
        case 'normal-item':
          $menu_type = MENU_NORMAL_ITEM;
          break;
        case 'default-local-task':
          $menu_type = MENU_DEFAULT_LOCAL_TASK;
          break;
        case 'local-task':
          $menu_type = MENU_LOCAL_TASK;
          break;
        default:
          $menu_type = MENU_CALLBACK;
      }

      //Replace the tokens with drupal menu wildcards
      $tokens = $this
        ->tokens($path);
      $new_path = $path;
      foreach ($tokens as $i => $token) {
        $new_path = str_replace(':' . $token, '%', $new_path);
        $args[] = $i;
      }

      // Now generate the callback arguments
      $parts = explode('/', $new_path);
      $page_args = array_keys($parts, '%');
      $path_args = $path_args ? rtrim($path, '/') . '/' . ltrim($path_args, '/') : $path;
      $page_args = array_merge(array(
        $path_args,
        $row->report_name,
      ), $page_args);

      // Set the access callback
      $access_callback = isset($cache['access']) ? 'forena_check_all_access' : TRUE;
      if ($menu_type == MENU_DEFAULT_LOCAL_TASK) {
        $parts = explode('/', $new_path);
        array_pop($parts);
        $parent_path = implode('/', $parts);

        // build the parent menu because we are also building the local task
        // but onlu do so if another report doesn't define the parent.
        if (!isset($items[$parent_path])) {
          $items[$parent_path] = array(
            'type' => MENU_CALLBACK,
            'title' => $row->title,
            'access callback' => $access_callback,
            'access arguments' => array(
              $cache['access'],
            ),
            'page callback' => 'forena_report_menu_callback',
            'page arguments' => $page_args,
          );
          if (module_exists('locale')) {
            $items[$parent_path]['title callback'] = 'forena_report_title_callback';
            $items[$parent_path]['title arguments'] = array(
              $row->report_name,
              FALSE,
            );
          }
          if ($access_callback === 'forena_check_all_access') {
            $items[$parent_path]['access arguments'][] = $cache['access'];
          }
        }
      }
      $items[$new_path] = array(
        'type' => $menu_type,
        'title' => $title,
        'access callback' => $access_callback,
        'access arguments' => array(
          @$cache['access'],
        ),
        'page callback' => 'forena_report_menu_callback',
        'page arguments' => $page_args,
      );
      if (module_exists('locale')) {
        $items[$new_path]['title callback'] = 'forena_report_title_callback';
        $items[$new_path]['title arguments'] = array(
          $row->report_name,
          TRUE,
        );
      }
      if ($access_callback === 'forena_check_all_access') {
        $items[$new_path]['access arguments'][] = $cache['access'];
      }
    }
  }
}