<?php
// $Id: custom_breadcrumbs_paths.module,v 1.1.2.22 2010/05/03 22:49:15 mgn Exp $

/**
 * @file
 * Assign custom breadcrumbs based on the Drupal path.
 */
module_load_include('inc', 'custom_breadcrumbs', 'custom_breadcrumbs.admin');
module_load_include('inc', 'custom_breadcrumbs', 'custom_breadcrumbs_common');

/**
 * Implements hook_cb_breadcrumb_info().
 *
 * @return an array with elements:
 *   'table' indicating the db_table to load the breadcrumb from,
 *   'field' a unique field of the database table used to identify the breadcrumb,
 *   'type' a string used for indicating the breadcrumb type on the admin list,
 *   'name_constructor' a function which generates the breadcrumb name from the breadcrumb.
 */
function custom_breadcrumbs_paths_cb_breadcrumb_info() {
  $breadcrumb_type_info = array();
  $breadcrumb_type_info['paths'] = array(
    'table' => 'custom_breadcrumbs_paths',
    'field' => 'specific_path',
    'type' => 'path',
    'name_constructor' => '_custom_breadcrumbs_paths_breadcrumb_name',
  );
  return $breadcrumb_type_info;
}

/**
 * Constructs a default name to display in the admin screen.
 *
 * @param $breadcrumb
 *   The breadcrumb object.
 *
 * @return
 *   A text string that will be used as the breadcrumb name.
 */
function _custom_breadcrumbs_paths_breadcrumb_name($breadcrumb) {
  if (isset($breadcrumb->specific_path)) {
    return $breadcrumb->specific_path;
  }
}

/**
 * Implements hook_menu().
 */
function custom_breadcrumbs_paths_menu() {
  $items = array();
  $items['admin/build/custom_breadcrumbs/path/add'] = array(
    'title'            => 'Path',
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('custom_breadcrumbs_paths_form', 'path'),
    'access arguments' => array('administer custom breadcrumbs'),
    'type'             => MENU_LOCAL_TASK,
    'weight'           => 5,
  );
  $items['admin/build/custom_breadcrumbs/path/edit'] = array(
    'title'            => 'Edit custom breadcrumb for path',
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('custom_breadcrumbs_paths_form', 'path'),
    'access arguments' => array('administer custom breadcrumbs'),
    'type'             => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_preprocess().
 */
function custom_breadcrumbs_paths_preprocess(&$variables, $hook) {
  // Check for a match to cover module callback pages.
  // Can't do token replacement without context.
  _custom_breadcrumbs_paths_set_breadcrumb();
}

/**
 * Implements hook_nodeapi().
 */
function custom_breadcrumbs_paths_nodeapi($node, $op, $teaser, $page) {
  if ($op == 'alter' && empty($teaser) && !empty($page)) {
    // Check for breadcrumb at this path and set if a match is found.
    _custom_breadcrumbs_paths_set_breadcrumb(array('node' => $node));
  }
}

/**
 * Implements hook_views_pre_render().
 */
function custom_breadcrumbs_paths_views_pre_render(&$view) {
  // Don't really do anything with the view. This is just a pretense to insert a breadcrumb.
  $curpath = drupal_get_normal_path($_GET['q']);
  // Check to see if the view path matches the current path.
  $viewpage = FALSE;
  foreach ($view->display as $display) {
    // We're only interested in main page views.
    if (!_custom_breadcrumbs_allowed_display($display)) continue;
    $viewpath = _custom_breadcrumbs_construct_view_path($display);
    $viewpage = $viewpage || _custom_breadcrumbs_match_path($curpath, $viewpath);
  }
  if ($viewpage) {
    // Check for breadcrumb at this path and set if a match is found.
    _custom_breadcrumbs_paths_set_breadcrumb(array('view' => $view));
  }
}

/**
 * Checks for a custom breadcrumb at the current path and sets it if one is found.
 *
 * @param $objs
 *    An array of objects to be used in token replacement. Array keys indicate type of object.
 */
function _custom_breadcrumbs_paths_set_breadcrumb($objs = array()) {
  $matchpath = variable_get('custom_breadcrumbs_paths_allow_wildcards', FALSE);
  $breadcrumbs = _custom_breadcrumbs_paths_get_breadcrumbs($matchpath);
  if (!empty($breadcrumbs)) {
    foreach ($breadcrumbs as $id => $breadcrumb) {
      if ((!$matchpath) || _custom_breadcrumbs_paths_page_match($breadcrumb)) {
        if (custom_breadcrumbs_is_visible($breadcrumb, $objs)) {
          if ($matchpath) {
            $num = substr_count($breadcrumb->specific_path, '*');
            if (!isset($min) || (isset($min) && ($num < $min))) {
                $min = $num;
                $min_id = $id;
            }
            if ($num == 0) break;
          }
          else {
            $min_id = $id;
            // Don't check any others once a visible breadcrumb is found.
            break;
          }
        }
      }
    }
    if (isset($min_id)) {
      custom_breadcrumbs_set_breadcrumb($breadcrumbs[$min_id], $objs);
    }
  }
}

/**
 * Gets the custom_breadcrumbs_paths breadcrumbs.
 *
 * @param $matchpath
 *   If TRUE, then load all paths breadcrumbs to allow wildcard matching,
 *   otherwise only the current path is queried.
 * @param $path
 *   The drupal path to match against.
 *
 * @return $breadcrumbs
 *   An array of breadcrumb objects meeting the query criteria.
 */
function _custom_breadcrumbs_paths_get_breadcrumbs($matchpath = FALSE, $path = NULL) {
  // Don't bother checking if we don't have a path to match against.
  if (isset($_REQUEST['q']) || !is_null($path)) {
    global $language;
    $languages = array('language' => $language->language, 'all' => '');
    // Load all path breadcrumbs for wildcard matching.
    $param = array();
    if (!$matchpath) {
      // Check for path prefix and strip it out if its found.
      $prefix = $language->language .'/';
      $path = is_null($path) ? str_replace($prefix, '', $_REQUEST['q']) : $path;
      $param = array('specific_path' => $path);
    }
    $breadcrumbs = custom_breadcrumbs_load_breadcrumbs('custom_breadcrumbs_paths', NULL, $param, $languages);
    return $breadcrumbs;
  }
}

/**
 * Determines if the current path matches the breadcrumb specific path.
 *
 * @param $breadcrumb
 *   The breadcrumb object.
 *
 * @return $page_match
 *   TRUE if the current path matches the breadcrumb specific path, FALSE otherwise.
 */
function _custom_breadcrumbs_paths_page_match($breadcrumb) {
  $page_match = FALSE;
  if (isset($_REQUEST['q'])) {
    if (isset($breadcrumb->language) && $breadcrumb->language != '') {
      // Check for a match on the prefixed path.
      $path = $breadcrumb->language .'/'. $breadcrumb->specific_path;
      $page_match = _custom_breadcrumbs_match_path($_REQUEST['q'], $path);
    }
    else {
      // Append the current language if the breadcrumb language is 'All'.
      global $language;
      $path = $language->language .'/'. $breadcrumb->specific_path;
      $page_match = _custom_breadcrumbs_match_path($_REQUEST['q'], $path);
    }
    if (!$page_match) {
      // Check for a direct match.
      $page_match = _custom_breadcrumbs_match_path($_REQUEST['q'], $breadcrumb->specific_path);
    }
  }
  return $page_match;
}

/**
 * Implements hook_cb_node_form_table().
 *
 * @param $node
 *   The node object being edited.
 *
 * @return $breadcrumbs
 *   An array of breadcrumb objects with the same path as the node
 *   used in the custom_breadcrumbs fieldset on the node edit page.
 */
function custom_breadcrumbs_paths_cb_node_form_table($node) {
  $matchpath = variable_get('custom_breadcrumbs_paths_allow_wildcards', FALSE);
  $param = ($matchpath) ? array() : array('specific_path' => $node->path);
  $breadcrumbs = custom_breadcrumbs_load_breadcrumbs('custom_breadcrumbs_paths', NULL, $param);
  foreach ($breadcrumbs as $key => $breadcrumb) {
    if (!_custom_breadcrumbs_match_path($node->path, $breadcrumb->specific_path)) {
      unset($breadcrumbs[$key]);
    }
  }
  return $breadcrumbs;
}

/**
 * Form builder; Provide an edit form for a custom breadcrumb paths breadcrumb.
 *
 * @ingroup forms
 * @see custom_breadcrumbs_form_validate()
 * @see custom_breadcrumbs_form_submit()
 */
function custom_breadcrumbs_paths_form(&$form_state, $type) {
  $form = array();
  $bid = arg(5);
  $breadcrumb = NULL;
  if (isset($bid)) {
    drupal_set_title(t('Edit Custom Breadcrumb for Path'));
    $breadcrumb = array_pop(custom_breadcrumbs_load_breadcrumbs('custom_breadcrumbs_paths', NULL, array('bid' => $bid)));
  }
  else {
    drupal_set_title(t('Add Custom Breadcrumb for Path'));
  }
  $description = t('The Drupal path that this custom breadcrumb trail will apply to.');
  if (variable_get('custom_breadcrumbs_paths_allow_wildcards', FALSE)) {
    $description .= ' '. t("The '*' character can be used as a wildcard to set a custom breadcrumb for all matching paths. For example, foo/bar* could be used to match every page with a path beginning with foo/bar. Do not include language prefixes in the path. This will be handled automatically according to the selected language.");
  }
  $form['specific_path'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Specific Path'),
    '#required'      => TRUE,
    '#description'   => $description,
    '#default_value' => isset($breadcrumb->specific_path) ? $breadcrumb->specific_path : NULL,
    '#weight'        => -10,
  );

  $form += custom_breadcrumbs_common_form_elements($bid, $breadcrumb);
  // Store the function to call to save this breadcrumb.
  $form['#module'] = 'custom_breadcrumbs_paths';
  $form['#infokey'] = 'paths';
  $form['#submit'][] = 'custom_breadcrumbs_form_submit';
  $form['#validate'][] = 'custom_breadcrumbs_form_validate';
  return $form;
}

/**
 * Implements hook_form_alter().
 */
function custom_breadcrumbs_paths_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'custom_breadcrumbs_admin_settings') {
    $form['adv_settings']['custom_breadcrumbs_paths_allow_wildcards'] = array(
      '#type'          => 'checkbox',
      '#title'         => t('Use wildcard pattern matching in paths'),
      '#default_value' => variable_get('custom_breadcrumbs_paths_allow_wildcards', FALSE),
      '#description'   => t("If checked, the '*' character can be used as a wildcard to set a custom breadcrumb for all matching paths. For example, foo/bar/* could be used to match every page with a path beginning with foo/bar."),
      '#weight'        => -20,
    );
  }
}
