function country_duplicate_field_check in Countries 8
Same name and namespace in other branches
- 7.2 countries.admin.inc \country_duplicate_field_check()
Helper function to check for duplicates.
1 call to country_duplicate_field_check()
- country_validate in ./
countries.module - Validates a country.
File
- ./
countries.admin.inc, line 337 - Admin page callbacks for the Countries module.
Code
function country_duplicate_field_check($country) {
$duplicates = array();
$cid = empty($country->cid) ? FALSE : $country->cid;
foreach (array(
'name',
'official_name',
'iso2',
'iso3',
'numcode',
) as $property) {
if (drupal_strlen($country->{$property})) {
if ($property == 'numcode' && empty($country->{$property})) {
continue;
}
$query = db_select('countries_country', 'c')
->fields('c', array(
'iso2',
));
if ($cid) {
$query
->condition('cid', $cid, '!=');
}
if ($property == 'official_name' || $property == 'name') {
$db_or = db_or();
$db_or
->condition('official_name', $country->{$property})
->condition('name', $country->{$property});
$query
->condition($db_or);
}
else {
$query
->condition($property, $country->{$property});
}
$value = $query
->execute()
->fetchColumn();
if (!empty($value)) {
$conflict = country_load($value);
$duplicates[$property] = t('The <em>@value</em> is already used by <em>@country</em>', array(
'@value' => country_property($country, $property),
'@country' => country_property($conflict, 'name'),
));
}
}
}
return empty($duplicates) ? FALSE : $duplicates;
}