| 10 |
* Compatible with Drupal 6.x |
* Compatible with Drupal 6.x |
| 11 |
* |
* |
| 12 |
* By Joe Turgeon [http://arithmetric.com] |
* By Joe Turgeon [http://arithmetric.com] |
| 13 |
* Version 2009/03/06 |
* Version 2009/11/12 |
| 14 |
* Licensed under GPL version 2 |
* Licensed under GPL version 2 |
| 15 |
*/ |
*/ |
| 16 |
|
|
| 17 |
// permissions definitions |
// permissions definitions |
| 18 |
define('UNIQUE_FIELD_PERM', 'designate fields as unique'); |
define('UNIQUE_FIELD_PERM_ADMIN', 'designate fields as unique'); |
| 19 |
|
define('UNIQUE_FIELD_PERM_BYPASS', 'bypass requirement that fields are unique'); |
| 20 |
|
|
| 21 |
// query scope definitions |
// query scope definitions |
| 22 |
define('UNIQUE_FIELD_SCOPE_NODE', 'node'); |
define('UNIQUE_FIELD_SCOPE_NODE', 'node'); |
| 40 |
* Implementation of hook_perm(). |
* Implementation of hook_perm(). |
| 41 |
*/ |
*/ |
| 42 |
function unique_field_perm() { |
function unique_field_perm() { |
| 43 |
return array(UNIQUE_FIELD_PERM); |
return array(UNIQUE_FIELD_PERM_ADMIN, UNIQUE_FIELD_PERM_BYPASS); |
| 44 |
} |
} |
| 45 |
|
|
| 46 |
/** |
/** |
| 47 |
* Implementation of hook_form_alter(). |
* Implementation of hook_form_alter(). |
| 48 |
*/ |
*/ |
| 49 |
function unique_field_form_alter(&$form, &$form_state, $form_id) { |
function unique_field_form_alter(&$form, &$form_state, $form_id) { |
| 50 |
if ($form_id == 'node_type_form' && user_access(UNIQUE_FIELD_PERM) && isset($form['#node_type'])) { |
if ($form_id == 'node_type_form' && user_access(UNIQUE_FIELD_PERM_ADMIN) && isset($form['#node_type'])) { |
| 51 |
unique_field_node_settings_form($form); |
unique_field_node_settings_form($form); |
| 52 |
} |
} |
| 53 |
|
else if (strpos($form_id, 'node_form') !== FALSE && user_access(UNIQUE_FIELD_PERM_BYPASS)) { |
| 54 |
|
$form['unique_field_override'] = array( |
| 55 |
|
'#type' => 'hidden', |
| 56 |
|
'#default_value' => '0', |
| 57 |
|
); |
| 58 |
|
} |
| 59 |
} |
} |
| 60 |
|
|
| 61 |
/** |
/** |
| 65 |
// check fields for unique requirements on node 'validate' operation |
// check fields for unique requirements on node 'validate' operation |
| 66 |
if ($op == 'validate') { |
if ($op == 'validate') { |
| 67 |
|
|
| 68 |
|
// skip validation if override value is set |
| 69 |
|
if (is_array($a3) && is_array($a3['unique_field_override']) && $a3['unique_field_override']['#value'] && user_access(UNIQUE_FIELD_PERM_BYPASS)) { |
| 70 |
|
return; |
| 71 |
|
} |
| 72 |
|
|
| 73 |
// get list of unique fields for node type |
// get list of unique fields for node type |
| 74 |
$fields = variable_get('unique_field_fields_'. $node->type, array()); |
$fields = variable_get('unique_field_fields_'. $node->type, array()); |
| 75 |
|
|
| 175 |
if ($comp == UNIQUE_FIELD_COMP_ALL && is_array($allmatch) && count($allmatch)) { |
if ($comp == UNIQUE_FIELD_COMP_ALL && is_array($allmatch) && count($allmatch)) { |
| 176 |
foreach ($fields as $field) { |
foreach ($fields as $field) { |
| 177 |
$errfld[] = $field; |
$errfld[] = $field; |
| 178 |
|
$matches[$field] = $allmatch; |
| 179 |
} |
} |
| 180 |
$errmsg = 'This form requires that the fields @labels are a unique combination. The specified values are already used.'; |
$errmsg = 'This form requires that the fields @labels are a unique combination. The specified values are already used.'; |
| 181 |
} |
} |
| 201 |
} |
} |
| 202 |
} |
} |
| 203 |
foreach ($errfld as $field) { |
foreach ($errfld as $field) { |
| 204 |
|
$msg = t($errmsg, array('@label' => $labels[$field], '@labels' => join(', ', $labels))); |
| 205 |
if ($show_matches && is_array($matches[$field])) { |
if ($show_matches && is_array($matches[$field])) { |
| 206 |
$list_items = array(); |
$list_items = array(); |
| 207 |
foreach ($matches[$field] as $nid) { |
foreach ($matches[$field] as $nid) { |
| 211 |
} |
} |
| 212 |
} |
} |
| 213 |
$list_html = theme('item_list', $list_items); |
$list_html = theme('item_list', $list_items); |
| 214 |
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))); |
$msg .= ' '. t('Matches are found in the following content: !list-html', array('!list-html' => $list_html)); |
| 215 |
} |
} |
| 216 |
else { |
if (user_access(UNIQUE_FIELD_PERM_BYPASS)) { |
| 217 |
form_set_error($field, t($errmsg, array('@label' => $labels[$field], '@labels' => join(', ', $labels)))); |
$msg .= '<p>'. t('Click !here to bypass this check and resubmit.', array('!here' => "<a href=\"#\" onclick=\"$('form#node-form input#edit-unique-field-override').val(1);$('form#node-form').submit();\">here</a>")) .'</p>'; |
| 218 |
|
} |
| 219 |
|
form_set_error($field, $msg); |
| 220 |
|
// if checking the fields in combination, then one error message |
| 221 |
|
// is enough for all of the fields |
| 222 |
|
if ($comp == UNIQUE_FIELD_COMP_ALL) { |
| 223 |
|
break; |
| 224 |
} |
} |
| 225 |
} |
} |
| 226 |
} |
} |
| 277 |
// generate comparison statement depending on field type |
// generate comparison statement depending on field type |
| 278 |
$qwhere_val .= $qtbl .'.'. db_escape_string($db['columns'][$key]['column']) .' = '; |
$qwhere_val .= $qtbl .'.'. db_escape_string($db['columns'][$key]['column']) .' = '; |
| 279 |
$dbtype = $db['columns'][$key]['type']; |
$dbtype = $db['columns'][$key]['type']; |
| 280 |
if ($dbtype == 'char' || $dbtype == 'varchar' || $dbtype == 'tinytext' || $dbtype == 'text' || $dbtype == 'mediumtext' || $dbtype == 'longtext') { |
if ($dbtype == 'char' || $dbtype == 'varchar' || $dbtype == 'tinytext' || $dbtype == 'text' || $dbtype == 'mediumtext' || $dbtype == 'longtext' || $dbtype == 'datetime' || $dbtype == 'date' || $dbtype == 'time' || $dbtype == 'timestamp') { |
| 281 |
$qwhere_val .= "'". db_escape_string($val) ."' "; |
$qwhere_val .= "'". db_escape_string($val) ."' "; |
| 282 |
} |
} |
| 283 |
else if (is_numeric($val)) { |
else if (is_numeric($val)) { |
| 286 |
else { |
else { |
| 287 |
$msg = t('Could not formulate query for unique_field_match_value on @field with data type @dbtype.', array('@field' => $field, '@dbtype' => $dbtype)); |
$msg = t('Could not formulate query for unique_field_match_value on @field with data type @dbtype.', array('@field' => $field, '@dbtype' => $dbtype)); |
| 288 |
drupal_set_message($msg, 'error'); |
drupal_set_message($msg, 'error'); |
| 289 |
watchdog('unique_field', $msg, WATCHDOG_WARNING); |
watchdog('unique_field', $msg, array(), WATCHDOG_WARNING); |
| 290 |
return; |
return; |
| 291 |
} |
} |
| 292 |
} |
} |