<?php 
class articleController extends Zend_Controller_Action
{

    public function init()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,1,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'ArticleController');
        
        // 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(),'ArticleController');
        
        // 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(),'ArticleController');
        
        	// kontrola opravneni
        	$authentication = new MyWeb_Controller_Plugin_Authentication();
        	$authentication->dispatchLoopStartup($this->_request);
        
        	$modelArticle = MyWeb_Model_Database::loadModel('Article');
        
        	$paginator = new MyWeb_Controller_Plugin_Paginator();
        	$paginator->setModel($modelArticle);
        	$paginator->setData($this->getRequest()->getParams());
        	$paginator->setRequest($this->getRequest());
        	
        	$this->view->select = $modelArticle->createFetchEntrySelect($this->getRequest()->getParams());
        	$this->view->pager = $paginator->getPaginator();
        }catch (Exception $e){
        	echo $e->getMessage();
        }
    }

    public function list2Action()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,3,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'ArticleController');
        
        // kontrola opravneni
        $authentication = new MyWeb_Controller_Plugin_Authentication();
        $authentication->dispatchLoopStartup($this->_request);
        
        $modelArticle = MyWeb_Model_Database::loadModel('Article');
        $form = $modelArticle->getForm('search');
        
        $page = (int) $this->getRequest()->getParam('page',1);
        $list2Article = $modelArticle->fetchEntries($this->getRequest()->getParams());
        $paginator = Zend_Paginator::factory($list2Article);
        $paginator->setCurrentPageNumber($page);
        
        //********** 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->paginator = $paginator;
        $this->view->list2Article = $list2Article;
        $this->view->form = $form;
    }

    public function editAction()
    {
        MyWeb_Log_ApplicationLogger::addLog($this,4,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'ArticleController');
        
        // kontrola opravneni
        $authentication = new MyWeb_Controller_Plugin_Authentication();
        $authentication->dispatchLoopStartup($this->_request);
        
        $modelArticle = MyWeb_Model_Database::loadModel('Article');
        $form = $modelArticle->getForm('update');
        
        $id_article = $this->getRequest()->getParam('id_article');
        $dataArticle = $modelArticle->fetchEntry($id_article);
        
    	$form->setAction('');
        
    	
        	
        	// !!! v poli post musi byt presne ty same polozky co jsou ve formulari a nesmi byt zadna navic
        	$dataArticleDb['controller'] = 'article';
        	$dataArticleDb['action'] = 'new';
        	$dataArticleDb['module'] = 'default';
        	$dataArticleDb['send'] = 'send';
        	
        	$dataArticleDb['id_article'] = 				$dataArticle['data']['id_article'];
        	//$dataArticleDb['user_id_user'] = 			$dataArticle['data']['user_id_user'];
        	
        	$dataArticleDb['article_content'] = 		$dataArticle['data']['article_content'];
        	$dataArticleDb['article_text'] = 			$dataArticle['data']['article_text'];
        	//$dataUserForm['user_userName'] = 			$dataArticle['data']['user_userName'];
        	$dataArticleDb['State_id_stateValue'] = 	$dataArticle['data']['nameState_name'];
        	$dataArticleDb['State_id_stateInputName'] = 'State_id_state';
        	$dataArticleDb['State_id_stateInputValue'] = $dataArticle['data']['State_id_state'];
        	$dataArticleDb['State_id_state'] = 			$dataArticle['data']['State_id_state'];
        	$dataArticleDb['nameState_nameInputName'] = 'nameState_name';
        	$dataArticleDb['nameState_nameInputValue'] =$dataArticle['data']['nameState_name'];
        	$dataArticleDb['article_dateTime'] = 		$dataArticle['data']['article_dateTime'];
        	
        if ($this->getRequest()->isPost() && 
        	((false !== $this->getRequest()->getPost('send',false)) || (false !== $this->getRequest()->getPost('delete',false)))){
        	
        	// musim pouzit getParams protoze getValue vraci jiz filtrovane hodnoty
        	$dataArticleFormPost = $this->getRequest()->getParams();
        	foreach ($dataArticleFormPost as $key => $value) {
        		$dataArticleForm[$key]=$dataArticleFormPost[$key];
        		//echo "<p>".$dataUserFormPost[$key]." ".$key."</p>";
        	}
        	$dataArticleForm['State_id_state'] = $dataArticleFormPost['State_id_stateInputValue'];
        	
        	
        	if (Zend_Auth::getInstance()->hasIdentity()){
        		$dataArticleForm['user_userName'] = Zend_Auth::getInstance()->getIdentity()->user_name;
        		$dataArticleForm['User_id_user'] = Zend_Auth::getInstance()->getIdentity()->id_user;
        	}
        
        	
        	
        	if ($form->isValid($this->getRequest()->getPost())){
        		//$modelArticle->update($dataArticleForm,$id_article);
        		//$this->_redirect('/article/new');
        		
        		
        		if ($this->getRequest()->isPost() && false !== $this->getRequest()->getPost('delete',false)){
        			$dataArticleForm['State_id_state'] = $modelArticle->getIdState('disabled', $this->getRequest()->getControllerName());
        			$modelArticle->update($dataArticleForm, $id_article);
        			//$this->_redirect('/article/list/');
        		}
        		
        		// overeni jedinecnosti article_text
        		$validator = new Zend_Validate_Db_NoRecordExists(
        			array(
        				'table' => 'article',
        				'field' => 'article_text',
        				'exclude' => array(
        					'field' => 'id_article',
        					'value' => $id_article
        					)
        				)
        			);
        		if ($this->getRequest()->isPost() && false !== $this->getRequest()->getPost('send',false)){
        			if ($validator->isValid($dataArticleForm['article_text'])) {
        				$modelArticle->update($dataArticleForm, $id_article);
        				//$this->_redirect('/user/edit/id_user/'.$id_user_db);
        			}else {
        				$messages = $validator->getMessages();
        				foreach ($messages as $message) {
        					echo "$message\n";
        				}
        			}
        		}
        		
        		
        		
        	}else {
        		$dataArticleDb['article_content'] = $dataArticleFormPost['article_content'];
        		$form->populate($dataArticleDb);// naplni formular pokud se vyskitla chyba
        	}
        }else {
        	$form->populate($dataArticleDb);// naplni formular pokud se nevyskitla chyba
        }
        
        //********** Nastaveni javascriptu pro popup window ***********************
        $jsPopupMain = MyWeb_JavaScript_Popup_Abstract::createJsPopupMain();
        $jsPopupMain->setSourceController('article');
        $jsPopupMain->setSourceAction('new');
        $jsPopupMain->addLinkPopupWindow('okno', 'list2');
        $jsPopupMain->addIdInputs('State_id_stateValue');
        $jsPopupMain->addControllerLinkOpenWindow('state');
        
        //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->levelDirectory(4);
        $js=$jsPopupMain->js();
        $jsPopupMain->setNameFile('mainNew');
        $jsPopupMain->writeFileJs();
        
        $jsPopupMain->setNameFileLink('mainNewLink');
        $jsPopupMain->addParamGetLinkOpenPopup(0,'tableDatabase_table', 'article');
        //$jsPopupMain->writeFileJsLinks();
        $jsOpenLinks = $jsPopupMain->jsOpenLinks();
        
        $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(),'ArticleController');
        
        // kontrola opravneni
        $authentication = new MyWeb_Controller_Plugin_Authentication();
        $authentication->dispatchLoopStartup($this->_request);
        
        $modelArticle = MyWeb_Model_Database::loadModel('Article');
        $form = $modelArticle->getForm('search');
        
        
    	if (Zend_Auth::getInstance()->hasIdentity()){
        	$acl = MyWeb_Acl::getInstance();
        	$url = new Zend_View_Helper_Url();
        	if (!$acl->isAllowed(Zend_Auth::getInstance()->getIdentity()->role_role,strtolower($this->getRequest()->getControllerName()),'articleSearchInputTextState')){
        		$form->removeElement('State_id_stateValue');
        	}
        }else {
        	$form->removeElement('State_id_stateValue');
        }
        
        
        
        if ($this->getRequest()->isPost() && false !== $this->getRequest()->getPost('send',false)){
        	if ($form->isValid($this->getRequest()->getPost())){
        		
        		//*** prevod formatu data z m/d/Y na Y-m-d ****
        		$dateTime = $form->getValue('article_dateTime');
        		if (!empty($dateTime)){
        			$dateArray = explode('/', $form->getValue('article_dateTime'));
        			$pom = $dateArray[0];
        			$dateArray[0] = $dateArray[2];
        			$dateArray[2] = $pom;
        			$pom = $dateArray[1];
        			$dateArray[1] = $dateArray[2];
        			$dateArray[2] = $pom;
        			$dateTime = implode('-', $dateArray);
        		}else {
        			$dateTime = "";
        		}
        		//***********************************************
        		
        		
        		$this->_redirect('/article/list'.
        		'/State_id_stateValue/'.$form->getValue('State_id_stateValue').
        		'/user_userNameValue/'.$form->getValue('User_id_userValue').
        		'/article_dateTime/'.$dateTime.
        		
        		'/State_id_stateInputName/'.$form->getValue('State_id_stateInputName').
        		'/State_id_stateInputValue/'.$form->getValue('State_id_stateInputValue').
        		
        		'/User_id_userInputValue/'.$form->getValue('User_id_userInputValue').
        		'/User_id_userInputName/'.$form->getValue('User_id_userInputName')
        		);
        	}
        }
        

        //********** Nastaveni javascriptu pro popup window ***********************
        $jsPopupMain = MyWeb_JavaScript_Popup_Abstract::createJsPopupMain();
        $jsPopupMain->setSourceController('article');
        $jsPopupMain->setSourceAction('search');
        $jsPopupMain->addLinkPopupWindow('okno', 'list2');
        $jsPopupMain->addLinkPopupWindow('okno1', 'list2');
        $jsPopupMain->addIdInputs('State_id_stateValue');
        $jsPopupMain->addControllerLinkOpenWindow('state');
        $jsPopupMain->addIdInputs('User_id_userValue');
        $jsPopupMain->addControllerLinkOpenWindow('user');
        
        //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,'User_id_userInputValue');
        $jsPopupMain->addIdHidden(1,'user_userNameInputValue');
        $jsPopupMain->addIdHidden(1,'User_id_userInputName');
        $jsPopupMain->addIdHidden(1,'user_userNameInputName');
        
        
        
        $js=$jsPopupMain->js();
        $jsPopupMain->setNameFile('mainSearch');
        $jsPopupMain->writeFileJs();
        
        $jsPopupMain->setNameFileLink('mainSearchLink');
        $jsPopupMain->addParamGetLinkOpenPopup(0,'tableDatabase_table', 'article');
        //$jsPopupMain->writeFileJsLinks();
        $jsOpenLinks = $jsPopupMain->jsOpenLinks();
        
        
        
        
        
        
        $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(),'ArticleController');
        
        // kontrola opravneni
        $authentication = new MyWeb_Controller_Plugin_Authentication();
        $authentication->dispatchLoopStartup($this->_request);
        
        $modelArticle = MyWeb_Model_Database::loadModel('Article');
        $form = $modelArticle->getForm('insert');
        
        $form->setAction('');
        
    	// !!! clanky mohou vytvaret jen prihlaseni uzivatele
        if (Zend_Auth::getInstance()->hasIdentity()){
        	$dataUserForm['user_userName'] = Zend_Auth::getInstance()->getIdentity()->user_name;
        	$dataUserForm['User_id_user'] = Zend_Auth::getInstance()->getIdentity()->id_user;
        }else {
        	$dataUserForm['User_userName'] = "guest";
        }
        
        if ($this->getRequest()->isPost() && false !== $this->getRequest()->getPost('send',false)){
        	// musim pouzit getParams protoze getValue vraci jiz filtrovane hodnoty
        	$dataUserFormPost = $this->getRequest()->getParams();
        	foreach ($dataUserFormPost as $key => $value) {
        		$dataUserForm[$key]=$dataUserFormPost[$key];
        		//echo "<p>".$dataUserFormPost[$key]." ".$key."</p>";
        	}
        	
        	// !!! v poli post musi byt presne ty same polozky co jsou ve formulari a nesmi byt zadna navic
        	$dataUserForm['controller'] = 'article';
        	$dataUserForm['action'] = 'new';
        	$dataUserForm['module'] = 'default';
        	$dataUserForm['send'] = 'send';
        	
        	$dataUserForm['article_content'] = $dataUserFormPost['article_content'];
        	$dataUserForm['article_text'] = $dataUserFormPost['article_text'];
        	//$dataUserForm['user_userName'] = $dataUserFormPost['user_userName'];
        	$dataUserForm['State_id_stateValue'] = $dataUserFormPost['State_id_stateValue'];
        	$dataUserForm['State_id_stateInputName'] = $dataUserFormPost['State_id_stateInputName'];
        	if (empty($dataUserFormPost['State_id_stateInputValue'])){
        		$dataUserForm['State_id_state'] = $modelArticle->getIdState($dataUserFormPost['State_id_stateValue'], 'Article');
        	}else {
        		$dataUserForm['State_id_state'] = $dataUserFormPost['State_id_stateInputValue'];
        	}
        	$dataUserForm['nameState_nameInputName'] = $dataUserFormPost['nameState_nameInputName'];
        	$dataUserForm['nameState_nameInputValue'] = $dataUserFormPost['nameState_nameInputValue'];
        	$date = getdate();
        	$dataUserForm['article_dateTime'] = $date['year']."-".$date['mon']."-".$date['mday'];//'2012-02-10 08:04:01';
        	
        	
        	// overeni jedinecnosti article_text
        	$validator = new Zend_Validate_Db_NoRecordExists(
        		array(
        			'table' => 'article',
        			'field' => 'article_text'
        			)
        		);
        		
        		
        	if ($form->isValid($this->getRequest()->getPost())){
        		if ($validator->isValid($dataUserForm['article_text'])) {
        			$modelArticle->insert($dataUserForm);
        			//$this->_redirect('/article/new');
        		}else {
        			$messages = $validator->getMessages();
        			foreach ($messages as $message) {
        				echo "$message\n";
        			}
        		}
        	}else {
        		$form->populate($dataUserForm);// naplni formular pokud se vyskitla chyba
        	}
        }else {
        	$form->populate($dataUserForm);// naplni formular pokud se nevyskitla chyba
        }
        
        //********** Nastaveni javascriptu pro popup window ***********************
        $jsPopupMain = MyWeb_JavaScript_Popup_Abstract::createJsPopupMain();
        $jsPopupMain->setSourceController('article');
        $jsPopupMain->setSourceAction('new');
        $jsPopupMain->addLinkPopupWindow('okno', 'list2');
        $jsPopupMain->addIdInputs('State_id_stateValue');
        $jsPopupMain->addControllerLinkOpenWindow('state');
        
        //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');
        
        
        $js=$jsPopupMain->js();
        $jsPopupMain->setNameFile('mainNew');
        $jsPopupMain->writeFileJs();
        
        $jsPopupMain->setNameFileLink('mainNewLink');
        $jsPopupMain->addParamGetLinkOpenPopup(0,'tableDatabase_table', 'article');
        //$jsPopupMain->writeFileJsLinks();
        $jsOpenLinks = $jsPopupMain->jsOpenLinks();
        
        $this->view->jsOpenLinks = $jsOpenLinks;
        $this->view->javascriptPopupMain = $js;
        $this->view->form=$form;
    }
    
    
    public function showAction()
    {
    	MyWeb_Log_ApplicationLogger::addLog($this,6,4);
        MyWeb_plugin::showMe(MyWeb_Log_ApplicationLogger::showLog(),'ArticleController');
        
        // kontrola opravneni
        $authentication = new MyWeb_Controller_Plugin_Authentication();
        $authentication->dispatchLoopStartup($this->_request);
        
        $modelArticle = MyWeb_Model_Database::loadModel('Article');
        $modelComment = MyWeb_Model_Database::loadModel('Comment');
        
        $form = $modelComment->getForm('insert');
        $form->setAction('');
        
        $idArticle = $this->getRequest()->getParam('id_article','false');
        
        if (false !== $idArticle){
        	//echo "<p>".'idArticle '.$this->getRequest()->getParam('id_article')."</p>";
        	//***********************************************************************************
        	$page = (int) $this->getRequest()->getParam('page',1);
        	$listComment = $modelComment->fetchEntries(array('Article_id_article'=>$this->getRequest()->getParam('id_article')));
        	$dataArticle = $modelArticle->fetchEntry($idArticle);
        	
        	$paginator = new MyWeb_Controller_Plugin_Paginator();
        	$paginator->setModel($modelComment);
        	$paginator->setData(array('Article_id_article'=>$this->getRequest()->getParam('id_article')));
        	$paginator->setRequest($this->getRequest());
        	
        	$this->view->select = $modelComment->createFetchEntrySelect(array('Article_id_article'=>$this->getRequest()->getParam('id_article')));
        	$this->view->pager = $paginator->getPaginator();
        	$this->view->listComment = $listComment;
        	$this->view->form=$form;
        	$this->view->idArticle = $idArticle;
        	$this->view->dataArticle = $dataArticle;
        	
        	if ($this->getRequest()->isPost() && false !== $this->getRequest()->getPost('send',false)){
        		if ($form->isValid($this->getRequest()->getPost())){
        			if (!$this->getRequest()->getParam('stop',false)){
        				$this->_forward('new','comment',null,array('id_article'=>$idArticle));
        			}
        		}
        	}
        }else {
        	$this->view->idArticle = NULL;
        }
    }
}
