Return to Snippet

Revision: 30984
at August 26, 2010 22:26 by sveggiani


Updated Code
function admin_reorder( $id = false ) {

		$this->autoRender = false;

		// obtengo el parent para ordenar por el
		$itemParent = $this->Category->getparentnode( $id );

		// si el contenido tiene un padre...
		if ( !empty( $itemParent['Category']['id'] ) ) :

			# CakeLog::write('debug', 'Ordenando un nodo de nivel > 0');

			// ordeno el padre
			$this->Category->reorder( array( 'id' => $itemParent['Category']['id'], 'field' => 'order_by', 'order' => 'ASC' ) );

		else:
		
			# CakeLog::write('debug', 'Ordenando un nodo de nivel = 0');
		
			// si no tiene padre traigo todos los contenidos sin padre y los ordeno
			$nodes = $this->Category->find('all', array( 'conditions' => array(
				'OR' => array(
					'Category.parent_id' => 0,
					'Category.parent_id IS NULL'
				)
			), 'order' => 'Category.order_by ASC' )  ); //pr( $nodes );
			
			// va tomando nodo por nodo y lo pone al final del arbol
			if ( $nodes ) {
				foreach ( $nodes as $node ) {
					$this->Category->moveDown( $node['Category']['id'], true);
				}
			}

			// si encuentra un error intenta recuperar el el arbol
			if ( !$this->Category->verify() ) :
				CakeLog::write('debug', 'Error al ordenar arbol de categorías con el ID: ' . $id .  '. Intentando recuperar...');
				$this->Category->recover('parent');
			endif;

		endif;

		$this->redirect('index');

	}

Revision: 30983
at August 26, 2010 21:52 by sveggiani


Initial Code
function admin_reorder( $id = false ) {
		
		$this->autoRender = false;

		// obtengo el parent para ordenar por el
		$itemParent = $this->Content->getparentnode( $id );

		// si el contenido tiene un padre...
		if ( !empty( $itemParent['Content']['id'] ) ) :

			// ordeno el padre
			$this->Content->reorder( array( 'id' => $itemParent['Content']['id'], 'field' => 'menu_order', 'order' => 'ASC' ) );

		else:
			// si no tiene padre traigo todos los contenidos sin padre y los ordeno
			$nodes = $this->Content->find('all', array('conditions' => array( 'Content.parent_id = 0' ), 'order' => 'Content.menu_order ASC' )  ); //pr( $nodes );
			
			// va tomando nodo por nodo y lo pone al final del arbol
			if ( $nodes ) {
				foreach ($nodes as $node ) {
					$this->Content->moveDown( $node['Content']['id'], true); //pr( $node['Content']['title'] );
				}
			}

			// si encuentra un error intenta recuperar el el arbol
			if ( !$this->Content->verify() ) :
				CakeLog::write('debug', 'Error al ordenar arbol de conenidos con el ID: ' . $id .  '. Intentando recuperar');
				$this->Content->recover('parent');
			endif;

		endif;

		$this->redirect('index');
	}

Initial URL


Initial Description


Initial Title
method to sort a tree node even with empty parents

Initial Tags
cakephp

Initial Language
PHP