2007-07-30 20:33:48 +00:00
|
|
|
<?php
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2007-11-04 15:10:09 +00:00
|
|
|
* Page callbacks for the taxonomy module.
|
2007-07-30 20:33:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Menu callback; displays all nodes associated with a term.
|
|
|
|
*/
|
2008-09-19 20:25:03 +00:00
|
|
|
function taxonomy_term_page($terms, $depth = 0, $op = 'page') {
|
2007-07-30 20:33:48 +00:00
|
|
|
if ($terms['operator'] != 'and' && $terms['operator'] != 'or') {
|
|
|
|
drupal_not_found();
|
|
|
|
}
|
2008-09-19 20:25:03 +00:00
|
|
|
$str_tids = $terms['str_tids'];
|
2007-07-30 20:33:48 +00:00
|
|
|
|
|
|
|
if ($terms['tids']) {
|
2009-04-15 14:12:55 +00:00
|
|
|
$query = db_select('taxonomy_term_data', 't');
|
|
|
|
$query->addTag('term_access');
|
|
|
|
|
|
|
|
// Load array with all tid's the user has access to in the format tid => name.
|
|
|
|
$term_results = $query
|
|
|
|
->fields('t', array('tid', 'name'))
|
|
|
|
->condition('tid', $terms['tids'], 'IN')
|
|
|
|
->execute()
|
|
|
|
->fetchAllKeyed();
|
|
|
|
$tids = array_keys($term_results);
|
|
|
|
$names = array_values($term_results);
|
2007-07-30 20:33:48 +00:00
|
|
|
|
|
|
|
if ($names) {
|
2009-04-18 06:32:24 +00:00
|
|
|
$title = implode(', ', $names);
|
2007-07-30 20:33:48 +00:00
|
|
|
drupal_set_title($title);
|
|
|
|
|
|
|
|
switch ($op) {
|
|
|
|
case 'page':
|
|
|
|
// Build breadcrumb based on first hierarchy of first term:
|
2008-12-19 03:55:23 +00:00
|
|
|
$current = (object) array(
|
|
|
|
'tid' => $tids[0],
|
|
|
|
);
|
2007-10-25 08:24:43 +00:00
|
|
|
$breadcrumb = array();
|
2007-07-30 20:33:48 +00:00
|
|
|
while ($parents = taxonomy_get_parents($current->tid)) {
|
|
|
|
$current = array_shift($parents);
|
2008-09-19 02:43:40 +00:00
|
|
|
$breadcrumb[] = l($current->name, taxonomy_term_path($current));
|
2007-07-30 20:33:48 +00:00
|
|
|
}
|
2007-10-25 08:24:43 +00:00
|
|
|
$breadcrumb[] = l(t('Home'), NULL);
|
|
|
|
$breadcrumb = array_reverse($breadcrumb);
|
|
|
|
drupal_set_breadcrumb($breadcrumb);
|
2008-04-14 17:48:46 +00:00
|
|
|
drupal_add_feed(url('taxonomy/term/' . $str_tids . '/' . $depth . '/feed'), 'RSS - ' . $title);
|
2009-01-27 00:22:27 +00:00
|
|
|
drupal_add_css(drupal_get_path('module', 'taxonomy') . '/taxonomy.css');
|
|
|
|
|
|
|
|
$build = array();
|
2009-06-12 13:59:56 +00:00
|
|
|
// Only display fields if we have a single term, to avoid clutter and
|
|
|
|
// confusion.
|
2009-01-27 00:22:27 +00:00
|
|
|
if (count($tids) == 1) {
|
|
|
|
$term = taxonomy_term_load($tids[0]);
|
2009-06-12 13:59:56 +00:00
|
|
|
$build += field_attach_view('taxonomy_term', $term);
|
2009-01-27 00:22:27 +00:00
|
|
|
if (!empty($term->description)) {
|
|
|
|
$build['term_description'] = array(
|
|
|
|
'#markup' => filter_xss_admin($term->description),
|
|
|
|
'#weight' => -1,
|
|
|
|
'#prefix' => '<div class="taxonomy-term-description">',
|
|
|
|
'#suffix' => '</div>',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($nids = taxonomy_select_nodes($tids, $terms['operator'], $depth, TRUE)) {
|
|
|
|
$nodes = node_load_multiple($nids);
|
|
|
|
$build += node_build_multiple($nodes);
|
|
|
|
$build['pager'] = array(
|
2009-04-26 19:44:40 +00:00
|
|
|
'#markup' => theme('pager', NULL),
|
2009-01-27 00:22:27 +00:00
|
|
|
'#weight' => 5,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$build['no_content'] = array(
|
|
|
|
'#prefix' => '<p>',
|
|
|
|
'#markup' => t('There are currently no posts in this category.'),
|
|
|
|
'#suffix' => '</p>',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-05-21 21:12:25 +00:00
|
|
|
return $build;
|
2007-07-30 20:33:48 +00:00
|
|
|
|
|
|
|
case 'feed':
|
2008-04-14 17:48:46 +00:00
|
|
|
$channel['link'] = url('taxonomy/term/' . $str_tids . '/' . $depth, array('absolute' => TRUE));
|
|
|
|
$channel['title'] = variable_get('site_name', 'Drupal') . ' - ' . $title;
|
2007-11-04 15:10:09 +00:00
|
|
|
// Only display the description if we have a single term, to avoid clutter and confusion.
|
|
|
|
if (count($tids) == 1) {
|
2008-09-19 20:25:03 +00:00
|
|
|
$term = taxonomy_term_load($tids[0]);
|
2007-11-04 15:10:09 +00:00
|
|
|
// HTML will be removed from feed description, so no need to filter here.
|
|
|
|
$channel['description'] = $term->description;
|
|
|
|
}
|
2007-07-30 20:33:48 +00:00
|
|
|
|
2009-01-27 00:22:27 +00:00
|
|
|
$nids = taxonomy_select_nodes($tids, $terms['operator'], $depth, FALSE);
|
2007-07-30 20:33:48 +00:00
|
|
|
|
2009-01-27 00:22:27 +00:00
|
|
|
node_feed($nids, $channel);
|
2007-07-30 20:33:48 +00:00
|
|
|
break;
|
2007-11-04 15:10:09 +00:00
|
|
|
|
2007-07-30 20:33:48 +00:00
|
|
|
default:
|
|
|
|
drupal_not_found();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
drupal_not_found();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-19 20:25:03 +00:00
|
|
|
/**
|
|
|
|
* Page to edit a vocabulary term.
|
|
|
|
*/
|
|
|
|
function taxonomy_term_edit($term) {
|
|
|
|
if (isset($term)) {
|
2008-10-13 00:33:05 +00:00
|
|
|
drupal_set_title($term->name);
|
2008-09-19 20:25:03 +00:00
|
|
|
return drupal_get_form('taxonomy_form_term', taxonomy_vocabulary_load($term->vid), (array)$term);
|
|
|
|
}
|
|
|
|
return drupal_not_found();
|
|
|
|
}
|
|
|
|
|
2007-07-30 20:33:48 +00:00
|
|
|
/**
|
|
|
|
* Helper function for autocompletion
|
|
|
|
*/
|
2009-06-28 13:37:29 +00:00
|
|
|
function taxonomy_autocomplete($vid = 0, $tags_typed = '') {
|
2007-07-30 20:33:48 +00:00
|
|
|
// The user enters a comma-separated list of tags. We only autocomplete the last tag.
|
2009-06-28 13:37:29 +00:00
|
|
|
$tags_typed = drupal_explode_tags($tags_typed);
|
|
|
|
$tag_last = drupal_strtolower(array_pop($tags_typed));
|
2007-07-30 20:33:48 +00:00
|
|
|
|
|
|
|
$matches = array();
|
2009-06-28 13:37:29 +00:00
|
|
|
if ($tag_last != '') {
|
2009-04-15 14:12:55 +00:00
|
|
|
$query = db_select('taxonomy_term_data', 't');
|
|
|
|
$query->addTag('term_access');
|
2009-06-28 13:37:29 +00:00
|
|
|
$query->leftJoin('taxonomy_term_synonym', 'ts', 't.tid = ts.tid');
|
|
|
|
// Don't select already entered terms.
|
|
|
|
if (count($tags_typed)) {
|
|
|
|
$query->condition('t.name', $tags_typed, 'NOT IN');
|
|
|
|
}
|
|
|
|
$tags_return = $query
|
2009-04-15 14:12:55 +00:00
|
|
|
->fields('t', array('tid', 'name'))
|
|
|
|
->condition('t.vid', $vid)
|
2009-06-28 13:37:29 +00:00
|
|
|
// Select rows that either match by term or synonym name.
|
|
|
|
->condition(db_or()
|
|
|
|
->where("LOWER(t.name) LIKE :last_string", array(':last_string' => '%' . $tag_last . '%'))
|
|
|
|
->where("LOWER(ts.name) LIKE :last_string", array(':last_string' => '%' . $tag_last . '%'))
|
|
|
|
)
|
2009-04-15 14:12:55 +00:00
|
|
|
->range(0, 10)
|
|
|
|
->execute()
|
|
|
|
->fetchAllKeyed();
|
2009-05-24 17:39:35 +00:00
|
|
|
|
2009-06-28 13:37:29 +00:00
|
|
|
$prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
|
2007-07-30 20:33:48 +00:00
|
|
|
|
2009-06-28 13:37:29 +00:00
|
|
|
// We use two arrays to make sure synonym suggestions appear last.
|
|
|
|
$term_matches = $synonym_matches = array();
|
|
|
|
foreach ($tags_return as $tid => $name) {
|
2009-04-15 14:12:55 +00:00
|
|
|
$n = $name;
|
2007-07-30 20:33:48 +00:00
|
|
|
// Commas and quotes in terms are special cases, so encode 'em.
|
2009-04-15 14:12:55 +00:00
|
|
|
if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
|
|
|
|
$n = '"' . str_replace('"', '""', $name) . '"';
|
2007-07-30 20:33:48 +00:00
|
|
|
}
|
2009-06-28 13:37:29 +00:00
|
|
|
// Inform the user his query matched a synonym rather than a term.
|
|
|
|
if (strpos(drupal_strtolower($name), $tag_last) === FALSE) {
|
|
|
|
$name = t('Did you mean %suggestion', array('%suggestion' => $name));
|
|
|
|
$synonym_matches[$prefix . $n] = filter_xss($name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$term_matches[$prefix . $n] = filter_xss($name);
|
|
|
|
}
|
2007-07-30 20:33:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-28 13:37:29 +00:00
|
|
|
drupal_json(array_merge($term_matches, $synonym_matches));
|
2007-07-30 20:33:48 +00:00
|
|
|
}
|