Advertisement
Guest User

Drupal: migration from NodeHierarchy to Taxonomy

a guest
Feb 6th, 2011
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.21 KB | None | 0 0
  1. function misc_menu() {
  2.   $items = array();
  3.   $items['taxonomy-transform'] = array(
  4.     'page callback' => 'misc_taxonomy_transform',
  5.     'access arguments' => array('administer nodes'),
  6.     'type' => MENU_CALLBACK,
  7.   );
  8.  
  9.   return $items;
  10. }
  11.  
  12. function misc_taxonomy_transform() {
  13.     ob_start();
  14.    
  15.     echo '<pre>';
  16.    
  17. /*  $stranky = db_query('DELETE FROM term_data WHERE vid = 5'); */
  18. /*  $stranky = db_query('TRUNCATE TABLE term_hierarchy'); */
  19.    
  20. /*
  21.     $test = node_load(1246, NULL, TRUE);
  22.     _misc_generate_taxonomy($test);
  23. */
  24.    
  25.     $stranky = db_query('SELECT n.nid as nid FROM {node} n WHERE n.type = "stranka" ');
  26.     while($stranka = db_fetch_array($stranky)) {
  27.         $node = node_load($stranka['nid'], NULL, TRUE);
  28.         $term = _misc_generate_taxonomy($node);
  29.         $node->taxonomy[] = $term;
  30. /*      print_r($term); */
  31.         node_save($node);
  32.        
  33.         echo '<br>' . $node->title;
  34.         $i++;
  35.     }
  36.     echo '<br><br>hotovo ' . $i;
  37.    
  38.     echo '</pre>';
  39.    
  40.     $res = ob_get_contents();
  41.     ob_end_clean();
  42.     return $res;
  43. }
  44.  
  45. function _misc_generate_taxonomy($node, $depth = 0) {
  46.    
  47.     $parent_nid = $node->nodehierarchy_menu_links[0]['pnid'];
  48.     $weight = $node->nodehierarchy_menu_links[0]['weight'];
  49.     $parent = node_load($parent_nid, NULL, TRUE);
  50.    
  51.     $last_tid = !empty($parent) ? _misc_generate_taxonomy($parent, ++$depth) : 0; // Pokud jsme na nejvyssi urovni, rodic je 0, jinak rekurze a rodic je TID posledniho vlozeneho terminu
  52.     $depth--;
  53.        
  54.     $new_term = array(
  55.         'vid' => 5, // Voacabulary ID
  56.     'name' => $node->title, // Term Name
  57.     'parent' => $last_tid,
  58.     'weight' => $weight
  59.     );
  60.    
  61.     if($depth == 0) {
  62.         // vratim pole taxonomickeho terminu
  63.         $new_term['tid'] = $last_tid;
  64.         $new_term['name'] = $parent->title;
  65.         return $new_term;
  66.     }
  67.    
  68.     if($depth > 0) {
  69.         $existing_term = db_result(db_query('SELECT d.tid FROM {term_data} d LEFT JOIN {term_hierarchy} h ON d.tid = h.tid WHERE d.name = "%s" AND h.parent = %d', $node->title, $last_tid));// Zkontroluju, jestli již existuje
  70.         if(!empty($existing_term)) {
  71.             $tid = $existing_term;
  72.         } else {   
  73.             taxonomy_save_term($new_term); // ulozim
  74.             $tid = db_result(db_query('SELECT tid FROM {term_data} ORDER BY tid DESC LIMIT 1'));
  75.         }
  76.         return $tid;// vratim posledni tid
  77.        
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement