View source
<?php
define('COUNTRIES_ALL', 0);
define('COUNTRIES_ENABLED', 1);
define('COUNTRIES_DISABLED', 2);
define('COUNTRIES_VIEWS_WIDGET_FIELD', 1);
define('COUNTRIES_VIEWS_WIDGET_CUSTOM', 2);
include_once 'countries.fields.inc';
function countries_element_info() {
module_load_include('elements.inc', 'countries');
return _countries_element_info();
}
function countries_theme() {
return array(
'countries_number' => array(),
'country_icon_adapter' => array(
'variables' => array(
'country' => NULL,
'settings' => array(),
),
),
);
}
function countries_menu() {
$items = array();
$items['admin/config/regional/countries'] = array(
'title' => 'Countries',
'description' => 'List, edit, or add countries.',
'page callback' => 'countries_admin_overview',
'access arguments' => array(
'administer site configuration',
),
'file' => 'countries.admin.inc',
);
$items['admin/config/regional/countries/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/regional/countries/add'] = array(
'title' => 'Add country',
'description' => 'Admin page to add a country.',
'page callback' => 'countries_admin_page',
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_LOCAL_ACTION,
'weight' => 1,
'file' => 'countries.admin.inc',
);
$items['admin/config/regional/countries/%country'] = array(
'title callback' => 'countries_page_title',
'title arguments' => array(
4,
),
'description' => 'Edit a country.',
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'page callback' => 'countries_admin_page',
'page arguments' => array(
4,
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'countries.admin.inc',
);
$items['admin/config/regional/countries/%country/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/regional/countries/%country/delete'] = array(
'title' => 'Delete country',
'description' => 'Country delete confirmation form.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'countries_admin_delete',
4,
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'file' => 'countries.admin.inc',
);
return $items;
}
function countries_page_title($country) {
return check_plain($country
->label());
}
function countries_t($country, $property = 'name', $langcode = NULL) {
global $language;
if (empty($langcode) || $langcode == LANGUAGE_NONE) {
$langcode = $language->language;
}
static $i18n = NULL;
if (!isset($i18n)) {
$i18n = module_exists('countries_i18n');
}
switch ($property) {
case 'name':
case 'official_name':
$value = (string) $country->{$property};
if ($i18n && $country->language != $langcode) {
$translations =& drupal_static(__FUNCTION__, array());
$key = $country->iso2 . ':' . $langcode . ':' . $property;
if (!isset($translations[$key])) {
$translations[$key] = countries_i18n_translate($country->iso2, $property, $value, $langcode, 'country');
}
return $translations[$key];
}
return $value;
default:
return isset($country->{$property}) ? $country->{$property} : '';
}
}
function countries_countries_alter(&$countries) {
$enabled_countries =& drupal_static(__FUNCTION__, array());
if (empty($enabled_countries)) {
$enabled_countries = countries_get_countries('name', array(
'enabled' => COUNTRIES_ENABLED,
), array(
'sanitize' => FALSE,
));
}
$countries = $enabled_countries;
}
function countries_entity_info() {
$return = array(
'country' => array(
'label' => t('Country'),
'entity class' => 'Entity',
'controller class' => 'EntityAPIControllerExportable',
'base table' => 'countries_country',
'fieldable' => TRUE,
'module' => 'countries',
'entity keys' => array(
'id' => 'cid',
'name' => 'iso2',
'label' => 'name',
),
'bundles' => array(
'country' => array(
'label' => t('Country'),
'admin' => array(
'path' => 'admin/config/regional/countries',
'access arguments' => array(
'administer site configuration',
),
),
),
),
'view modes' => array(
'full' => array(
'label' => t('Country'),
'custom settings' => FALSE,
),
),
'translation' => array(
'entity_translation' => array(
'base path' => 'admin/config/regional/countries/%country',
),
),
),
);
return $return;
}
function countries_field_extra_fields() {
$extra = array();
$extra['country']['country'] = array(
'form' => array(),
'display' => array(),
);
$weight = -20;
foreach (countries_core_properties() as $key => $title) {
$extra['country']['country']['form'][$key] = array(
'label' => $title,
'description' => $title,
'weight' => $weight++,
);
}
return $extra;
}
function countries_load($cid, $reset = FALSE) {
return entity_load('country', array(
$cid,
), array(), $reset);
}
function countries_load_multiple($cids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('country', $cids, $conditions, $reset);
}
function country_create($properties = array()) {
return entity_create('country', $properties + array(
'cid' => NULL,
'iso2' => '',
'iso3' => '',
'name' => '',
'official_name' => '',
'continent' => 'UN',
'enabled' => 1,
'numcode' => 0,
));
}
function country_load($iso2) {
$iso2 = $iso2 ? drupal_strtoupper($iso2) : $iso2;
return entity_load_single('country', $iso2);
}
function countries_get_country($iso2) {
watchdog('countries', 'Use of deprecated function countries_get_country(), use country_load() instead.', array(), WATCHDOG_NOTICE);
return country_load($iso2);
}
function country_validate(&$country) {
module_load_include('admin.inc', 'countries');
$errors = array();
$defaults = country_create();
foreach (countries_core_properties() as $key => $label) {
$country->{$key} = isset($country->{$key}) ? trim($country->{$key}) : $defaults->{$key};
if ($length = drupal_strlen($country->{$key})) {
if ($error = countries_property_invalid($key, $country->{$key})) {
$errors[$key] = $error;
}
}
elseif ($key == 'name') {
$errors['name'] = t('Name is required');
}
elseif ($key == 'iso2') {
$errors['iso2'] = t('ISO alpha-2 field is required');
}
}
if (empty($errors['iso2'])) {
if (empty($country->cid) && ($existing = country_load($country->iso2))) {
$errors['iso2'] = t('The ISO alpha-2 code is already in use.');
}
else {
if ($duplicates = country_duplicate_field_check($country)) {
foreach ($duplicates as $key => $error) {
if (empty($errors[$key])) {
$errors[$key] = $error;
}
}
}
}
}
if (!empty($errors)) {
$country->_errors = $errors;
}
return empty($errors);
}
function country_save(&$country, $clear_cache = TRUE) {
$result = entity_save('country', $country);
if ($clear_cache) {
countries_clear_caches();
}
return $result;
}
function country_delete($iso2, $clear_cache = TRUE) {
$iso2 = drupal_strtoupper($iso2);
$result = entity_delete('country', $iso2);
if ($clear_cache) {
countries_clear_caches();
}
return $result;
}
function country_is_locked($country) {
include_once DRUPAL_ROOT . '/includes/iso.inc';
$countries = _country_get_predefined_list();
return !empty($country->iso2) && array_key_exists($country->iso2, $countries);
}
function countries_clear_caches() {
drupal_static_reset('countries_t');
drupal_static_reset('countries_get_countries');
drupal_static_reset('countries_get_continents');
drupal_static_reset('countries_countries_alter');
entity_get_controller('country')
->resetCache();
}
function countries_get_continents($t_options = array()) {
$continents =& drupal_static(__FUNCTION__, array());
global $language;
$langcode = empty($t_options['langcode']) ? $language->language : $t_options['langcode'];
if (empty($continents[$langcode])) {
$continents[$langcode] = variable_get('countries_continents', countries_get_default_continents($t_options));
$known = countries_known_continents($t_options);
$continents[$langcode] = array_intersect_key($known, $continents[$langcode]) + array_diff_key($continents[$langcode], $known);
uasort($continents[$langcode], 'countries_sort');
}
return $continents[$langcode];
}
function countries_known_continents($t_options = array()) {
return countries_get_default_continents($t_options) + array(
'AE' => t('Afro-Eurasia', array(), $t_options),
'1S' => t('Southern Africa', array(), $t_options),
'1W' => t('Western Africa', array(), $t_options),
'1N' => t('Northern Africa', array(), $t_options),
'1M' => t('Middle Africa', array(), $t_options),
'1E' => t('Eastern Africa', array(), $t_options),
'2S' => t('Southern Asia', array(), $t_options),
'2W' => t('Western Asia', array(), $t_options),
'2Z' => t('South-Eastern Asia', array(), $t_options),
'2E' => t('Eastern Asia', array(), $t_options),
'2C' => t('Central Asia', array(), $t_options),
'IC' => t('Indian subcontinent', array(), $t_options),
'2M' => t('Middle East', array(), $t_options),
'2G' => t('Greater Middle East', array(), $t_options),
'3S' => t('Southern Europe', array(), $t_options),
'3W' => t('Western Europe', array(), $t_options),
'3E' => t('Eastern Europe', array(), $t_options),
'3N' => t('Northern Europe', array(), $t_options),
'CE' => t('Continental Europe', array(), $t_options),
'ER' => t('Eurasia', array(), $t_options),
'AM' => t('Americas', array(), $t_options),
'CA' => t('Caribbean', array(), $t_options),
'AC' => t('Central America', array(), $t_options),
'AU' => t('Australasia', array(), $t_options),
'AZ' => t('Australia and New Zealand', array(), $t_options),
'PO' => t('Polynesia', array(), $t_options),
'ME' => t('Melanesia', array(), $t_options),
'MI' => t('Micronesia', array(), $t_options),
);
}
function countries_get_default_continents($t_options = array()) {
return array(
'AF' => t('Africa', array(), $t_options),
'AS' => t('Asia', array(), $t_options),
'EU' => t('Europe'),
'NA' => t('North America', array(), $t_options),
'SA' => t('South America', array(), $t_options),
'OC' => t('Oceania', array(), $t_options),
'AN' => t('Antarctica', array(), $t_options),
'UN' => t('Unknown', array(), $t_options + array(
'context' => 'countries',
)),
);
}
function countries_filter($countries, $filters = array()) {
if (!empty($filters)) {
$target_countries = array();
foreach (countries_get_countries() as $country) {
$include = TRUE;
if (isset($filters['enabled'])) {
$include &= $filters['enabled'] == COUNTRIES_ALL || $filters['enabled'] == COUNTRIES_ENABLED && $country->enabled || $filters['enabled'] == COUNTRIES_DISABLED && !$country->enabled;
}
if (!empty($filters['countries'])) {
$include &= in_array($country->iso2, $filters['countries']);
}
if (!empty($filters['continents'])) {
$include &= in_array($country->continent, $filters['continents']);
}
if ($include) {
$target_countries[$country->iso2] = TRUE;
}
}
$countries = array_intersect_key($countries, $target_countries);
}
return $countries;
}
function countries_filter_query_alter(&$query, $filters = array(), $alias = 'c') {
if (!empty($filters)) {
$filters += array(
'countries' => array(),
'continents' => array(),
);
$filters['countries'] = array_filter($filters['countries']);
$filters['continents'] = array_filter($filters['continents']);
if (isset($filters['enabled']) && !$filters['enabled'] == COUNTRIES_ALL) {
$query
->condition($alias . '.enabled', $filters['enabled']);
}
if (!empty($filters['countries'])) {
$query
->condition($alias . '.iso2', $filters['countries']);
}
if (!empty($filters['continents'])) {
$query
->condition($alias . '.continent', $filters['continents']);
}
}
}
function countries_sort($a, $b) {
$a = countries_transliterate(is_object($a) ? $a->name : $a);
$b = countries_transliterate(is_object($b) ? $b->name : $b);
return strnatcasecmp($a, $b);
}
function countries_transliterate($word) {
static $words = array();
if (!isset($words[$word])) {
if (function_exists('transliteration_get')) {
$words[$word] = transliteration_get($word, '');
}
else {
$words[$word] = str_replace(array(
'€',
'ƒ',
'Š',
'Ž',
'š',
'ž',
'Ÿ',
'¢',
'¥',
'µ',
'À',
'Á',
'Â',
'Ã',
'Ä',
'Å',
'Ç',
'È',
'É',
'Ê',
'Ë',
'Ì',
'Í',
'Î',
'Ï',
'Ñ',
'Ò',
'Ó',
'Ô',
'Õ',
'Ö',
'Ø',
'Ù',
'Ú',
'Û',
'Ü',
'Ý',
'à',
'á',
'â',
'ã',
'ä',
'å',
'ç',
'è',
'é',
'ê',
'ë',
'ì',
'í',
'î',
'ï',
'ñ',
'ò',
'ó',
'ô',
'õ',
'ö',
'ø',
'ù',
'ú',
'û',
'ü',
'ý',
'ÿ',
'Œ',
'œ',
'Æ',
'Ð',
'Þ',
'ß',
'æ',
'ð',
'þ',
), array(
'E',
'f',
'S',
'Z',
's',
'z',
'Y',
'c',
'Y',
'u',
'A',
'A',
'A',
'A',
'A',
'A',
'C',
'E',
'E',
'E',
'E',
'I',
'I',
'I',
'I',
'N',
'O',
'O',
'O',
'O',
'O',
'O',
'U',
'U',
'U',
'U',
'Y',
'a',
'a',
'a',
'a',
'a',
'a',
'c',
'e',
'e',
'e',
'e',
'i',
'i',
'i',
'i',
'n',
'o',
'o',
'o',
'o',
'o',
'o',
'u',
'u',
'u',
'u',
'y',
'y',
'OE',
'oe',
'AE',
'DH',
'TH',
'ss',
'ae',
'dh',
'th',
), $word);
}
}
return $words[$word];
}
function countries_invoke_additional_countries_alter(&$countries) {
foreach (module_implements('countries_alter') as $module) {
if ($module != 'countries') {
$func = $module . '_countries_alter';
$func($countries);
}
}
}
function countries_get_countries($property = 'all', $filters = array(), $options = array()) {
$countries = entity_load_multiple_by_name('country');
$filtered_countries = countries_filter($countries, $filters);
if ($property == 'all' || empty($property)) {
return $filtered_countries;
}
$mapped_countries = array();
foreach ($filtered_countries as $country) {
$mapped_countries[$country->iso2] = country_property($country, $property, $options);
}
uasort($mapped_countries, 'countries_sort');
return $mapped_countries;
}
function countries_core_properties() {
$core_properties = NULL;
if (!isset($core_properties)) {
$core_properties = array(
'name' => t('Name'),
'official_name' => t('Official name'),
'enabled' => t('Status'),
'iso2' => t('ISO alpha-2 code'),
'iso3' => t('ISO alpha-3 code'),
'numcode' => t('ISO numeric-3 code'),
'continent' => t('Continent'),
);
}
return $core_properties;
}
function country_property($country, $property = 'name', $options = array()) {
$args = func_get_args();
if (!is_array($options)) {
$options = array(
'default' => $options,
);
}
if (isset($args[3])) {
$options['sanitize'] = $args[3];
}
$options += array(
'default' => NULL,
'sanitize' => TRUE,
'langcode' => NULL,
);
$t_options = array(
'langcode' => $options['langcode'],
);
$output = NULL;
switch ($property) {
case 'cid':
return $country->cid;
case 'enabled':
$t_options['context'] = 'countries';
return empty($country->enabled) ? t('Disabled', array(), $t_options) : t('Enabled', array(), $t_options);
case 'continent_code':
if (!empty($country->continent)) {
$output = $country->continent;
}
break;
case 'continent':
case 'continent_name':
$continents = countries_get_continents($t_options);
if (!empty($country->continent) && !empty($continents[$country->continent])) {
$output = $continents[$country->continent];
}
elseif (!isset($options['default'])) {
$t_options['context'] = 'countries';
$output = t('Unknown', array(), $t_options);
}
break;
case 'official_name':
$output = countries_t($country, 'official_name', $options['langcode']);
if (!$output) {
if (isset($options['default'])) {
$output = $options['default'];
}
else {
$output = countries_t($country, 'name', $options['langcode']);
}
}
break;
case 'numcode':
if (!empty($country->numcode)) {
return theme('countries_number', array(
'country' => $country,
'langcode' => $options['langcode'],
));
}
break;
case 'name':
$output = countries_t($country, 'name', $options['langcode']);
if (!$output && isset($options['default'])) {
$output = $options['default'];
}
break;
default:
if (!empty($country->{$property})) {
$output = $country->{$property};
}
}
if (!isset($output)) {
$output = isset($options['default']) ? $options['default'] : '';
}
return $options['sanitize'] ? check_plain($output) : $output;
}
function countries_country_lookup($value, $property = NULL) {
if (empty($value)) {
return FALSE;
}
if (empty($property)) {
$property = _countries_estimate_property_type($value);
}
switch ($property) {
case 'iso2':
case 'iso3':
$value = drupal_strtoupper($value);
break;
case 'numcode':
$value = intval($value);
break;
default:
$value = trim($value);
break;
}
foreach (countries_get_countries($property, array(), array(
'sanitize' => FALSE,
)) as $iso2 => $country_value) {
if ($value == $country_value) {
return country_load($iso2);
}
}
if ($property == 'name') {
foreach (countries_get_countries('official_name', array(), array(
'sanitize' => FALSE,
)) as $iso2 => $country_value) {
if ($value == $country_value) {
return country_load($iso2);
}
}
$property = _countries_estimate_property_type($value);
if ($property != 'name') {
return countries_country_lookup($value, $property);
}
}
return FALSE;
}
function _countries_estimate_property_type($value) {
if (is_numeric($value)) {
return 'numcode';
}
switch (drupal_strlen($value)) {
case 2:
return 'iso2';
case 3:
return 'iso3';
default:
return 'name';
}
}
function theme_countries_number($variables) {
if (isset($variables['country']) && !empty($variables['country']->numcode)) {
return sprintf("%03s", $variables['country']->numcode);
}
return '';
}
function theme_country_icon_adapter($variables) {
$country = $variables['country'];
$icon_set = $variables['settings']['countryiconset'];
$property = $variables['settings']['property'];
list($icon_type, $icon_set_name) = explode('_', $icon_set, 2);
$variables = array(
'code' => $country->iso2,
'iconset' => $icon_set_name,
'alt' => $country->iso2,
'title' => $country
->label(),
);
$icon = '';
if ($icon_type == 'sprite') {
$icon = theme('countryicons_icon_sprite', $variables);
}
elseif ($icon_type == 'icon') {
$icon = theme('countryicons_icon', $variables);
}
if ($property && ($value = country_property($country, $property))) {
return $icon . ' ' . $value;
}
return $icon;
}
function form_type_country_value($element, $input = FALSE) {
module_load_include('elements.inc', 'countries');
return _form_type_country_value($element, $input);
}
function countries_country_element_validate($element, &$form_state) {
module_load_include('elements.inc', 'countries');
return _countries_country_element_validate($element, $form_state);
}
function countries_country_expand($element) {
module_load_include('elements.inc', 'countries');
return _countries_country_expand($element);
}
function countries_ctools_plugin_directory($module, $plugin) {
if (in_array($module, array(
'ctools',
)) && $plugin == 'arguments') {
return "plugins/{$plugin}";
}
}
function countries_views_api() {
return array(
'api' => '3',
'path' => drupal_get_path('module', 'countries') . '/views',
);
}
function countries_migrate_api() {
$api = array(
'api' => 2,
'field handlers' => array(
'MigrateCountryFieldHandler',
),
);
return $api;
}
function countries_action_info() {
return array(
'country_enable_action' => array(
'type' => 'country',
'label' => t('Enable country'),
'configurable' => FALSE,
'behavior' => array(
'changes_property',
),
),
'country_disable_action' => array(
'type' => 'country',
'label' => t('Disable country'),
'configurable' => FALSE,
'behavior' => array(
'changes_property',
),
),
);
}
function country_enable_action($country, $context = array()) {
$country->enabled = 1;
country_save($country);
}
function country_disable_action($country, $context = array()) {
$country->enabled = 0;
country_save($country);
}
function countries_apachesolr_field_mappings() {
$mappings = array(
'country' => array(
'display_callback' => 'countries_apachesolr_display_callback',
'indexing_callback' => 'countries_apachesolr_indexing_callback',
'map callback' => 'countries_apachesolr_map_callback',
'index_type' => 'string',
'facets' => TRUE,
),
);
return $mappings;
}
function countries_apachesolr_indexing_callback($entity, $field_name, $index_key, $field_info) {
$fields = array();
if (!empty($entity->{$field_name})) {
$field = $entity->{$field_name};
list($lang, $values) = each($field);
foreach ($values as $fval) {
$fields[] = array(
'key' => $index_key,
'value' => $fval['iso2'],
);
}
}
return $fields;
}
function countries_apachesolr_map_callback(array $values) {
$map = array();
foreach ($values as $value) {
if ($country = country_load($value)) {
$map[$value] = country_property($country, 'name', array(
'sanitize' => 0,
));
}
}
return $map;
}
function countries_apachesolr_display_callback($facet, $options) {
$fields = field_info_fields();
$field_name = $options['delta'];
if (isset($fields[$field_name])) {
$allowed_values = list_allowed_values($field);
if (isset($allowed_values[$facet])) {
$country = country_load($allowed_values[$facet]);
}
}
else {
$country = country_load($allowed_values[$facet]);
}
if ($country) {
return country_property($country);
}
return '';
}
function countries_get_properties($data = FALSE, array $options, $name) {
return country_property($data, $name);
}