ACC SHELL

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

<?php

/**
 * Ucet presenter.
 */
class UcetPresenter extends BasePresenter
{

    private $name = 'můj účet';
    private $presenter = 'Ucet';
    private $table = 'uzivatele';
    private $resource = 'ucet';

    public function renderDefault()
    {
        if (!$this->user->isAllowed($this->resource, 'edit'))
        {
            $this->flashMessage("Ke správě účtů nemáš oprávnění!", 'error');
            $this->redirectToLogin($this->getName(), $this->request->parameters);
        }
    }

	public function renderSpravovat($id)
	{
        if (!$this->user->isAllowed($this->resource, 'edit', $this->user->identity->id, $id))
        {
            $this->flashMessage("Ke správě tohoto účtu nemáš oprávnění!", 'error');
            $this->redirectToLogin($this->getName(), $this->request->parameters);
        }

        if (file_exists('images/hraci/' . $id . '.png'))
        {
            $this->template->fotka = $id;
        }
        else
        {
            $this->template->fotka = 'default-profile';
        }
	}

    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');

        return $form;
    }

    public function rotatePhotoCWFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
    {
        $values = $submitButton->getForm()->getValues();
        $id = $values['id'];
        if ($this->user->isAllowed($this->resource, 'edit', @$this->user->identity->id, $values->id))
        {
            $image = \Nette\Image::fromFile("images/hraci/" . $id . ".png");
            $image->rotate(270, 0);
            $image->save("images/hraci/" . $id . ".png");
            $this->flashMessage('Fotka byla otočena! Pokud ji vidíš neotočenou, zkus Ctrl + R.', 'success');
            $this->redirect("$this->presenter:spravovat", $id);
        }
        else
        {
            $this->flashMessage("Ke správě tohoto účtu nemáš oprávnění!", 'error');
            $this->redirectToLogin($this->getName(), $this->request->parameters);
        }
    }

    public function rotatePhotoCCWFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
    {
        $values = $submitButton->getForm()->getValues();
        $id = $values['id'];
        if ($this->user->isAllowed($this->resource, 'edit', @$this->user->identity->id, $values->id))
        {
            $image = \Nette\Image::fromFile("images/hraci/" . $id . ".png");
            $image->rotate(90, 0);
            $image->save("images/hraci/" . $id . ".png");
            $this->flashMessage('Fotka byla otočena! Pokud ji vidíš neotočenou, zkus Ctrl + R.', 'success');
            $this->redirect("$this->presenter:spravovat", $id);
        }
        else
        {
            $this->flashMessage("Ke správě tohoto účtu nemáš oprávnění!", 'error');
            $this->redirectToLogin($this->getName(), $this->request->parameters);
        }
    }

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

        $form->addHidden('id', $this->getParam('id'));
        $form->addUpload('photo', '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 2 MB.', 2 * 1024 * 1024 /* v bytech */);
        if ($this->user->isAllowed('ucet', 'edit') && !($this->user->id == $this->getParam('id')))
        {
            $form->addText('email', 'Email:', 30)
                ->setAttribute('class', 'qtip exclamationmark')
                ->setAttribute('title', 'Pokud vymažeš/nevložíš email, uživatel se nebude moci přihlásit!')
                ->addCondition(Nette\Application\UI\Form::FILLED)
                ->addRule(Nette\Application\UI\Form::EMAIL, 'Zadej platný email!');
        }
        else
        {
            $form->addText('email', 'Email:', 30)
                ->addRule(Nette\Application\UI\Form::EMAIL, 'Zadej platný email!');
        }
        $form->addText('jmeno', 'Jméno:', 30)
            ->setRequired('Jméno nesmí zůstat prázdné.');
        $form->addText('prijmeni', 'Příjmení:', 30)
            ->setRequired('Příjmení nesmí zůstat prázdné.');
        $form->addText('prezdivka', 'Přezdívka:', 20);
        $form->addDatePicker('clenemOd', 'Členem od:');
        $form->addDatePicker('narozen', 'Narozen:');
        $form->addText('telefon', 'Telefonní číslo:');
        $form->addText('typickaVlastnost', 'Typická vlastnost:');
        $form->addHidden('typickaVlastnostZmenena');
        if ($this->user->identity->id == $this->getParam('id'))
        {
            $form->addSelect('aktivni', 'Status:', array(1 => 'Hraju!', 2 => 'Nehraju :-('));
        }
        else
        {
            $form->addSelect('aktivni', 'Status:', array(1 => 'Hraje!', 2 => 'Nehraje :-('));
        }

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

        $typickaVlastnostZmenena = $this->db->table($this->table)->get($this->getParam('id'))->typickaVlastnostZmenena;
        $typickaVlastnostMuzeBytZmenena = clone $typickaVlastnostZmenena;
        $typickaVlastnostMuzeBytZmenena->add(DateInterval::createFromDateString($this->typickaVlastnostProdleva));
        if ($typickaVlastnostMuzeBytZmenena > new DateTime())
        {
            $keZmeneZbyva = $typickaVlastnostMuzeBytZmenena->diff(new DateTime());
            $form['typickaVlastnost']
                ->setAttribute('readonly', 'readonly')
                ->setAttribute('class', 'qtip questionmark')
                ->setAttribute('title', 'Půjde znovu změnit až za ' . $keZmeneZbyva->format("%d dny, %h hod a %i min") . '.');
        }
        else
        {
            $form['typickaVlastnost']
                ->setAttribute('class', 'qtip exclamationmark')
                ->setAttribute('title', 'Pokud upravíš tuto vlastnost, půjde znovu změnit až za ' . DateInterval::createFromDateString($this->typickaVlastnostProdleva)->format("%d") . ' dny.');
        }

        if ($defaults["clenemOd"]) {
            $defaults["clenemOd"] = $defaults["clenemOd"]->format('d. m. Y');
        } else {
            $defaults["clenemOd"] = '';
        }

        if ($defaults["narozen"]) {
            $defaults["narozen"] = $defaults["narozen"]->format('d. m. Y');
        } else {
            $defaults["narozen"] = '';
        }

        $form->setDefaults($defaults);

        $form->addSubmit('submit', 'Uložit')
            ->onClick[] = callback($this, 'ucetFormSubmitted');

        return $form;
    }

    public function ucetFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
    {
        $values = $submitButton->getForm()->getValues();
        $id = $values['id'];
        unset($values['id']);

        if (strlen($values["email"]) == 0)
        {
            $values["email"] = null;
        }

        $photo = $values['photo'];
        if ($photo->error == 0)
        {
            try {
                $image = $photo->toImage();
                $image->resize(250, 250);
                $image->save('images/hraci/' . $id . '.png', 100, IMAGETYPE_PNG);
            } catch (Nette\UnknownImageFileException $e) {
                $this->flashMessage('Nahrání obrázku selhalo. Povolené přípony jsou JPG, GIF a PNG.', 'error');
            }
        }
        unset($values['photo']);

        if ($this->db->table($this->table)->get($id)->typickaVlastnost != $values['typickaVlastnost'])
        {
            $values['typickaVlastnostZmenena'] = date('Y-m-d H:i');
        }
        else
        {
            unset($values['typickaVlastnostZmenena']);
        }

        if (strlen($values["prezdivka"]) == 0)
        {   // prezdivka by nemela byt prazdna
            $values["prezdivka"] = $values["jmeno"];
        }

        if ($values["clenemOd"]) {
            $values['clenemOd'] = DateTime::createFromFormat('d. m. Y', $values["clenemOd"])->format('Y-m-d');
        }
        if ($values['narozen']) {
            $values['narozen'] = DateTime::createFromFormat('d. m. Y', $values["narozen"])->format('Y-m-d');
        } else {
            $values['narozen'] = NULL;
        }

        $this->db->table($this->table)->get($id)->update($values);
        $this->flashMessage('Údaje byly uloženy!', 'success');
        if ($this->user->isAllowed($this->resource, 'edit'))
        {
            $this->redirect("$this->presenter:");
        }
        else
        {
            $this->redirect("$this->presenter:spravovat", $this->user->id);
        }
    }

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

        $form->addHidden('id', $this->getParam('id'));
        $form->addPassword('heslo', 'Heslo:', 20)
            ->setRequired('Vyplň heslo, chceš-li ho změnit.')
            ->addRule(Nette\Application\UI\Form::MIN_LENGTH, 'Heslo musí mít alespoň %d znaky.', 4);
        $form->addPassword('heslo2', 'Heslo (podruhé):', 20)
            ->setRequired('Zadej heslo ještě jednou pro kontrolu.')
            ->addRule(Nette\Application\UI\Form::EQUAL, 'Hesla se neshodují.', $form['heslo']);

        $form->addSubmit('submit', 'Změnit')
            ->onClick[] = callback($this, 'zmenaHeslaFormSubmitted');

        return $form;
    }

    public function zmenaHeslaFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
    {
        $values = $submitButton->getForm()->getValues();

        $id = $values['id'];
        unset($values['id']);

        $values["heslo"] = md5($values["heslo"]);
        unset($values["heslo2"]);

        $this->db->table($this->table)->get($id)->update($values);
        $this->flashMessage('Heslo bylo změněno!', 'success');
        $this->redirect("$this->presenter:");
    }

}

ACC SHELL 2018