ACC SHELL

Path : /srv/www/vhosts/duklavysco/app/presenters/
File Upload :
Current File : //srv/www/vhosts/duklavysco/app/presenters/ForumPresenter.php

<?php

/**
 * Akce presenter.
 */
class ForumPresenter extends BasePresenter
{
    
    private $name = 'zpráva';
    private $presenter = 'Forum';
    private $table = 'forum';
    private $resource = 'zprava';
    
	public function renderDefault()
	{
        if ($this->user->isAllowed($this->resource, 'view'))
        {
            $this->template->zpravy = $this->db->table($this->table)->order('datum DESC');
            $this->template->users = $this->db->table('uzivatele');
        }
        else
        {
            $this->flashMessage("K prohlížení sekce Fórum nemáš oprávnění!", 'error');
            $this->redirectToLogin($this->getName(), $this->request->parameters);
        }
	}
    
    public function renderSmazat($id)
    {
        if ($this->user->isAllowed($this->resource, 'edit', @$this->user->identity->id, $id))
        {
            $this->db->table($this->table)->get($id)->delete();
            $this->flashMessage(ucfirst($this->name) . ' byla smazána!', 'success');
            $this->redirect("$this->presenter:");
        }
        else
        {
            $this->flashMessage("Ke smazání této $this->name nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:");
        }
    }
    
    public function renderUpravit($id)
    {
        if (!$this->user->isAllowed($this->resource, 'edit', @$this->user->identity->id, $id))
        {
            $this->flashMessage("K editaci této $this->name nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:");
        }
    }
    
    protected function createComponentEditForm()
    {
        $form = new Nette\Application\UI\Form;
        
        $form->addHidden('id');
//        $form->addText('nazev', 'Název:', 40)
//            ->setRequired('Název nesmí být prázdný.');
        $form->addTextArea('popis', 'Text:')
            ->setRequired('Text zprávy nesmí být prázdný.');

        $defaults = $this->db->table($this->table)->get($this->getParam('id'))->toArray();
        $form->setDefaults($defaults);
        
        $form->addSubmit('edit', 'Uložit')
            ->onClick[] = callback($this, 'editFormSubmitted');
        $presenter = $this;
        $form->addSubmit('cancel', 'Zrušit')
            ->setValidationScope(FALSE)
            ->onClick[] = function() use ($presenter) {$presenter->redirect("Forum:");};
        
        return $form;
    }
    
    public function editFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
    {
        $values = $submitButton->getForm()->getValues();
        if ($this->user->isAllowed($this->resource, 'edit', @$this->user->identity->id, $values->id))
        {
            $this->db->table($this->table)->get($values->id)->update($values);
            $this->flashMessage(ucfirst($this->name) . ' byla upravena!', 'success');
            $this->redirect("$this->presenter:");
        }
        else
        {
            $this->flashMessage("K editaci této $this->name nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:");
        }
    }
    
    public function renderNapsat()
    {
        if (!$this->user->isAllowed($this->resource, 'add'))
        {
            $this->flashMessage("K napsání $this->name nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:");
        }
    }
    
    protected function createComponentAddForm()
    {
        $form = new Nette\Application\UI\Form;
        

        $form->addHidden('vlozil', $this->user->identity->id);
        $form->addHidden('datum', date('d. m. Y H:i'));
//        $form->addText('nazev', 'Název:', 40)
//            ->setRequired('Název nesmí být prázdný.');
        $form->addTextArea('popis', 'Text:')
            ->setRequired('Zadej text zprávy.');
        $form->addSubmit('add', 'Vložit')
            ->onClick[] = callback($this, 'addFormSubmitted');
        $presenter = $this;
        $form->addSubmit('cancel', 'Zrušit')
            ->setValidationScope(FALSE)
            ->onClick[] = function() use ($presenter) {$presenter->redirect("Forum:");};
        
        return $form;
    }
    
    public function addFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
    {
        if ($this->user->isAllowed($this->resource, 'add'))
        {
            $values = $submitButton->getForm()->getValues();
            $values["datum"] = DateTime::createFromFormat('d. m. Y H:i', $values["datum"])->format('Y-m-d H:i');
            $this->db->table($this->table)->insert($values);
            $this->flashMessage(ucfirst($this->name) . ' byla vložena!');
            $this->redirect("$this->presenter:");
        }
        else
        {
            $this->flashMessage("Ke vložení $this->name nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:");
        }
    }
    
}

ACC SHELL 2018