Commit f0147279c2c05ed16eb41dd8f8269b6e5145c082
1 parent
0b7fb0e5
test
Showing
1 changed file
with
245 additions
and
0 deletions
Show diff stats
1 | +<?php | |
2 | + | |
3 | +use Phalcon\Mvc\Model\Criteria; | |
4 | +use Phalcon\Paginator\Adapter\Model as Paginator; | |
5 | + | |
6 | + | |
7 | +class UsersController extends ControllerBase | |
8 | +{ | |
9 | + /** | |
10 | + * Index action | |
11 | + */ | |
12 | + public function indexAction() | |
13 | + { | |
14 | + $this->persistent->parameters = null; | |
15 | + } | |
16 | + | |
17 | + /** | |
18 | + * Searches for users | |
19 | + */ | |
20 | + public function searchAction() | |
21 | + { | |
22 | + $numberPage = 1; | |
23 | + if ($this->request->isPost()) { | |
24 | + $query = Criteria::fromInput($this->di, 'Users', $_POST); | |
25 | + $this->persistent->parameters = $query->getParams(); | |
26 | + } else { | |
27 | + $numberPage = $this->request->getQuery("page", "int"); | |
28 | + } | |
29 | + | |
30 | + $parameters = $this->persistent->parameters; | |
31 | + if (!is_array($parameters)) { | |
32 | + $parameters = []; | |
33 | + } | |
34 | + $parameters["order"] = "id"; | |
35 | + | |
36 | + $users = Users::find($parameters); | |
37 | + if (count($users) == 0) { | |
38 | + $this->flash->notice("The search did not find any users"); | |
39 | + | |
40 | + $this->dispatcher->forward([ | |
41 | + "controller" => "users", | |
42 | + "action" => "index" | |
43 | + ]); | |
44 | + | |
45 | + return; | |
46 | + } | |
47 | + | |
48 | + $paginator = new Paginator([ | |
49 | + 'data' => $users, | |
50 | + 'limit'=> 10, | |
51 | + 'page' => $numberPage | |
52 | + ]); | |
53 | + | |
54 | + $this->view->page = $paginator->getPaginate(); | |
55 | + } | |
56 | + | |
57 | + /** | |
58 | + * Displays the creation form | |
59 | + */ | |
60 | + public function newAction() | |
61 | + { | |
62 | + | |
63 | + } | |
64 | + | |
65 | + /** | |
66 | + * Edits a user | |
67 | + * | |
68 | + * @param string $id | |
69 | + */ | |
70 | + public function editAction($id) | |
71 | + { | |
72 | + if (!$this->request->isPost()) { | |
73 | + | |
74 | + $user = Users::findFirstByid($id); | |
75 | + if (!$user) { | |
76 | + $this->flash->error("user was not found"); | |
77 | + | |
78 | + $this->dispatcher->forward([ | |
79 | + 'controller' => "users", | |
80 | + 'action' => 'index' | |
81 | + ]); | |
82 | + | |
83 | + return; | |
84 | + } | |
85 | + | |
86 | + $this->view->id = $user->id; | |
87 | + | |
88 | + $this->tag->setDefault("id", $user->id); | |
89 | + $this->tag->setDefault("name", $user->name); | |
90 | + $this->tag->setDefault("pass", $user->pass); | |
91 | + $this->tag->setDefault("email", $user->email); | |
92 | + $this->tag->setDefault("role", $user->role); | |
93 | + $this->tag->setDefault("created_at", $user->created_at); | |
94 | + $this->tag->setDefault("updated_at", $user->updated_at); | |
95 | + | |
96 | + } | |
97 | + } | |
98 | + | |
99 | + /** | |
100 | + * Creates a new user | |
101 | + */ | |
102 | + public function createAction() | |
103 | + { | |
104 | + if (!$this->request->isPost()) { | |
105 | + $this->dispatcher->forward([ | |
106 | + 'controller' => "users", | |
107 | + 'action' => 'index' | |
108 | + ]); | |
109 | + | |
110 | + return; | |
111 | + } | |
112 | + | |
113 | + $user = new Users(); | |
114 | + $user->name = $this->request->getPost("name"); | |
115 | + $user->pass = $this->request->getPost("pass"); | |
116 | + $user->email = $this->request->getPost("email", "email"); | |
117 | + $user->role = $this->request->getPost("role"); | |
118 | + $user->created_at = $this->request->getPost("created_at"); | |
119 | + $user->updated_at = $this->request->getPost("updated_at"); | |
120 | + | |
121 | + | |
122 | + if (!$user->save()) { | |
123 | + foreach ($user->getMessages() as $message) { | |
124 | + $this->flash->error($message); | |
125 | + } | |
126 | + | |
127 | + $this->dispatcher->forward([ | |
128 | + 'controller' => "users", | |
129 | + 'action' => 'new' | |
130 | + ]); | |
131 | + | |
132 | + return; | |
133 | + } | |
134 | + | |
135 | + $this->flash->success("user was created successfully"); | |
136 | + | |
137 | + $this->dispatcher->forward([ | |
138 | + 'controller' => "users", | |
139 | + 'action' => 'index' | |
140 | + ]); | |
141 | + } | |
142 | + | |
143 | + /** | |
144 | + * Saves a user edited | |
145 | + * | |
146 | + */ | |
147 | + public function saveAction() | |
148 | + { | |
149 | + | |
150 | + if (!$this->request->isPost()) { | |
151 | + $this->dispatcher->forward([ | |
152 | + 'controller' => "users", | |
153 | + 'action' => 'index' | |
154 | + ]); | |
155 | + | |
156 | + return; | |
157 | + } | |
158 | + | |
159 | + $id = $this->request->getPost("id"); | |
160 | + $user = Users::findFirstByid($id); | |
161 | + | |
162 | + if (!$user) { | |
163 | + $this->flash->error("user does not exist " . $id); | |
164 | + | |
165 | + $this->dispatcher->forward([ | |
166 | + 'controller' => "users", | |
167 | + 'action' => 'index' | |
168 | + ]); | |
169 | + | |
170 | + return; | |
171 | + } | |
172 | + | |
173 | + $user->name = $this->request->getPost("name"); | |
174 | + $user->pass = $this->request->getPost("pass"); | |
175 | + $user->email = $this->request->getPost("email", "email"); | |
176 | + $user->role = $this->request->getPost("role"); | |
177 | + $user->created_at = $this->request->getPost("created_at"); | |
178 | + $user->updated_at = $this->request->getPost("updated_at"); | |
179 | + | |
180 | + | |
181 | + if (!$user->save()) { | |
182 | + | |
183 | + foreach ($user->getMessages() as $message) { | |
184 | + $this->flash->error($message); | |
185 | + } | |
186 | + | |
187 | + $this->dispatcher->forward([ | |
188 | + 'controller' => "users", | |
189 | + 'action' => 'edit', | |
190 | + 'params' => [$user->id] | |
191 | + ]); | |
192 | + | |
193 | + return; | |
194 | + } | |
195 | + | |
196 | + $this->flash->success("user was updated successfully"); | |
197 | + | |
198 | + $this->dispatcher->forward([ | |
199 | + 'controller' => "users", | |
200 | + 'action' => 'index' | |
201 | + ]); | |
202 | + } | |
203 | + | |
204 | + /** | |
205 | + * Deletes a user | |
206 | + * | |
207 | + * @param string $id | |
208 | + */ | |
209 | + public function deleteAction($id) | |
210 | + { | |
211 | + $user = Users::findFirstByid($id); | |
212 | + if (!$user) { | |
213 | + $this->flash->error("user was not found"); | |
214 | + | |
215 | + $this->dispatcher->forward([ | |
216 | + 'controller' => "users", | |
217 | + 'action' => 'index' | |
218 | + ]); | |
219 | + | |
220 | + return; | |
221 | + } | |
222 | + | |
223 | + if (!$user->delete()) { | |
224 | + | |
225 | + foreach ($user->getMessages() as $message) { | |
226 | + $this->flash->error($message); | |
227 | + } | |
228 | + | |
229 | + $this->dispatcher->forward([ | |
230 | + 'controller' => "users", | |
231 | + 'action' => 'search' | |
232 | + ]); | |
233 | + | |
234 | + return; | |
235 | + } | |
236 | + | |
237 | + $this->flash->success("user was deleted successfully"); | |
238 | + | |
239 | + $this->dispatcher->forward([ | |
240 | + 'controller' => "users", | |
241 | + 'action' => "index" | |
242 | + ]); | |
243 | + } | |
244 | + | |
245 | +} | ... | ... |