<?php

/**
 * Description of Action
 *
 * @author arson
 */
class Eppe_Crud_Tree_Controller_Action extends Eppe_Crud_Controller_Action {

	public function editAction() {
		$dba = Zend_Db_Table::getDefaultAdapter();
		$dba->beginTransaction();
		try {
			if($this->_edit()){
				$dba->commit();
				$this->_redirectIndex();				
			}
		}
		catch (Exception $e) {
			$dba->rollBack();
			throw $e;
		}
	}
	
	
	public function addAction() {
		$dba = Zend_Db_Table::getDefaultAdapter();
		$dba->beginTransaction();
		try {
			if($this->_add()){
				$dba->commit();
				$this->_redirectIndex();				
			}
		}
		catch (Exception $e) {
			$dba->rollBack();
			throw $e;
		}
	}

	/**
	 * Wczytywanie danych grida 
	 */
	public function ajaxLoadDatagridAction() {
		$this->_disableView();

		$model = new $this->dbModelName();

		$rows = array();
		foreach ($model->getTree() as $row) {
			$rows[] = array(
				'id' => $row->id,
				'name' => $row->name,
				'lft' => $row->left,
				'rgt' => $row->right,
				'level' => $row->depth
			);
		}

		foreach(array_keys($rows) as $row_k){
			$rows[$row_k] = $this->_extraTransformRow($rows[$row_k]);
			
			$rows[$row_k]['_grid_extra']['can_edit'] = $this->_canRowBeEdited($rows[$row_k]);
			$rows[$row_k]['_grid_extra']['can_clone'] = $this->_canRowBeCloned($rows[$row_k]);
			$rows[$row_k]['_grid_extra']['can_delete'] = $this->_canRowBeDeleted($rows[$row_k]);
		}		
		
		$grid_params = Eppe_Jqgrid_Helper::get_prss();

		$response = Eppe_Jqgrid_Helper::render_response(count($rows), $grid_params['rows'], $grid_params['page'], $rows);

		echo $response;
	}	
	
}

?>
