/
EntryController.php
275 lines (265 loc) · 7.95 KB
/
EntryController.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
* Entry Controller
*
*/
class EntryController extends Zend_Controller_Action
{
/**
* @var Postr_Model_EntryMapper
*/
private $_entryMapper;
/**
* @var Zend_Controller_Router_Interface
*/
private $_router;
/**
* Init
*
* @return void
*/
public function init()
{
$this->_helper->redirector->setPrependBase(false);
//TODO: Pass these in so that it can be substitued
$this->_entryMapper = new Postr_Model_EntryMapper();
$this->_router = $this->getFrontController()->getRouter();
$this->view->headTitle()->prepend('Entry');
}
/**
* Index Action
*
* @return void
*/
public function indexAction()
{
$page = $this->_getParam('page', 1);
$count = $this->_getParam('count', 5);
$this->view->headTitle()->prepend('Index');
$entries = $this->_entryMapper->indexOfEntries();
$entries
->setCurrentPageNumber($page)
->setItemCountPerPage($count)
;
$this->view->entries = $entries;
}
/**
* New Action
*
* @return void
*/
public function newAction()
{
$this->view->headTitle()->prepend('New');
$entryForm = new Postr_Form_Entry();
$now = new Zend_Date();
$entryForm
->setMethod('post')
->setAction(
$this->_router->assemble(
array(
'action' => 'post',
'controller' => 'entry',
),
null,
true
)
)
->populate(
array(
'updated' => $now->get(Zend_Date::DATETIME_SHORT),
'published' => $now->get(Zend_Date::DATETIME_SHORT),
)
)
;
$this->view->entryForm = $entryForm;
}
/**
* Get Action
*
* @return void
*/
public function getAction()
{
$id = $this->_getParam('id');
$this->view->headTitle()->prepend('Get');
$entry = $this->_entryMapper->getEntry($id);
if (null === $entry) {
throw new Zend_Controller_Dispatcher_Exception();
}
$this->view->entry = $entry;
}
/**
* Edit Action
*
* @return void
*/
public function editAction()
{
$id = $this->_getParam('id');
$this->view->headTitle()->prepend('Edit');
$entry = $this->_entryMapper->getEntry($id);
if (null === $entry) {
throw new Zend_Controller_Dispatcher_Exception();
}
$entryForm = new Postr_Form_Entry();
$now = new Zend_Date();
$entryForm
->setMethod('post')
->setAction(
$this->_router->assemble(
array(
'action' => 'put',
'controller' => 'entry',
'id' => $entry->getId(),
),
null,
true
)
)
->populate(
array(
'title' => $entry->getTitle(),
'entry_content' => $entry->getContent(),
'entry_summary' => $entry->getSummary(),
'updated' => $now->get(Zend_Date::DATETIME_SHORT),
'published' => $entry->getPublished()->get(Zend_Date::DATETIME_SHORT),
)
)
;
$this->view->entryForm = $entryForm;
}
/**
* Post Action
*
* @return void
*/
public function postAction()
{
$entryForm = new Postr_Form_Entry();
$this->view->headTitle()->prepend('Post');
if ($entryForm->isValid($this->_getAllParams())) {
$title = $entryForm->getValue('title');
$content = $entryForm->getValue('entry_content');
$summary = $entryForm->getValue('entry_summary');
$updated = new Zend_Date($entryForm->getValue('updated'), Zend_Date::DATETIME_SHORT);
$published = new Zend_Date($entryForm->getValue('published'), Zend_Date::DATETIME_SHORT);
$entry = new Postr_Model_Entry();
$entry
->setTitle($title)
->setContent($content)
->setSummary($summary)
->setUpdated($updated)
->setPublished($published)
;
$this->_entryMapper->postEntry($entry);
$this->_setParam('id', $entry->getId());
$this->_redirect(
$this->_router->assemble(
array(
'action' => 'get',
'controller' => 'entry',
'id' => $entry->getId(),
),
null,
true
)
);
}
$entryForm
->setMethod('post')
->setAction(
$this->_router->assemble(
array(
'action' => 'post',
'controller' => 'entry',
),
null,
true
)
)
;
$this->view->entryForm = $entryForm;
}
/**
* Put Action
*
* @return void
*/
public function putAction()
{
$id = $this->_getParam('id');
$this->view->headTitle()->prepend('Put');
$entry = $this->_entryMapper->getEntry($id);
if (null === $entry) {
throw new Zend_Controller_Dispatcher_Exception();
}
$entryForm = new Postr_Form_Entry();
if ($entryForm->isValid($this->_getAllParams())) {
$title = $entryForm->getValue('title');
$content = $entryForm->getValue('entry_content');
$summary = $entryForm->getValue('entry_summary');
$updated = new Zend_Date($entryForm->getValue('updated'), Zend_Date::DATETIME_SHORT);
$published = new Zend_Date($entryForm->getValue('published'), Zend_Date::DATETIME_SHORT);
$entry
->setTitle($title)
->setContent($content)
->setSummary($summary)
->setUpdated($updated)
->setPublished($published)
;
$this->_entryMapper->putEntry($entry);
$this->_redirect(
$this->_router->assemble(
array(
'action' => 'get',
'controller' => 'entry',
'id' => $entry->getId(),
),
null,
true
)
);
}
$entryForm
->setMethod('post')
->setAction(
$this->_router->assemble(
array(
'action' => 'put',
'controller' => 'entry',
'id' => $entry->getId(),
),
null,
true
)
)
;
$this->view->entryForm = $entryForm;
}
/**
* Delete Action
*
* @return void
*/
public function deleteAction()
{
$id = $this->_getParam('id');
$this->view->headTitle()->prepend('Delete');
$entry = $this->_entryMapper->getEntry($id);
if (null === $entry) {
throw new Zend_Controller_Dispatcher_Exception();
}
$this->_entryMapper->deleteEntry($entry);
$this->_redirect(
$this->_router->assemble(
array(
'action' => 'index',
'controller' => 'entry',
),
null,
true
)
);
}
}