-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRUDController.php
More file actions
206 lines (158 loc) · 5.22 KB
/
CRUDController.php
File metadata and controls
206 lines (158 loc) · 5.22 KB
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
<?php
/**
* CRUDControllerAbstract
*
* Abstraktní magický nástoj, který pomůže snadno vytvořit webovou aplikaci umožňující CRUD operace s databázovou tabulkou
* Magie je provedena ve třech krocích
* 1. Implementace metody getForm(), která vrátí formulář (Zend_Form) a metody getModel(), která vrátí Data Table Gateway třídu pro tabulku (Zend_Db_Table)
* 2. Implementace metody indexAction() podle požadavků konkrétní aplikace
* 3. Vytvoření příslušných šablon (index.phtml, create.phtml, edit.phtml)
*
* @author Štěpán Zikmund (stepan.zikmund@gmail.com)
* @version 0.1
*/
abstract class Stepiiik_CRUDController extends Zend_Controller_Action
{
/**
* FlashMessenger
*
* @var Zend_Controller_Action_Helper_FlashMessenger
*/
protected $_flashMessenger = null;
public function init()
{
$this->_flashMessenger =
$this->_helper->getHelper('FlashMessenger');
$this->initView();
}
/**
* Metoda se pro použitíí implementuje tak, že vrátí pole zpráv s indexy
* create - Pro zprávu po vytvoření řádku
* update - Pro zprávu po úpravě řádku
* delete - Pro zpárvu po smazání řádku
*
* @return array
*/
protected function _flashMessages()
{
return array();
}
protected function _getFlashMessage($title)
{
if (array_key_exists($title, $this->_flashMessages)) {
$arr = $this->_flashMessages;
return $arr[$title];
}
return null;
}
public function postDispatch()
{
//$this->view->messages = $this->_flashMessenger->getMessages();
}
protected function _preEdit($args = null)
{
}
protected function _postEdit($args = null)
{
}
protected function _preDelete($args = null)
{
}
protected function _postDelete($args = null)
{
}
protected function _preInsert($args = null)
{
}
protected function _postInsert($args = null)
{
}
/**
* @return Zend_Db_Table
*/
abstract protected function getModel();
/**
* @retun Zend_Form
*/
abstract protected function getForm();
/**
* Controller ocekava vzdy akci index
*/
abstract function indexAction();
public function createAction()
{
$this->_preInsert();
$form = $this->getForm();
if ($this->_request->isPost()) {
$data = $this->_request->getPost();
if (! $form->isValid($data)) {
$this->view->form = $form;
}
else {
$model = $this->getModel();
$args = array('id' => $model->insert($form->getValues()));
if ($this->_getFlashMessage('create')) {
$this->_flashMessenger->addMessage($this->_getFlashMessage('create'));
}
$this->_postInsert($args);
$this->_redirect($this->_helper->url('index'));
}
}
$this->view->form = $form;
}
public function editAction()
{
$this->_preEdit();
$id = $this->_getParam('id');
$form = $this->getForm();
$model = $this->getModel();
$result = $model->find($id);
if ($result->count() !== 1) {
$this->_redirect($this->_helper->url('index'));
}
$row = $result->current();
if ($this->_request->isPost()) {
$data = $this->_request->getPost();
if (! $form->isValid($data)) {
$this->view->form = $form;
}
else {
$model->update($form->getValues(), 'id=' . $model->getAdapter()->quote($id, 'INTEGER'));
if ($this->_getFlashMessage('edit')) {
$this->_flashMessenger->addMessage($this->_getFlashMessage('edit'));
}
$this->_postEdit();
$this->_redirect($this->_helper->url('index'));
}
}
$form->populate($row->toArray());
$this->view->data = $row;
$this->view->form = $form;
}
public function deleteAction()
{
$model = $this->getModel();
$id = $this->_getParam('id');
$this->_preDelete(array('id' => $id));
$result = $model->find($id);
if ($result->count() !== 1) {
return $this->_redirect($this->_helper->url('index'));
}
$result->current()->delete();
if ($this->_getFlashMessage('delete')) {
$this->_flashMessenger->addMessage($this->_getFlashMessage('delete'));
}
$this->_postDelete();
return $this->_redirect($this->_helper->url('index'));
}
public function detailAction()
{
$model = $this->getModel();
$id = $this->_getParam('id');
$result = $model->find($id);
if ($result->count() !== 1) {
return $this->_redirect($this->_helper->url('index'));
}
$this->view->data = $result->current();
}
}