<?php

/**
 * Generated: 2014-03-24 15:01:35
 * @author Rtn_Module_Tool
 * @descrition Odpowiad za widoczne formularze w systemie.
 */
class Forms_Controller_Admin_IndexController extends Eppe_Crud_Controller_Action {

	protected $grid = 'Forms_Jqgrid_Admin_Index';
	protected $crud = 'Forms_CRUD_Admin_Index';

	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 (isset($post['Pola'])) {
				if ($form->isValid($post)) {
					if ($this->crud->create($post)) {
						$this->_addMessage(__('Zmiany zostały zapisane'));
					}
				}
				if($this->isAjax){
					$this->ajaxResponse->evalJS = 'ajx.loader.show(); window.location = "'.$this->_getFieldsUrl(). '"';
				}else{
					$this->_redirect($this->_getFieldsUrl());
				}
			}

			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 (isset($post['Pola'])) {
				if ($form->isValid($post)) {
					if ($this->crud->update($beanId, $post)) {
						$this->_addMessage(__('Zmiany zostały zapisane'));
					}
				}
				if($this->isAjax){
					$this->ajaxResponse->evalJS = 'ajx.loader.show(); window.location = "'.$this->_getFieldsUrl(). '"';
				}else{
					$this->_redirect($this->_getFieldsUrl());
				}
			}

			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;
	}
	
	protected function _getFieldsUrl(){
		$module = $this->_request->getModuleName();
		$controller = $this->_request->getControllerName();
		$action = 'fields';
		$id = $this->_request->id;

		return $this->view->url(array('module' => $module, 'controller' => $controller, 'action' => $action, 'id' => $id), null, true);
	}
	
	public function fieldsAction(){
		$bean = R::findOne('forms', "WHERE `id` = {$this->getParam('id')} ORDER BY `id` desc");
		
		$formFieldsBeans = R::findAll('viewformfields', "WHERE `active` = 1 ORDER BY `inputname`");
		
		$formFormFieldsBeans = R::findAll('formformfields', "WHERE `forms_id` = {$this->getParam('id')} ORDER BY `order`");
		$fields = array();
		foreach($formFormFieldsBeans as $ben){
			$fields[] = $ben->formfields_id;
		}
		
		$this->view->bean = $bean;
		$this->view->formFieldsBeans = $formFieldsBeans;
		$this->view->fields = json_encode($fields);
	}
	
	public function ajaxSaveFormFieldsAction(){
		$this->_disableView();
		$form = $this->_getParam('id');
		$fields = $this->_getParam('jObject');
		
		R::exec("DELETE FROM `formformfields` WHERE `forms_id` = '$form'");
		
		$order = 0;
		foreach($fields as $field){
			$bean = R::dispense('formformfields');
			$bean->forms_id = $form;
			$bean->formfields_id =  $field;
			$bean->order = $order;
			$order++;
			R::store($bean);
		}
	}
	
	/**
	 * Akcja do kasowania z grida
	 */
	public function ajaxRemoveAction() {
		$this->_disableView();
		$beanId = $this->_getParam('id');

		try {
			if ($this->crud->delete($beanId)) {
				$this->ajaxResponse->evalJS = 'new Success("' . __('Element został usunięty') . '"); return true;';
			} else {
				$this->ajaxResponse->evalJS = 'new Warning("' . __('Element nie może być usunięty') . '");';
			}
		} catch (Exception $exc) {
			$this->ajaxResponse->evalJS = 'new Warning("' . __('Element nie może być usunięty') . '");';
		}
	}

}
