You are here

public function FrxMenu::addMenuItems in Forena Reports 7.4

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

Add menu items to the items array

Parameters

$items array of menu items.:

File

./FrxMenu.inc, line 143
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 = Frx::File()
    ->menuReports();
  $reports = array();
  foreach ($result as $row) {
    $access = TRUE;
    $cache = $row->cache;
    if ($cache) {

      // Load menu item defaults
      $menu = @$cache['menu'];
      $path = $menu['path'];
      $path_args = @$menu['args'];
      $type = @$menu['type'];
      $title = isset($menu['title']) ? $menu['title'] : $row->cache['title'];
      $weight = @$menu['weight'];
      $parent_path = '';

      //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->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->cache['title'],
            'access callback' => $access_callback,
            'access arguments' => array(
              $cache['access'],
            ),
            'page callback' => 'forena_report_menu_callback',
            'page arguments' => $page_args,
          );
          if (isset($row->cache['menu']['menu_name'])) {
            $items['parent_path']['menu_name'] = $row->cache['menu']['menu_name'];
          }
          if (isset($row->cache['menu']['tab_parent'])) {
            $items['parent_path']['tab_parent'] = $row->cache['menu']['tab_parent'];
          }
          if (isset($row->cache['menu']['tab_root'])) {
            $items['parent_path']['tab_root'] = $row->cache['menu']['tab_root'];
          }
          if (isset($row->cache['menu']['weight'])) {
            $items['parent_path']['weight'] = $row->cache['menu']['weight'];
          }
          if (isset($row->cache['menu']['weight'])) {
            $items['parent_path']['weight'] = $row->cache['menu']['weight'];
          }
          if (module_exists('locale')) {
            $items[$parent_path]['title callback'] = 'forena_report_title_callback';
            $items[$parent_path]['title arguments'] = array(
              $row->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 ($parent) $items[$new_path]['parent'] = $parent;
      if (isset($menu['weight'])) {
        $items[$new_path]['weight'] = $menu['weight'];
      }
      if (!$parent_path) {
        if (isset($menu['menu_name'])) {
          $items[$new_path]['menu_name'] = $menu['menu_name'];
        }
        if (isset($menu['tab_parent'])) {
          $items[$new_path]['tab_parent'] = $menu['tab_parent'];
        }
        if (isset($menu['tab_root'])) {
          $items[$new_path]['tab_root'] = $menu['tab_root'];
        }
        if (isset($menu['plid'])) {
          $items[$new_path]['plid'] = (int) $menu['plid'];
        }
      }
      if (module_exists('locale')) {
        $items[$new_path]['title callback'] = 'forena_report_title_callback';
        $items[$new_path]['title arguments'] = array(
          $row->name,
          TRUE,
        );
      }
      if ($access_callback === 'forena_check_all_access') {
        $items[$new_path]['access arguments'][] = $cache['access'];
      }
    }
  }
}