You are here

function unique_field_match_value in Unique field 6

Same name and namespace in other branches
  1. 5 unique_field.module \unique_field_match_value()
  2. 7 unique_field.module \unique_field_match_value()

Find nodes with a matching field value within a given scope.

1 call to unique_field_match_value()
unique_field_nodeapi in ./unique_field.module
Implementation of hook_nodeapi().

File

./unique_field.module, line 283
Provides content validation requirement that a node's title, author, language, taxonomy terms, or CCK fields are unique.

Code

function unique_field_match_value($field, $values, $scope, $ntype = NULL, $nlanguage = NULL) {

  // initialize query variables
  $qtbl = '';
  $qwhere = '';

  // generate query where clause for title field
  if ($field === UNIQUE_FIELD_FIELDS_TITLE) {
    $qwhere = "node.title = '" . db_escape_string($values) . "' ";
  }
  elseif ($field === UNIQUE_FIELD_FIELDS_AUTHOR) {
    $qwhere = "node.uid = '" . intval($values) . "' ";
  }
  elseif ($field === UNIQUE_FIELD_FIELDS_LANGUAGE) {
    $qwhere = "node.language = '" . db_escape_string($values) . "' ";
  }
  elseif (strpos($field, UNIQUE_FIELD_FIELDS_TAXONOMY) === 0) {
    $qtbl = 'term_node';
    $qwhere = "term_node.tid IN(" . join(',', $values) . ") ";
  }
  else {
    $f = content_fields($field, $ntype);
    $db = content_database_info($f);
    $qtbl = db_escape_string($db['table']);

    // check all entries in the field
    foreach ($values as $index => $value) {
      $qwhere_val = '';

      // check all fields/columns in the entry
      foreach ($value as $key => $val) {

        // skip values that are not stored in the database
        if (!isset($db['columns'][$key]['column'])) {
          continue;
        }

        // skip if the value is empty or is not a scalar
        if (empty($val) || !is_scalar($val)) {
          continue;
        }

        // if query is not empty, add AND operator
        if (!empty($qwhere_val)) {
          $qwhere_val .= 'AND ';
        }

        // generate comparison statement depending on field type
        $qwhere_val .= $qtbl . '.' . db_escape_string($db['columns'][$key]['column']) . ' = ';
        $dbtype = $db['columns'][$key]['type'];
        if ($dbtype === 'char' || $dbtype === 'varchar' || $dbtype === 'tinytext' || $dbtype === 'text' || $dbtype === 'mediumtext' || $dbtype === 'longtext' || $dbtype === 'datetime' || $dbtype === 'date' || $dbtype === 'time' || $dbtype === 'timestamp') {
          $qwhere_val .= "'" . db_escape_string($val) . "' ";
        }
        elseif (is_numeric($val)) {
          $qwhere_val .= db_escape_string($val) . " ";
        }
        else {
          $msg = t('Could not formulate query for unique_field_match_value on @field with data type @dbtype.', array(
            '@field' => $field,
            '@dbtype' => $dbtype,
          ));
          drupal_set_message($msg, 'error');
          watchdog('unique_field', $msg, array(), WATCHDOG_WARNING);
          return;
        }
      }
      if (!empty($qwhere_val)) {
        if (!empty($qwhere)) {
          $qwhere .= ') OR ( ';
        }
        $qwhere .= $qwhere_val;
      }
    }

    // if no values can be queried, then return no matches
    if (empty($qwhere)) {
      return array();
    }
    $qwhere = '(( ' . $qwhere . ')) ';
  }

  // add query where clause if scope is limited to content type
  if ($scope === UNIQUE_FIELD_SCOPE_TYPE && is_string($ntype) && !empty($ntype)) {
    $qwhere .= "AND node.type = '" . db_escape_string($ntype) . "' ";
  }
  elseif ($scope === UNIQUE_FIELD_SCOPE_LANGUAGE && is_string($nlanguage)) {
    $qwhere .= "AND node.language = '" . db_escape_string($nlanguage) . "' ";
  }

  // do query
  $q = "SELECT node.nid FROM {node} node ";
  if (!empty($qtbl)) {
    $q .= "JOIN {" . $qtbl . "} " . $qtbl . " USING (vid) ";
  }
  $q .= "WHERE " . $qwhere;
  $res = db_query($q);
  $nids = array();
  while ($obj = db_fetch_object($res)) {
    if ($obj && $obj->nid) {
      $nids[] = $obj->nid;
    }
  }
  return array_unique($nids);
}