<?php
/**
 * Generated: 2014-03-24 15:01:35
 * @author Rtn_Module_Tool
 * @descrition Odpowiad za widoczne formularze w systemie.
 */
class Forms_Controller_Admin_FieldsController extends Eppe_Crud_Controller_Action {
	
	protected $grid = 'Forms_Jqgrid_Admin_Fields';
	protected $crud = 'Forms_CRUD_Admin_Fields';
	
	public function init() {
                parent::init();
        }
	
	public function indexAction() {
		
		if ($this->isAjax) {
			$this->ajaxResponse->data = $this->grid->render();
		}
		else {
			$this->view->grid = $this->grid->render();
		}
	}
	
	protected function _add() {
		$formClassName = $this->crud->getFormClassName();

		$form = new $formClassName();

		if ($this->_isPost()) {
			$post = $this->_getPost();
			if (isset($post['Anuluj'])) {
				$$this->_redirectIndex();
			}

			if ($form->isValid($post) && $beanId = $this->crud->create($post)) {
				$this->_addMessage(__('Zmiany zostały zapisane'));
				$this->_redirectIndex();
			}
			else {
				$form->populate($this->_getPost());
			}
		}

		$this->view->form = $form;
	}
	
	protected function _edit($beanId) {
		$bean = $this->crud->findBean($beanId);

		$formClassName = $this->crud->getFormClassName();

		$form = new $formClassName(array('bean' => $bean));

		if ($this->_isPost()) {
			$post = $this->_getPost();

			if (isset($post['Anuluj'])) {
				$$this->_redirectIndex();
			}

			if ($form->isValid($post)) {
				if ($this->crud->update($beanId, $post)) {
					$this->_addMessage(__('Zmiany zostały zapisane'));
					$this->_redirectIndex();
				}
			}
			else {
				$form->populate($this->_getPost());
			}
		} else {
			$form->populateWithBean($bean);
		}
		
		$this->view->form = $form;
		
	}
}