1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
<?php
namespace App\Presenters;
use App\Model;
use Nette;
use Nette\Application\UI\Form;
class DashboardPresenter extends Nette\Application\UI\Presenter
{
/** @var Model\AlbumRepository */
private $albums;
public function __construct(Model\AlbumRepository $albums)
{
$this->albums = $albums;
}
protected function startup()
{
parent::startup();
if (!$this->getUser()->isLoggedIn()) {
if ($this->getUser()->getLogoutReason() === Nette\Security\IUserStorage::INACTIVITY) {
$this->flashMessage('You have been signed out due to inactivity. Please sign in again.');
}
$this->redirect('Sign:in', ['backlink' => $this->storeRequest()]);
}
}
/********************* view default *********************/
public function renderDefault()
{
$this->template->albums = $this->albums->findAll()->order('artist')->order('title');
}
/********************* views add & edit *********************/
public function renderAdd()
{
$this['albumForm']['save']->caption = 'Add';
}
public function renderEdit($id = 0)
{
$form = $this['albumForm'];
if (!$form->isSubmitted()) {
$album = $this->albums->findById($id);
if (!$album) {
$this->error('Record not found');
}
$form->setDefaults($album);
}
}
/********************* view delete *********************/
public function renderDelete($id = 0)
{
$this->template->album = $this->albums->findById($id);
if (!$this->template->album) {
$this->error('Record not found');
}
}
/********************* component factories *********************/
/**
* Edit form factory.
* @return Form
*/
protected function createComponentAlbumForm()
{
$form = new Form;
$form->addText('artist', 'Artist:')
->setRequired('Please enter an artist.');
$form->addText('title', 'Title:')
->setRequired('Please enter a title.');
$form->addSubmit('save', 'Save')
->setAttribute('class', 'default')
->onClick[] = [$this, 'albumFormSucceeded'];
$form->addSubmit('cancel', 'Cancel')
->setValidationScope([])
->onClick[] = [$this, 'formCancelled'];
$form->addProtection();
return $form;
}
public function albumFormSucceeded($button)
{
$values = $button->getForm()->getValues();
$id = (int) $this->getParameter('id');
if ($id) {
$this->albums->findById($id)->update($values);
$this->flashMessage('The album has been updated.');
} else {
$this->albums->insert($values);
$this->flashMessage('The album has been added.');
}
$this->redirect('default');
}
/**
* Delete form factory.
* @return Form
*/
protected function createComponentDeleteForm()
{
$form = new Form;
$form->addSubmit('cancel', 'Cancel')
->onClick[] = [$this, 'formCancelled'];
$form->addSubmit('delete', 'Delete')
->setAttribute('class', 'default')
->onClick[] = [$this, 'deleteFormSucceeded'];
$form->addProtection();
return $form;
}
public function deleteFormSucceeded()
{
$this->albums->findById($this->getParameter('id'))->delete();
$this->flashMessage('Album has been deleted.');
$this->redirect('default');
}
public function formCancelled()
{
$this->redirect('default');
}
}
|