| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This is a library of useful taxonomy functions and methods.
|
| 7 |
* please insert the link if you have a patch for core to introduce a function from this lib to core.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Taxonomy REST API: Build a path to a taxonomy listing page
|
| 12 |
* Patch pending at http://drupal.org/node/64664
|
| 13 |
*
|
| 14 |
* @param $terms
|
| 15 |
* a single numeric term id, or a single $term object
|
| 16 |
* or an array of numeric term ids or $term objects
|
| 17 |
* @param $op
|
| 18 |
* a string "AND" or "OR"
|
| 19 |
* @param $depth
|
| 20 |
* depth of the taxonomy tree that is to be returned. 0 equals the entire tree
|
| 21 |
* @param $feed
|
| 22 |
* TRUE for RSS feed, FALSE for page
|
| 23 |
*/
|
| 24 |
function taxonomy_get_path($terms, $op = 'AND', $depth = 0, $feed = FALSE) {
|
| 25 |
// prepare $tids associative array $tid => $term accounting for the possibility
|
| 26 |
// that $terms is either a tid, a $term, an array of tids, or an array of $term objects
|
| 27 |
if (is_array($terms) && count($array) > 0) {
|
| 28 |
foreach ($terms as $term) {
|
| 29 |
if (is_numeric($term)) {
|
| 30 |
$term = taxonomy_get_term($term);
|
| 31 |
}
|
| 32 |
if (is_object($term)) {
|
| 33 |
$tids[$term->tid] = $term;
|
| 34 |
}
|
| 35 |
}
|
| 36 |
}
|
| 37 |
else {
|
| 38 |
if (is_numeric($terms)) {
|
| 39 |
$tids[$terms] = taxonomy_get_term($terms);
|
| 40 |
}
|
| 41 |
elseif (is_object($terms)) {
|
| 42 |
$tids[$terms->tid] = $terms;
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
// build the $path
|
| 47 |
$path = 'taxonomy/term';
|
| 48 |
$op = strtolower($op);
|
| 49 |
switch ($op) {
|
| 50 |
case 'and':
|
| 51 |
$path .= '/'. implode(',', array_keys($tids));
|
| 52 |
break;
|
| 53 |
|
| 54 |
case 'or':
|
| 55 |
$path .= '/'. implode('+', array_keys($tids));
|
| 56 |
break;
|
| 57 |
}
|
| 58 |
|
| 59 |
if ($depth > 0) {
|
| 60 |
$path .= "/$depth";
|
| 61 |
}
|
| 62 |
|
| 63 |
if ($feed) {
|
| 64 |
if ($depth > 0) {
|
| 65 |
$path .= '/feed';
|
| 66 |
}
|
| 67 |
else {
|
| 68 |
$path .= '0/feed';
|
| 69 |
}
|
| 70 |
}
|
| 71 |
return $path;
|
| 72 |
}
|