You are here

crumbs.module in Crumbs, the Breadcrumbs suite 6.2

Same filename and directory in other branches
  1. 6 crumbs.module
  2. 7.2 crumbs.module
  3. 7 crumbs.module

File

crumbs.module
View source
<?php

function crumbs_init() {

  // $data = crumbs_get_breadcrumb_data();
}
function crumbs_perm() {
  return array(
    'administer crumbs',
  );
}
function crumbs_menu() {
  $items = array();
  $items['admin/build/crumbs'] = array(
    'title' => 'Crumbs',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'crumbs_admin_form',
    ),
    'access arguments' => array(
      'administer crumbs',
    ),
    'file' => 'crumbs.admin.inc',
  );
  $items['admin/build/crumbs/debug'] = array(
    'title' => 'Debug',
    'page callback' => 'crumbs_debug_page',
    'access arguments' => array(
      'administer crumbs',
    ),
    'file' => 'crumbs.debug.inc',
  );
  return $items;
}
function crumbs_theme() {
  return array(
    'crumbs_breadcrumb' => array(
      'breadcrumb_items' => NULL,
    ),
  );
}
function crumbs_preprocess_page(&$vars) {
  $breadcrumb_data = crumbs_get_breadcrumb_data();
  $vars['crumbs_trail'] = $breadcrumb_data['trail'];
  $vars['crumbs_breadcrumb_items'] = $breadcrumb_data['items'];
  $vars['crumbs_breadcrumb_html'] = $breadcrumb_data['html'];
  $vars['breadcrumb'] = $breadcrumb_data['html'];

  // TODO: Invoke preprocessPage() plugin method.
  $plugin_engine = crumbs_get_plugin_engine();
  $action = new _crumbs_InvokeAction_preprocessPage($vars);
  $plugin_engine
    ->invokeAll_alter($action);
}
function crumbs_get_breadcrumb_data() {
  static $_data;
  if (!isset($_data)) {
    $trail = crumbs_get_trail();
    module_load_include('inc', 'crumbs', 'crumbs.breadcrumb');
    $items = crumbs_build_breadcrumb($trail);
    $html = theme('crumbs_breadcrumb', $items);
    $_data = compact('trail', 'items', 'html');
  }
  return $_data;
}
function crumbs_get_trail($path = NULL) {
  static $_trails = array();
  if (!isset($path)) {
    $path = $_GET['q'];
  }
  $path = drupal_get_normal_path($path);
  if (!isset($_trails[$path])) {
    module_load_include('inc', 'crumbs', 'crumbs.plugin_engine');
    module_load_include('inc', 'crumbs', 'crumbs.trail');
    $_trails[$path] = crumbs_build_trail($path);
  }
  return $_trails[$path];
}
function crumbs_match_pattern($patterns, $path = NULL) {
  if (is_string($patterns)) {
    $patterns = explode("\n", $patterns);
  }
  if (!isset($path)) {
    $path = $_GET['q'];
  }
  $trail = crumbs_get_trail($path);
  $patterns_normalized = array();
  foreach ($patterns as $pattern) {
    $pattern = trim(strtolower($pattern));
    if (strlen($pattern)) {
      $patterns_normalized[$pattern] = TRUE;
    }
  }
  $patterns = $patterns_normalized;
  foreach (array_reverse($trail) as $item) {
    $alias = $item['alias'];
    if (isset($patterns[$alias])) {
      return TRUE;
    }
    else {
      if (isset($patterns['! ' . $alias])) {
        return FALSE;
      }
      else {
        if (isset($patterns[$alias . '/+'])) {
          return TRUE;
        }
        else {
          if (isset($patterns['! ' . $alias . '/+'])) {
            return FALSE;
          }
        }
      }
    }
  }
  $alias = drupal_get_path_alias($path);
  $fragments = explode('/', $alias);
  $partial_alias = array_shift($fragments);
  $match = NULL;
  while (TRUE) {
    if (empty($fragments)) {
      break;
    }
    if (isset($patterns[$partial_alias . '/*'])) {
      $match = TRUE;
    }
    else {
      if (isset($patterns['! ' . $partial_alias . '/*'])) {
        $match = FALSE;
      }
    }
    $partial_alias .= '/' . array_shift($fragments);
  }
  if (isset($match)) {
    return $match;
  }
  if (isset($patterns['*'])) {
    return TRUE;
  }
  return FALSE;
}
function crumbs_get_plugin_engine() {
  static $_plugin_engine;
  if (!isset($_plugin_engine)) {
    module_load_include('inc', 'crumbs', 'crumbs.plugin_engine');
    $_plugin_engine = _crumbs_load_plugin_engine();
  }
  return $_plugin_engine;
}

/**
 * This function has exactly one possible input value for
 * each possible return value, except the return value FALSE.
 * 
 * @param $router_path :string
 *   The router path can contain any character, but will typically
 *   have a format like "node/%/edit".
 * @return :string or FALSE
 *   A string that can be used as a method suffix,
 *   or FALSE, where that is not possible.
 *   The route "node/%/edit" will resolve as "node_x_edit".
 */
function crumbs_build_method_suffix($router_path) {
  $method_suffix = strtolower($router_path);
  $method_suffix = preg_replace('#[^a-z0-9\\%]#', '_', $method_suffix);
  $method_suffix = strtr($method_suffix, array(
    '%' => 'x',
  ));
  $reverse = strtr($method_suffix, array(
    '_' => '/',
  ));
  $reverse = preg_replace(array(
    '#/x/#',
    '#/x$#',
  ), array(
    '/%/',
    '/%',
  ), $reverse);

  // we need to do this two time to catch things like "/x/x/x/x".
  $reverse = strtr($reverse, array(
    '/x/' => '/%/',
  ));
  if ($reverse === $router_path) {
    return $method_suffix;
  }
  return FALSE;
}
class _crumbs_InvokeAction_preprocessPage implements crumbs_InvokeActionInterface_alter {
  protected $_vars;
  function __construct(array &$vars) {
    $this->_vars =& $vars;
  }

  /**
   * The point of the preprocessPage() method hook is a guaranteed execution
   * directly after crumbs_preprocess_page().
   */
  function invoke($plugin, $plugin_key) {
    if (method_exists($plugin, 'preprocessPage')) {
      $plugin
        ->preprocessPage($this->_vars);
    }
  }

}
function crumbs_benchmark($label = NULL, $dpm = FALSE) {
  static $_t;
  $t = microtime(TRUE);
  if (isset($_t) && isset($label)) {
    $str = 'Duration: ' . number_format(1000 * ($t - $_t), 3) . ' ms for ' . $label;
    if ($dpm) {
      dpm($str);
    }
  }
  $_t = $t;
  return $str;
}

// ============================================ interfaces =====================
interface crumbs_InvokeActionInterface_find {
  function invoke($plugin, $plugin_key, $weight_keeper);

}
interface crumbs_InvokeActionInterface_alter {
  function invoke($plugin, $plugin_key);

}