You are here

function coffee_get_menu_response in Coffee 7.2

Function coffee_get_menu_response().

Menu callback function which outputs the menu structure as JSON ready for populating the autocomplete data source.

1 string reference to 'coffee_get_menu_response'
coffee_menu in ./coffee.module
Implements hook_menu().

File

./coffee.module, line 117
Coffee primary module file

Code

function coffee_get_menu_response() {
  global $user, $language;
  $output = array();

  // Get the commands list from the cache if possible.
  $cid = 'coffee_commands:' . $user->uid . ':' . $language->language;
  if ($cache = cache_get($cid, 'cache_coffee')) {
    if (isset($cache->data)) {
      $output = $cache->data;
    }
  }

  // Nothing from cache, rebuild and then cache.
  if (empty($output)) {

    // Grab menu data.
    $menus = variable_get('coffee_settings_menus', array(
      'management',
      'user-menu',
    ));
    foreach ($menus as $v) {
      if ($v == '0') {
        continue;
      }
      $menu = menu_tree_all_data($v);
      foreach ($menu as $k => $link) {
        $command = $v == 'user-menu' ? ':user' : NULL;
        coffee_traverse_below($link, $output, $command);
      }
    }

    // Include the commands.
    // Include the default hooks for Coffee.
    module_load_include('inc', 'coffee', 'coffee.hooks');

    // Invoke all implementations of hook_coffee_commands().
    $commands = array();
    foreach (module_implements('coffee_commands') as $module) {
      $commands = array_merge($commands, module_invoke($module, 'coffee_commands', ''));
    }
    if (!empty($commands)) {
      $output = array_merge($output, $commands);
    }
    foreach ($output as $k => $v) {

      // If there are links to <front> remove them.
      if ($v['value'] === '<front>') {
        unset($output[$k]);
        continue;
      }
      if (!empty($v['parent'])) {
        $output[$k]['parent'] = $v['parent'];
      }

      // Prevent any nasty commands from getting through.
      $output[$k]['label'] = filter_xss($output[$k]['label']);

      // If clean URLs are turned off, alter the command URLs.
      if (empty($GLOBALS['conf']['clean_url'])) {
        $output[$k]['value'] = '?q=' . $output[$k]['value'];
      }
    }

    // Re-index the array, it matters to jQuery if we've removed items.
    $output = array_values($output);

    // Set the cache for this user.
    cache_set($cid, $output, 'cache_coffee');
  }
  drupal_json_output($output);
  drupal_exit();
}