Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function misc_menu() {
- $items = array();
- $items['taxonomy-transform'] = array(
- 'page callback' => 'misc_taxonomy_transform',
- 'access arguments' => array('administer nodes'),
- 'type' => MENU_CALLBACK,
- );
- return $items;
- }
- function misc_taxonomy_transform() {
- ob_start();
- echo '<pre>';
- /* $stranky = db_query('DELETE FROM term_data WHERE vid = 5'); */
- /* $stranky = db_query('TRUNCATE TABLE term_hierarchy'); */
- /*
- $test = node_load(1246, NULL, TRUE);
- _misc_generate_taxonomy($test);
- */
- $stranky = db_query('SELECT n.nid as nid FROM {node} n WHERE n.type = "stranka" ');
- while($stranka = db_fetch_array($stranky)) {
- $node = node_load($stranka['nid'], NULL, TRUE);
- $term = _misc_generate_taxonomy($node);
- $node->taxonomy[] = $term;
- /* print_r($term); */
- node_save($node);
- echo '<br>' . $node->title;
- $i++;
- }
- echo '<br><br>hotovo ' . $i;
- echo '</pre>';
- $res = ob_get_contents();
- ob_end_clean();
- return $res;
- }
- function _misc_generate_taxonomy($node, $depth = 0) {
- $parent_nid = $node->nodehierarchy_menu_links[0]['pnid'];
- $weight = $node->nodehierarchy_menu_links[0]['weight'];
- $parent = node_load($parent_nid, NULL, TRUE);
- $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
- $depth--;
- $new_term = array(
- 'vid' => 5, // Voacabulary ID
- 'name' => $node->title, // Term Name
- 'parent' => $last_tid,
- 'weight' => $weight
- );
- if($depth == 0) {
- // vratim pole taxonomickeho terminu
- $new_term['tid'] = $last_tid;
- $new_term['name'] = $parent->title;
- return $new_term;
- }
- if($depth > 0) {
- $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
- if(!empty($existing_term)) {
- $tid = $existing_term;
- } else {
- taxonomy_save_term($new_term); // ulozim
- $tid = db_result(db_query('SELECT tid FROM {term_data} ORDER BY tid DESC LIMIT 1'));
- }
- return $tid;// vratim posledni tid
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement