%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /www/varak.net/catalog.varak.net/app/presenters/
Upload File :
Create Path :
Current File : //www/varak.net/catalog.varak.net/app/presenters/HomepagePresenter.php

<?php

use Nette\Application\UI\Form;

/**
 * Homepage presenter.
 */
class HomepagePresenter extends BasePresenter
{

    private $model;

    public function injectModels(Bookshelf $model)
    {
        $this->model = $model;
    }

	public function renderDefault()
	{
		$this->template->anyVariable = 'any value';
	}

    protected function createComponentAddForm()
    {
        $httpRequest = $this->getHttpRequest();
        $authorId = $httpRequest->getCookie('authorId');
        $genreId = $httpRequest->getCookie('genreId');
        $languageId = $httpRequest->getCookie('languageId');
        $authors = $this->model->getAuthorsList();
        $languages = $this->model->getLanguagesList();
        $genres = $this->model->getGenresList();

        $form = new Form();
        $form->addText("name", 'Nazev knihy', 40, 255)->addRule(Form::FILLED, 'Je nutné zadat název.');
        $form->addSelect('author', 'Autor', $authors)->setDefaultValue($authorId);
        $form->addSelect('genre', 'Žánr', $genres)->setDefaultValue($genreId);
        $form->addSelect('language', 'Jazyk', $languages)->setDefaultValue($languageId);
        $form->addUpload('file', 'Soubor s knihou')->addRule(Form::MAX_FILE_SIZE, 'Soubor je příliš velký! Povolená velikost je 32M.', 32 * 1024 * 1024);
        $form->addSubmit('create', 'Vytvořit');
        $form->onSuccess[] = [$this, 'addFormSubmitted'];
        return $form;
    }

    protected function createComponentAddAuthorForm()
    {
        $form = new Form();
        $form->addText("surname", 'Příjmení autora', 40, 60)->addRule(Form::FILLED, 'Je nutné zadat příjmení.')->addRule(Form::MAX_LENGTH, 'Maximální délka příjmení je 60 znaků', 60);
        $form->addText("name", 'Jméno autora', 40, 255)->addRule(Form::FILLED, 'Je nutné zadat jméno.')->addRule(Form::MAX_LENGTH, 'Maximální délka jména je 255 znaků', 255);
        $form->addSubmit('create', 'Vytvořit');
        $form->onSuccess[] = [$this, 'addAuthorFormSubmitted'];
        return $form;
    }

    protected function createComponentAddGenreForm()
    {
        $form = new Form();
        $form->addText("name", 'Název žánru', 40, 255)->addRule(Form::FILLED, 'Je nutné zadat název.')->addRule(Form::MAX_LENGTH, 'Maximální délka názvu žánru je 255 znaků', 255);
        $form->addSubmit('create', 'Vytvořit');
        $form->onSuccess[] = [$this, 'addGenreFormSubmitted'];
        return $form;
    }

    protected function createComponentAddLanguageForm()
    {
        $form = new Form();
        $form->addText("tag", 'Zkratka', 5, 2)->addRule(Form::FILLED, 'Je nutné zadat zkratku.')->addRule(Form::LENGTH, 'Délka zkratky jsou dva znaky', 2);
        $form->addText("name", 'Jazyk', 40, 50)->addRule(Form::FILLED, 'Je nutné zadat název.')->addRule(Form::MAX_LENGTH, 'Maximální délka jazyka je 50 znaků', 50);
        $form->addSubmit('create', 'Vytvořit');
        $form->onSuccess[] = [$this, 'addLanguageFormSubmitted'];
        return $form;
    }

    public function addAuthorFormSubmitted(Form $form)
    {
        if ($form->isValid())
        {
            // submitted and valid
            $values = $form->getValues();
            $this->model->addAuthor($values['surname'], $values['name']);
            $this->redirect(302, "Homepage:add");
        }
    }

    public function addGenreFormSubmitted(Form $form)
    {
        if ($form->isValid())
        {
            // submitted and valid
            $values = $form->getValues();
            $this->model->addGenre($values['name']);
            $this->redirect(302, "Homepage:add");
        }
    }

    public function addLanguageFormSubmitted(Form $form)
    {
        if ($form->isValid())
        {
            // submitted and valid
            $values = $form->getValues();
            $this->model->addLanguage($values['name'], $values['tag']);
            $this->redirect(302, "Homepage:add");
        }
    }

    public function addFormSubmitted(Form $form)
    {
        if ($form->isValid())
        {
            // submitted and valid
            $values = $form->getValues();

            if ($values['file']->isOk())
            {
                $newid = $this->model->addBook($values['name'], $values['author'], $values['genre'], $values['language']);
                $httpResponse = $this->getHttpResponse();
                // Set cookies
                $httpResponse->setCookie('authorId', $values['author'], 0);
                $httpResponse->setCookie('genreId', $values['genre'], 0);
                $httpResponse->setCookie('languageId', $values['language'], 0);

                // Determine shard no.
                $shard = floor($newid / 1000);

                $fname = __DIR__."/../books/$shard/$newid.epub";
                $mfname = __DIR__."/../books/$shard/$newid.mobi";
		$fbname = __DIR__."/../books/$shard/$newid.fb2";
                if(!file_exists(__DIR__."/../books/$shard/"))
                {
                    // shard directory doesn't exist, create new one
                    mkdir(__DIR__."/../books/$shard/");
                }
                $values['file']->move($fname);
                exec("ebook-convert $fname $mfname >> /tmp/convert.log 2>> /tmp/convert2.log &");
		exec("ebook-convert $fname $fbname >> /tmp/convert.log 2>> /tmp/convert2.log &");
                $this->redirect(302, "Homepage:add");
            }
        }
    }

}

Zerion Mini Shell 1.0