Created
August 14, 2012 13:52
-
-
Save yvmarques/3349417 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
_refresh_and_assign_country_to_node($nid, $country, $region) { | |
if (empty($nid) || empty($country) || empty($region)) { | |
return FALSE; | |
} | |
// Some pseudo-constants for config | |
$VOCABULARY_COUNTRIES = 0; | |
$node = node_load($nid); | |
if (!is_object($node)) { | |
return FALSE; | |
} | |
// Get the country | |
$countries = taxonomy_get_term_by_name($country); | |
$country = array( | |
'vid' => $VOCABULARY_COUNTRIES, | |
'name' => check_plain($country), | |
); | |
if (!empty($countries)) { | |
$saved_countries = array(); | |
foreach ($countries as $tid => $term) { | |
if ($VOCABULARY_COUNTRIES == $term->vid) { | |
$saved_countries[] = $term; | |
} | |
} | |
if (!empty($saved_countries) && is_array($saved_countries)) { | |
$country += (array) array_shift($saved_countries); // get first country | |
} | |
} | |
// Get the region | |
$region = array( | |
'vid' => $VOCABULARY_COUNTRIES, | |
'name' => check_plain($region), | |
); | |
if (isset($country['tid']) && !empty($country['tid'])) { | |
$regions = taxonomy_get_parents($country->tid); | |
if (!empty($regions)) { | |
$saved_regions = array(); | |
foreach ($regions as $tid => $term) { | |
if ($VOCABULARY_COUNTRIES == $term->vid && | |
drupal_strtolower($region) == drupal_strtolower($region)) { | |
$saved_regions[] = $term; | |
} | |
} | |
if (!empty($saved_regions) && is_array($saved_regions)) { | |
$region += (array) array_shift($saved_regions); // get first region | |
} | |
} | |
} | |
// Save the terms | |
$return = taxonomy_save_term($region); | |
if ($return != SAVED_UPDATED || $return != SAVED_NEW) { | |
watchdog('membership_country', 'Unable to save the region'); | |
return FALSE; | |
} | |
$country['parent'] = $region->tid; | |
$return = taxonomy_save_term($country); | |
if ($return != SAVED_UPDATED || $return != SAVED_NEW) { | |
watchdog('membership_country', 'Unable to save the country'); | |
return FALSE; | |
} | |
// Assign it to the node | |
taxonomy_node_save($node, array((object) $country)); | |
return TRUE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment