<?php 
class commentController extends Zend_Controller_Action
{

    public function init()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,1,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'CommentController');
        
        // 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(),'CommentController');
        
        // kontrola opravneni
        $authentication = new MyWeb_Controller_Plugin_Authentication();
        $authentication->dispatchLoopStartup($this->_request);
    }

    public function listAction()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,3,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'CommentController');
        
        // kontrola opravneni
        $authentication = new MyWeb_Controller_Plugin_Authentication();
        $authentication->dispatchLoopStartup($this->_request);
        
        $modelComment = MyWeb_Model_Database::loadModel('Comment');
        
        $paginator = new MyWeb_Controller_Plugin_Paginator();
        $paginator->setModel($modelComment);
        $paginator->setData($this->getRequest()->getParams());
        $paginator->setRequest($this->getRequest());
        
        
        $this->view->select = $modelComment->createFetchEntrySelect($this->getRequest()->getParams());
        $this->view->pager = $paginator->getPaginator();
    }

    public function list2Action()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,3,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'CommentController');
        
        // kontrola opravneni
        $authentication = new MyWeb_Controller_Plugin_Authentication();
        $authentication->dispatchLoopStartup($this->_request);
        
        $modelComment = MyWeb_Model_Database::loadModel('Comment');
        $form = $modelComment->getForm('search');
        $form->setAction('controller');
        
        $page = (int) $this->getRequest()->getParam('page',1);
        
        $list2Comment = $modelComment->fetchEntries($this->getRequest()->getParams());
        $paginator = Zend_Paginator::factory($list2Comment);
        
        $paginator->setCurrentPageNumber($page);
        
        $controller = "state";
        $action = "list2";
        $this->view->controller = $controller;
        $this->view->action = $action;
        //id_state, state_state, NameState_id_NameState, TableDatabase_id_tableDatabase
        if ((false !== $this->getRequest()->getParam('sourceController',false)) && (false !== $this->getRequest()->getParam('sourceAction',false))){
        	$controller = $this->getRequest()->getParam('sourceController');
        	$action = $this->getRequest()->getParam('sourceAction');
        	$this->view->controller = $controller;
        	$this->view->action = $action;
        }
        $this->view->paginator = $paginator;
        $this->view->list2Comment = $list2Comment;
        $this->view->form = $form;
    }

    public function editAction()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,4,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'CommentController');
        
        // kontrola opravneni
        $authentication = new MyWeb_Controller_Plugin_Authentication();
        $authentication->dispatchLoopStartup($this->_request);
        
        $modelComment = MyWeb_Model_Database::loadModel('Comment');
        $form = $modelComment->getForm('update');
        
    	//$form->setAction('');
        
        try {
    		if ((false !== $this->getRequest()->getParam('Article_id_article',false)) && (false !== $this->getRequest()->getParam('id_comment',false))){
        		$dataCommentForm['Article_id_article'] = $this->getRequest()->getParam('Article_id_article');
        		$dataCommentForm['id_comment'] = $this->getRequest()->getParam('id_comment');
        	}else {
        		$zprava = "commentController: Not exists param Article_id_article or id_comment: "."\n";
        		throw new Exception($zprava);
        	}
        	$dataComment = $modelComment->fetchEntry($dataCommentForm['id_comment']);
        	
        		
        		// !!! v poli post musi byt presne ty same polozky co jsou ve formulari a nesmi byt zadna navic
        		$dataCommentDb['controller'] = 'comment';
        		$dataCommentDb['action'] = 'new';
        		$dataCommentDb['module'] = 'default';
        		$dataCommentDb['send'] = 'send';
        		
        		$dataCommentDb['comment_text'] 				= 	$dataComment['data']['comment_text'];
        		
        		$dataCommentDb['State_id_stateValue'] 		= 	$dataComment['data']['nameState_name'];
        		$dataCommentDb['State_id_stateInputName'] 	= 	'State_id_state';
        		$dataCommentDb['State_id_state'] 			= 	$dataComment['data']['State_id_state'];
        		$dataCommentDb['State_id_stateInputValue'] 	= 	$dataComment['data']['State_id_state'];
        		$dataCommentDb['nameState_nameInputName'] 	= 	'nameState_name';
        		$dataCommentDb['nameState_nameInputValue'] 	= 	$dataComment['data']['nameState_name'];
        		
        		$dataCommentDb['Article_id_articleValue'] 		= 	$dataComment['data']['article_text'];
        		$dataCommentDb['Article_id_articleInputName'] 	= 	'Article_id_article';
        		$dataCommentDb['Article_id_article'] 			= 	$dataComment['data']['Article_id_article'];
        		$dataCommentDb['Article_id_articleInputValue'] 	= 	$dataComment['data']['id_article'];
        		$dataCommentDb['article_textInputName'] 		= 	'article_text';
        		$dataCommentDb['article_textInputValue'] 		= 	$dataComment['data']['article_text'];
        		
        		$date = getdate();
        		$dataCommentDb['comment_dateTime'] = $date['year']."-".$date['mon']."-".$date['mday'];//'2012-02-10 08:04:01';
        		
        		
        
        	if ($this->getRequest()->isPost() && false !== $this->getRequest()->getPost('send',false)){
        		
        	$dataCommentFormPost = $this->getRequest()->getPost();
        	//$dataCommentFormPost = $form->getValues();
        		foreach ($dataCommentFormPost as $key => $value) {
        			$dataCommentForm[$key]=$dataCommentFormPost[$key];
        			//echo "<p>".$dataCommentForm[$key]." key ".$key."</p>";
        		}
        		// !!! v poli post musi byt presne ty same polozky co jsou ve formulari a nesmi byt zadna navic
        		$dataCommentForm['controller'] = 'comment';
        		$dataCommentForm['action'] = 'edit';
        		$dataCommentForm['module'] = 'default';
        		$dataCommentForm['send'] = 'send';
        		
        		
        		$commentName = "<p>".$dataCommentFormPost['commentName']."</p>";
        		$dataCommentForm['comment_text'] = $commentName.$dataCommentFormPost['comment_text'];
        		$dataCommentForm['commentName'] = $dataCommentFormPost['commentName'];
        		$dataCommentForm['Article_id_article'] = $dataCommentFormPost['Article_id_articleInputValue'];
        		
        		$dataCommentForm['State_id_stateValue'] = $dataCommentFormPost['State_id_stateValue'];
        		$dataCommentForm['State_id_stateInputName'] = $dataCommentFormPost['State_id_stateInputName'];
        		$dataCommentForm['State_id_state'] = $dataCommentFormPost['State_id_stateInputValue'];
        		$dataCommentForm['State_id_stateInputValue'] = $dataCommentFormPost['State_id_stateInputValue'];
        		$dataCommentForm['nameState_nameInputName'] = $dataCommentFormPost['nameState_nameInputName'];
        		$dataCommentForm['nameState_nameInputValue'] = $dataCommentFormPost['nameState_nameInputValue'];
        		
        		//$dataCommentForm['Article_id_articleValue'] 		= 	$dataCommentFormPost['Article_id_articleValue'];
        		$dataCommentForm['Article_id_articleInputName'] 	= 	$dataCommentFormPost['Article_id_articleInputName'];
        		$dataCommentForm['Article_id_article'] 				= 	$dataCommentFormPost['Article_id_articleInputValue'];
        		$dataCommentForm['Article_id_articleInputValue'] 	= 	$dataCommentFormPost['Article_id_articleInputValue'];
        		$dataCommentForm['article_textInputName'] 			= 	$dataCommentFormPost['article_textInputName'];
        		$dataCommentForm['article_textInputValue']	 		= 	$dataCommentFormPost['article_textInputValue'];
        		
        		$date = getdate();
        		
        		$dataCommentForm['comment_dateTime'] = $date['year']."-".$date['mon']."-".$date['mday'];//'2012-02-10 08:04:01';
        	
        		if ($form->isValid($this->getRequest()->getPost())){
        			$modelComment->update($dataCommentForm, $dataCommentForm['id_comment']);
        			//$this->_redirect('/article/new');
        		}else {
        			$form->populate($dataCommentForm);// naplni formular pokud se vyskitla chyba
        		}
        	}else {
        		$form->populate($dataCommentDb);// naplni formular pokud se nevyskitla chyba
        	}
        	
        	//********** Nastaveni javascriptu pro popup window ***********************
        	$jsPopupMain = MyWeb_JavaScript_Popup_Abstract::createJsPopupMain();
        	
        	$jsPopupMain->setSourceController('comment');
        	$jsPopupMain->setSourceAction('edit');
        	$jsPopupMain->addLinkPopupWindow('okno', 'list2');
        	$jsPopupMain->addLinkPopupWindow('okno1', 'list2');
        	$jsPopupMain->addIdInputs('State_id_stateValue');
        	$jsPopupMain->addControllerLinkOpenWindow('state');
        	$jsPopupMain->addIdInputs('Article_id_articleValue');
        	$jsPopupMain->addControllerLinkOpenWindow('article');
        	
        	
        	
        	//lepsi je pro kazde individualni nazev bez indexace
        	$jsPopupMain->addIdHidden(0,'State_id_stateInputValue');
        	$jsPopupMain->addIdHidden(0,'nameState_nameInputValue');
        	$jsPopupMain->addIdHidden(0,'State_id_stateInputName');
        	$jsPopupMain->addIdHidden(0,'nameState_nameInputName');
        	
        	$jsPopupMain->addIdHidden(1,'Article_id_articleInputValue');
        	$jsPopupMain->addIdHidden(1,'article_textInputValue');
        	$jsPopupMain->addIdHidden(1,'Article_id_articleInputName');
        	$jsPopupMain->addIdHidden(1,'article_textInputName');
        	
        	$jsPopupMain->levelDirectory(6);
        	$js=$jsPopupMain->js();
        	$jsPopupMain->setNameFile('mainNew');
        	//$jsPopupMain->writeFileJs();
        	
        	$jsPopupMain->setNameFileLink('mainNewLink');
        	$jsPopupMain->addParamGetLinkOpenPopup(0,'tableDatabase_table', 'comment');
        	//$jsPopupMain->writeFileJsLinks();
        	$jsOpenLinks = $jsPopupMain->jsOpenLinks();
        	
        	$this->view->jsOpenLinks = $jsOpenLinks;
        	$this->view->javascriptPopupMain = $js;
        	}
    	catch (Exception $e){
    		echo $e->getMessage();
    	}
        $this->view->form=$form;
    }

    
    
    
    public function searchAction()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,5,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'CommentController');
        
        // kontrola opravneni
        $authentication = new MyWeb_Controller_Plugin_Authentication();
        $authentication->dispatchLoopStartup($this->_request);
        
        $modelComment = MyWeb_Model_Database::loadModel('Comment');
        $form = $modelComment->getForm('search');
        
        $form->setAction('');
        
        if ($this->getRequest()->isPost() && false !== $this->getRequest()->getPost('send',false)){
        	if ($form->isValid($this->getRequest()->getPost())){
        		
        		$this->_redirect('/comment/list'
        		);
        	}
        }
        $this->view->form=$form;
    }

    public function newAction()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,6,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'CommentController');
        
        // kontrola opravneni
        $authentication = new MyWeb_Controller_Plugin_Authentication();
        $authentication->dispatchLoopStartup($this->_request);
        
        $modelComment = MyWeb_Model_Database::loadModel('Comment');
        $form = $modelComment->getForm('insert');
        
        
        $form->setAction('');
        $dataCommentFormPost = array();
        try {
    		if (false !== $this->getRequest()->getParam('id_article',false)){
        		$dataCommentFormPost['id_article'] = $this->getRequest()->getParam('id_article');
        	}else {
        		$zprava = "commentController: Not exists param id_article: "."\n";
        		throw new Exception($zprava);
        	}
        			
        			
        	if ($this->getRequest()->isPost() && false !== $this->getRequest()->getPost('send',false)){
        		// musim pouzit getPost protoze getValue vraci jiz filtrovane hodnoty
        		$dataCommentFormPost = $this->getRequest()->getPost();
        		foreach ($dataCommentFormPost as $key => $value) {
        			$dataCommentForm[$key]=$dataCommentFormPost[$key];
        			//echo "<p>".$dataCommentFormPost[$key]." ".$key."</p>";
        		}
        		
        		// !!! v poli post musi byt presne ty same polozky co jsou ve formulari a nesmi byt zadna navic
        		$dataCommentForm['controller'] = 'comment';
        		$dataCommentForm['action'] = 'new';
        		$dataCommentForm['module'] = 'default';
        		$dataCommentForm['send'] = 'send';
        		
        		$commentName = "<p>".$dataCommentFormPost['commentName']."</p>";
        		$date = getdate();
        		$dataCommentForm['Article_id_article'] 	= $this->getRequest()->getParam('id_article');
        		$dataCommentForm['State_id_state'] 		= $modelComment->getIdState('enabled', 'comment');
        		$dataCommentForm['comment_text'] 		= $commentName.$dataCommentFormPost['comment_text'];
        		$dataCommentForm['commentName'] 		= $dataCommentFormPost['commentName'];
        		$dataCommentForm['comment_dateTime'] 	= $date['year']."-".$date['mon']."-".$date['mday'];//'2012-02-10 08:04:01';
        		
        		if (false !== $this->getRequest()->getParam('controllerName',false)){
        			if (0 === strcmp($this->getRequest()->getParam('controllerName'), 'article')){
        					$this->_forward('show','article',null,array('id_article'=>$this->getRequest()->getParam('id_article'),'stop'=>'true'));
        			}
        		}
        		
        		if ($form->isValid($this->getRequest()->getPost())){
        			$modelComment->insert($dataCommentForm);
        		}else {
        			$form->populate($dataCommentForm);// naplni formular pokud se vyskitla chyba
        		}
        	}
        }
    	catch (Exception $e){
    		echo $e->getMessage();
    	}
    	$this->view->form=$form;
    }


}
