--- import_export_tool/import_export_tool.module 2009-03-17 16:26:03.000000000 -0400 +++ ../import_export_tool/import_export_tool.module 2009-04-22 15:57:09.000000000 -0400 @@ -1,379 +1,445 @@ 'Import/Export settings', - 'description' => 'Import/Export settings allows user to configure paths etc.', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('import_export_tool_settings'), - 'access callback' => 'user_access', - 'access arguments' => array('administer permissions'), - 'type' => MENU_NORMAL_ITEM); - $items['admin/build/export'] = array( - 'title' => 'Export CCK types and views', - 'description' => 'Export types and views into text files.', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('import_export_tool_types_views_export'), - 'access callback' => 'user_access', - 'access arguments' => array('administer permissions'), - 'type' => MENU_NORMAL_ITEM); - $items['admin/build/import'] = array( - 'title' => 'Import CCK types and views', - 'description' => 'Import types and views into text files.', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('import_export_tool_types_views_import'), - 'access callback' => 'user_access', - 'access arguments' => array('administer permissions'), - 'type' => MENU_NORMAL_ITEM); + 'title' => t('Import/Export tool'), + 'description' => t('Specify the path where exported files will be saved.'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('import_export_tool_settings'), + 'access callback' => 'user_access', + 'access arguments' => array('administer permissions'), + ); + $items['admin/build/import_export_tool'] = array( + 'title' => t('Import and Export'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('import_export_tool_export_form'), + 'access callback' => 'user_access', + 'access arguments' => array('administer permissions'), + 'type' => MENU_NORMAL_ITEM, + ); + $items['admin/build/import_export_tool/export'] = array( + 'title' => t('Export content types, views and taxonomies'), + 'description' => t('Content types and views will be exported into text files.'), + 'access callback' => 'user_access', + 'access arguments' => array('administer permissions'), + 'type' => MENU_DEFAULT_LOCAL_TASK, + ); + $items['admin/build/import_export_tool/import'] = array( + 'title' => t('Import content types, views and taxonomies'), + 'description' => t('Find content types and views that have been serialized to files and import them.'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('import_export_tool_import_form'), + 'access callback' => 'user_access', + 'access arguments' => array('administer permissions'), + 'type' => MENU_LOCAL_TASK, + 'weight' => 5, + ); return $items; } - function import_export_tool_settings() { - $form['import_export_tool_path'] = array( - '#type' => 'textfield', - '#title' => t('Specify path for export_import'), - '#default_value' => variable_get('import_export_tool_path', file_directory_temp() .'/store'), - '#description' => t('Enter the path to store files with cck types and views.') + '#type' => 'textfield', + '#title' => t('Specify path for where content type and views files will be saved.'), + '#default_value' => variable_get('import_export_tool_path', import_export_tool_get_directory()), + '#description' => t('Enter the path where exported files will be saved. The webserver must have permissions to write to this directory. The path may be relative or absolute. Relative paths will be relative to your files directory (%files).', array('%files' => file_directory_path())), ); - + return system_settings_form($form); } +function import_export_tool_get_directory() { + return variable_get('import_export_tool_path', file_directory_path() . '/import_export_tool'); +} + function import_export_tool_theme() { return array( - 'import_export_tool_types_views_export' => array( - 'arguments' => array('form' => array())), - 'import_export_tool_types_views_import' => array( - 'arguments' => array('form' => array()))); -} - -function import_export_tool_types_views_import() { - - $dir = variable_get('import_export_tool_path', file_directory_temp() .'/store'); - $files = array(); - if ($handle = opendir($dir)) { - while (false !== ($file = readdir($handle))) { - $files[] = $file; - } - closedir($handle); - } - - $views = array(); - $types = array(); - - foreach ($files as $file_name) { - if (strstr($file_name, '.view')) { - $view_temp = explode(".", $file_name); - $views[$view_temp[0]] = $view_temp[0]; - } - elseif (strstr($file_name, '.cck')) { - $cck_temp = explode(".", $file_name); - $types[$cck_temp[0]] = $cck_temp[0]; - } - } - - if (count ($types) > 0) { - foreach ($types as $type_k => $type_v) { - $form['types'][$type_k]['value'] = array('#type' => 'checkbox', '#name' => $type_k .'_cck_', '#value' => $type_k, '#width' => 10000); - $form['types'][$type_k]['title'] = array('#type' => 'item', '#value' => $type_v); - } - } - - $form['header_cck'] = array('#type' => 'value', '#value' => array( - theme('table_select_header_cell'), - array('data' => t('CCK type Name'), 'field' => 'title'))); - - if (count ($views) > 0) { - foreach ($views as $view_k => $view_v) { - $form['views'][$view_k]['value'] = array('#type' => 'checkbox', '#name' => $view_k .'_vw_', '#value' => $view_k); - $form['views'][$view_k]['title'] = array('#type' => 'item', '#value' => $view_v); - } - } - - $form['header_views'] = array('#type' => 'value', '#value' => array( - theme('table_select_header_cell'), - array('data' => t('View Name'), 'field' => 'title'), - )); - - if (count ($views) > 0 || count ($types)) { + 'import_export_tool_export_form' => array( + 'arguments' => array('form' => array()), + ), + 'import_export_tool_import_form' => array( + 'arguments' => array('form' => array()), + ), + ); +} + +function import_export_tool_import_form() { + $dir = import_export_tool_get_directory(); + $ignore = array('.', '..', 'CVS'); + $cck_files = file_scan_directory(realpath($dir), '.cck', $ignore); + $views_files = file_scan_directory(realpath($dir), '.views', $ignore); + $taxonomy_files = file_scan_directory(realpath($dir), '.taxonomy', $ignore); + + if (count($cck_files) > 0) { + foreach ($cck_files as $file) { + $content_type_options[$file->filename] = $file->name; + } + $form['content_types'] = array( + '#type' => 'checkboxes', + '#options' => $content_type_options, + '#title' => t('Content types'), + ); + } + + if (count($views_files) > 0) { + foreach ($views_files as $file) { + $views_options[$file->filename] = $file->name; + } + $form['views'] = array( + '#type' => 'checkboxes', + '#options' => $views_options, + '#title' => t('Views'), + ); + } + + if (count($taxonomy_files) > 0) { + foreach ($taxonomy_files as $file) { + $taxonomy_options[$file->filename] = $file->name; + } + $form['vocabularies'] = array( + '#type' => 'checkboxes', + '#options' => $taxonomy_options, + '#title' => t('Taxonomy'), + ); + } + + + if (isset($form['views']) || isset($form['content_types']) || isset($form['vocabularies'])) { $form['import'] = array( - '#type' => 'submit', - '#value' => 'Import', - '#validate' => array('import_export_tool_types_views_import_validate')); + '#type' => 'submit', + '#value' => t('Import'), + ); } - + return $form; } -function import_export_tool_types_views_export() { - foreach (module_implements ('views_exportables') as $module) { - $function = $module .'_views_exportables'; - $exportables[$module] = $function ('list'); +function import_export_tool_export_form() { + $exportables = array(); + foreach (module_implements('views_exportables') as $module) { + $function = $module . '_views_exportables'; + $exportables += $function('list'); } - - foreach ($exportables['views'] as $view) { + foreach ($exportables as $view) { $views[$view['name']] = $view['name']; } - - $types = content_copy_types(); - - foreach ($types as $type_k => $type_v) { - $form['types'][$type_k]['value'] = array('#type' => 'checkbox', '#name' => $type_k .'_cck_', '#value' => $type_k, '#width' => 10000); - $form['types'][$type_k]['title'] = array('#type' => 'item', '#value' => $type_v); - } - - $form['header_cck'] = array('#type' => 'value', '#value' => array( - theme('table_select_header_cell'), - array('data' => t('CCK type Name'), 'field' => 'title'))); - - foreach ($views as $view_k => $view_v) { - $form['views'][$view_k]['value'] = array('#type' => 'checkbox', '#name' => $view_k .'_vw_', '#value' => $view_k); - $form['views'][$view_k]['title'] = array('#type' => 'item', '#value' => $view_v); - } - - $form['header_views'] = array('#type' => 'value', '#value' => array( - theme('table_select_header_cell'), - array('data' => t('View Name'), 'field' => 'title'))); - - $form['export'] = array( - '#type' => 'submit', - '#value' => 'Export', - '#validate' => array('import_export_tool_types_views_export_validate'), - '#weight' => 5); - + if (count($views) > 0) { + $form['views'] = array( + '#type' => 'checkboxes', + '#title' => t('Views'), + '#options' => $views, + ); + } + + $content_types = content_copy_types(); + if (count($content_types) > 0) { + $form['content_types'] = array( + '#type' => 'checkboxes', + '#title' => t('Content types'), + '#options' => $content_types, + ); + } + + $vocabulary_list = taxonomy_get_vocabularies(); + $vocabularies = array(); + foreach($vocabulary_list as $vocabulary) { + $vocabularies[$vocabulary->vid] = check_plain($vocabulary->name); + } + if (count($vocabularies) > 0) { + $form['vocabularies'] = array( + '#type' => 'checkboxes', + '#title' => t('Vocabularies'), + '#options' => $vocabularies, + ); + } + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Export'), + ); + return $form; } -function theme_import_export_tool_types_views_import($form) { - $rows_type = array(); - - foreach ($form['types'] as $key => $value) { - if ($key[0] == "#") continue; - $rows_type[] = array( - array('data' => drupal_render($form['types'][$key]['value'])), - array('data' => drupal_render($form['types'][$key]['title'])) - ); - } - - foreach ($form['views'] as $key => $value) { - if ($key[0] == "#") continue; - $rows_view[] = array( - array('data' => drupal_render($form['views'][$key]['value'])), - array('data' => drupal_render($form['views'][$key]['title'])) - ); - } - - $output .= theme('table', $form['header_cck']['#value'], $rows_type); - $output .= theme('table', $form['header_views']['#value'], $rows_view); +function theme_import_export_tool_import_form($form) { + $views_rows = $content_type_rows = array(); + if (is_array($form['views'])) { + $views_title = $form['views']['#title']; + unset($form['views']['#title']); + ksort($form['views']); + foreach (element_children($form['views']) as $position => $name) { + $title = $form['views'][$name]['#title']; + unset($form['views'][$name]['#title']); + $views_rows[] = array(drupal_render($form['views'][$name]), $title); + } + $output .= theme('table', array(theme('table_select_header_cell'), array('data' => $views_title, 'width' => '95%')), $views_rows); + } + + if (is_array($form['content_types'])) { + $content_types_title = $form['content_types']['#title']; + unset($form['content_types']['#title']); + ksort($form['content_types']); + foreach (element_children($form['content_types']) as $position => $name) { + $title = $form['content_types'][$name]['#title']; + unset($form['content_types'][$name]['#title']); + $content_type_rows[] = array(drupal_render($form['content_types'][$name]), $title); + } + $output .= theme('table', array(theme('table_select_header_cell'), array('data' => $content_types_title, 'width' => '95%')), $content_type_rows); + } + + if (is_array($form['vocabularies'])) { + $vocabularies_title = $form['vocabularies']['#title']; + unset($form['vocabularies']['#title']); + ksort($form['vocabularies']); + foreach (element_children($form['vocabularies']) as $position => $name) { + $title = $form['vocabularies'][$name]['#title']; + unset($form['vocabularies'][$name]['#title']); + $vocabularie_rows[] = array(drupal_render($form['vocabularies'][$name]), $title); + } + $output .= theme('table', array(theme('table_select_header_cell'), array('data' => $vocabularies_title, 'width' => '95%')), $vocabularie_rows); + } + $output .= drupal_render($form); return $output; } -function theme_import_export_tool_types_views_export($form) { - $rows_type = array(); - - foreach ($form['types'] as $key => $value) { - if ($key[0] == "#") continue; - $rows_type[] = array( - array('data' => drupal_render($form['types'][$key]['value'])), - array('data' => drupal_render($form['types'][$key]['title'])) - ); - } - - foreach ($form['views'] as $key => $value) { - if ($key[0] == "#") continue; - $rows_view[] = array( - array('data' => drupal_render($form['views'][$key]['value'])), - array('data' => drupal_render($form['views'][$key]['title'])) - ); - } - - $output .= theme('table', $form['header_cck']['#value'], $rows_type); - $output .= theme('table', $form['header_views']['#value'], $rows_view); - $output .= drupal_render($form); - return $output; +function theme_import_export_tool_export_form($form) { + return theme('import_export_tool_import_form', $form); +} + +function import_export_tool_import_form_validate($form, $form_state) { + if (count(array_filter($form_state['values']['content_types'])) == 0 && count(array_filter($form_state['values']['views'])) == 0 && count(array_filter($form_state['values']['vocabularies'])) == 0) { + form_set_error('content_types', t('Please select at least one content type or view.')); + } +} + +function import_export_tool_export_form_validate($form, $form_state) { + import_export_tool_import_form_validate($form, $form_state); } -function import_export_tool_types_views_import_validate($form, $form_state) { - $selected = $form_state['clicked_button']['#post']; - $types = array(); - $views = array(); - - foreach ($selected as $key => $value) { - if (strstr($key, '_vw_')) { - $view_temp = str_replace('_vw_', '', $key); - $views[$view_temp] = $view_temp; - } - elseif (strstr($key, '_cck_')) { - $type_temp = str_replace('_cck_', '', $key); - $types[$type_temp] = $type_temp; - } - } - - $total_checked = count($types) + count($views); - - if ($total_checked == 0) { - drupal_set_message('Please select at least 1 value', 'error'); - } -} - -function import_export_tool_types_views_export_validate($form, $form_state) { - $selected = $form_state['clicked_button']['#post']; - $types = array(); - $views = array(); - - foreach ($selected as $key => $value) { - if (strstr($key, '_vw_')) { - $view_temp = str_replace('_vw_', '', $key); - $views[$view_temp] = $view_temp; - } - elseif (strstr($key, '_cck_')) { - $type_temp = str_replace('_cck_', '', $key); - $types[$type_temp] = $type_temp; - } - } - - $total_checked = count($types) + count($views); - if ($total_checked == 0) { - drupal_set_message('Please select at least 1 value', 'error'); - } -} - -function import_export_tool_types_views_import_submit($form, &$form_state) { - $selected = $form_state['clicked_button']['#post']; - $types = array(); - $views = array(); - - foreach ($selected as $key => $value) { - if (strstr($key, '_vw_')) { - $view_temp = str_replace('_vw_', '', $key); - $views[$view_temp] = $view_temp; - } - elseif (strstr($key, '_cck_')) { - $type_temp = str_replace('_cck_', '', $key); - $types[$type_temp] = $type_temp; - } - } - - $dir = variable_get('import_export_tool_path', file_directory_temp() .'/store'); - - if (count ($types) > 0) { - foreach ($types as $type_name) { - $dump = file_get_contents($dir .'/'. $type_name .'.cck'); - $import_dump = ' array(), + 'finished' => '', + 'title' => t('Importing from files'), + 'init_message' => t('Starting the import'), + 'progress_message' => t('Imported @current out of @total'), + 'error_message' => t('An error occurred and some or all of the imports have failed.'), + ); + + if (count($form_state['values']['content_types']) > 0) { + foreach ($form_state['values']['content_types'] as $filepath) { + if ($filepath) { + $batch['operations'][] = array('import_export_tool_import_content_type', array($filepath)); + } } } - - if (count($views) > 0) { + + if (count($form_state['values']['views']) > 0) { views_include('view'); - foreach ($views as $view_name) { - $dump = file_get_contents($dir .'/'. $view_name .'.view'); - ob_start(); - eval($dump); - ob_end_clean(); - $view->save(); - $message = "View : ". $view->name ." has been imported"; - drupal_set_message($message, 'status'); + foreach ($form_state['values']['views'] as $filepath) { + if ($filepath) { + $batch['operations'][] = array('import_export_tool_import_view', array($filepath)); + } } } -} -function import_export_tool_types_views_export_submit($form, &$form_state) { - $selected = $form_state['clicked_button']['#post']; - $types = array(); - $views = array(); - - foreach ($selected as $key => $value) { - if (strstr($key, '_vw_')) { - $view_temp = str_replace('_vw_', '', $key); - $views[$view_temp] = $view_temp; - } - elseif (strstr($key, '_cck_')) { - $type_temp = str_replace('_cck_', '', $key); - $types[$type_temp] = $type_temp; - } - } - - if (count($types) > 0 || count($views) > 0) { - $path = variable_get('import_export_tool_path', file_directory_temp() .'/store'); - if (!is_dir($path)) { - mkdir($path, 0777); - } - } - - if (count($types) > 0) { - foreach ($types as $type_index => $type_name) { - $fields = array_keys(content_copy_fields($type_name)); - if (module_exists('fieldgroup')) { - $groups = content_copy_groups($type_name); - } - $frm['type_name'] = $type_name; - foreach ($groups as $group_index => $group_name) { - $frm['groups'][] = $group_name; - } - foreach ($fields as $field_index => $field_name) { - $frm['fields'][] = $field_name; - } - $dump = content_copy_export($frm); - $filename = $type_name .'.cck'; - if (!$handle = fopen($path .'/'. $filename, 'a')) { - drupal_set_message("Cannot open file ($filename)", 'error', TRUE); - } - if (fwrite($handle, $dump) === FALSE) { - drupal_set_message("Cannot write to file ($filename)", 'error', TRUE); + if (count($form_state['values']['vocabularies']) > 0) { + foreach ($form_state['values']['vocabularies'] as $filepath) { + if ($filepath) { + $batch['operations'][] = array('import_export_tool_import_vocabulary', array($filepath)); } - else{ - $message = 'CCK Export Complete'; - drupal_set_message($message, 'status', TRUE); - } - fclose($handle); } } - - $tmp_views = $form_state['values']['views']; - $all_views = views_get_all_views(); - - if (count($views) > 0) { - foreach ($views as $view_name) { - $export_dump = $all_views[$view_name]->export(); - $filename = $view_name .'.view'; - if (!$handle = fopen($path .'/'. $filename, 'a')) { - drupal_set_message("Cannot open file ($filename)", 'error', TRUE); - } - if (fwrite($handle, $export_dump) === FALSE) { - drupal_set_message("Cannot write to file ($filename)", 'error', TRUE); - } - else{ - $message = 'VIEWS Export Complete'; - drupal_set_message($message, 'status', TRUE); + + if (!empty($batch['operations'])) { + batch_set($batch); + } +} + +function import_export_tool_export_form_submit($form, &$form_state) { + $export_dir = import_export_tool_get_directory(); + // Create the directory. Return FALSE on failure. + if (!file_check_directory($export_dir, FILE_CREATE_DIRECTORY) && !mkdir($export_dir, 0775, TRUE)) { + drupal_set_message(t('Failed to create export directory: %dir', array('%dir' => $export_dir)), 'error'); + return FALSE; + } + + // Batch API + $batch = array('operations' => array(), + 'finished' => '', + 'title' => t('Exporting to files'), + 'init_message' => t('Starting the export'), + 'progress_message' => t('Exported @current out of @total'), + 'error_message' => t('An error occurred and some or all of the exports have failed.'), + ); + + foreach ($form_state['values']['content_types'] as $type) { + if ($type) { + // Batch processing content types is proving to be difficult, disabled for now + $batch['operations'][] = array('import_export_tool_export_content_type', array($type, $export_dir)); + } + } + + foreach ($form_state['values']['views'] as $view) { + if ($view) { + $batch['operations'][] = array('import_export_tool_export_view', array($view, $export_dir)); + } + } + + if (count($form_state['values']['vocabularies']) > 0) { + foreach ($form_state['values']['vocabularies'] as $vid) { + if (!empty($vid)) { + $batch['operations'][] = array('import_export_tool_export_vocabulary', array($vid, $export_dir)); } - fclose($handle); } } - + + if (!empty($batch['operations'])) { + batch_set($batch); + } + } - -function install_content_copy_import_from_file($file) { + +function import_export_tool_export_content_type($type, $export_dir) { + // Pause the batch + // http://drupal.org/node/283594 + $batch =& batch_get(); + $ref = $batch; + $batch = null; + + $fields = content_copy_fields($type); + $groups = array(); + if (module_exists('fieldgroup')) { + $groups = fieldgroup_groups($type); + } + $prototype['type_name'] = $type; + foreach ($groups as $group) { + $prototype['groups'][] = $group['group_name']; + } + foreach ($fields as $field_index => $field_name) { + $prototype['fields'][] = $field_name; + } + $dump = content_copy_export($prototype); + $filepath = "{$export_dir}/{$type}.cck"; + + // Unpause the batch + $batch = $ref; + + // Write the .cck file. Return FALSE on failure. + if (!file_save_data($dump, $filepath, FILE_EXISTS_REPLACE)) { + drupal_set_message(t('Failed to write .cck file: %dir', array('%dir' => $filepath)), 'error'); + return FALSE; + } + else{ + drupal_set_message(t('Wrote content type file: %filepath', array('%filepath' => $filepath)), 'status', TRUE); + } +} + +function import_export_tool_export_view($view, $export_dir) { + $all_views = views_get_all_views(); + $dump = $all_views[$view]->export(); + $filepath = "{$export_dir}/{$view}.views"; + // Write the .views file. Return FALSE on failure. + if (!file_save_data($dump, $filepath, FILE_EXISTS_REPLACE)) { + drupal_set_message(t('Failed to write .views file: %dir', array('%dir' => $filepath)), 'error'); + return FALSE; + } + else{ + drupal_set_message(t('Wrote views file: %filepath', array('%filepath' => $filepath)), 'status', TRUE); + } +} + +function import_export_tool_export_vocabulary($vid, $export_dir) { + $vocabulary = taxonomy_vocabulary_load($vid); + $vocabulary_export = "\$taxonomy['vocabulary'] = " . var_export((array) $vocabulary, TRUE) . ";\n"; + $terms = taxonomy_get_tree($vid); + $vocabulary_terms = array(); + $vocabulary_export .= "\$taxonomy['terms'] = array(\n"; + foreach ($terms as $index => $term) { + $vocabulary_export .= $index . " =>\n" . var_export((array) $term, TRUE) . ",\n"; + } + $vocabulary_export .= ");\n"; + //$filename = strtolower(str_replace(" ", "_", $vocabulary->name)); + $filename = strtolower(preg_replace(array("/\s/", "/[^A-Za-z0-9_]/"), array("_", ""), $vocabulary->name)); + $filepath = "{$export_dir}/{$filename}.taxonomy"; + if (!file_save_data($vocabulary_export, $filepath, FILE_EXISTS_REPLACE)) { + drupal_set_message(t('Failed to write taxonomy file: %filepath', array('%filepath' => $filepath)), 'error'); + return FALSE; + } + else{ + drupal_set_message(t('Wrote taxonomy file: %filepath', array('%filepath' => $filepath))); + } +} + +function import_export_tool_import_content_type($filepath) { + // Pause the batch + // http://drupal.org/node/283594 + $batch =& batch_get(); + $ref = $batch; + $batch = null; + + $macro = file_get_contents($filepath); + $basename = basename($filepath, '.cck'); + $types = node_get_types(); + if (in_array($basename, array_keys($types))) { + $state['values']['type_name'] = $basename; + } + else { + $state['values']['type_name'] = ''; + } + $state['values']['macro'] = $macro; + $state['values']['op'] = t('Import'); + drupal_execute('content_copy_import_form', $state); + + // Unpause the batch + $batch = $ref; +} + +function import_export_tool_import_view($filepath) { + $dump = file_get_contents($filepath); ob_start(); - include $file; + eval($dump); ob_end_clean(); - $form_state['values']['type_name'] = ''; - $form_state['values']['macro'] = '$content = '. var_export($content, 1) .';'; - $form_state['values']['op'] = t('Import'); - drupal_execute('content_copy_import_form', $form_state); -} \ No newline at end of file + $view->save(); + return $view->name; +} + +function import_export_tool_import_vocabulary($filepath) { + $vocabulary = file_get_contents($filepath); + eval($vocabulary); + // If the vocabulary does not exist, we will assume that it should be added with + // the same vid as the import file. This will ensure that future imports will + // update the vocabulary to reflect changes in the import file. + if (!taxonomy_vocabulary_load($taxonomy['vocabulary']['vid'])) { + db_query("INSERT INTO {vocabulary} VALUES (%d, '%s', '', '', 0, 0, 0, 0, 0, 'taxonomy', 0)", $taxonomy['vocabulary']['vid'], $taxonomy['vocabulary']['name']); + } + $status = taxonomy_save_vocabulary($taxonomy['vocabulary']); + $status_message = ($status == 2) ? t("Taxonomy vocabulary %name updated", array('%name' => $taxonomy['vocabulary']['name'])) : t("Taxonomy vocabulary %name created", array('%name' => $taxonomy['vocabulary']['name'])); + drupal_set_message($status_message); + + foreach($taxonomy['terms'] as $term) { + // If the term does not exist, we will assume that it should be added with + // the same tid as the import file. This will ensure that future imports will + // update the term to reflect changes in the import file. + if (!taxonomy_get_term($term['tid'])) { + db_query("INSERT INTO {term_data} VALUES (%d, %d, '%s', '%s', %d)", $term['tid'], $taxonomy['vocabulary']['vid'], $term['name'], $term['description'], $term['weight']); + } + // For some reason the cck form uses the key "parent" while the + // cck array uses the key "parents", so we need to modify here + if (!empty($term['parents']) || !isset($term['parents'])) { + $term['parent'] = array(); + $term['parent'] = $term['parents']; + } + $status = taxonomy_save_term($term); + $status_message = ($status == 2) ? t("Taxonomy term %name updated", array('%name' => $term['name'])) : t("Taxonomy term %name created", array('%name' => $term['name'])); + drupal_set_message($status_message); + } +}