/[drupal]/contributions/modules/unique_field/unique_field.module
ViewVC logotype

Diff of /contributions/modules/unique_field/unique_field.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.7.2.2, Mon Feb 9 19:52:20 2009 UTC revision 1.7.2.3, Sat Mar 7 01:54:08 2009 UTC
# Line 10  Line 10 
10   * Compatible with Drupal 5.x   * Compatible with Drupal 5.x
11   *   *
12   * By Joe Turgeon [http://arithmetric.com]   * By Joe Turgeon [http://arithmetric.com]
13   * Version 2009/02/09   * Version 2009/03/06
14   * Licensed under GPL version 2   * Licensed under GPL version 2
15   */   */
16    
# Line 83  function unique_field_nodeapi(&$node, $o Line 83  function unique_field_nodeapi(&$node, $o
83              $new_values[] = $node->title;              $new_values[] = $node->title;
84            }            }
85            else {            else {
86                // if cck_field_perms is enabled and the current user is
87                // not permitted to access a field, then the field data is cleared
88                if (!is_array($node->$field)) {
89                  continue;
90                }
91              $f = $node->$field;              $f = $node->$field;
92              $field_info = content_fields($field, $node->type);              $field_info = content_fields($field, $node->type);
93              $db = content_database_info($field_info);              $db = content_database_info($field_info);
# Line 123  function unique_field_nodeapi(&$node, $o Line 128  function unique_field_nodeapi(&$node, $o
128              $values = $node->uid;              $values = $node->uid;
129            }            }
130            else {            else {
131                // if cck_field_perms is enabled and the current user is
132                // not permitted to access a field, then the field data is cleared
133                if (!is_array($node->$field)) {
134                  continue;
135                }
136              $f = $node->$field;              $f = $node->$field;
137              $values = array();              $values = array();
138              foreach ($f as $index => $value) {              foreach ($f as $index => $value) {
# Line 224  function unique_field_match_value($field Line 234  function unique_field_match_value($field
234          if (!isset($db['columns'][$key]['column'])) {          if (!isset($db['columns'][$key]['column'])) {
235            continue;            continue;
236          }          }
237          // skip if the value is empty          // skip if the value is empty or is not a scalar
238          if (empty($val)) {          if (empty($val) || !is_scalar($val)) {
239            continue;            continue;
240          }          }
241          // if query is not empty, add AND operator          // if query is not empty, add AND operator
# Line 261  function unique_field_match_value($field Line 271  function unique_field_match_value($field
271        return array();        return array();
272      }      }
273    
274      $qwhere = '( '. $qwhere .') ';      $qwhere = '(( '. $qwhere .')) ';
275    }    }
276    
277    // add query where clause if scope is limited to content type    // add query where clause if scope is limited to content type
# Line 307  function unique_field_node_settings_form Line 317  function unique_field_node_settings_form
317    // build the form    // build the form
318    $form['unique_field'] = array(    $form['unique_field'] = array(
319      '#type' => 'fieldset',      '#type' => 'fieldset',
320      '#title' => t('Unique field configuration'),      '#title' => t('Unique field settings'),
321      '#weight' => 0,      '#weight' => 1,
322      '#collapsible' => TRUE,      '#collapsible' => TRUE,
323      '#collapsed' => TRUE,      '#collapsed' => TRUE,
324    );    );
325      $form['unique_field']['unique_field_fields'] = array(
326        '#type' => 'checkboxes',
327        '#title' => t('Choose the fields that should be unique'),
328        '#options' => $fieldopts,
329        '#default_value' => variable_get('unique_field_fields_'. $ntype, array()),
330        '#description' => t('After designating that certain fields should be unique, users will not be able to submit the content form to create a new node or update an existing one if it contains values in the designated fields that duplicate others.'),
331      );
332    $form['unique_field']['unique_field_scope'] = array(    $form['unique_field']['unique_field_scope'] = array(
333      '#type' => 'radios',      '#type' => 'radios',
334      '#title' => t('Choose the scope for the unique values'),      '#title' => t('Choose the scope for the unique values'),
# Line 327  function unique_field_node_settings_form Line 344  function unique_field_node_settings_form
344      '#type' => 'radios',      '#type' => 'radios',
345      '#title' => t('Choose whether values must be unique individually or in combination'),      '#title' => t('Choose whether values must be unique individually or in combination'),
346      '#options' => array(      '#options' => array(
347        UNIQUE_FIELD_COMP_EACH => t('Each of the specified fields must have a unique value.'),        UNIQUE_FIELD_COMP_EACH => t('Each of the specified fields must have a unique value'),
348        UNIQUE_FIELD_COMP_ALL => t('The combination of values from the specified fields must be unique.'),        UNIQUE_FIELD_COMP_ALL => t('The combination of values from the specified fields must be unique'),
349      ),      ),
350      '#default_value' => variable_get('unique_field_comp_'. $ntype, UNIQUE_FIELD_COMP_EACH),      '#default_value' => variable_get('unique_field_comp_'. $ntype, UNIQUE_FIELD_COMP_EACH),
351    );      '#description' => t('For example, if you have fields for the parts of a street address (street number and name, city, and zip code) on a node, and want to allow only one node per complete address, but not only one node per city or per zip code, then you would want to choose that the fields must be unique in combination.'),
   $form['unique_field']['unique_field_fields'] = array(  
     '#type' => 'checkboxes',  
     '#title' => t('Choose the fields that should be unique'),  
     '#options' => $fieldopts,  
     '#default_value' => variable_get('unique_field_fields_'. $ntype, array()),  
352    );    );
353    $form['unique_field']['unique_field_show_matches'] = array(    $form['unique_field']['unique_field_show_matches'] = array(
354      '#type' => 'checkboxes',      '#type' => 'checkboxes',
# Line 350  function unique_field_node_settings_form Line 362  function unique_field_node_settings_form
362  }  }
363    
364  /**  /**
365   * Form validation callback for unique_field_node_settings_form   * Form validation callback for unique_field_node_settings_form.
366   */   */
367  function unique_field_node_settings_form_validate($form_id, $form_values) {  function unique_field_node_settings_form_validate($form_id, $form_values) {
368    if ($form_values['unique_field_scope'] == UNIQUE_FIELD_SCOPE_NODE && $form_values['unique_field_comp'] == UNIQUE_FIELD_COMP_ALL) {    if ($form_values['unique_field_scope'] == UNIQUE_FIELD_SCOPE_NODE) {
369      form_set_error('unique_field_comp', t('The scope of a single node requires that each field must be unique.'));      if ($form_values['unique_field_comp'] == UNIQUE_FIELD_COMP_ALL) {
370    }        form_set_error('unique_field_comp', t('The scope of a single node requires that each field must be unique.'));
371    if ($form_values['unique_field_scope'] == UNIQUE_FIELD_SCOPE_NODE && $form_values['unique_field_fields'][UNIQUE_FIELD_FIELDS_AUTHOR] === UNIQUE_FIELD_FIELDS_AUTHOR) {      }
372      form_set_error('unique_field_fields', t('The author field is not supported within the scope of a single node.'));      if ($form_values['unique_field_fields'][UNIQUE_FIELD_FIELDS_AUTHOR] == UNIQUE_FIELD_FIELDS_AUTHOR) {
373          form_set_error('unique_field_fields', t('The author field is not supported within the scope of a single node.'));
374        }
375    }    }
376  }  }

Legend:
Removed from v.1.7.2.2  
changed lines
  Added in v.1.7.2.3

  ViewVC Help
Powered by ViewVC 1.1.2