Created
February 23, 2012 13:14
-
-
Save kasperg/1892739 to your computer and use it in GitHub Desktop.
D6 vid & tid handling
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 | |
/** | |
* Configuration for the mealplan specific vocabularies | |
*/ | |
function fdb_platform_mealplan_vocabularies() { | |
// Configuration for the mealplan specific vocabularies | |
$vocabularies = array(); | |
$vocabularies['meal_type'] = array('name' => t('Måltidstype'), | |
'help' => t('Pick a type for the meal'), | |
'relations' => 1, | |
'hierarchy' => 0, | |
'multiple' => 0, | |
'required' => 1, | |
'tags' => 0, | |
'weight' => 0, | |
'nodes' => array('meal' => 'meal'), | |
'terms' => array('breakfast' => t('Morgenmad'), | |
'brunch' => t('Brunch'), | |
'snack_morning' => t('Mellemmåltid - formiddag'), | |
'lunch' => t('Frokost'), | |
'snack_afternoon' => t('Mellemmåltid - eftermiddag'), | |
'dinner' => t('Aftensmad'), | |
'snack_evening' => t('Mellemmåltid - aften'))); | |
$vocabularies['season'] = array('name' => t('Årstid'), | |
'help' => t('Pick a season'), | |
'relations' => 1, | |
'hierarchy' => 0, | |
'multiple' => 0, | |
'required' => 1, | |
'tags' => 0, | |
'weight' => 0, | |
'nodes' => array('mealplan' => 'mealplan'), | |
'terms' => array('sprint' => t('Forår'), | |
'summer' => t('Sommer'), | |
'autumn' => t('Efterår'), | |
'winter' => t('Vinter'))); | |
return $vocabularies; | |
} | |
/** | |
* Update vocabularies. | |
*/ | |
function fdb_platform_mealplan_update_6000() { | |
fdb_platform_mealplan_setup_vocabularies(fdb_platform_mealplan_vocabularies()); | |
} | |
/** | |
* Implementation of hook_install(). | |
*/ | |
function fdb_platform_mealplan_install() { | |
fdb_platform_mealplan_setup_vocabularies(fdb_platform_mealplan_vocabularies()); | |
} | |
/** | |
* Implementation of hook_uninstall(). | |
*/ | |
function fdb_platform_mealplan_uninstall() { | |
// Cleanup our vocabularies | |
foreach (fdb_platform_mealplan_vocabularies() as $key => $v) { | |
// Unset the term variables | |
// The terms will be deleted with the vocabulary | |
if (isset($v['terms']) && is_array($v['terms'])) { | |
foreach ($v['terms'] as $term_key => $term) { | |
variable_del('fdb_platform_mealplan_term_' . $term_key . '_tid'); | |
} | |
} | |
// Delete the vocabulary | |
$variable_name = 'fdb_platform_mealplan_' . $key . '_vid'; | |
if ($vid = variable_get($variable_name, FALSE)) { | |
taxonomy_del_vocabulary($vid); | |
variable_del($variable_name); | |
} | |
} | |
} | |
/** | |
* Utility function to convert a vocabulary configuration in code to database | |
* values. | |
*/ | |
function fdb_platform_mealplan_setup_vocabularies($vocabularies) { | |
foreach (fdb_platform_mealplan_vocabularies() as $key => $vocabulary) { | |
$variable_name = 'fdb_platform_mealplan_' . $key . '_vid'; | |
$vid = variable_get($variable_name, FALSE); | |
// Create the vocabulary if it does not exist already | |
if (!$vid || !taxonomy_vocabulary_load($vid)) { | |
$vocabulary['module'] = 'fdb_platform_mealplan'; | |
taxonomy_save_vocabulary($vocabulary); | |
$vid = $vocabulary['vid']; | |
// Store the vid for future reference: | |
variable_set($variable_name, $vid); | |
} | |
// Create the terms | |
if (isset($vocabulary['terms']) && is_array($vocabulary['terms'])) { | |
foreach ($vocabulary['terms'] as $term_key => $term) { | |
// Check to see if the term already existings | |
// If the term has been created using this function then skip it | |
$variable_name = 'fdb_platform_mealplan_term_' . $term_key . '_tid'; | |
if (variable_get($variable_name, FALSE)) { | |
continue; | |
} | |
// Determine the weight of the term based on the position in the original array | |
$position = array_search($term_key, array_keys($vocabulary['terms'])); | |
$term = array('vid' => $vid, 'name' => $term, 'weight' => $position); | |
// If a user already created a term in the vocabulary with the same name | |
// then use it | |
$terms = taxonomy_get_term_by_name($term['name']); | |
if (!empty($terms)) { | |
foreach ($terms as $t) { | |
if ($term['vid'] == $vid) { | |
// Merge configuration and current values | |
$term = array_merge((array) $t, $term); | |
break; | |
} | |
} | |
} | |
taxonomy_save_term($term); | |
// Store the tid for future reference | |
variable_set($variable_name, $term['tid']); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment