View source
<?php
require_once 'location_taxonomize.inc';
require_once 'location_taxonomize.admin.inc';
function location_taxonomize_menu() {
$items = array();
$items['admin/config/content/location_taxonomize'] = array(
'title' => 'Location Taxonomize',
'description' => 'Configuration for Location taxonomize module',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'location_taxonomize_form',
),
'access callback' => 'location_taxonomize_access_admin',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function location_taxonomize_access_admin() {
$access_lt = user_access('administer location_taxonomize');
$access_admin = user_access('access administration pages');
return $access_lt && $access_admin;
}
function location_taxonomize_permission() {
return array(
'administer location_taxonomize' => array(
'title' => t('Administer Location taxonomize'),
),
);
}
function location_taxonomize_locationapi(&$obj, $op, $a3, $a4, $a5) {
$settings = variable_get('location_taxonomize_settings');
$vid = variable_get('location_taxonomize_vid');
if (!$settings['enable'] || !$vid) {
return;
}
switch ($op) {
case 'save':
$saved = location_taxonomize_process_loc($obj);
$msg = t('Location taxonomize: @num new terms saved', array(
'@num' => $saved,
));
drupal_set_message($msg);
}
}
function location_taxonomize_initialize($form, $form_state) {
$values_init = $form_state['values']['location_taxonomize_vocab'];
$method = $values_init['method'];
switch ($method) {
case 'new':
taxonomy_vocabulary_save((object) array(
'name' => t('Location'),
'machine_name' => LOCATION_TAXONOMIZE_VOCAB_NAME,
'description' => t('This vocabulary is synchronized with Location data automatically by the @name module', array(
'@name' => LOCATION_TAXONOMIZE_MODULE_NAME,
)),
));
$vocab = taxonomy_vocabulary_machine_name_load(LOCATION_TAXONOMIZE_VOCAB_NAME);
$vid = $vocab->vid;
variable_set('location_taxonomize_vid', $vid);
$msg = t('Successfully created the Location taxonomy (vid @vid)', array(
'@vid' => $vid,
));
break;
case 'existing':
$vid = $values_init['possible_vid'];
variable_set('location_taxonomize_vid', $vid);
$msg = t('Successfully connected to the existing Location Vocabulary (vid @vid)', array(
'@vid' => $vid,
));
break;
}
drupal_set_message($msg);
if (!field_info_field('location_taxonomize_longname')) {
$field = array(
'field_name' => 'location_taxonomize_longname',
'type' => 'text',
);
field_create_field($field);
}
if (!field_info_instance('taxonomy_term', 'location_taxonomize_longname', 'location_taxonomize')) {
$instance = array(
'field_name' => 'location_taxonomize_longname',
'label' => t('Long Name'),
'entity_type' => 'taxonomy_term',
'bundle' => 'location_taxonomize',
);
field_create_instance($instance);
}
_location_taxonomize_init_var_longname();
drupal_set_message(t('Location Taxonomize initialized successfully'));
}
function location_taxonomize_process_loc($obj) {
$settings = variable_get('location_taxonomize_settings');
$vid = variable_get('location_taxonomize_vid');
$hierarchy = _location_taxonomize_get_hierarchy();
$saved = 0;
$location = _fix_loc_tmp($obj);
$tids = array();
for ($hlevel = 0; $hlevel < count($hierarchy); $hlevel++) {
$hlevel_name = $hierarchy[$hlevel];
$name = _location_taxonomize_create_term_name($hlevel_name, $location, $settings);
if ($hlevel == 0) {
$parentid = -1;
}
else {
$parentid = $tids[$hlevel - 1];
}
$findterm = _find_term($name, $hlevel, $parentid);
if (!$findterm) {
$term = (object) array(
'name' => $name,
'vid' => $vid,
);
if ($settings['longname_enable']) {
_location_taxonomy_add_longname($term, $hlevel, $location);
}
if ($hlevel == 0) {
$term->parent = array(
0,
);
}
else {
$term->parent = array(
$parentid,
);
}
taxonomy_term_save($term);
$tids[] = $term->tid;
$saved++;
}
elseif ($findterm->tid) {
$tids[] = $findterm->tid;
}
}
return $saved;
}
function _find_term($name, $hlevel, $parentid) {
$terms = taxonomy_get_term_by_name($name);
if (!$terms) {
return FALSE;
}
$withparent = array();
foreach ($terms as $term) {
$parents = taxonomy_get_parents($term->tid);
if (!$parents && $parentid == -1) {
$withparent[] = $term;
}
elseif (array_pop($parents)->tid == $parentid) {
$withparent[] = $term;
}
}
if (count($withparent) == 1) {
return array_pop($withparent);
}
elseif (count($withparent) == 0) {
return FALSE;
}
$msg = t("Location taxonomize couldn't save a term because its hierarchy\n location was ambiguous.");
drupal_set_message($msg);
return TRUE;
}
function _fix_loc_tmp($loc) {
$loc['country_name'] = location_country_name($loc['country']);
$loc['province_name'] = location_province_name($loc['country'], $loc['province']);
return $loc;
}
function _location_taxonomy_add_longname(&$term, $hlevel, $location) {
$settings = variable_get('location_taxonomize_settings');
$ln_settings = $settings['longname'];
$fields = $ln_settings['fields'];
$hierarchy = _location_taxonomize_get_hierarchy();
$term_hlevel_name = $hierarchy[$hlevel];
if (!$fields[$term_hlevel_name]) {
return '';
}
$longname = array();
for ($i = 0; $i <= $hlevel; $i++) {
$hlevel_name = $hierarchy[$i];
$include = $fields[$hlevel_name];
if ($include) {
switch ($hlevel_name) {
case 'country':
if ($term_hlevel_name == 'country' || $ln_settings['country_naming'] == 'name') {
$longname[] = $location['country_name'];
}
elseif ($location['country'] == 'us' && $ln_settings['usa']) {
$longname[] = 'USA';
}
else {
$longname[] = drupal_strtoupper($location['country']);
}
break;
case 'province':
if ($term_hlevel_name == 'province' || $ln_settings['province_naming'] == 'name') {
$longname[] = $location['province_name'];
}
else {
$longname[] = $location['province'];
}
break;
default:
$longname[] = $location[$hlevel_name];
break;
}
}
}
$longname = array_reverse($longname);
$count = count($longname);
$longname_str = '';
for ($i = 0; $i < $count; $i++) {
$longname_str .= $longname[$i];
if ($i != $count - 1) {
$longname_str .= check_plain($ln_settings['separator']) . ' ';
}
}
$term->location_taxonomize_longname['und'][0]['value'] = $longname_str;
}
function _location_taxonomize_init_var_longname() {
$hierarchy = _location_taxonomize_get_hierarchy(FALSE);
$defaults = array();
foreach ($hierarchy as $field) {
if ($field != 'country' && $field != 'province' && $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_create_term_name($hlevel_name, &$location, &$settings) {
switch ($hlevel_name) {
case 'country':
if (empty($location['country'])) {
$name = $settings['na_text'];
}
elseif ($settings['naming']['country'] == 'name') {
$name = $location['country_name'];
}
elseif ($location['country'] == 'us' && $settings['naming']['usa']) {
$name = 'USA';
}
else {
$name = drupal_strtoupper($location['country']);
}
break;
case 'province':
if (empty($location['province'])) {
$name = $settings['na_text'];
}
elseif ($settings['naming']['province'] == 'name') {
$name = $location['province_name'];
}
else {
$name = $location['province'];
}
break;
default:
if (empty($location[$hlevel_name])) {
$name = $settings['na_text'];
}
else {
$name = $location[$hlevel_name];
}
break;
}
return $name;
}
function location_taxonomize_bulk_taxonomize($form, $form_state) {
$batch = array(
'operations' => array(
array(
'location_taxonomize_bulk_taxonomize_op',
array(
$form_state,
),
),
),
'finished' => 'location_taxonomize_bulk_taxonomize_finished',
'title' => t('Taxonomizing Locations'),
'init_message' => t('Initializing...'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Bulk Taxonomize encountered an error.'),
);
batch_set($batch);
}
function location_taxonomize_bulk_taxonomize_op($form_state, &$context) {
if (!isset($context['sandbox']['progress'])) {
$max = db_query('SELECT COUNT(DISTINCT lid) FROM {location}')
->fetchField();
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = $max;
$context['sandbox']['current'] = 0;
$context['results']['added'] = 0;
$context['results']['processed'] = 0;
}
$limit = 10;
$result = db_query_range("SELECT lid, name, street, additional, city, province, postal_code, country, latitude, longitude, source FROM {location} WHERE lid <> 0 ORDER BY lid ASC", $context['sandbox']['current'], $limit, array());
$result_array = $result
->fetchAllAssoc('lid');
foreach ($result_array as $row) {
$added = location_taxonomize_process_loc((array) $row);
$context['results']['added'] += $added;
$context['sandbox']['progress']++;
$context['message'] = t('Processing location lid %n', array(
'%n' => $row->lid,
));
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
$context['sandbox']['current'] = $context['sandbox']['progress'];
$context['results']['processed'] = $context['sandbox']['progress'];
}
function location_taxonomize_bulk_taxonomize_finished($success, $results, $operations) {
$success = t('Bulk Taxonomize completed successfully!');
$locs = t('Processed @num locations', array(
'@num' => $results['processed'],
));
$terms = t('Saved @num new terms to the Location Vocabulary', array(
'@num' => $results['added'],
));
drupal_set_message($success);
drupal_set_message($locs);
drupal_set_message($terms);
}