View source
<?php
define('LT_MODULE_ID', 'location_taxonomize');
define('LT_MODULE_NAME', 'Location taxonomize');
define('LT_VOCAB_NAME', 'location_taxonomize');
define('LT_TERM_ATTACH_FIELD', 'location_taxonomize_terms');
function _location_taxonomize_reset() {
location_taxonomize_empty_vocab();
_location_taxonomize_set_defaults();
drupal_set_message(t('Location Taxonomize has been reset'));
}
function _location_taxonomize_variables() {
return array(
'location_taxonomize_vid' => NULL,
'location_taxonomize_vocab' => array(
'source' => NULL,
'method' => 'existing',
'possible_vid' => NULL,
'hierarchy' => NULL,
),
'location_taxonomize_settings' => array(
'enable' => 1,
'term_attach' => 0,
'hide_terms_field' => 0,
'naming' => array(
'country' => 'name',
'province' => 'name',
'usa' => 1,
),
'na_text' => 'Unknown',
'longname_enable' => 0,
'longname' => array(
'main' => 0,
'fields' => NULL,
'separator' => ',',
'country_naming' => 'code',
'province_naming' => 'code',
'usa' => 1,
),
),
'location_taxonomize_show_messages' => 1,
);
}
function location_taxonomize_empty_vocab() {
$terms = taxonomy_get_tree(variable_get('location_taxonomize_vid'));
foreach ($terms as $term) {
taxonomy_term_delete($term->tid);
}
$termsnow = taxonomy_get_tree(variable_get('location_taxonomize_vid'));
if (!$termsnow) {
return t('Location vocabulary emptied.');
}
return t('There was a problem emptying the Location Vocabulary.');
}
function _location_taxonomize_get_hierarchy($assoc = FALSE, $labels = FALSE) {
$settings = variable_get('location_taxonomize_vocab');
$fields = $settings['hierarchy'];
if ($labels) {
$withlabels = _location_taxonomize_get_fields(TRUE, TRUE);
}
$hierarchy = array();
foreach ($fields as $key => $value) {
if ($value) {
if ($assoc) {
if ($labels) {
$hierarchy[$key] = $withlabels[$key];
}
else {
$hierarchy[$key] = $key;
}
}
else {
$hierarchy[] = $key;
}
}
}
return $hierarchy;
}
function _location_taxonomize_init_var_longname() {
$hierarchy = _location_taxonomize_get_hierarchy();
$defaults = array();
foreach ($hierarchy as $field) {
if (location_taxonomize_convert_field_name($field) != 'country' && location_taxonomize_convert_field_name($field) != 'province' && location_taxonomize_convert_field_name($field) != 'city') {
$defaults[$field] = 0;
}
else {
$defaults[$field] = $field;
}
}
$settings = variable_get('location_taxonomize_settings');
$settings['longname']['fields'] = $defaults;
variable_set('location_taxonomize_settings', $settings);
}
function location_taxonomize_get_sources() {
$possibilities = module_invoke_all('location_taxonomize_source');
$available = array();
foreach ($possibilities as $module => $name) {
if (module_exists($module)) {
$available[$module] = $name;
}
}
return $available;
}
function location_taxonomize_disassociate($form, $form_state) {
variable_del('location_taxonomize_vid');
drupal_set_message(t('Location Taxonomy successfully disassociated'));
}
function _location_taxonomize_set_defaults() {
$defaults = _location_taxonomize_variables();
foreach ($defaults as $key => $value) {
if ($value) {
variable_set($key, $value);
}
else {
variable_del($key);
}
}
}
function _location_taxonomize_del_variables() {
$vars = _location_taxonomize_variables();
foreach ($vars as $key => $value) {
variable_del($key);
}
return t('All variables deleted');
}
function _location_taxonomize_get_fields($assoc = TRUE, $labels = FALSE, $source = NULL) {
if ($source == NULL) {
$source = _location_taxonomize_get_source();
}
$suffix = '_get_fields';
$function = $source . $suffix;
return $function($assoc, $labels);
}
function location_taxonomize_term_attach_check_field($form) {
$field_name = 'field_' . LT_TERM_ATTACH_FIELD;
if (!isset($form[$field_name])) {
return FALSE;
}
$field_info = field_info_field($field_name);
if ($field_info['deleted'] == 1) {
return FALSE;
}
if ($field_info['type'] != 'taxonomy_term_reference') {
return FALSE;
}
if ($field_info['settings']['allowed_values'][0]['vocabulary'] != LT_MODULE_ID) {
return FALSE;
}
return $field_name;
}
function _location_taxonomize_get_source() {
$settings = variable_get('location_taxonomize_vocab');
return $settings['source'];
}
function _location_taxonomize_get_source_settings($source = NULL) {
if ($source == NULL) {
$source = _location_taxonomize_get_source();
}
$suffix = '_settings';
return variable_get($source . $suffix);
}
function location_taxonomize_source_default_set($module) {
$settings = variable_get('location_taxonomize_vocab');
if (empty($settings['source']) || !module_exists($settings['source'])) {
$settings['source'] = $module;
variable_set('location_taxonomize_vocab', $settings);
return TRUE;
}
return FALSE;
}
function location_taxonomize_get_primary_fields($reverse = FALSE) {
$source = _location_taxonomize_get_source();
$suffix = '_primary_field_names';
$function = $source . $suffix;
$source_fields = $function();
$internal_fields = array(
'country',
'country_name',
'province',
'province_name',
'city',
);
if (!$reverse) {
return array_combine($internal_fields, $source_fields);
}
else {
return array_combine($source_fields, $internal_fields);
}
}
function location_taxonomize_convert_field_name($name) {
$primary_fields = location_taxonomize_get_primary_fields();
foreach ($primary_fields as $key => $value) {
if ($value == $name) {
$name = $key;
break;
}
}
return $name;
}
function location_taxonomize_source_field_name($internal_name) {
$primary_fields = location_taxonomize_get_primary_fields();
if (isset($primary_fields[$internal_name])) {
return $primary_fields[$internal_name];
}
return $internal_name;
}
function location_taxonomize_enabled() {
$settings = variable_get('location_taxonomize_settings');
$vid = variable_get('location_taxonomize_vid');
$enabled = $settings['enable'] && isset($vid);
return $enabled;
}
function location_taxonomize_term_attach_enabled() {
$settings = variable_get('location_taxonomize_settings');
$enabled = $settings['term_attach'];
return $enabled;
}
function location_taxonomize_get_settings() {
$settings = variable_get('location_taxonomize_settings');
return $settings;
}
function location_taxonomize_get_country_code($name) {
require_once DRUPAL_ROOT . '/includes/locale.inc';
$countries = country_get_list();
$names = array_keys($countries, $name);
if (!empty($names)) {
return array_pop($names);
}
return NULL;
}
function location_taxonomize_get_country_name($code) {
require_once DRUPAL_ROOT . '/includes/locale.inc';
$countries = country_get_list();
if (isset($countries[strtoupper($code)])) {
return $countries[strtoupper($code)];
}
return NULL;
}
function location_taxonomize_us_states() {
return array(
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
'DC' => 'District Of Columbia',
'FL' => 'Florida',
'GA' => 'Georgia',
'HI' => 'Hawaii',
'ID' => 'Idaho',
'IL' => 'Illinois',
'IN' => 'Indiana',
'IA' => 'Iowa',
'KS' => 'Kansas',
'KY' => 'Kentucky',
'LA' => 'Louisiana',
'ME' => 'Maine',
'MD' => 'Maryland',
'MA' => 'Massachusetts',
'MI' => 'Michigan',
'MN' => 'Minnesota',
'MS' => 'Mississippi',
'MO' => 'Missouri',
'MT' => 'Montana',
'NE' => 'Nebraska',
'NV' => 'Nevada',
'NH' => 'New Hampshire',
'NJ' => 'New Jersey',
'NM' => 'New Mexico',
'NY' => 'New York',
'NC' => 'North Carolina',
'ND' => 'North Dakota',
'OH' => 'Ohio',
'OK' => 'Oklahoma',
'OR' => 'Oregon',
'PA' => 'Pennsylvania',
'RI' => 'Rhode Island',
'SC' => 'South Carolina',
'SD' => 'South Dakota',
'TN' => 'Tennessee',
'TX' => 'Texas',
'UT' => 'Utah',
'VT' => 'Vermont',
'VA' => 'Virginia',
'WA' => 'Washington',
'WV' => 'West Virginia',
'WI' => 'Wisconsin',
'WY' => 'Wyoming',
);
}