You are here

function hs_taxonomy_token_values in Hierarchical Select 6.3

Same name and namespace in other branches
  1. 5.3 modules/hs_taxonomy.module \hs_taxonomy_token_values()

Implementation of hook_token_values().

File

modules/hs_taxonomy.module, line 615
Implementation of the Hierarchical Select API for the Taxonomy module.

Code

function hs_taxonomy_token_values($type, $object = NULL, $options = array()) {
  static $hs_vids;
  static $all_vids;
  $separator = variable_get('hs_taxonomy_separator', variable_get('pathauto_separator', '-'));
  $values = array();
  switch ($type) {
    case 'node':
      $node = $object;

      // Default values.
      $values['save-lineage-termpath'] = $values['save-lineage-termpath-raw'] = '';

      // If $node->taxonomy doesn't exist, these tokens cannot be created!
      if (!is_object($node) || !isset($node->taxonomy) || !is_array($node->taxonomy)) {
        return $values;
      }

      // Find out which vocabularies are using Hierarchical Select.
      if (!isset($hs_vids)) {
        $hs_vids = array();
        $result = db_query("SELECT SUBSTRING(name, 30, 3) AS vid FROM {variable} WHERE name LIKE 'taxonomy_hierarchical_select_%' AND value LIKE 'i:1\\;';");
        while ($o = db_fetch_object($result)) {
          $hs_vids[] = $o->vid;
        }
      }

      // Get a list of all existent vids, so we can generate an empty token
      // when a token is requested for a vocabulary that's not associated with
      // the current content type.
      if (!isset($all_vids)) {
        $all_vids = array();
        $result = db_query("SELECT vid FROM {vocabulary}");
        while ($row = db_fetch_object($result)) {
          $all_vids[] = $row->vid;
        }
      }

      // Generate the per-vid "save-lineage-termpath" tokens.
      foreach ($all_vids as $vid) {
        $terms = array();
        if (in_array($vid, $hs_vids) && isset($node->taxonomy[$vid])) {
          $selection = $node->taxonomy[$vid];
          $terms = _hs_taxonomy_token_termpath_for_vid($selection, $vid);
        }
        $terms_raw = $terms;
        $terms = array_map('check_plain', $terms);
        $values["save-lineage-termpath:{$vid}"] = !empty($options['pathauto']) ? $terms : implode($separator, $terms);
        $values["save-lineage-termpath-raw:{$vid}"] = !empty($options['pathauto']) ? $terms_raw : implode($separator, $terms_raw);
      }

      // We use the terms of the first vocabulary that uses Hierarchical
      // Select for the default "save-lineage-termpath" tokens.
      $vids = array_intersect(array_keys($node->taxonomy), $hs_vids);
      if (!empty($vids)) {
        $vid = $vids[0];
        $values['save-lineage-termpath'] = $values["save-lineage-termpath:{$vid}"];
        $values['save-lineage-termpath-raw'] = $values["save-lineage-termpath-raw:{$vid}"];
      }
      break;
  }
  return $values;
}