You are here

function country_duplicate_field_check in Countries 7.2

Same name and namespace in other branches
  1. 8 countries.admin.inc \country_duplicate_field_check()

Helper function to check for duplicates.

2 calls to country_duplicate_field_check()
countries_admin_import_form_submit in ./countries.admin.inc
Submit handler.
country_validate in ./countries.module
Validates a country.

File

./countries.admin.inc, line 578
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;
}