This repository has been archived by the owner on Nov 26, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
DashboardPresenter.php
153 lines (102 loc) Β· 3.1 KB
/
DashboardPresenter.php
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
<?php
use Nette\Application\UI\Form,
Nette\Application as NA;
class DashboardPresenter extends BasePresenter
{
/** @var Nette\Database\Table\Selection */
private $albums;
protected function startup()
{
parent::startup();
$this->albums = $this->getService('albums');
// user authentication
if (!$this->user->isLoggedIn()) {
if ($this->user->logoutReason === Nette\Http\UserStorage::INACTIVITY) {
$this->flashMessage('You have been signed out due to inactivity. Please sign in again.');
}
$this->redirect('Sign:in', array('backlink' => $this->storeRequest()));
}
}
/********************* view default *********************/
public function renderDefault()
{
$this->template->albums = $this->albums->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()) {
$row = $this->albums->get($id);
if (!$row) {
$this->error('Record not found');
}
$form->setDefaults($row);
}
}
/********************* view delete *********************/
public function renderDelete($id = 0)
{
$this->template->album = $this->albums->get($id);
if (!$this->template->album) {
$this->error('Record not found');
}
}
/********************* component factories *********************/
/**
* Album edit form component factory.
* @return mixed
*/
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');
$form->addSubmit('cancel', 'Cancel')->setValidationScope(NULL);
$form->onSuccess[] = callback($this, 'albumFormSubmitted');
$form->addProtection('Please submit this form again (security token has expired).');
return $form;
}
public function albumFormSubmitted(Form $form)
{
if ($form['save']->isSubmittedBy()) {
$id = (int) $this->getParameter('id');
if ($id > 0) {
$this->albums->find($id)->update($form->values);
$this->flashMessage('The album has been updated.');
} else {
$this->albums->insert($form->values);
$this->flashMessage('The album has been added.');
}
}
$this->redirect('default');
}
/**
* Album delete form component factory.
* @return mixed
*/
protected function createComponentDeleteForm()
{
$form = new Form;
$form->addSubmit('cancel', 'Cancel');
$form->addSubmit('delete', 'Delete')->setAttribute('class', 'default');
$form->onSuccess[] = callback($this, 'deleteFormSubmitted');
$form->addProtection('Please submit this form again (security token has expired).');
return $form;
}
public function deleteFormSubmitted(Form $form)
{
if ($form['delete']->isSubmittedBy()) {
$this->albums->find($this->getParameter('id'))->delete();
$this->flashMessage('Album has been deleted.');
}
$this->redirect('default');
}
}