You are here

function similarterms_handler_argument_node_nid::validate_arg in Similar By Terms 6.2

Same name and namespace in other branches
  1. 7.2 views/similarterms_handler_argument_node_nid.inc \similarterms_handler_argument_node_nid::validate_arg()

File

views/similarterms_handler_argument_node_nid.inc, line 64
Provide node nid argument handler.

Class

similarterms_handler_argument_node_nid
Argument handler to accept a node id. based on node_handler_argument_node_nid except that it doesn't add a where clause to the query

Code

function validate_arg($arg) {

  // first run the inherited arg validation
  if (!parent::validate_arg($arg)) {
    return FALSE;
  }

  // hmmm... @todo: wildcard validation?
  // see views_handler_argument.inc for possible code
  if (!empty($this->options['break_phrase'])) {
    views_break_phrase($this->argument, $this);
  }
  else {
    $this->value = array(
      $this->argument,
    );
  }

  // $vids is array node version ids
  $vids = array();
  foreach ($this->value as $nid) {

    // get the current revision id (vid) for this node id (nid)
    $vids[] = db_result(db_query("SELECT vid FROM {node} WHERE nid = %d", $nid));
  }

  // $vocabs is array of vocabulary ids (a.k.a. vids, confusing right?)
  $vocabs = empty($this->options['vocabularies']) ? array() : $this->options['vocabularies'];
  foreach ($vocabs as $key => $val) {
    if ($val == 0) {
      unset($vocabs[$key]);
    }
  }
  $addwhere = '';
  $addjoin = '';
  if (count($vocabs) == 1) {

    // we're limiting the terms to those of given vocabs
    $addjoin = ' INNER JOIN {term_data} td ON tn.tid = td.tid ';
    $addwhere = " AND td.vid = %d";
  }
  elseif (count($vocabs) > 1) {
    $addjoin = ' INNER JOIN {term_data} td ON tn.tid = td.tid ';
    $placeholders = implode(', ', array_fill(0, sizeof($vocabs), '%d'));
    $addwhere = " AND td.vid IN ({$placeholders})";
  }
  $args = array_merge($vids, $vocabs);
  if (count($vids) > 1) {
    $placeholders = implode(', ', array_fill(0, sizeof($vids), '%d'));
    $result = db_query("SELECT tn.tid FROM {term_node} tn {$addjoin} WHERE tn.vid IN ({$placeholders}) {$addwhere}", $args);
  }
  else {
    $result = db_query("SELECT tn.tid FROM {term_node} tn {$addjoin} WHERE tn.vid = %d {$addwhere}", $args);
  }
  $tids = array();
  while ($row = db_fetch_object($result)) {

    // adding a key to ensure there aren't duplicates
    $tids[$row->tid] = $row->tid;
  }
  $this->tids = $tids;
  $this->view->tids = $tids;
  if (count($tids) == 0) {

    // there are no terms...
    // we need to cancel the query and bail out
    return FALSE;
  }
  return TRUE;
}