function unique_field_nodeapi in Unique field 5
Same name and namespace in other branches
- 6 unique_field.module \unique_field_nodeapi()
Implementation of hook_nodeapi().
File
- ./
unique_field.module, line 54 - Provides content validation requirement that the node's title, author, or specified CCK fields are unique.
Code
function unique_field_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
// check fields for unique requirements on node 'validate' operation
if ($op == 'validate') {
// get list of unique fields for node type
$fields = variable_get('unique_field_fields_' . $node->type, array());
// check if there are unique fields for this node type
if (count($fields)) {
// get unique field settings for this node type
$scope = variable_get('unique_field_scope_' . $node->type, UNIQUE_FIELD_SCOPE_TYPE);
$comp = variable_get('unique_field_comp_' . $node->type, UNIQUE_FIELD_COMP_EACH);
$var_show_matches = variable_get('unique_field_show_matches_' . $node->type, array());
$show_matches = is_array($var_show_matches) && in_array(UNIQUE_FIELD_SHOW_MATCHES, $var_show_matches);
// initialization
$errmsg = NULL;
$errfld = array();
$matches = array();
$allmatch = NULL;
// check fields for node scope
if ($scope == UNIQUE_FIELD_SCOPE_NODE) {
$values = array();
foreach ($fields as $field) {
$new_values = array();
if ($field == UNIQUE_FIELD_FIELDS_TITLE) {
$new_values[] = $node->title;
}
else {
// if cck_field_perms is enabled and the current user is
// not permitted to access a field, then the field data is cleared
if (!is_array($node->{$field})) {
continue;
}
$f = $node->{$field};
$field_info = content_fields($field, $node->type);
$db = content_database_info($field_info);
$field_keys = array_keys($db['columns']);
foreach ($f as $index => $value) {
if (is_numeric($index)) {
$field_combined = array();
foreach ($field_keys as $key) {
if (!empty($f[$index][$key])) {
$field_combined[$key] = $f[$index][$key];
}
}
if (!empty($field_combined)) {
$new_values[] = serialize($field_combined);
}
}
}
}
$new_values = array_merge($values, $new_values);
$values = array_unique($new_values);
if (serialize($values) !== serialize($new_values)) {
$errfld[] = $field;
}
}
if (count($errfld) > 0) {
$errmsg = 'The @labels fields must have unique values. The @label field has a value that is already used.';
}
}
else {
foreach ($fields as $field) {
$values = '';
if ($field == UNIQUE_FIELD_FIELDS_TITLE) {
$values = $node->title;
}
else {
if ($field == UNIQUE_FIELD_FIELDS_AUTHOR) {
$values = $node->uid;
}
else {
// if cck_field_perms is enabled and the current user is
// not permitted to access a field, then the field data is cleared
if (!is_array($node->{$field})) {
continue;
}
$f = $node->{$field};
$values = array();
foreach ($f as $index => $value) {
if (is_numeric($index) && is_array($value)) {
$values[] = $value;
}
}
}
}
$match = unique_field_match_value($field, $values, $scope, $node->type);
// remove matches of this node
if ($node->nid && is_array($match) && in_array($node->nid, $match)) {
$key = array_search($node->nid, $match);
unset($match[$key]);
}
if ($comp == UNIQUE_FIELD_COMP_EACH && is_array($match) && count($match)) {
$errfld[] = $field;
$errmsg = 'The @label field requires a unique value, and the specified value is already used.';
}
$matches[$field] = $match;
$allmatch = is_array($allmatch) ? array_intersect($allmatch, $match) : $match;
}
// check for fields in combination
if ($comp == UNIQUE_FIELD_COMP_ALL && is_array($allmatch) && count($allmatch)) {
foreach ($fields as $field) {
$errfld[] = $field;
}
$errmsg = 'This form requires that the fields @labels are a unique combination. The specified values are already used.';
}
}
// common error messages
if ($errmsg && !empty($errmsg) && is_array($errfld) && count($errfld) > 0) {
$labels = array();
foreach ($fields as $field) {
if ($field == UNIQUE_FIELD_FIELDS_TITLE) {
$nodetype = node_get_types('type', $node->type);
$labels[$field] = $nodetype->title_label;
}
else {
if ($field == UNIQUE_FIELD_FIELDS_AUTHOR) {
$labels[$field] = t('Author');
}
else {
$fld = content_fields($field, $node->type);
$labels[$field] = $fld['widget']['label'];
}
}
}
foreach ($errfld as $field) {
if ($show_matches && is_array($matches[$field])) {
$list_items = array();
foreach ($matches[$field] as $nid) {
$match_node = node_load($nid);
if (node_access('view', $match_node)) {
$list_items[] = l($match_node->title, 'node/' . $nid);
}
}
$list_html = theme('item_list', $list_items);
form_set_error($field, t($errmsg . ' Matches are found in the following content: !list-html', array(
'@label' => $labels[$field],
'@labels' => join(', ', $labels),
'!list-html' => $list_html,
)));
}
else {
form_set_error($field, t($errmsg, array(
'@label' => $labels[$field],
'@labels' => join(', ', $labels),
)));
}
}
}
}
}
}