/
SongPresenter.php
143 lines (124 loc) · 3.43 KB
/
SongPresenter.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
<?php
use Nette\Application\UI\Form;
class SongPresenter extends BasePresenter
{
/** @persistent int */
public $id;
/** @var Songs */
private $song;
/** @var Songs */
private $songs;
private $query;
public function actionDefault() {
$this->ensureLoggedUser();
}
public function actionDetails($id)
{
$this->ensureLoggedUser();
$this->song = $this->context->createSongs()->get($id);
if ($this->song === FALSE) {
$this->setView('notFound');
return;
}
}
public function actionEdit($id)
{
$this->ensureLoggedUser();
$this->song = $this->context->createSongs()->get($id);
if ($this->song === FALSE) {
$this->setView('notFound');
return;
}
$this["songForm"]->setDefaults($this->song);
}
public function actionDelete($id)
{
$this->ensureLoggedUser();
$this->song = $this->context->createSongs()->get($id);
if ($this->song === FALSE) {
$this->setView('notFound');
return;
}
$this->context->createSongs()->where(array('id' => $id))->delete();
$this->flashMessage('Písnička byla smazána.', 'success');
$this->id = null;
$this->redirect('default');
}
public function actionSearch($query)
{
$this->ensureLoggedUser();
$this->query = $query;
$this["searchForm"]->setDefaults(array('query' => $this->query));
}
public function renderDetails()
{
$this->template->song = $this->song;
}
public function renderEdit()
{
$this->template->song = $this->song;
}
public function renderSearch() {
$this->template->query = $this->query;
}
public function createComponentSongs()
{
$q = $this->context->createSongs();
if ($this->query) {
$term = "%$this->query%";
$q->where("title LIKE ? OR author LIKE ? OR description LIKE ? OR lyrics LIKE ?",
array($term, $term, $term, $term));
}
$q->order('title');
return new SongList($q, $this->context->createSongs());
}
protected function createComponentSongForm()
{
$form = new Form();
$form->addText('title', 'Název')
->addRule(Form::FILLED, 'Je nutné zadat název písničky.');
$form->addText('author', 'Autor');
$form->addTextArea('description', 'Popis');
$form->addTextArea('lyrics', 'Text písničky');
$form->addSubmit('save', ($this->song) ? 'Upravit' : 'Vytvořit');
$form->onSuccess[] = callback($this, 'processSongForm');
$presenter = $this;
$form->addSubmit('cancel', 'Zrušit')
->setValidationScope(FALSE)
->onClick[] = function () use ($presenter) {
$presenter->id = null;
$presenter->redirect('default');
};
return $form;
}
public function processSongForm(Form $form)
{
$this->ensureLoggedUser();
if ($this->id AND !$this->song) {
// record existence check in case of editing
throw new BadRequestException;
}
$values = $form->values;
foreach (array('author', 'description', 'lyrics') as $field) {
$values[$field] = $this->nullizeEmptyString($values[$field]);
}
if ($this->id) {
$this->context->createSongs()
->where(array('id' => $this->id))
->update($values);
$this->flashMessage('Písnička byla upravena.', 'success');
} else {
$this->id = $this->context->createSongs()->insert($values)->id;
$this->flashMessage('Písnička byla vytvořena.', 'success');
}
$this->redirect('default');
}
protected function createComponentSearchForm()
{
$form = new Form();
$form->addText('query', 'Hledaný text')
->addRule(Form::FILLED, 'Je nutné zadat hledaný text.');
$form->addSubmit('search', 'Hledat');
return $form;
}
}