Index: unique_field.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/unique_field/unique_field.module,v retrieving revision 1.6.2.6 diff -u -p -r1.6.2.6 unique_field.module --- unique_field.module 13 Nov 2009 01:30:15 -0000 1.6.2.6 +++ unique_field.module 16 Nov 2009 04:00:54 -0000 @@ -72,6 +72,7 @@ function unique_field_nodeapi(&$node, $o // get list of unique fields for node type $fields = variable_get('unique_field_fields_'. $node->type, array()); + $fieldsLink = variable_get('unique_field_link_fields_'. $node->type, array()); // check if there are unique fields for this node type if (count($fields)) { @@ -154,6 +155,13 @@ function unique_field_nodeapi(&$node, $o foreach ($f as $index => $value) { if (is_numeric($index) && is_array($value)) { $values[] = $value; + + // Add additional values for link fields + if(in_array($field, $fieldsLink)) { + // Generate other similar URLs to additionally check for + $additionalLinkValues = generate_additional_url_values($value); + $values = array_merge($values, $additionalLinkValues); + } } } } @@ -345,11 +353,16 @@ function unique_field_node_settings_form if (module_exists('locale') && variable_get('language_content_type_'. $ntype, 0)) { $fieldopts[UNIQUE_FIELD_FIELDS_LANGUAGE] = t('Language'); } + $fieldLinkOptions = array(); if (module_exists('content')) { $ctype = content_types($ntype); if (is_array($ctype) && is_array($ctype['fields'])) { foreach ($ctype['fields'] as $field => $info) { $fieldopts[$field] = $info['widget']['label'] .' ('. $field .')'; + // Create list of fields that are of type 'link' + if(module_exists('link') && $info['type'] == 'link') { + $fieldLinkOptions[$field] = $info['widget']['label'] .' ('. $field .')'; + } } } } @@ -397,6 +410,16 @@ function unique_field_node_settings_form '#options' => array(UNIQUE_FIELD_SHOW_MATCHES => t('Enabled')), '#default_value' => variable_get('unique_field_show_matches_'. $ntype, array()), ); + // Additional options for Link fields + if($fieldLinkOptions) { + $form['unique_field']['unique_field_link_fields'] = array( + '#type' => 'checkboxes', + '#title' => t('Choose the fields to do additional URL checking'), + '#options' => $fieldLinkOptions, + '#default_value' => variable_get('unique_field_link_fields_'. $ntype, array()), + '#description' => t('Additional URL checks will go into making sure URL values are truely unique. For example http://example.com/ will match www.example.com/. Notice: This probably does not work with node scope and has not been tested with multiple URLs per field.'), + ); + } // add validation function $form['#validate'][] = 'unique_field_node_settings_form_validate'; @@ -415,3 +438,52 @@ function unique_field_node_settings_form } } } + +/** + * Generates and returns addition url values that can be used by unique_field_match_value + * to check for unique truely unique URLs + * + * @param array $value Should be in the form + * @return array Array of new $value arrays + */ +function generate_additional_url_values($value) { + + $url = array(); + $additionalValues = array(); + + // Match different sections of the URL ( Copied and modified from http://www.php.net/manual/en/function.preg-match.php#93824 ) + $regex = "(?P(https?|ftp)\:\/\/)?"; // SCHEME + $regex .= "(?P([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass + $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP + $regex .= "(\:[0-9]{2,5})?"; // Port + $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path + $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query + $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?)"; // Anchor + + + if(!preg_match("/^$regex$/", $value['url'], $urlMatches)){ + // URL isn't valid so return + // TODO: Some kind of message here? + return(null); + } + + //TODO: Add additional URL checks here + + if($urlMatches['scheme'] == 'http://') { + // Create url without http:// + $additionalValues[] = create_new_link_value($urlMatches['all_except_scheme'], $value); + } else if(!$urlMatches['scheme']) { + // Create url with http:// + $additionalValues[] = create_new_link_value('http://' . $urlMatches['all_except_scheme'], $value); + } + + return($additionalValues); +} + +/** + * Helper to create new values to ultimately be used in unique_field_match_value + */ +function create_new_link_value($url, $original_value) { + $original_value['url'] = $url; + return($original_value); +}