ACC SHELL

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

<?php

/**
 * Fotogalerie presenter.
 */
class FotogaleriePresenter extends BasePresenter
{

    private $name = 'fotogalerie';
    private $presenter = 'Fotogalerie';
    private $tablePhoto = 'fotky';
    private $tableAlbum = 'alba';
    private $resourcePhoto = 'fotka';
    private $resourceAlbum = 'album';

	public function renderDefault()
	{
        if ($this->user->isAllowed($this->resourceAlbum, 'view'))
        {
            $this->template->alba = $this->db->table($this->tableAlbum)->order('datum DESC, nazev DESC');
            $this->template->fotky = array();
            foreach ($this->template->alba as $album)
            {
                $this->template->fotky[$album->id] = $this->db->table($this->tablePhoto)->where('album', $album->id)->count();
            }
        }
        else
        {
            $this->flashMessage("K prohlížení sekce " . ucfirst($this->name) . " nemáš oprávnění!", 'error');
            $this->redirectToLogin($this->getName(), $this->request->parameters);
        }
	}

    public function renderAlbum($id)
    {
        if ($this->user->isAllowed($this->resourceAlbum, 'view'))
        {
            $this->template->album = $this->db->table($this->tableAlbum)->where('id', $id)->fetch();
            $this->template->fotky = $this->db->table($this->tablePhoto)->where('album', $id)->order('vlozeno');
        }
        else
        {
            $this->flashMessage("K prohlížení tohoto alba nemáš oprávnění!", 'error');
            $this->redirectToLogin($this->getName(), $this->request->parameters);
        }
    }

    public function renderSmazatAlbum($id)
    {
        if ($this->user->isAllowed($this->resourceAlbum, 'edit', @$this->user->identity->id, $id))
        {
            if ($this->db->table($this->tablePhoto)->where('album', $id)->count() == 0)
            {
                $this->db->table($this->tableAlbum)->get($id)->delete();
                $this->flashMessage('Album bylo smazáno!', 'success');
                $this->redirect("$this->presenter:");
            }
            else
            {
                $this->flashMessage('Album musí být prázdné, aby jej bylo možné smazat.', 'error');
                $this->redirect("$this->presenter:");
            }
        }
        else
        {
            $this->flashMessage("Ke smazání tohoto alba nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:");
        }
    }

    public function renderSmazatFotku($id)
    {
        if ($this->user->isAllowed($this->resourcePhoto, 'edit', @$this->user->identity->id, $id))
        {
            $albumId = $this->db->table($this->tablePhoto)->get($id)->album;
            $album = $this->db->table($this->tableAlbum)->get($albumId);
            $uvodniFotka = $album->uvodniFotka;
            if ($uvodniFotka == $id)
            {
                $album->update(array('uvodniFotka' => null));
                $this->flashMessage('Smazal jsi fotografii, která byla obalem alba!', 'info');
            }
            $this->db->table($this->tablePhoto)->get($id)->delete();
            unlink('images/fotogalerie/' . $id . '.jpg');
            unlink('images/fotogalerie/thumb_' . $id . '.jpg');
            $this->flashMessage('Fotka byla smazána!', 'success');
            $this->redirect("$this->presenter:album", $albumId);
        }
        else
        {
            $this->flashMessage("Ke smazání této fotky nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:");
        }
    }

    public function renderUpravitAlbum($id)
    {
        if (!$this->user->isAllowed($this->resourceAlbum, 'edit', @$this->user->identity->id, $id))
        {
            $this->flashMessage("K editaci tohoto alba nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:");
        }
        $this->template->album = $id;
    }

    public function renderUpravitFotku($id)
    {
        if (!$this->user->isAllowed($this->resourcePhoto, 'edit', @$this->user->identity->id, $id))
        {
            $this->flashMessage("K editaci této fotky nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:album", $this->db->table($this->resourcePhoto)->get($id)->album);
        }
        $this->template->fotka = $this->db->table($this->tablePhoto)->get($id)->id;
    }

    public function renderNastavitJakoObalAlba($id)
    {
        if (!$this->user->isAllowed($this->resourceAlbum, 'edit', @$this->user->identity->id, $id))
        {
            $this->flashMessage("K editaci tohoto alba nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:");
        }
        $this->db->table($this->tableAlbum)->get($this->db->table($this->tablePhoto)->get($id)->album)->update(array('uvodniFotka' => $id));
        $this->flashMessage("Úvodní fotka alba nastavena!", 'success');
        $this->redirect("$this->presenter:");
    }

    protected function createComponentEditPhotoForm()
    {
        $form = new Nette\Application\UI\Form;

        $form->addHidden('id', $this->getParam('id'));
        $form->addHidden('vlozil', $this->user->identity->id);
        $form->addText('popis', 'Popis:', 40);

        $defaults = $this->db->table($this->tablePhoto)->get($this->getParam('id'))->toArray();
        $form->setDefaults($defaults);

        $form->addSubmit('edit', 'Uložit')
            ->onClick[] = callback($this, 'editPhotoFormSubmitted');
        $presenter = $this;
        $album = $this->db->table($this->tablePhoto)->get($this->getParam('id'))->album;
        $form->addSubmit('cancel', 'Jít zpět')
            ->setValidationScope(FALSE)
            ->onClick[] = function() use ($presenter, $album) {$presenter->redirect("Fotogalerie:album", $album);};

        return $form;
    }

    protected function createComponentRotatePhotoForm()
    {
        $form = new Nette\Application\UI\Form;

        $form->addHidden('id', $this->getParam('id'));

        $form->addSubmit('cw', 'Otočit po směru')
            ->onClick[] = callback($this, 'rotatePhotoCWFormSubmitted');
        $form->addSubmit('ccw', 'Otočit proti směru')
            ->onClick[] = callback($this, 'rotatePhotoCCWFormSubmitted');
        $presenter = $this;
        $album = $this->db->table($this->tablePhoto)->get($this->getParam('id'))->album;
        $form->addSubmit('cancel', 'Jít zpět')
            ->setValidationScope(FALSE)
            ->onClick[] = function() use ($presenter, $album) {$presenter->redirect("Fotogalerie:album", $album);};

        return $form;
    }

    protected function createComponentEditAlbumForm()
    {
        $form = new Nette\Application\UI\Form;

        $form->addHidden('id');
        $form->addDatePicker('datum', 'Datum:');
        $form->addText('nazev', 'Název:', 40)
            ->setRequired('Název nesmí být prázdný.');
        $form->addTextArea('popis', 'Popis:')
            ->setRequired('Zadej alespoň stručný popis akce.');

        if (in_array('administrator', $this->getUser()->identity->roles))
        {
            $users = array();
            $users[$this->user->identity->id] = 'já';
            foreach ($this->template->uzivatele as $uzivatel)
            {
                if ($uzivatel->id != $this->user->identity->id)
                    $users[$uzivatel->id] = "$uzivatel->prijmeni, $uzivatel->jmeno";
            }
            $form->addSelect('vlozil', 'Majitel:', $users);
        }

        $defaults = $this->db->table($this->tableAlbum)->get($this->getParam('id'))->toArray();
        if (strlen($defaults["datum"]) > 0)
        {
            $defaults["datum"] = $defaults["datum"]->format('d. m. Y');
        }
        $form->setDefaults($defaults);

        $form->addHidden('upravil', $this->user->identity->id);

        $form->addSubmit('edit', 'Uložit')
            ->onClick[] = callback($this, 'editAlbumFormSubmitted');
        $presenter = $this;
        $form->addSubmit('cancel', 'Zrušit')
            ->setValidationScope(FALSE)
            ->onClick[] = function() use ($presenter) {$presenter->redirect("Fotogalerie:");};

        return $form;
    }

    public function editAlbumFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
    {
        $values = $submitButton->getForm()->getValues();
        if (strlen($values["datum"]) > 0)
        {
            $values["datum"] = DateTime::createFromFormat('d. m. Y', $values["datum"])->format('Y-m-d');
        }
        else
        {
            $values["datum"] = NULL;
        }
        if ($this->user->isAllowed($this->resourceAlbum, 'edit', @$this->user->identity->id, $values->id))
        {
            $this->db->table($this->tableAlbum)->get($values->id)->update($values);
            $this->flashMessage('Album bylo upraveno!', '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 editPhotoFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
    {
        $values = $submitButton->getForm()->getValues();
        if ($this->user->isAllowed($this->resourcePhoto, 'edit', @$this->user->identity->id, $values->id))
        {
            $this->db->table($this->tablePhoto)->get($values->id)->update($values);
            $this->flashMessage('Fotka byla upravena!', 'success');
            $this->redirect("$this->presenter:upravitFotku", $values['id']);
        }
        else
        {
            $this->flashMessage("K editaci této fotky nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:album", $this->db->table($this->tablePhoto)->get($values['id'])->album);
        }
    }

    public function rotatePhotoCWFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
    {
        $values = $submitButton->getForm()->getValues();
        $id = $values['id'];
        if ($this->user->isAllowed($this->resourcePhoto, 'edit', @$this->user->identity->id, $values->id))
        {
            $image = \Nette\Image::fromFile("images/fotogalerie/" . $id . ".jpg");
            $image->rotate(270, 0);
            $image->save("images/fotogalerie/" . $id . ".jpg");
            $imageThumb = \Nette\Image::fromFile("images/fotogalerie/thumb_" . $id . ".jpg");
            $imageThumb->rotate(270, 0);
            $imageThumb->save("images/fotogalerie/thumb_" . $id . ".jpg");
            $this->flashMessage('Fotka byla otočena! Pokud ji vidíš neotočenou, zkus Ctrl + R.', 'success');
            $this->redirect("$this->presenter:upravitFotku", $id);
        }
        else
        {
            $this->flashMessage("K editaci této fotky nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:album", $this->db->table($this->tablePhoto)->get($values['id'])->album);
        }
    }

    public function rotatePhotoCCWFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
    {
        $values = $submitButton->getForm()->getValues();
        $id = $values['id'];
        if ($this->user->isAllowed($this->resourcePhoto, 'edit', @$this->user->identity->id, $values->id))
        {
            $image = \Nette\Image::fromFile("images/fotogalerie/" . $id . ".jpg");
            $image->rotate(90, 0);
            $image->save("images/fotogalerie/" . $id . ".jpg");
            $imageThumb = \Nette\Image::fromFile("images/fotogalerie/thumb_" . $id . ".jpg");
            $imageThumb->rotate(90, 0);
            $imageThumb->save("images/fotogalerie/thumb_" . $id . ".jpg");
            $this->flashMessage('Fotka byla otočena! Pokud ji vidíš neotočenou, zkus Ctrl + R.', 'success');
            $this->redirect("$this->presenter:upravitFotku", $id);
        }
        else
        {
            $this->flashMessage("K editaci této fotky nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:album", $this->db->table($this->tablePhoto)->get($values['id'])->album);
        }
    }

    public function renderVytvorit()
    {
        if (!$this->user->isAllowed($this->resourceAlbum, 'add'))
        {
            $this->flashMessage("K vytvoření alba nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:");
        }
    }

    protected function createComponentCreateAlbumForm()
    {
        $form = new Nette\Application\UI\Form;

        $form->addHidden('upravil', $this->user->identity->id);
        $form->addDatePicker('datum', 'Datum konání:');
        $form->addText('nazev', 'Název alba:', 40)
            ->setRequired('Název nesmí být prázdný.');
        $form->addTextArea('popis', 'Popis akce:')
            ->setRequired('Zadej alespoň stručný popis.');

        if (in_array('administrator', $this->getUser()->identity->roles))
        {
            $users = array();
            $users[$this->user->identity->id] = 'já';
            foreach ($this->template->uzivatele as $uzivatel)
            {
                if ($uzivatel->id != $this->user->identity->id)
                    $users[$uzivatel->id] = "$uzivatel->prijmeni, $uzivatel->jmeno";
            }
            $form->addSelect('vlozil', 'Majitel:', $users)
                ->setAttribute('class', 'qtip questionmark')
                ->setAttribute('title', "Jako administrátor můžeš určit jiného vlastníka alba než sám sebe. Upravovat album budou moci nejen všichni administrátoři, ale i zvolený 'majitel'.");

        }
        else
        {
            $form->addHidden('vlozil', $this->user->identity->id);
        }

        $form->addSubmit('add', 'Vytvořit')
            ->onClick[] = callback($this, 'createAlbumFormSubmitted');
        $presenter = $this;
        $form->addSubmit('cancel', 'Zrušit')
            ->setValidationScope(FALSE)
            ->onClick[] = function() use ($presenter) {$presenter->redirect("Fotogalerie:");};

        return $form;
    }

    public function createAlbumFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
    {
        if ($this->user->isAllowed($this->resourceAlbum, 'add'))
        {
            $values = $submitButton->getForm()->getValues();
            if (strlen($values["datum"]) > 0)
            {
                $values["datum"] = DateTime::createFromFormat('d. m. Y', $values["datum"])->format('Y-m-d');
            }
            else
            {
                unset($values["datum"]);
            }
            $this->db->table($this->tableAlbum)->insert($values);
            $this->flashMessage('Album bylo vytvořeno!');
            $this->redirect("$this->presenter:");
        }
        else
        {
            $this->flashMessage("K vytvoření alba nemáš oprávnění!", 'error');
            $this->redirect("$this->presenter:");
        }
    }

    public function renderPridatFotky($id)
    {
        $this->template->nazevAlba = $this->db->table($this->tableAlbum)->get($id)->nazev;
    }

    protected function createComponentAddPhotoForm()
    {
        $form = new Nette\Application\UI\Form;

        $form->addHidden('vlozil', $this->user->identity->id);
        $form->addHidden('album', $this->getParam('id'));
        $form->addUpload('photo1', 'Fotka:')
            ->addCondition(Nette\Application\UI\Form::FILLED)
            ->addRule(Nette\Application\UI\Form::IMAGE, 'Fotka musí být formátu JPEG, PNG nebo GIF.')
            ->addRule(Nette\Application\UI\Form::MAX_FILE_SIZE, 'Maximální velikost souboru je 5 MB.', 5 * 1024 * 1024 /* v bytech */);
        $form->addText('popis1', 'Popis:', 60)
                ->setAttribute('class', 'qtip questionmark')
                ->setAttribute('title', 'Pokud necháš prázdné, použije se jako popis název souboru fotografie.');
        $form->addUpload('photo2', 'Fotka:')
            ->addCondition(Nette\Application\UI\Form::FILLED)
            ->addRule(Nette\Application\UI\Form::IMAGE, 'Fotka musí být formátu JPEG, PNG nebo GIF.')
            ->addRule(Nette\Application\UI\Form::MAX_FILE_SIZE, 'Maximální velikost souboru je 5 MB.', 5 * 1024 * 1024 /* v bytech */);
        $form->addText('popis2', 'Popis:', 60)
                ->setAttribute('class', 'qtip questionmark')
                ->setAttribute('title', 'Pokud necháš prázdné, použije se jako popis název souboru fotografie.');
        $form->addUpload('photo3', 'Fotka:')
            ->addCondition(Nette\Application\UI\Form::FILLED)
            ->addRule(Nette\Application\UI\Form::IMAGE, 'Fotka musí být formátu JPEG, PNG nebo GIF.')
            ->addRule(Nette\Application\UI\Form::MAX_FILE_SIZE, 'Maximální velikost souboru je 5 MB.', 5 * 1024 * 1024 /* v bytech */);
        $form->addText('popis3', 'Popis:', 60)
                ->setAttribute('class', 'qtip questionmark')
                ->setAttribute('title', 'Pokud necháš prázdné, použije se jako popis název souboru fotografie.');
        $form->addUpload('photo4', 'Fotka:')
            ->addCondition(Nette\Application\UI\Form::FILLED)
            ->addRule(Nette\Application\UI\Form::IMAGE, 'Fotka musí být formátu JPEG, PNG nebo GIF.')
            ->addRule(Nette\Application\UI\Form::MAX_FILE_SIZE, 'Maximální velikost souboru je 5 MB.', 5 * 1024 * 1024 /* v bytech */);
        $form->addText('popis4', 'Popis:', 60)
                ->setAttribute('class', 'qtip questionmark')
                ->setAttribute('title', 'Pokud necháš prázdné, použije se jako popis název souboru fotografie.');
        $form->addUpload('photo5', 'Fotka:')
            ->addCondition(Nette\Application\UI\Form::FILLED)
            ->addRule(Nette\Application\UI\Form::IMAGE, 'Fotka musí být formátu JPEG, PNG nebo GIF.')
            ->addRule(Nette\Application\UI\Form::MAX_FILE_SIZE, 'Maximální velikost souboru je 5 MB.', 5 * 1024 * 1024 /* v bytech */);
        $form->addText('popis5', 'Popis:', 60)
                ->setAttribute('class', 'qtip questionmark')
                ->setAttribute('title', 'Pokud necháš prázdné, použije se jako popis název souboru fotografie.');
        $form->addSubmit('add', 'Přidat fotky')
            ->onClick[] = callback($this, 'addPhotoFormSubmitted');
        $presenter = $this;
        $form->addSubmit('cancel', 'Zpět do alba')
            ->setValidationScope(FALSE)
            ->onClick[] = function() use ($presenter) {$presenter->redirect("Fotogalerie:album", $presenter->getParam('id'));};

        return $form;
    }

    public function addPhotoFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
    {
        $values = $submitButton->getForm()->getValues();
        $photos[] = $values['photo1'];
        $photos[] = $values['photo2'];
        $photos[] = $values['photo3'];
        $photos[] = $values['photo4'];
        $photos[] = $values['photo5'];
        unset($values['photo1']);
        unset($values['photo2']);
        unset($values['photo3']);
        unset($values['photo4']);
        unset($values['photo5']);

        $popisy[] = $values['popis1'];
        $popisy[] = $values['popis2'];
        $popisy[] = $values['popis3'];
        $popisy[] = $values['popis4'];
        $popisy[] = $values['popis5'];
        unset($values['popis1']);
        unset($values['popis2']);
        unset($values['popis3']);
        unset($values['popis4']);
        unset($values['popis5']);

        foreach ($photos as $number => $photo)
        {

            if ($photo->error == 0)
            {
                $popis = $popisy[$number];
                if (strlen($popis) == 0)
                {
                    $values['popis'] = $photo->name;
                }
                else
                {
                    $values['popis'] = $popis;
                }

                $this->db->table($this->tablePhoto)->insert($values);
            }

            switch ($photo->error)
            {
                case 0:
                    try {
                        $image = $photo->toImage();
                        $image->resize(1200, 1200);
                        $image->save('images/fotogalerie/' . $this->db->lastInsertId() . '.jpg', 100, IMAGETYPE_JPEG);
                        $image->resize(250, 250);
                        $image->save('images/fotogalerie/thumb_' . $this->db->lastInsertId() . '.jpg', 100, IMAGETYPE_JPEG);
                    } catch (Nette\UnknownImageFileException $e) {
                        $this->flashMessage('Nahrání obrázku selhalo. Povolené přípony jsou JPG, GIF a PNG.', 'error');
                    }
                    $this->flashMessage("Fotografie '$photo->name' byla úspěšně přidána!", 'success');
                    break;
                case 4:
                    break;
                default:
                    $this->flashMessage("Nahrání obrázku '$photo->name' selhalo. Povolené přípony jsou JPG, GIF a PNG.", 'error');
            }

        }
        $this->redirect("$this->presenter:pridatFotky", $values['album']);
    }

}

ACC SHELL 2018