View source
<?php
require_once 'location_taxonomize.inc';
require_once 'location_taxonomize.admin.inc';
function location_taxonomize_menu() {
$items = array();
$items['admin/config/content/' . LT_MODULE_ID] = 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_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
if (isset($form_state['field'])) {
$fields = $form_state['field'];
}
else {
return;
}
$settings = location_taxonomize_get_settings();
if ($settings['hide_terms_field']) {
$field_name = 'field_' . LT_TERM_ATTACH_FIELD;
if (in_array($field_name, array_keys($fields))) {
$form[$field_name]['#access'] = FALSE;
}
}
}
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' => LT_VOCAB_NAME,
'description' => t('This vocabulary is synchronized with Location data automatically by the @name module', array(
'@name' => LT_MODULE_NAME,
)),
));
$vocab = taxonomy_vocabulary_machine_name_load(LT_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();
$settings = array(
'source' => $values_init['source'],
'method' => $values_init['method'],
'hierarchy' => $values_init['hierarchy'],
);
module_invoke_all('location_taxonomize_initialize', $settings);
drupal_set_message(t('Location Taxonomize initialized successfully'));
}
function location_taxonomize_taxonomize($items, $form, &$form_state) {
$saved = 0;
$tids = array();
foreach ($items as $item) {
$results = location_taxonomize_process_item($item);
$saved += $results['saved'];
$tids = array_merge($tids, $results['tids']);
}
if (!empty($tids)) {
if (location_taxonomize_term_attach_enabled()) {
location_taxonomize_term_attach($tids, $form, $form_state);
}
}
if (variable_get('location_taxonomize_show_messages', 1)) {
$msg = t('Location taxonomize: @num new terms saved', array(
'@num' => $saved,
));
drupal_set_message($msg);
}
return array(
"saved" => $saved,
"tids" => $tids,
);
}
function location_taxonomize_taxonomize_bulk($items) {
$saved = 0;
foreach ($items as $nid => $items) {
$tids = array();
foreach ($items as $item) {
$results = location_taxonomize_process_item($item);
$saved += $results['saved'];
$tids = array_merge($tids, $results['tids']);
}
if (!empty($tids)) {
if (location_taxonomize_term_attach_enabled()) {
location_taxonomize_term_attach_bulk($tids, $nid);
}
}
}
return array(
"saved" => $saved,
);
}
function location_taxonomize_process_item($obj) {
if (!location_taxonomize_enabled()) {
return NULL;
}
$settings = variable_get('location_taxonomize_settings');
$vid = variable_get('location_taxonomize_vid');
$hierarchy = _location_taxonomize_get_hierarchy();
$longname_main = $settings['longname_enable'] && $settings['longname']['main'];
$saved = 0;
$tids = array();
for ($hlevel = 0; $hlevel < count($hierarchy); $hlevel++) {
$hlevel_name = $hierarchy[$hlevel];
if ($longname_main) {
$name = location_taxonomize_make_longname($hlevel, $obj);
$longname = $name;
}
else {
$name = _location_taxonomize_create_term_name($hlevel_name, $obj, $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'] && !$longname_main) {
_location_taxonomy_add_longname($term, $hlevel, $obj);
}
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 array(
'saved' => $saved,
'tids' => $tids,
);
}
function _find_term($name, $hlevel, $parentid) {
$terms = taxonomy_get_term_by_name($name, 'location_taxonomize');
if (!$terms) {
return FALSE;
}
$withparent = array();
foreach ($terms as $term) {
$parents = taxonomy_get_parents($term->tid);
if (empty($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;
}
if (variable_get('location_taxonomize_show_messages', 1)) {
$msg = t("Location taxonomize couldn't save a term because its hierarchy\n location was ambiguous.");
drupal_set_message($msg);
}
return TRUE;
}
function _location_taxonomy_add_longname(&$term, $hlevel, $location, $longname = NULL) {
if ($longname == NULL) {
$longname = location_taxonomize_make_longname($hlevel, $location);
}
$term->location_taxonomize_longname['und'][0]['value'] = $longname;
}
function location_taxonomize_make_longname($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];
$term_hlevel_name_lt = location_taxonomize_convert_field_name($term_hlevel_name);
if (!$fields[$term_hlevel_name]) {
return '';
}
$longname = array();
for ($i = 0; $i <= $hlevel; $i++) {
$hlevel_name = $hierarchy[$i];
$hlevel_name_lt = location_taxonomize_convert_field_name($hlevel_name);
$include = $fields[$hlevel_name];
if ($include) {
switch ($hlevel_name_lt) {
case 'country':
if ($term_hlevel_name_lt == 'country' || $ln_settings['country_naming'] == 'name') {
if (isset($location[location_taxonomize_source_field_name('country_name')])) {
$longname[] = $location[location_taxonomize_source_field_name('country_name')];
}
else {
$longname[] = $location[location_taxonomize_source_field_name('country')];
}
}
elseif ($location[$hlevel_name] == 'us' && $ln_settings['usa']) {
$longname[] = 'USA';
}
else {
$longname[] = drupal_strtoupper($location[$hlevel_name]);
}
break;
case 'province':
if ($term_hlevel_name_lt == 'province' || $ln_settings['province_naming'] == 'name') {
if (isset($location[location_taxonomize_source_field_name('province_name')])) {
$longname[] = $location[location_taxonomize_source_field_name('province_name')];
}
else {
$longname[] = $location[location_taxonomize_source_field_name('province')];
}
}
else {
$longname[] = $location[$hlevel_name];
}
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']) . ' ';
}
}
return $longname_str;
}
function _location_taxonomize_create_term_name($hlevel_name, &$location, &$settings) {
$field_name = location_taxonomize_convert_field_name($hlevel_name);
switch ($field_name) {
case 'country':
if (empty($location[$hlevel_name])) {
$name = $settings['na_text'];
}
elseif ($settings['naming']['country'] == 'name') {
$name = $location[location_taxonomize_source_field_name('country_name')];
}
elseif ($location[$hlevel_name] == 'us' && $settings['naming']['usa']) {
$name = 'USA';
}
else {
$name = drupal_strtoupper($location[$hlevel_name]);
}
break;
case 'province':
$us_states = location_taxonomize_us_states();
if (empty($location[$hlevel_name])) {
$name = $settings['na_text'];
}
elseif ($settings['naming']['province'] == 'name') {
if (!empty($location[location_taxonomize_source_field_name('province_name')])) {
$name = $location[location_taxonomize_source_field_name('province_name')];
}
else {
if (isset($us_states[strtoupper($location[$hlevel_name])])) {
$name = $us_states[strtoupper($location[$hlevel_name])];
}
else {
$name = $location[$hlevel_name];
}
}
}
else {
$name = $location[$hlevel_name];
}
break;
default:
if (empty($location[$hlevel_name])) {
$name = $settings['na_text'];
}
else {
$name = $location[$hlevel_name];
}
break;
}
return $name;
}
function location_taxonomize_term_attach($tids, $form, &$form_state) {
$lang = 'und';
$field = location_taxonomize_term_attach_check_field($form);
if ($field == FALSE) {
drupal_set_message(t('Location Taxonomize could not attach the terms to the node because the Taxonomy Reference field is not set up correctly'), 'warning');
return;
}
$form_state['values'][$field][$lang] = array();
$tids = array_reverse($tids);
$tids = array_unique($tids);
foreach ($tids as $tid) {
$form_state['values'][$field][$lang][]['tid'] = $tid;
}
}
function location_taxonomize_term_attach_bulk($tids, $nid) {
$langcode = 'und';
$node = node_load($nid);
if (!isset($node->field_location_taxonomize_terms)) {
return;
}
$tids = array_reverse($tids);
$tids = array_unique($tids);
$save = array();
foreach ($tids as $tid) {
$save[$langcode][]['tid'] = $tid;
}
$node->field_location_taxonomize_terms = $save;
node_save($node);
}
function location_taxonomize_bulk_taxonomize($form, $form_state) {
$source = _location_taxonomize_get_source();
$batch_function = $source . '_bulk_taxonomize_op';
$batch = array(
'operations' => array(
array(
$batch_function,
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_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);
}