Skip to content

Instantly share code, notes, and snippets.

@pohy
Created November 21, 2013 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pohy/7585242 to your computer and use it in GitHub Desktop.
Save pohy/7585242 to your computer and use it in GitHub Desktop.
Nette example application
<?php
use Nette\Application\UI\Form;
class TaskPresenter extends BasePresenter
{
private $listRepository;
private $userRepository;
private $taskRepository;
private $list;
protected function startup()
{
parent::startup();
if (!$this->getUser()->isLoggedIn()) {
$this->redirect('Sign:in');
}
$this->listRepository = $this->context->listRepository;
$this->userRepository = $this->context->userRepository;
$this->taskRepository = $this->context->taskRepository;
}
public function actionDefault($id)
{
$this->list = $this->listRepository->find($id);
if($this->list === FALSE)
$this->setView('notFound');
}
public function renderDefault()
{
$this->template->list = $this->list;
}
protected function createComponentTaskForm($name)
{
$userPairs = $this->userRepository->findAll()->fetchPairs('id', 'name');
$form = new Form($this, $name);
$form->addText('text', 'Úkol:', 40, 100)->addRule(Form::FILLED, 'Je nutné zadat text úkolu.');
$form->addSelect('userId', 'Pro:', $userPairs)->setPrompt('- Vyberte -')->addRule(Form::FILLED, 'Je nutné vybrat, komu je úkol přirazen.');
$form->addSubmit('create', 'Vytvořit');
$form->onSuccess[] = $this->taskFormSubmitted;
}
public function taskFormSubmitted(Form $form)
{
$this->taskRepository->createTask($this->list->id, $form->values->text, $form->values->userId);
$this->flashMessage('Úkol přidán.', 'success');
$this->redirect('this');
}
protected function createComponentTaskList()
{
if($this->list === NULL)
$this->error('Wrong action');
return new Todo\TaskListControl($this->listRepository->taskOf($this->list), $this->taskRepository);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment