| 1 |
<?php
|
| 2 |
// $Id: node_breadcrumb.module,v 1.25 2009/10/19 10:03:43 edhel Exp $
|
| 3 |
|
| 4 |
function _node_breadcrumb_set_location($mid, $last_path, $last_title) {
|
| 5 |
$add_active_class = variable_get('node_breadcrumb_add_active_class', FALSE);
|
| 6 |
$menu_item = menu_link_load($mid);
|
| 7 |
menu_local_tasks(0);
|
| 8 |
menu_local_tasks(1);
|
| 9 |
menu_set_item($_GET['q'], $menu_item);
|
| 10 |
if ($add_active_class) {
|
| 11 |
$q = $_GET['q'];
|
| 12 |
$_GET['q'] = $menu_item['link_path'];
|
| 13 |
}
|
| 14 |
$breadcrumb = array();
|
| 15 |
while ($mid && ($item = menu_link_load($mid))) {
|
| 16 |
if (!isset($menu_name) && $add_active_class) {
|
| 17 |
menu_tree($menu_name = $item['menu_name']);
|
| 18 |
}
|
| 19 |
array_unshift($breadcrumb, l($item['link_title'], $item['link_path']));
|
| 20 |
$mid = $item['plid'];
|
| 21 |
}
|
| 22 |
array_unshift($breadcrumb, l(t('Home'), NULL));
|
| 23 |
drupal_set_breadcrumb($breadcrumb);
|
| 24 |
if ($add_active_class) {
|
| 25 |
$_GET['q'] = $q;
|
| 26 |
}
|
| 27 |
}
|
| 28 |
|
| 29 |
function node_breadcrumb_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
|
| 30 |
if ($op == 'view' && $page) {
|
| 31 |
$db_rules = db_query("select * from {node_breadcrumb_rule} order by weight, rid");
|
| 32 |
while ($rule = db_fetch_object($db_rules)) {
|
| 33 |
// check node type
|
| 34 |
if ($rule->node_type != '' && $node->type != $rule->node_type) continue;
|
| 35 |
// check terms
|
| 36 |
foreach (array($rule->tid1, $rule->tid2) as $tid) {
|
| 37 |
if ($tid > 0) {
|
| 38 |
if (empty($node->taxonomy[$tid])) continue 2;
|
| 39 |
}
|
| 40 |
elseif ($tid < 0) {
|
| 41 |
foreach ($node->taxonomy as $term) {
|
| 42 |
if ($term->vid == -$tid) continue 2;
|
| 43 |
}
|
| 44 |
continue 2;
|
| 45 |
}
|
| 46 |
}
|
| 47 |
// check php condition
|
| 48 |
if ($rule->condition != '') {
|
| 49 |
eval("\$condition=$rule->condition;");
|
| 50 |
if (!$condition) {
|
| 51 |
continue;
|
| 52 |
}
|
| 53 |
}
|
| 54 |
// apply menu location
|
| 55 |
_node_breadcrumb_set_location($rule->mid, "node/$node->nid", $node->title);
|
| 56 |
break;
|
| 57 |
}
|
| 58 |
module_invoke_all("node_breadcrumb", $node);
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
function node_breadcrumb_perm() {
|
| 63 |
return array('administer node breadcrumb');
|
| 64 |
}
|
| 65 |
|
| 66 |
function node_breadcrumb_init() {
|
| 67 |
$menu_item = menu_get_item();
|
| 68 |
if ($menu_item) {
|
| 69 |
$menu = db_result(db_query("select menu_name from {menu_links} where link_path='%s'", $menu_item['href']));
|
| 70 |
if ($menu != 'admin_menu') {
|
| 71 |
menu_set_active_menu_name($menu);
|
| 72 |
}
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
function node_breadcrumb_menu() {
|
| 77 |
$items = array();
|
| 78 |
$items['admin/settings/node_breadcrumb'] = array(
|
| 79 |
'title' => 'Node breadcrumb',
|
| 80 |
'page callback' => 'drupal_get_form',
|
| 81 |
'page arguments' => array('node_breadcrumb_admin_settings'),
|
| 82 |
'access arguments' => array('administer node breadcrumb'),
|
| 83 |
);
|
| 84 |
return $items;
|
| 85 |
}
|
| 86 |
|
| 87 |
function _node_breadcrumb_rule_form($fieldset_title, $node_type, $tids, $condition, $mid, $weight, $weight_delta, $submit) {
|
| 88 |
$form = array('#type' => 'fieldset', '#title' => $fieldset_title);
|
| 89 |
|
| 90 |
// node type
|
| 91 |
$types = node_get_types();
|
| 92 |
$types_select = array(
|
| 93 |
'#type' => 'select',
|
| 94 |
'#title' => t('Content type'),
|
| 95 |
'#options' => array('' => "<". t('none') .">")
|
| 96 |
);
|
| 97 |
foreach ($types as $type) {
|
| 98 |
$types_select['#options'][$type->type] = $type->name;
|
| 99 |
}
|
| 100 |
if ($type) {
|
| 101 |
$types_select['#default_value'] = $node_type;
|
| 102 |
}
|
| 103 |
$form['node_type'] = $types_select;
|
| 104 |
|
| 105 |
// term
|
| 106 |
if (module_exists('taxonomy')) {
|
| 107 |
$form['tid'] = array('#type' => 'fieldset', '#title' => t('Categories'), '#attributes' => array('class' => 'node_breadcrumb_categories'));
|
| 108 |
$vocabularies = taxonomy_get_vocabularies();
|
| 109 |
$none_option = "<". t('none') .">";
|
| 110 |
$vid2tid = array();
|
| 111 |
if (is_array($tids)) {
|
| 112 |
foreach ($tids as $tid) {
|
| 113 |
if ($tid > 0) {
|
| 114 |
$term = taxonomy_get_term($tid);
|
| 115 |
$vid2tid[$term->vid] = $tid;
|
| 116 |
}
|
| 117 |
elseif ($tid < 0) {
|
| 118 |
$vid2tid[-$tid] = $tid;
|
| 119 |
}
|
| 120 |
}
|
| 121 |
}
|
| 122 |
foreach ($vocabularies as $vocabulary) {
|
| 123 |
$taxonomy_form = taxonomy_form($vocabulary->vid);
|
| 124 |
unset($taxonomy_form['#options']['']);
|
| 125 |
$taxonomy_form['#multiple'] = FALSE;
|
| 126 |
$taxonomy_form['#default_value'] = $vid2tid[$vocabulary->vid] ? $vid2tid[$vocabulary->vid] : 0;
|
| 127 |
unset($taxonomy_form['#size']);
|
| 128 |
unset($all_option);
|
| 129 |
$all_option->option = array(-$vocabulary->vid => "<". t("any") .">");
|
| 130 |
array_unshift($taxonomy_form['#options'], $all_option);
|
| 131 |
array_unshift($taxonomy_form['#options'], $none_option);
|
| 132 |
$form['tid']["vid_$vocabulary->vid"] = $taxonomy_form;
|
| 133 |
}
|
| 134 |
}
|
| 135 |
|
| 136 |
// php condition
|
| 137 |
$form['condition'] = array(
|
| 138 |
'#type' => 'textarea',
|
| 139 |
'#rows' => 1,
|
| 140 |
'#title' => t('Condition'),
|
| 141 |
'#description' => t('Additional PHP expression, e. g., $node->type == \'story\' || $node->type == \'news\''),
|
| 142 |
'#default_value' => $condition,
|
| 143 |
);
|
| 144 |
|
| 145 |
// menu item
|
| 146 |
$menu_item = menu_link_load($mid);
|
| 147 |
$form['mid'] = array(
|
| 148 |
'#type' => 'select',
|
| 149 |
'#title' => t('Menu item'),
|
| 150 |
'#required' => TRUE,
|
| 151 |
'#options' => menu_parent_options(menu_get_menus(), array('mlid' => 0)),
|
| 152 |
'#default_value' => "$menu_item[menu_name]:$mid",
|
| 153 |
);
|
| 154 |
|
| 155 |
// weight
|
| 156 |
$form['weight'] = array(
|
| 157 |
'#type' => 'weight',
|
| 158 |
'#title' => t('Weight'),
|
| 159 |
'#default_value' => $weight,
|
| 160 |
'#required' => TRUE,
|
| 161 |
'#delta' => $weight_delta,
|
| 162 |
);
|
| 163 |
|
| 164 |
// submit
|
| 165 |
$form['submit'] = array('#type' => 'submit', '#value' => $submit);
|
| 166 |
|
| 167 |
return $form;
|
| 168 |
}
|
| 169 |
|
| 170 |
function node_breadcrumb_admin_settings($dummy = NULL, $rid = NULL) {
|
| 171 |
drupal_add_css(drupal_get_path('module', 'node_breadcrumb') .'/node_breadcrumb.css');
|
| 172 |
|
| 173 |
$weight_delta = max(5, db_result(db_query("select max(abs(weight)) from {node_breadcrumb_rule}")) + 1);
|
| 174 |
if (module_exists('taxonomy')) $vocabularies = taxonomy_get_vocabularies();
|
| 175 |
$types = node_get_types();
|
| 176 |
|
| 177 |
// add rule
|
| 178 |
$form['add'] = _node_breadcrumb_rule_form(t('Add rule'), NULL, NULL, NULL, NULL, 0, $weight_delta, t('Add rule'));
|
| 179 |
|
| 180 |
// edit rule
|
| 181 |
if ($rid) {
|
| 182 |
$db_rule = db_query("select * from {node_breadcrumb_rule} where rid=%d", $rid);
|
| 183 |
$rule = db_fetch_object($db_rule);
|
| 184 |
if ($rule->rid) {
|
| 185 |
$form['edit'] = _node_breadcrumb_rule_form(t('Edit rule'), $rule->node_type, array($rule->tid1, $rule->tid2), $rule->condition, $rule->mid, $rule->weight, $weight_delta, t('Save rule'));
|
| 186 |
$form['edit']['rid'] = array('#type' => 'hidden', '#value' => $rid);
|
| 187 |
$form['edit']['cancel'] = array('#type' => 'submit', '#value' => t('Cancel'));
|
| 188 |
unset($form['add']);
|
| 189 |
}
|
| 190 |
}
|
| 191 |
|
| 192 |
// rules
|
| 193 |
$db_rules = db_query("select * from {node_breadcrumb_rule} order by weight, rid");
|
| 194 |
while ($rule = db_fetch_object($db_rules)) {
|
| 195 |
$menu_item = menu_link_load($rule->mid);
|
| 196 |
$terms = array();
|
| 197 |
if (module_exists('taxonomy')) {
|
| 198 |
if ($rule->tid1) {
|
| 199 |
$term = taxonomy_get_term($rule->tid1);
|
| 200 |
$terms[] = $rule->tid1 > 0 ? l($term->name, "taxonomy/term/$rule->tid1") : (t("any of") ." <". $vocabularies[-$rule->tid1]->name .">");
|
| 201 |
}
|
| 202 |
if ($rule->tid2) {
|
| 203 |
$term = taxonomy_get_term($rule->tid2);
|
| 204 |
$terms[] = l($term->name, "taxonomy/term/$rule->tid2");
|
| 205 |
}
|
| 206 |
}
|
| 207 |
unset($condition);
|
| 208 |
if ($rule->condition != '') {
|
| 209 |
$condition = check_plain($rule->condition);
|
| 210 |
$js_condition = check_plain(str_replace("'", "\\'", $rule->condition));
|
| 211 |
$condition = "<a title=\"$condition\" href=\"javascript:prompt('". t('Condition') ."','$js_condition');void(0);\">PHP</a>";
|
| 212 |
}
|
| 213 |
$rids[$rule->rid] = '';
|
| 214 |
$form['rules']['node_type'][$rule->rid] = array('#value' => $types[$rule->node_type]->name);
|
| 215 |
$form['rules']['tid'][$rule->rid] = array('#value' => empty($terms) ? "<". t('none') .">" : join(", ", $terms));
|
| 216 |
$form['rules']['condition'][$rule->rid] = array('#value' => $condition);
|
| 217 |
$form['rules']['mid'][$rule->rid] = array('#value' => l($menu_item['title'], $menu_item['href']));
|
| 218 |
$form['rules']['weight']["weight_$rule->rid"] = array('#type' => 'weight', '#default_value' => $rule->weight, '#required' => TRUE, '#delta' => $weight_delta);
|
| 219 |
$form['rules']['edit'][$rule->rid] = array('#value' => l(t('edit'), "admin/settings/node_breadcrumb/$rule->rid"));
|
| 220 |
}
|
| 221 |
$form['rules']['delete'] = array('#type' => 'checkboxes', '#options' => $rids);
|
| 222 |
if ($form['rules']['node_type']) {
|
| 223 |
$form['rules']['submit'] = array('#type' => 'submit', '#value' => t('Delete rule'));
|
| 224 |
$form['rules']['save'] = array('#type' => 'submit', '#value' => t('Save weights'));
|
| 225 |
}
|
| 226 |
|
| 227 |
// more options
|
| 228 |
$form['more'] = array('#type' => 'fieldset', '#title' => 'More options');
|
| 229 |
$form['more']['add_active_class'] = array('#type' => 'checkbox', '#title' => 'Add <strong>active</strong> class for menu item', '#default_value' => variable_get('node_breadcrumb_add_active_class', FALSE));
|
| 230 |
$form['more']['save'] = array('#type' => 'submit', '#value' => t('Save options'));
|
| 231 |
|
| 232 |
return $form;
|
| 233 |
}
|
| 234 |
|
| 235 |
function node_breadcrumb_theme() {
|
| 236 |
return array(
|
| 237 |
'node_breadcrumb_admin_settings' => array(
|
| 238 |
'arguments' => array('form' => NULL),
|
| 239 |
),
|
| 240 |
);
|
| 241 |
}
|
| 242 |
|
| 243 |
function theme_node_breadcrumb_admin_settings($form) {
|
| 244 |
$output = "";
|
| 245 |
if (!empty($form['rules']['node_type'])) {
|
| 246 |
$header = array(theme('table_select_header_cell'), t('Type'), t('Term'), t('Condition'), t('Menu item'), t('Weight'), t('Operations'));
|
| 247 |
foreach (element_children($form['rules']['node_type']) as $rid) {
|
| 248 |
$rows[] = array(
|
| 249 |
drupal_render($form['rules']['delete'][$rid]),
|
| 250 |
drupal_render($form['rules']['node_type'][$rid]),
|
| 251 |
drupal_render($form['rules']['tid'][$rid]),
|
| 252 |
drupal_render($form['rules']['condition'][$rid]),
|
| 253 |
drupal_render($form['rules']['mid'][$rid]),
|
| 254 |
drupal_render($form['rules']['weight']["weight_$rid"]),
|
| 255 |
drupal_render($form['rules']['edit'][$rid]),
|
| 256 |
);
|
| 257 |
}
|
| 258 |
$rows[] = array(
|
| 259 |
array('data' => drupal_render($form['rules']['submit']), 'colspan' => 5),
|
| 260 |
array('data' => drupal_render($form['rules']['save'])),
|
| 261 |
array('data' => ''),
|
| 262 |
);
|
| 263 |
$rules = theme('table', $header, $rows);
|
| 264 |
$output .= "<fieldset><legend>". t("Rules") ."</legend>$rules</fieldset>";
|
| 265 |
}
|
| 266 |
$output .= drupal_render($form);
|
| 267 |
return $output;
|
| 268 |
}
|
| 269 |
|
| 270 |
function node_breadcrumb_admin_settings_validate($form, &$form_state) {
|
| 271 |
if ($form_state['values']['op'] == t('Delete rule') || $form_state['values']['op'] == t('Save weights') || $form_state['values']['op'] == t('Save options')) {
|
| 272 |
return;
|
| 273 |
}
|
| 274 |
|
| 275 |
foreach ($form_state['values'] as $key => $value) {
|
| 276 |
if (substr($key, 0, 4) == 'vid_' && $value != 0) $tids++;
|
| 277 |
}
|
| 278 |
if ($tids > 2) {
|
| 279 |
form_set_error('tid', t('You may select not more than 2 terms.'));
|
| 280 |
}
|
| 281 |
elseif ($form_state['values']['node_type'] == '' && $tids == 0 && empty($form_state['values']['condition'])) {
|
| 282 |
form_set_error('', t('Fill the form below.'));
|
| 283 |
}
|
| 284 |
}
|
| 285 |
|
| 286 |
function node_breadcrumb_admin_settings_submit($form, &$form_state) {
|
| 287 |
if (isset($form_state['values']['mid'])) {
|
| 288 |
$mid = $form_state['values']['mid'];
|
| 289 |
$mid_colon = strpos($mid, ":");
|
| 290 |
$mid = $mid_colon === FALSE ? NULL : substr($mid, $mid_colon + 1);
|
| 291 |
}
|
| 292 |
if ($form_state['values']['op'] == t('Delete rule')) {
|
| 293 |
foreach ($form_state['values']['delete'] as $value) {
|
| 294 |
if ($value) {
|
| 295 |
$rids[] = $value + 0;
|
| 296 |
}
|
| 297 |
}
|
| 298 |
if ($rids) {
|
| 299 |
db_query("DELETE FROM {node_breadcrumb_rule} WHERE rid IN (%s)", join(",", $rids));
|
| 300 |
drupal_set_message(t('Rule(s) has been deleted.'));
|
| 301 |
}
|
| 302 |
}
|
| 303 |
elseif ($form_state['values']['op'] == t('Save weights')) {
|
| 304 |
foreach ($form_state['values'] as $key => $weight) {
|
| 305 |
if (substr($key, 0, 7) == 'weight_') {
|
| 306 |
$rid = substr($key, 7);
|
| 307 |
db_query("update {node_breadcrumb_rule} set weight=%d where rid=%d", $weight, $rid);
|
| 308 |
}
|
| 309 |
}
|
| 310 |
drupal_set_message(t("Weights has been applied."));
|
| 311 |
}
|
| 312 |
elseif ($form_state['values']['op'] == t('Add rule')) {
|
| 313 |
$tid = array();
|
| 314 |
foreach ($form_state['values'] as $key => $value) {
|
| 315 |
if (substr($key, 0, 4) == 'vid_' && $value != 0) $tid[] = $value;
|
| 316 |
}
|
| 317 |
$a = $GLOBALS['db_type'] == 'pgsql' ? "" : "`";
|
| 318 |
db_query("INSERT INTO {node_breadcrumb_rule} (node_type, tid1, tid2, mid, weight, %scondition%s) VALUES ('%s', %d, %d, %d, %d, '%s')", $a, $a, $form_state['values']['node_type'], $tid[0], $tid[1], $mid, $form_state['values']['weight'], $form_state['values']['condition']);
|
| 319 |
drupal_set_message(t('Rule has been added.'));
|
| 320 |
}
|
| 321 |
elseif ($form_state['values']['op'] == t('Save rule')) {
|
| 322 |
$tid = array();
|
| 323 |
foreach ($form_state['values'] as $key => $value) {
|
| 324 |
if (substr($key, 0, 4) == 'vid_' && $value != 0) $tid[] = $value;
|
| 325 |
}
|
| 326 |
$a = $GLOBALS['db_type'] == 'pgsql' ? "" : "`";
|
| 327 |
db_query("UPDATE {node_breadcrumb_rule} SET node_type='%s', tid1=%d, tid2=%d, mid=%d, weight=%d, %scondition%s='%s' WHERE rid=%d", $form_state['values']['node_type'], $tid[0], $tid[1], $mid, $form_state['values']['weight'], $a, $a, $form_state['values']['condition'], $form_state['values']['rid']);
|
| 328 |
drupal_set_message(t('Rule has been saved.'));
|
| 329 |
$form_state['redirect'] = "admin/settings/node_breadcrumb";
|
| 330 |
}
|
| 331 |
elseif ($form_state['values']['op'] == t('Cancel')) {
|
| 332 |
$form_state['redirect'] = "admin/settings/node_breadcrumb";
|
| 333 |
}
|
| 334 |
elseif ($form_state['values']['op'] == t('Save options')) {
|
| 335 |
variable_set('node_breadcrumb_add_active_class', $form_state['values']['add_active_class'] ? TRUE : FALSE);
|
| 336 |
drupal_set_message(t('Options have been saved.'));
|
| 337 |
}
|
| 338 |
}
|
| 339 |
|
| 340 |
//function _d($var) {if ($GLOBALS['user']->uid == 1) drupal_set_message("<pre>" . print_r($var, 1) . "</pre>");}
|