Pastoid

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

This paste was last updated on March 1, 2010 at 12:39 pm.

Pasted Coderaw

	/**
	 * 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, $target_term = null, $before = FALSE )
	{
		// 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() ) {
 
			$source_left = $term->mptt_left;
			$source_right = $term->mptt_right;
			$shift = $source_right - $source_left + 1;
 
			$nodes_moving = ( $shift ) / 2; // parent and descendants
 
			// we also need the destination right.
 
 			DB::begin_transaction();
 
			if ( $this->hierarchical ) {
				if ( null == $target_term ) {
					// If no target is specified, put the new term after the last term
					$ref = DB::get_value( 'SELECT mptt_right FROM {terms} WHERE vocabulary_id=? ORDER BY mptt_right DESC LIMIT 1', array( $this->id ) );
					$offset = $ref + 1;
				}
				else {
					if ( FALSE == $before ) {
						$ref = $target_term->mptt_right - 1; // - $shift; // parent
						$offset = $target_term->mptt_right;
					}
					else {
						$ref = $target_term->mptt_left - 1; // - $shift ; // before
						$offset = $target_term->mptt_left + ( $target_term->mptt_left < $source_left ) * $shift; // only increase when target is to the left of the source
					}
				}
Utils::debug( "ref = $ref" );
Utils::debug( "offset = $offset" );
			} 
			else {
				// vocabulary is not hierarchical
				if ( FALSE != $before ) {
					$ref = $target_term->mptt_left - 1; // - $shift;
				}
				else {
					// send it all the way to the right.
					$ref = DB::get_value( 'SELECT mptt_right FROM {terms} WHERE vocabulary_id=? ORDER BY mptt_right DESC LIMIT 1', array($this->id) );
					$offset = $ref + 1;
// /* would this be faster? */ 		$destination_right = DB::get_value( 'SELECT MAX(mptt_right) FROM {terms} WHERE vocabulary_id=?', array($this->id) );
				}
			}
 
			// 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.
Utils::debug( $term );
			$res = $this->displace_term( $term, ( -1 - $source_right ) ); 
 			if( ! $res ) {
 				DB::rollback();
// Utils::debug( "failed on first displace" );
				return FALSE;
			}
// Utils::debug( DB::get_results( 'SELECT term, mptt_left, mptt_right from {terms} where vocabulary_id = 2 order by mptt_left ' ) );
 
			// move the nodes between source and end of the vocabulary
			$params = array( $shift, $this->id, $source_left );
			$res = DB::query( 'UPDATE {terms} SET mptt_left=mptt_left-? WHERE vocabulary_id=? AND mptt_left > ?', $params );
			if( ! $res ) {
 				DB::rollback();
// Utils::debug( "failed on first move left" );
				return FALSE;
			}
// Utils::debug( DB::get_results( 'SELECT term, mptt_left, mptt_right from {terms} where vocabulary_id = 2 order by mptt_left ' ) );
 
			$params = array( $shift, $this->id, $source_right ); // just repeat it?
			$res = DB::query( 'UPDATE {terms} SET mptt_right=mptt_right-? WHERE vocabulary_id=? AND mptt_right > ?', $params );
			if( ! $res ) {
 				DB::rollback();
// Utils::debug( "failed on first move right" );
				return FALSE;
			}
// Utils::debug( DB::get_results( 'SELECT term, mptt_left, mptt_right from {terms} where vocabulary_id = 2 order by mptt_left ' ) );
 
			// make room for the displaced nodes
			$params = array( $shift, $this->id, $ref - $shift );
			$res = DB::query( 'UPDATE {terms} SET mptt_left=mptt_left+? WHERE vocabulary_id=? AND mptt_left > ? and mptt_left > 0', $params );
			if( ! $res ) {
 				DB::rollback();
// Utils::debug( "failed on second move left" );
				return FALSE;
			}
// Utils::debug( DB::get_results( 'SELECT term, mptt_left, mptt_right from {terms} where vocabulary_id = 2 order by mptt_left ' ) );
 
			$params = array( $shift, $this->id, $ref - $shift ); // <- this is the last bug
			$res = DB::query( 'UPDATE {terms} SET mptt_right=mptt_right+? WHERE vocabulary_id=? AND mptt_right > ? AND mptt_right > 0', $params );
			if( ! $res ) {
 				DB::rollback();
// Utils::debug( "failed on second move right" );
				return FALSE;
			}
// Utils::debug( DB::get_results( 'SELECT term, mptt_left, mptt_right from {terms} where vocabulary_id = 2 order by mptt_left ' ) );
 
			// Move the existing nodes back into the space left for them
			// Cannot finish before redoing this to bring the terms positive again.
 
Utils::debug( $this->get_term( $term ) );
			// $term needs to be updated with the new values
			$res = $this->displace_term( $this->get_term( $term ), $offset ); 
			if( ! $res ) {
 				DB::rollback();
// Utils::debug( "failed on second displace" );
				return FALSE;
			}
Utils::debug( DB::get_results( 'SELECT term, mptt_left, mptt_right from {terms} where vocabulary_id = 2 order by mptt_left ' ) );
 
			// Success!
 			DB::commit();
// Utils::debug( DB::get_results( 'SELECT term, mptt_left, mptt_right from {terms} where vocabulary_id = 2 order by mptt_left ' ) );
 
			return $term; // need to return something, but what? The term in its new place?
 
		}
		return FALSE; // probably should put this in an else.
	}
 
	 /**
	  * Temporarily set a term and its descendants aside from the rest of a vocabulary.
	  * If this is not done a second time, corruption will result.
	  * make private, only public for testing
	  **/
	public function displace_term( $term, $displacement )
	{
Utils::debug( "Displacing by $displacement" );
		// took this from delete_term below
		if ( is_string( $term ) ) { 
			$term = $this->get_term( $term );
		}
 
		$params = array( $displacement, $displacement, $this->id, $term->mptt_left, $term->mptt_right );
		return DB::query( 'UPDATE {terms} SET mptt_left = mptt_left + ?, mptt_right = mptt_right + ? WHERE vocabulary_id = ? AND mptt_left BETWEEN ? AND ?', $params );
	}

Toggle wordwrap

Pasted Coderaw

/* this worked, run one line at a time */
$x = Vocabulary::get( 'categories' );
$x->move_term( $x->get_term( 'B' ), $x->get_term( 'F' ), true );
$x->displace_term( 'B', -8 );
$params = array( 6, $x->id, 2 );
$res = DB::query( 'UPDATE {terms} SET mptt_left=mptt_left-? WHERE vocabulary_id=? AND mptt_left > ?', $params );
$res = DB::query( 'UPDATE {terms} SET mptt_right=mptt_right-? WHERE vocabulary_id=? AND mptt_right > ?', $params );
$params = array( 6, $x->id, 2 );
$res = DB::query( 'UPDATE {terms} SET mptt_left=mptt_left+? WHERE vocabulary_id=? AND mptt_left > ?', $params );
$res = DB::query( 'UPDATE {terms} SET mptt_right=mptt_right+? WHERE vocabulary_id=? AND mptt_right > ?', $params );
$x->displace_term( 'B', 9 );
 
/* this didn't work */
$x->move_term( $x->get_term( 'B' ), $x->get_term( 'F' ), true );
 

Toggle wordwrap

Pasted Coderaw

//create the vocabulary in hconsole
$x = Vocabulary::get( 'categories' );
$x->add_term( 'A' );
$x->add_term( 'G' );
$x->add_term( 'H' );
$x->add_term( 'B',$x->get_term('A') );
$x->add_term( 'E',$x->get_term('A') );
$x->add_term( 'C',$x->get_term('B') );
$x->add_term( 'D',$x->get_term('B') );
$x->add_term( 'F',$x->get_term('E') );

Toggle wordwrap

Referring DomainHits
Unknown Referer 190
pastoid.com 16
Is this paste spam?
<Hide