You are here

function coffee_redirect in Coffee 6

Same name and namespace in other branches
  1. 7 coffee.hooks.inc \coffee_redirect()

The default implementation of Coffee. It will look in the menu_links table for administrative links based on the user input

Parameters

string $input: The user input that will be used to query the database.

1 call to coffee_redirect()
coffee_result_handler in ./coffee.module
Handler for Coffee

File

./coffee.hooks.inc, line 16
The hooks that are used by Coffee includes an example of hook_coffee_command()

Code

function coffee_redirect($input) {

  // Glue the arguments together if there are more than 3 arguments.
  // This happens when the user $input typed a / in the input field.
  $arguments = arg();
  if (count($arguments) > 3) {
    $argument_query = array_splice($arguments, 3);
    $input = implode('/', $argument_query);
  }

  // Add the % for the LIKE query.
  $input = '%' . $input . '%';

  // Build the query for the result set.
  // Look up items in the menu_links table.
  // Limit the result set to 7.
  // Join menu_router table, for the menu link description.
  // Exclude % in link_path.
  // Exclude 'help' in link_path.
  // Filter out empty link_title.
  // Execute the query above.
  $query = "SELECT\n  \t\t\t\t\t  path, title\n  \t\t\t\t\tFROM\n  \t\t\t\t\t  {menu_router} mr\n  \t\t\t\t\tWHERE\n  \t\t\t\t\t  (\n  \t\t\t\t\t   mr.path LIKE '%s' OR\n  \t\t\t\t\t   mr.title LIKE '%s'\n  \t\t\t\t\t  )\n  \t\t\t\t\t  AND mr.title <> '%%'\n  \t\t\t\t\t  AND mr.path NOT LIKE '%help%'\n  \t\t\t\t\t  AND mr.path NOT LIKE '%\\%%'\n  \t\t\t\t\t  ORDER BY mr.path ASC\n  \t\t\t\t\t  LIMIT 0, 75\n  \t\t\t\t\t";
  $args = array(
    'admin/' . $input,
    'admin/' . $input,
  );
  $query = db_query($query, $args);
  while ($result = db_fetch_object($query)) {
    $results[] = $result;
  }
  if (is_array($results)) {
    $return = array();
    foreach ($results as $record) {

      // Load the menu item and check if the user has persmission.
      $item = menu_get_item($record->path);

      // Only add items to the return array if the user has access.
      if ($item['access']) {
        $match = array(
          'path' => $record->path,
          'title' => $record->title,
        );

        // Make this the top item if the title is an exact match.
        if (drupal_strtolower(trim($record->title)) == drupal_strtolower(trim($input, '% '))) {
          array_unshift($return, $match);
        }
        else {
          $return[] = $match;
        }
      }
    }
  }
  else {
    return FALSE;
  }

  // Return only a slice of a maximum of 7 results.
  if (is_array($return)) {
    $number_of_items = variable_get('coffee_number_of_results', 7);
    $return = array_slice($return, 0, $number_of_items);
  }
  return $return;
}