<?php


/*
 * created on 16.10.2007
 * project: zf-konference
 * author: Vlastimil Vávrů (http://vavru.cz)
 * version: $
 */

class AdminController extends Zend_Controller_Action {
	private $_articleService;

	/**
	 * Initialization - invoked in parent class constructor.
	 */
	public function init() {
		$this->_articleService = Zend_Registry :: get('articleService');
		$this->view->addHelperPath('../library/Venturia/View/Helper/', 'Venturia_View_Helper');
		$this->view->wwwroot = $this->_request->getBaseUrl();
	}

	/**
	 * Article administration
	 */
	public function clanekAction() {
		$article = null;
		$new = false;

		if ($this->_request->id != null) {
			$article = $this->_articleService->getArticleById($this->_request->id);
			if ($article == null) {
				$this->_forward('error404', 'error');
				return;
			}

		} else {
			$new = true;
			$article = $this->_articleService->getArticles()->createRow();
		}

		$db = Zend_Registry :: get('db');
		$categories = $db->fetchPairs('select id, name from categories order by name asc');

		//$categoriesTable = new Categories ( ) ;
		//$categories = Venturia_Db_Rowset_ScrollAble::toAssocArray ( $categoriesTable->fetchAll ( null, 'name asc' )->toArray () ) ;		

		if ($this->_request->isPost()) {
			if ($this->_request->delete && !$new) {
				$this->_articleService->deleteArticle($article);
				$fm = $this->_helper->getHelper('FlashMessenger');
				$fm->addMessage("Článek byl smazán");
				$this->_redirect('/');
			} else {
				$filters = array (
					'StringTrim'
				);

				$validators = array (
					'category_id' => array (
						array (
							'InArray',
							array_keys($categories
						)
					),
					'messages' => array (
						Zend_Validate_InArray :: NOT_IN_ARRAY => 'Prosím zvolte kategorii'
					)
				), 'rewrite_string' => array (
					'NotEmpty',
					'messages' => array (
						Zend_Validate_NotEmpty :: IS_EMPTY => 'Prosím vyplňte rewrite string'
					)
				), 'name' => array (
					array (
						'StringLength',
						5
					),
					'messages' => array (
						Zend_Validate_StringLength :: TOO_SHORT => 'Název článku je příliš krátký - nejméně %min% znaků'
					)
				), 'description' => array (
					array (
						'StringLength',
						10
					),
					'messages' => array (
						Zend_Validate_StringLength :: TOO_SHORT => 'Popis má méně než požadovaných %min% znaků',
						Zend_Validate_StringLength :: TOO_LONG => 'Popis má více než požadovaných %max% znaků'
					)
				), 'content' => array (
					array (
						'StringLength',
						10,
						100
					),
					'messages' => array (
						Zend_Validate_StringLength :: TOO_SHORT => 'Text článku má méně než požadovaných %min% znaků',
						Zend_Validate_StringLength :: TOO_LONG => 'Text článku má více než požadovaných %max% znaků'
					)
				), 'date' => array (
					'Date',
					'messages' => array (
						Zend_Validate_Date :: NOT_YYYY_MM_DD => 'Prosím vyplňte datum ve správném formátu',
						Zend_Validate_Date :: INVALID => 'Prosím vyplňte validní datum'
					)
				));
				$options = array (
					'breakChainOnFailure' => true
				);
				
				$input = new Zend_Filter_Input($filters, $validators, $this->_request->getPost(), $options);
				
				Article :: bind($article, $this->_request);

				if ($input->isValid()) {				
					$this->_articleService->saveArticle($article);
					$fm = $this->_helper->getHelper('FlashMessenger');
					$fm->addMessage($new ? "Článek byl vytvořen" : "Změny v článku byly uloženy");
					$this->_helper->redirector->goToRoute(array (
						'articleRewriteString' => $article->rewrite_string
					), 'article');
				} else {
					$this->view->errors = $input->getMessages();
				}
			}
		}
		

		$this->view->categories = array ('' => '- vyberte předpis -') + $categories;
		$this->view->new = $new;
		$this->view->title = 'Editace článku';
		$this->view->article = $article;
		$this->view->url = $this->_request->getRequestUri();
	}

}
