<?php

/**
 * Description of indexController
 *
 * @author Arkadiusz Rychlik <a.rychlik at eppe.com.pl>
 */
class Users_Controller_Admin_IndexController extends Main_Users_Controller_Admin_IndexController {

	protected function _add() {
		$formClassName = $this->_request->getModuleName() . '_Form_Admin_' . ucfirst($this->beanName);
		$form = new $formClassName();

		if ($this->_isPost()) {
			$post = $this->_getPost();
			
			if (isset($post['cancel'])) {
				$this->_redirectIndex();
			}

			if ($form->isValid($post) && $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->_request->getModuleName() . '_Form_Admin_' . ucfirst($this->beanName);

		$form = new $formClassName(array('bean' => $bean));

		if ($this->_isPost()) {
			$post = $this->_getPost();
			
			if (isset($post['cancel'])) {
				$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;
	}

}
