<?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 AdminModule;
use \Nette\Application\UI\Form;
use \Nette\Utils\Image;

 
class StrankyPresenter extends BasePresenter {
    
    
    private $strankyModel;
    
    
 
    public function __construct(\StrankyModel $strankyModel) {
    $this->strankyModel = $strankyModel;
    }
    
  
    
    public function renderStranka($url) {
      $this->template->text = $this->strankyModel->getStrankaByUrl($url);
       
    }
    
    
    
    public function renderEdit($url) {
      $this->template->text = $this->strankyModel->getStrankaByUrl($url);
    }
    
    


protected function createComponentEditTextForm() {
    $form = new Form();
    $str = $this->strankyModel->getStrankaByUrl($this->getParam('url'));
    $form->addText('nazev','Název:')->setDefaultValue($str->nazev);
    $form->addTextArea('text', 'Text:')->setDefaultValue($str->text)->getControlPrototype()->addAttributes(array('class' => 'strankaTextarea'));
    $form->addHidden('id')->setDefaultValue($str->id);
    $form->addSubmit("send", "Odeslat");
    $form->onSuccess[] = $this->editTextFormSucceeded;
    return $form;
}

public function editTextFormSucceeded(Form $form) {
    if (!$this->getUser()->isLoggedIn()) {
        $this->error('Pro vytvoření, nebo editování příspěvku se musíte přihlásit.');
    }
    $values = $form->getValues();
    $this->strankyModel->updateStranka($values->id, $values);
    $this->flashMessage('Text byl editován', 'info');
    $this->redirect('Stranky:edit',$this->getParam('url'));
}

  
}
