You are here

function node_get_placeholders in Pathauto 5

2 calls to node_get_placeholders()
node_pathauto_bulkupdate in ./pathauto_node.inc
pathauto_nodeapi in ./pathauto_node.inc
Implementation of hook_nodeapi().

File

./pathauto_node.inc, line 114

Code

function node_get_placeholders($node) {

  // Do the simple stuff first
  $placeholders[t('[title]')] = pathauto_cleanstring($node->title);
  $placeholders[t('[yyyy]')] = date('Y', $node->created);
  $placeholders[t('[mm]')] = date('m', $node->created);
  $placeholders[t('[mon]')] = pathauto_cleanstring(date('M', $node->created));
  $placeholders[t('[dd]')] = date('d', $node->created);
  $placeholders[t('[day]')] = pathauto_cleanstring(date('D', $node->created));
  $placeholders[t('[hour]')] = date('H', $node->created);
  $placeholders[t('[min]')] = date('i', $node->created);
  $placeholders[t('[sec]')] = date('s', $node->created);
  $placeholders[t('[week]')] = date('W', $node->created);
  $placeholders[t('[nid]')] = $node->nid;
  $placeholders[t('[type]')] = pathauto_cleanstring(node_get_types('name', $node));
  $placeholders[t('[uid]')] = $node->uid;

  // Then the slightly less simple stuff
  $result = db_query("SELECT name FROM {users} WHERE uid='%d'", $node->uid);
  $userinfo = db_fetch_object($result);
  $placeholders[t('[user]')] = pathauto_cleanstring($userinfo->name);

  // Book title
  if (module_exists('book') and $node->type == 'book') {
    $path = book_location($node);
    $placeholders[t('[book]')] = pathauto_cleanstring($path[0]->title);
    $bookhierarchy = book_location($node);
    $bookpath = '';
    foreach ($bookhierarchy as $bookelement) {
      if ($bookpath == '') {
        $bookpath = pathauto_cleanstring($bookelement->title);
      }
      else {
        $bookpath = $bookpath . '/' . pathauto_cleanstring($bookelement->title);
      }
    }
    $placeholders[t('[bookpath]')] = $bookpath;
  }
  else {
    $placeholders[t('[book]')] = '';
    $placeholders[t('[bookpath]')] = '';
  }

  // And now taxonomy, which is a bit more work
  if (module_exists('taxonomy') && is_array($node->taxonomy) && count($node->taxonomy) > 0) {
    $first_term_id = FALSE;

    // We loop through the array assuming it's in wheigh order, which is false, but close enough TODO: fix that
    // I'd use more descriptive variables, but the content isn't always a tid
    foreach ($node->taxonomy as $key => $value) {
      if (is_object($value)) {
        $value = (array) $value;
      }
      if ($key === 'tags') {

        // It's a freetag so make sure that there is a value in the array
        // Shamelessly stolen from taxonomy_node_save - most of it should be refactored out of that
        // So it can be easily reused, but until then: copy-paste-antipattern here we come!
        $value = (array) $value;
        foreach ($value as $vid => $vid_value) {
          if ($first_term_id) {
            break;
          }

          // This regexp allows the following types of user input:
          // this, "somecmpany, llc", "and ""this"" w,o.rks", foo bar
          $regexp = '%(?:^|,\\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
          preg_match_all($regexp, $vid_value, $matches);
          $typed_terms = array_unique($matches[1]);
          $inserted = array();
          foreach ($typed_terms as $typed_term) {
            if ($first_term_id) {
              break;
            }

            // If a user has escaped a term (to demonstrate that it is a group,
            // or includes a comma or quote character), we remove the escape
            // formatting so to save the term into the DB as the user intends.
            $typed_term = str_replace('""', '"', preg_replace('/^"(.*)"$/', '\\1', $typed_term));
            $typed_term = trim($typed_term);
            if ($typed_term == "") {
              continue;
            }

            // See if the term exists in the chosen vocabulary
            // and return the tid, otherwise, add a new record.
            $possibilities = taxonomy_get_term_by_name($typed_term);
            $typed_term_tid = NULL;

            // tid match if any.
            foreach ($possibilities as $possibility) {
              if ($possibility->vid == $vid) {
                $first_term_id = $possibility->tid;
                break;

                //we've got our first term ID, get out of here
              }
            }
          }
        }
      }
      elseif (is_array($value)) {
        $temptid = array_shift($value);
        if ($temptid) {
          $first_term_id = $temptid;
          break;
        }
      }
      elseif ($value) {
        $first_term_id = $value;
        break;
      }
    }
    $term = taxonomy_get_term($first_term_id);
    $placeholders[t('[cat]')] = pathauto_cleanstring($term->name);
    $vid = $term->vid;
    $vocabulary = taxonomy_get_vocabulary($vid);
    $placeholders[t('[vocab]')] = pathauto_cleanstring($vocabulary->name);
    $placeholders[t('[catalias]')] = drupal_get_path_alias('taxonomy/term/' . $first_term_id);
    if (!strncasecmp($placeholders['[catalias]'], 'taxonomy', 8)) {
      $placeholders[t('[catalias]')] = $placeholders['[cat]'];
    }
    $catpath = '';
    $parents = taxonomy_get_parents_all($first_term_id);
    foreach ($parents as $parent) {
      $catpath = pathauto_cleanstring($parent->name) . '/' . $catpath;
    }
    $placeholders[t('[catpath]')] = $catpath;
  }
  else {
    $placeholders[t('[cat]')] = '';
    $placeholders[t('[catpath]')] = '';
    $placeholders[t('[vocab]')] = '';
    $placeholders[t('[catalias]')] = '';
  }

  // Append any additional extensions
  $extplaceholders = module_invoke_all('pathauto_node', 'values', $node);
  $placeholders = array_merge($placeholders, $extplaceholders);
  return $placeholders;
}