Pastoid

The page you are looking at now is at this URL: http://pastoid.com/b0a

This paste was last updated on February 18, 2010 at 12:13 am.

Pasted Coderaw

			$moving_right = $term->mptt_right;
 
			$nodes_moving = 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
			// Cannot finish before redoing this to bring the terms positive again.
			$res = displace_term( $term ); 
			if( ! $res ) {
				DB::rollback();
				return FALSE;
			}
 
			// set up the where clauses
			// determine if the source node is left of the destination node
			if( $source_node->mptt_left < $destination_node->mptt_left )
			{
				$difference = $nodes_moving;
			}
			else { // source_node > destination_node 
				$difference = -$nodes_moving;
			}
			// should we update the moved nodes now? Why not?
 
			$between_lefts = array( $source_node->mptt_left, $destination_node->mptt_left );
			sort( $between_lefts ); // must be in numeric order for BETWEEN
 
			$between_rights = array( $source_node->mptt_right, $destination_node->mptt_right );
			sort( $between_rights ); // must be in numeric order for BETWEEN
 
// $target_right = something....
			// Shift the nodes that need to be moved, now that the others are out of the way
 
			// first, update mptt_left
			$params = array_merge( array( $difference, $this->id ), $between_lefts );
			$res = DB::query( 'UPDATE {terms} SET mptt_left=mptt_left+? WHERE vocabulary_id=? AND mptt_left BETWEEN ? AND ?', $params );
			if( ! $res ) {
				DB::rollback();
				return FALSE;
			}
 
			// second, update mptt_right
			$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_right BETWEEN ? AND ?', $params );
			if( ! $res ) {
				DB::rollback();
				return FALSE;
			}
 
			// 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.
	}
 

Toggle wordwrap

Referring DomainHits
Unknown Referer 127
pastoid.com 2
Is this paste spam?
<Hide