/** * Moves a term within the vocabulary. Returns a Term object. null parameters append the term to the end of any hierarchies. * @return Term The Term object added **/ public function move_term($term, $parent_term = null, $before_term = null) { // We assume that the arguments passed are valid terms. Check them before calling this. // If there are terms in the vocabulary, work out the reference point if ( !$this->is_empty() ) { $moving_left = $term->mptt_left; $moving_right = $term->mptt_right; $nodes_moved = count( $term->descendants() ) + 1; DB::begin_transaction(); if ( $this->hierarchical ) { // Check first if this is to be inserted to the left of a term if ( null != $before_term ) { $before_left = $before_term->mptt_left; // there should be no need to check for parent term if before_term is being used. } else { // If no parent is specified, put the new term after the last term if ( null != $parent_term ) { $parent_term_left = $parent_term->mptt_left; $parent_term_right = $parent_term->mptt_right; } } } else { // check before term only. parent should be null. Or do we throw an exception? if ( null != $before_term ) { $before_left = $before_term->mptt_left; } else { // send it all the way to the right. $rightmost_right = DB::get_value( 'SELECT mptt_right FROM {terms} WHERE vocabulary_id=? ORDER BY mptt_right DESC LIMIT 1', array($this->id) ); // /* would this be faster? */ $rightmost_right = DB::get_value( 'SELECT MAX(mptt_right) FROM {terms} WHERE vocabulary_id=?', array($this->id) ); // once we have this, we can subtract number of nodes * 2 from everything between source_mptt_left and this, then start renumbering and end up at this. } } // Move the existing nodes out of the way by making mptt_left and mptt_right negative // $target_right = something.... // Figure out where to make the space ... does this need to be separate updates? $res = DB::query('UPDATE {terms} SET mptt_left=mptt_left+2 WHERE vocabulary_id=? AND mptt_left>?', $params); if( ! $res ) { DB::rollback(); return FALSE; } $params = array( $nodes_moved, $nodes_moved, $this->id, $moving_right, $target_right ); // not sure about that last one. $res = DB::query('UPDATE {terms} SET mptt_right=mptt_right-? WHERE vocabulary_id=? AND mptt_left>? AND mptt_rightmptt_left = $ref + 1; $new_term->mptt_right = $ref + 2; */ // Update the node $result = $moving_term->update(); // update() not insert(), right? if ( $result ) { DB::commit(); return $moving_term; } else { DB::rollback(); return FALSE; } } return FALSE; // probably should put this in an else. }