<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



namespace FrontModule;
use \Nette\Application\UI\Form;
 
class NovinkyPresenter extends BasePresenter {
 
/** @var \PostsRepository */
 private $novinkyModel;
 
 /** @persistent */
    public $rok;
 
 public function __construct(\NovinkyModel $novinkyModel) {
 $this->novinkyModel = $novinkyModel;
 }
 
 public function beforeRender() {
        $this->template->title = 'Novinky';
        $this->template->description = "Novinky na webu sekce velššpringršpaněl.";
    }
 
 public function renderList() {
  if ($this->rok == null) { $this->rok=date('Y'); } // aktualni rok
     
        $this->template->novinky = $this->novinkyModel->getNovinkyByYear($this->rok); 
 }
 
 protected function createComponentVybratRokForm() {
        $form = new form();
        $roky = $this->novinkyModel->getRoky();
        $rokyAsoc;
        foreach ($roky as $r) {
          $rokyAsoc[$r->year] = $r->year;  
        }
        
        $form->addSelect("rok", "Rok:", $rokyAsoc)
        ->setPrompt('Zvolte rok');
        $form->addSubmit("send", "Zvolit rok");
        $form->onSuccess[] = $this->vybratRokFormSucceeded;
        return $form;
    }

    public function vybratRokFormSucceeded(Form $form) {
        $valu = $form->getValues();
        $this->rok = $valu->rok;
        $this->redirect("Novinky:list");
    }
 
}
