| 1 |
<?php |
<?php |
| 2 |
// $Id: custom_breadcrumbs.module,v 1.4 2007/01/02 21:54:55 eaton Exp $ |
// $Id: custom_breadcrumbs.module,v 1.5.2.6 2007/08/25 06:36:26 eaton Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Implementation of hook_menu(). |
* Implementation of hook_menu(). |
| 6 |
*/ |
*/ |
| 7 |
function custom_breadcrumbs_menu($may_cache) { |
function custom_breadcrumbs_menu() { |
| 8 |
$items = array(); |
$items = array(); |
| 9 |
|
|
| 10 |
if ($may_cache) { |
$items['admin/build/custom_breadcrumbs'] = array( |
| 11 |
$access = user_access('administer custom breadcrumbs'); |
'title' => t('Custom breadcrumbs'), |
| 12 |
$items[] = array( |
'description' => t('Add custom breadcrumb trails for content types.'), |
| 13 |
'path' => 'admin/build/custom_breadcrumbs', |
'page callback' => 'custom_breadcrumbs_page', |
| 14 |
'title' => t('Custom breadcrumbs'), |
'access callback' => 'user_access', |
| 15 |
'description' => t('Add custom breadcrumb trails for content types.'), |
'access arguments' => array('administer custom breadcrumbs'), |
| 16 |
'callback' => 'custom_breadcrumbs_page', |
'file' => 'custom_breadcrumbs.admin.inc', |
| 17 |
'access' => $access |
); |
| 18 |
); |
|
| 19 |
|
$items['admin/build/custom_breadcrumbs/add'] = array( |
| 20 |
$items[] = array( |
'title' => t('Add custom breadcrumb'), |
| 21 |
'path' => 'admin/build/custom_breadcrumbs/add', |
'type' => MENU_CALLBACK, |
| 22 |
'title' => t('Add custom breadcrumb'), |
'page callback' => 'drupal_get_form', |
| 23 |
'type' => MENU_CALLBACK, |
'page arguments' => array('custom_breadcrumbs_form'), |
| 24 |
'callback' => 'drupal_get_form', |
'access callback' => 'user_access', |
| 25 |
'callback arguments' => array('custom_breadcrumbs_form'), |
'access arguments' => array('administer custom breadcrumbs'), |
| 26 |
'access' => $access |
'file' => 'custom_breadcrumbs.admin.inc', |
| 27 |
); |
); |
| 28 |
|
|
| 29 |
$items[] = array( |
$items['admin/build/custom_breadcrumbs/edit'] = array( |
| 30 |
'path' => 'admin/build/custom_breadcrumbs/edit', |
'title' => t('Edit custom breadcrumb'), |
| 31 |
'title' => t('Edit custom breadcrumb'), |
'type' => MENU_CALLBACK, |
| 32 |
'type' => MENU_CALLBACK, |
'page callback' => 'drupal_get_form', |
| 33 |
'callback' => 'drupal_get_form', |
'page arguments' => array('custom_breadcrumbs_form'), |
| 34 |
'callback arguments' => array('custom_breadcrumbs_form'), |
'access callback' => 'user_access', |
| 35 |
'access' => $access |
'access arguments' => array('administer custom breadcrumbs'), |
| 36 |
); |
'file' => 'custom_breadcrumbs.admin.inc', |
| 37 |
} |
); |
| 38 |
|
|
| 39 |
return $items; |
return $items; |
| 40 |
} |
} |
| 41 |
|
|
| 42 |
function custom_breadcrumbs_perm() { |
function custom_breadcrumbs_perm() { |
| 43 |
return array('administer custom breadcrumbs'); |
return array('administer custom breadcrumbs', 'use php in custom breadcrumbs'); |
| 44 |
} |
} |
| 45 |
|
|
| 46 |
function custom_breadcrumbs_nodeapi($node, $op, $teaser, $page) { |
function custom_breadcrumbs_nodeapi($node, $op, $teaser, $page) { |
| 47 |
if ($page) { |
if ($op == 'view' && !$teaser && $page) { |
| 48 |
if ($breadcrumb = _custom_breadcrumbs_load_for_type($node->type)) { |
if ($breadcrumb = _custom_breadcrumbs_load_for_type($node)) { |
| 49 |
$titles = explode("\n", $breadcrumb->titles); |
$titles = explode("\n", $breadcrumb->titles); |
| 50 |
$paths = explode("\n", $breadcrumb->paths); |
$paths = explode("\n", $breadcrumb->paths); |
| 51 |
|
|
| 54 |
|
|
| 55 |
$trail = array(l(t('Home'), '')); |
$trail = array(l(t('Home'), '')); |
| 56 |
for ($i = 0; $i < count($titles); $i++) { |
for ($i = 0; $i < count($titles); $i++) { |
| 57 |
$trail[] = l(trim($titles[$i]), trim($paths[$i])); |
// Skip empty titles |
| 58 |
|
if ($title = trim($titles[$i])) { |
| 59 |
|
// Output plaintext instead of a link if there is a title without a path. |
| 60 |
|
$path = trim($paths[$i]); |
| 61 |
|
if (strlen($path) > 0 && $path != '<none>') { |
| 62 |
|
$trail[] = l($title, trim($paths[$i])); |
| 63 |
|
} |
| 64 |
|
else { |
| 65 |
|
$trail[] = check_plain($title); |
| 66 |
|
} |
| 67 |
|
} |
| 68 |
} |
} |
| 69 |
drupal_set_breadcrumb($trail); |
drupal_set_breadcrumb($trail); |
| 70 |
} |
} |
| 72 |
|
|
| 73 |
} |
} |
| 74 |
|
|
|
// Lists all current custom breadcrumbs, and provides a link to the edit page. |
|
|
function custom_breadcrumbs_page() { |
|
|
$breadcrumbs = _custom_breadcrumbs_load_all_breadcrumbs(TRUE); |
|
|
|
|
|
$header = array(t('node type'), ''); |
|
|
|
|
|
$rows = array(); |
|
|
foreach ($breadcrumbs as $breadcrumb) { |
|
|
$row = array(); |
|
|
$row[] = $breadcrumb->node_type; |
|
|
$row[] = l(t('edit'), 'admin/build/custom_breadcrumbs/edit/' . $breadcrumb->bid); |
|
|
$rows[] = $row; |
|
|
} |
|
|
if (count($rows) == 0) { |
|
|
$rows[] = array(array('data' => t('No custom breadcrumbs have been defined.'), 'colspan' => 2)); |
|
|
} |
|
|
|
|
|
$rows[] = array(array('data' => l(t('Add a new custom breadcrumb'), 'admin/build/custom_breadcrumbs/add'), 'colspan' => 2)); |
|
|
|
|
|
|
|
|
return theme('table', $header, $rows); |
|
|
} |
|
|
|
|
|
|
|
|
// Displays an edit form for a custom breadcrumb record. |
|
|
function custom_breadcrumbs_form() { |
|
|
$bid = arg(4); |
|
|
if (isset($bid)) { |
|
|
$breadcrumb = _custom_breadcrumbs_load_breadcrumb($bid); |
|
|
$form['bid'] = array( |
|
|
'#type' => 'hidden', |
|
|
'#value' => $bid, |
|
|
); |
|
|
} |
|
|
|
|
|
$options = array(); |
|
|
foreach (node_get_types('names') as $type => $name) { |
|
|
$options[$type] = $name; |
|
|
} |
|
|
|
|
|
$form['node_type'] = array( |
|
|
'#type' => 'select', |
|
|
'#title' => t('Node type'), |
|
|
'#required' => TRUE, |
|
|
'#options' => $options, |
|
|
'#description' => t('The node type this custom breadcrumb trail will apply to.'), |
|
|
'#default_value' => $bid ? $breadcrumb->node_type : NULL, |
|
|
); |
|
|
|
|
|
$form['titles'] = array( |
|
|
'#type' => 'textarea', |
|
|
'#title' => t('Titles'), |
|
|
'#required' => TRUE, |
|
|
'#description' => t('A list of titles for the breadcrumb links, one on each line.'), |
|
|
'#default_value' => $bid ? $breadcrumb->titles : NULL |
|
|
); |
|
|
|
|
|
$form['paths'] = array( |
|
|
'#type' => 'textarea', |
|
|
'#title' => t('Paths'), |
|
|
'#required' => TRUE, |
|
|
'#description' => t('A list of Drupal paths for the breadcrumb links, one on each line.'), |
|
|
'#default_value' => $bid ? $breadcrumb->paths : NULL |
|
|
); |
|
|
|
|
|
$form['help'] = array( |
|
|
'#type' => 'fieldset', |
|
|
'#collapsible' => TRUE, |
|
|
'#collapsed' => TRUE, |
|
|
'#title' => t('Placeholder tokens'), |
|
|
'#description' => t("The following placeholder tokens can be used in both paths and titles. When used in a path or title, they will be replaced with the appropriate values."), |
|
|
); |
|
|
|
|
|
if (module_exists('token')) { |
|
|
$form['help']['tokens'] = array( |
|
|
'#value' => theme('token_help', 'node'), |
|
|
); |
|
|
} |
|
|
else { |
|
|
$form['help']['#description'] = t("To use dynamic placeholder tokens in your breadcrumb trails (the ID or title of the current node, for example), download and install the <a href='@token'>Token module</a> from Drupal.org.", array('@token' => 'http://www.drupal.org/project/token')); |
|
|
$form['help']['#collapsible'] = FALSE; |
|
|
$form['help']['#collapsed'] = FALSE; |
|
|
} |
|
|
|
|
|
$form['buttons']['submit'] = array( |
|
|
'#type' => 'submit', |
|
|
'#value' => t('Submit'), |
|
|
); |
|
|
if ($bid) { |
|
|
$form['buttons']['delete'] = array( |
|
|
'#type' => 'submit', |
|
|
'#value' => t('Delete'), |
|
|
); |
|
|
} |
|
|
|
|
|
return $form; |
|
|
} |
|
|
|
|
|
function custom_breadcrumbs_form_validate($form_id, $form_values) { |
|
|
$path_count = count(explode("\n", $form_values['paths'])); |
|
|
$title_count = count(explode("\n", $form_values['titles'])); |
|
|
if ($title_count != $path_count) { |
|
|
form_set_error(t('Every link path must have a matching title. There are !paths paths, and !titles titles.', array('!paths' => $path_count, '!titles' => $title_count))); |
|
|
} |
|
|
} |
|
|
|
|
|
function custom_breadcrumbs_form_submit($form_id, $form_values) { |
|
|
if ($form_values['op'] == t('Delete')) { |
|
|
_custom_breadcrumbs_delete_breadcrumb($form_values['bid']); |
|
|
} |
|
|
else { |
|
|
$breadcrumb = (object)$form_values; |
|
|
_custom_breadcrumbs_save_breadcrumb($breadcrumb); |
|
|
} |
|
|
return 'admin/build/custom_breadcrumbs'; |
|
|
} |
|
|
|
|
| 75 |
function _custom_breadcrumbs_load_breadcrumb($bid) { |
function _custom_breadcrumbs_load_breadcrumb($bid) { |
| 76 |
$sql = 'SELECT * FROM {custom_breadcrumb} WHERE bid = %d'; |
$sql = 'SELECT * FROM {custom_breadcrumb} WHERE bid = %d'; |
| 77 |
$result = db_query($sql, $bid); |
$result = db_query($sql, $bid); |
| 79 |
return $breadcrumb; |
return $breadcrumb; |
| 80 |
} |
} |
| 81 |
|
|
| 82 |
function _custom_breadcrumbs_load_for_type($type) { |
function _custom_breadcrumbs_load_for_type($node) { |
| 83 |
$sql = "SELECT * FROM {custom_breadcrumb} WHERE node_type = '%s'"; |
$sql = "SELECT * FROM {custom_breadcrumb} WHERE node_type = '%s'"; |
| 84 |
$result = db_query($sql, $type); |
$result = db_query($sql, $node->type); |
| 85 |
while ($breadcrumb = db_fetch_object($result)) { |
while ($breadcrumb = db_fetch_object($result)) { |
| 86 |
return $breadcrumb; |
if (!empty($breadcrumb->visibility_php)) { |
| 87 |
|
// Use PHP code to check the visibility. |
| 88 |
|
ob_start(); |
| 89 |
|
$result = eval(trim($breadcrumb->visibility_php)); |
| 90 |
|
ob_end_clean(); |
| 91 |
|
if ($result == TRUE) { |
| 92 |
|
return $breadcrumb; |
| 93 |
|
} |
| 94 |
|
} |
| 95 |
|
else { |
| 96 |
|
return $breadcrumb; |
| 97 |
|
} |
| 98 |
} |
} |
| 99 |
} |
} |
| 100 |
|
|
| 112 |
return $breadcrumbs; |
return $breadcrumbs; |
| 113 |
} |
} |
| 114 |
|
|
| 115 |
function _custom_breadcrumbs_save_breadcrumb($breadcrumb = NULL) { |
function custom_breadcrumbs_save_breadcrumb($breadcrumb = NULL) { |
| 116 |
|
if (is_array($breadcrumb->paths)) { |
| 117 |
|
$breadcrumb->paths = implode("\n", $breadcrumb->paths); |
| 118 |
|
} |
| 119 |
|
if (is_array($breadcrumb->titles)) { |
| 120 |
|
$breadcrumb->titles = implode("\n", $breadcrumb->titles); |
| 121 |
|
} |
| 122 |
|
|
| 123 |
if (isset($breadcrumb->bid)) { |
if (isset($breadcrumb->bid)) { |
| 124 |
$sql = "UPDATE {custom_breadcrumb} SET"; |
drupal_write_record('custom_breadcrumb', $breadcrumb, 'bid'); |
|
$sql .= " titles = '%s', paths = '%s', node_type = '%s'"; |
|
|
$sql .= " WHERE bid = %d"; |
|
|
db_query($sql, $breadcrumb->titles, $breadcrumb->paths, $breadcrumb->node_type, $breadcrumb->bid); |
|
| 125 |
} |
} |
| 126 |
else { |
else { |
| 127 |
$sql = "INSERT INTO {custom_breadcrumb}"; |
drupal_write_record('custom_breadcrumb', $breadcrumb); |
|
$sql .= " (titles, paths, node_type)"; |
|
|
$sql .= " VALUES ('%s', '%s', '%s')"; |
|
|
db_query($sql, $breadcrumb->titles, $breadcrumb->paths, $breadcrumb->node_type); |
|
| 128 |
} |
} |
| 129 |
} |
} |
| 130 |
|
|