<?php 
class table2Controller extends Zend_Controller_Action
{

    public function init()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,1,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'Table2Controller');
        
        // 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(),'Table2Controller');
        
        // 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(),'Table2Controller');
        
        	// kontrola opravneni
        	//$authentication = new MyWeb_Controller_Plugin_Authentication();
        	//$authentication->dispatchLoopStartup($this->_request);
        
        	$modelTable2 = MyWeb_Model_Database::loadModel('Table2');
        
        	$paginator = new MyWeb_Controller_Plugin_Paginator();
        	$paginator->setModel($modelTable2);
        	$paginator->setData($this->getRequest()->getParams());
        	$paginator->setRequest($this->getRequest());
        
        	$this->view->select = $modelTable2->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(),'Table2Controller');
        
        	// kontrola opravneni
        	//$authentication = new MyWeb_Controller_Plugin_Authentication();
        	//$authentication->dispatchLoopStartup($this->_request);
        
        	$modelTable2 = MyWeb_Model_Database::loadModel('Table2');
        	$form = $modelTable2->getForm('search');
        
        	$paginator = new MyWeb_Controller_Plugin_Paginator();
        	$paginator->setModel($modelTable2);
        	$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 = $modelTable2->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(),'Table2Controller');
        // kontrola opravneni
        //$authentication = new MyWeb_Controller_Plugin_Authentication();
        //$authentication->dispatchLoopStartup($this->_request);
        
        $id_table2 = $this->getRequest()->getParam('id_table2');
        $modelTable2 = MyWeb_Model_Database::loadModel('Table2');
        $form = $modelTable2->getForm('update');
        $dataTable2 = $modelTable2->fetchEntry($id_table2);
        $dataTable2Db = $dataTable2['data'];
        $dataTable2Form = $this->getRequest()->getParams();
        
        
        $id_table2_db = $dataTable2['data']['id_table2'];
        $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
        			$modelTable2->update($dataTable2Form, $id_table2_db);
        			$this->_redirect('/table2/list/');
        		}
        		if ($this->getRequest()->isPost() && false !== $this->getRequest()->getPost('send',false)){
        			$dataTable2Form['table1_id_table1'] = 1;
        			$modelTable2->update($dataTable2Form, $id_table2_db);
        			//$this->_redirect(\ id_table2_db);
        		}
        	}
        	}else {
        		$form->populate($dataTable2Db);
        	}
        
        $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(),'Table2Controller');
        
        // kontrola opravneni
        //$authentication = new MyWeb_Controller_Plugin_Authentication();
        //$authentication->dispatchLoopStartup($this->_request);
        
        $modelTable2 = MyWeb_Model_Database::loadModel('Table2');
        $form = $modelTable2->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('/table2/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(),'Table2Controller');
        
        // kontrola opravneni
        //$authentication = new MyWeb_Controller_Plugin_Authentication();
        //$authentication->dispatchLoopStartup($this->_request);
        
        $modelTable2 = MyWeb_Model_Database::loadModel('Table2');
        $form = $modelTable2->getForm('insert');
        $dataTable2Form = $this->getRequest()->getParams();
        
        
        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('send',false))){
        	if ($form->isValid($this->getRequest()->getPost())){
        		$dataTable2Form['table1_id_table1'] = 1;
        		$modelTable2->insert($dataTable2Form);
        		//$this->_redirect(\ id_table2_db);
        	}else {
        		$form->populate($dataTable2Form);// naplni formular pokud se vyskitla chyba
        	}
        }
        //********** Nastaveni javascriptu pro popup window ***********************
        $this->view->jsOpenLinks = $jsOpenLinks;
        $this->view->javascriptPopupMain = $js;
        $this->view->form=$form;
    }


}
