You are here

function unique_field_match_value in Unique field 5

Same name and namespace in other branches
  1. 6 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 206
Provides content validation requirement that the node's title, author, or specified CCK fields are unique.

Code

function unique_field_match_value($field, $values, $scope, $ntype = 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) . "' ";
  }
  else {
    if ($field === UNIQUE_FIELD_FIELDS_AUTHOR) {
      $qwhere = "node.uid = '" . intval($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') {
            $qwhere_val .= "'" . db_escape_string($val) . "' ";
          }
          else {
            if (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, 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) . "' ";
  }

  // do query
  $q = "SELECT node.nid FROM {node} node ";
  if (!empty($qtbl)) {
    $q .= "JOIN {" . $qtbl . "} AS " . $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);
}