<?php 
class table1Controller extends Zend_Controller_Action
{

    public function init()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,1,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'Table1Controller');
        
        // kontrola opravneni
        //$authentication = new MyWeb_Controller_Plugin_Authentication();
        //$authentication->dispatchLoopStartup($this->_request);
    }

    public function indexAction()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,2,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'Table1Controller');
        
        // kontrola opravneni
        //$authentication = new MyWeb_Controller_Plugin_Authentication();
        //$authentication->dispatchLoopStartup($this->_request);
    }

    public function listAction()
    {
        try {
        	MyWeb_Log_ApplicationLogger::addLog($this,3,4);
        	MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'Table1Controller');
        
        	// kontrola opravneni
        	//$authentication = new MyWeb_Controller_Plugin_Authentication();
        	//$authentication->dispatchLoopStartup($this->_request);
        
        	$modelTable1 = MyWeb_Model_Database::loadModel('Table1');
        
        	$paginator = new MyWeb_Controller_Plugin_Paginator();
        	$paginator->setModel($modelTable1);
        	$paginator->setData($this->getRequest()->getParams());
        	$paginator->setRequest($this->getRequest());
        
        	$this->view->select = $modelTable1->createFetchEntrySelect($this->getRequest()->getParams());
        	$this->view->pager = $paginator->getPaginator();
        }catch (Exception $e){
        	echo $e->getMessage();
        }
    }

    public function list2Action()
    {
        try {
        	MyWeb_Log_ApplicationLogger::addLog($this,3,4);
        	MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'Table1Controller');
        
        	// kontrola opravneni
        	//$authentication = new MyWeb_Controller_Plugin_Authentication();
        	//$authentication->dispatchLoopStartup($this->_request);
        
        	$modelTable1 = MyWeb_Model_Database::loadModel('Table1');
        	$form = $modelTable1->getForm('search');
        
        	$paginator = new MyWeb_Controller_Plugin_Paginator();
        	$paginator->setModel($modelTable1);
        	$paginator->setData($this->getRequest()->getParams());
        	$paginator->setRequest($this->getRequest());
        
        	//********** Nastaveni javascriptu pro popup window ***********************
        	/*$jsPopupSlave = MyWeb_JavaScript_Popup_Abstract::createJsPopupSlave();
        	$jsPopupSlave->setIdInput('in');
        	$jsPopupSlave->addIdHidden('inid');
        	$jsPopupSlave->addIdHidden('inid');
        	$jsPopupSlave->addIdHidden('inid');
        	$jsPopupSlave->addIdHidden('inid');
        	$jsPopupSlave->setIdLink('button');
        	$js=$jsPopupSlave->js();
        
        	$jsPopupSlave->setNameFile('slaveList2');
        	//$jsPopupSlave->writeFileJs();*/
        	$this->view->javascriptPopupSlave = $js;
        	$this->view->select = $modelTable1->createFetchEntrySelect($this->getRequest()->getParams());
        	$this->view->pager = $paginator->getPaginator();
        }catch (Exception $e){
        	echo $e->getMessage();
        }
    }

    public function editAction()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,4,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'Table1Controller');
        // kontrola opravneni
        //$authentication = new MyWeb_Controller_Plugin_Authentication();
        //$authentication->dispatchLoopStartup($this->_request);
        
        $id_table1 = $this->getRequest()->getParam('id_table1');
        $modelTable1 = MyWeb_Model_Database::loadModel('Table1');
        $form = $modelTable1->getForm('update');
        $dataTable1 = $modelTable1->fetchEntry($id_table1);
        $dataTable1Db = $dataTable1['data'];
        $dataTable1Form = $this->getRequest()->getParams();
        
        
        $id_table1_db = $dataTable1['data']['id_table1'];
        $form->setAction('');
        
        if ($this->getRequest()->isPost() && 
        	((false !== $this->getRequest()->getPost('send',false)) || (false !== $this->getRequest()->getPost('delete',false)))){
        	if ($form->isValid($this->getRequest()->getPost())){
        		if ($this->getRequest()->isPost() && false !== $this->getRequest()->getPost('delete',false)){
        			// state disabled or enabled
        			$modelTable1->update($dataTable1Form, $id_table1_db);
        			$this->_redirect('/table1/list/');
        		}
        		if ($this->getRequest()->isPost() && false !== $this->getRequest()->getPost('send',false)){
        			$modelTable1->update($dataTable1Form, $id_table1_db);
        			//$this->_redirect(\ id_table1_db);
        		}
        	}
        	}else {
        		$form->populate($dataTable1Db);
        	}
        
        $this->view->jsOpenLinks = $jsOpenLinks;
        $this->view->javascriptPopupMain = $js;
        $this->view->form=$form;
    }

    public function searchAction()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,5,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'Table1Controller');
        
        // kontrola opravneni
        //$authentication = new MyWeb_Controller_Plugin_Authentication();
        //$authentication->dispatchLoopStartup($this->_request);
        
        $modelTable1 = MyWeb_Model_Database::loadModel('Table1');
        $form = $modelTable1->getForm('search');
        
        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('send',false))){
        	if ($form->isValid($this->getRequest()->getPost())){
        		$searchValue = $form->getValues();
        		$searchValueLink = "";
        		foreach ($searchValue as $key => $value) {
        			$searchValueLink .= $key."/".$value."/";
        		}
        		$this->_redirect('/table1/list/'.$searchValueLink);
        	}
        }
        
        
        //********** Nastaveni javascriptu pro popup window ***********************
        $this->view->jsOpenLinks = $jsOpenLinks;
        $this->view->javascriptPopupMain = $js;
        $this->view->form=$form;
    }

    public function newAction()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,6,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'Table1Controller');
        
        // kontrola opravneni
        //$authentication = new MyWeb_Controller_Plugin_Authentication();
        //$authentication->dispatchLoopStartup($this->_request);
        
        $modelTable1 = MyWeb_Model_Database::loadModel('Table1');
        $form = $modelTable1->getForm('insert');
        $dataTable1Form = $this->getRequest()->getParams();
        
        
        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('send',false))){
        	if ($form->isValid($this->getRequest()->getPost())){
        		$modelTable1->insert($dataTable1Form);
        		//$this->_redirect(\ id_table1_db);
        	}else {
        		$form->populate($dataTable1Form);// naplni formular pokud se vyskitla chyba
        	}
        }
        //********** Nastaveni javascriptu pro popup window ***********************
        $this->view->jsOpenLinks = $jsOpenLinks;
        $this->view->javascriptPopupMain = $js;
        $this->view->form=$form;
    }


}
