Commit d497e24de49f6d8e5d1e8a16cbdbc443b6209c0d
1 parent
f0147279
test
Showing
5 changed files
with
23 additions
and
458 deletions
Show diff stats
app/library/App/Controllers/UsersController.php deleted
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 | -} |
app/library/App/Model/Project.php
@@ -33,9 +33,6 @@ class Project extends \App\Mvc\DateTrackingModel { | @@ -33,9 +33,6 @@ class Project extends \App\Mvc\DateTrackingModel { | ||
33 | 33 | ||
34 | $this->setSchema("public"); | 34 | $this->setSchema("public"); |
35 | 35 | ||
36 | -// $this->belongsTo('user_id', User::class, 'id', [ | ||
37 | -// 'alias' => 'User', | ||
38 | -// ]); | ||
39 | } | 36 | } |
40 | 37 | ||
41 | } | 38 | } |
42 | \ No newline at end of file | 39 | \ No newline at end of file |
app/library/App/Model/Projects.php deleted
1 | -<?php | ||
2 | - | ||
3 | -class Projects extends \Phalcon\Mvc\Model | ||
4 | -{ | ||
5 | - | ||
6 | - /** | ||
7 | - * | ||
8 | - * @var integer | ||
9 | - * @Primary | ||
10 | - * @Column(type="integer", length=32, nullable=false) | ||
11 | - */ | ||
12 | - public $id; | ||
13 | - | ||
14 | - /** | ||
15 | - * | ||
16 | - * @var string | ||
17 | - * @Column(type="string", length=255, nullable=true) | ||
18 | - */ | ||
19 | - public $name; | ||
20 | - | ||
21 | - /** | ||
22 | - * | ||
23 | - * @var integer | ||
24 | - * @Column(type="integer", length=32, nullable=true) | ||
25 | - */ | ||
26 | - public $user_id; | ||
27 | - | ||
28 | - /** | ||
29 | - * | ||
30 | - * @var string | ||
31 | - * @Column(type="string", nullable=true) | ||
32 | - */ | ||
33 | - public $created_at; | ||
34 | - | ||
35 | - /** | ||
36 | - * | ||
37 | - * @var string | ||
38 | - * @Column(type="string", nullable=true) | ||
39 | - */ | ||
40 | - public $updated_at; | ||
41 | - | ||
42 | - /** | ||
43 | - * Initialize method for model. | ||
44 | - */ | ||
45 | - public function initialize() | ||
46 | - { | ||
47 | - $this->setSchema("public"); | ||
48 | - $this->belongsTo('user_id', '\User', 'id', ['alias' => 'User']); | ||
49 | - } | ||
50 | - | ||
51 | - /** | ||
52 | - * Returns table name mapped in the model. | ||
53 | - * | ||
54 | - * @return string | ||
55 | - */ | ||
56 | - public function getSource() | ||
57 | - { | ||
58 | - return 'projects'; | ||
59 | - } | ||
60 | - | ||
61 | - /** | ||
62 | - * Allows to query a set of records that match the specified conditions | ||
63 | - * | ||
64 | - * @param mixed $parameters | ||
65 | - * @return Projects[]|Projects | ||
66 | - */ | ||
67 | - public static function find($parameters = null) | ||
68 | - { | ||
69 | - return parent::find($parameters); | ||
70 | - } | ||
71 | - | ||
72 | - /** | ||
73 | - * Allows to query the first record that match the specified conditions | ||
74 | - * | ||
75 | - * @param mixed $parameters | ||
76 | - * @return Projects | ||
77 | - */ | ||
78 | - public static function findFirst($parameters = null) | ||
79 | - { | ||
80 | - return parent::findFirst($parameters); | ||
81 | - } | ||
82 | - | ||
83 | -} |
app/library/App/Model/User.php
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | namespace App\Model; | 3 | namespace App\Model; |
4 | +use Phalcon\Mvc\Model\Validator\Email as EmailValidator; | ||
4 | 5 | ||
5 | class User extends \App\Mvc\DateTrackingModel | 6 | class User extends \App\Mvc\DateTrackingModel |
6 | { | 7 | { |
7 | public $id; | 8 | public $id; |
8 | - public $role; | 9 | + public $name; |
10 | + public $pass; | ||
9 | public $email; | 11 | public $email; |
10 | - public $username; | ||
11 | - public $password; | 12 | + public $role; |
12 | 13 | ||
13 | public function getSource() | 14 | public function getSource() |
14 | { | 15 | { |
@@ -19,10 +20,27 @@ class User extends \App\Mvc\DateTrackingModel | @@ -19,10 +20,27 @@ class User extends \App\Mvc\DateTrackingModel | ||
19 | { | 20 | { |
20 | return parent::columnMap() + [ | 21 | return parent::columnMap() + [ |
21 | 'id' => 'id', | 22 | 'id' => 'id', |
22 | - 'username' => 'username', | ||
23 | - 'password' => 'password', | 23 | + 'name' => 'name', |
24 | + 'pass' => 'pass', | ||
24 | 'email' => 'email', | 25 | 'email' => 'email', |
25 | 'role' => 'role' | 26 | 'role' => 'role' |
26 | ]; | 27 | ]; |
27 | } | 28 | } |
29 | + | ||
30 | + public function validation() | ||
31 | + { | ||
32 | + $validator = new Validation(); | ||
33 | + | ||
34 | + $validator->add( | ||
35 | + 'email', | ||
36 | + new EmailValidator( | ||
37 | + [ | ||
38 | + 'model' => $this, | ||
39 | + 'message' => 'Please enter a correct email address', | ||
40 | + ] | ||
41 | + ) | ||
42 | + ); | ||
43 | + | ||
44 | + return $this->validate($validator); | ||
45 | + } | ||
28 | } | 46 | } |
app/library/App/Model/Users.php deleted
1 | -<?php | ||
2 | - | ||
3 | -use Phalcon\Validation; | ||
4 | -use Phalcon\Mvc\Model\Validator\Email as EmailValidator; | ||
5 | - | ||
6 | -class Users extends \Phalcon\Mvc\Model | ||
7 | -{ | ||
8 | - | ||
9 | - /** | ||
10 | - * | ||
11 | - * @var integer | ||
12 | - * @Primary | ||
13 | - * @Identity | ||
14 | - * @Column(type="integer", length=32, nullable=false) | ||
15 | - */ | ||
16 | - public $id; | ||
17 | - | ||
18 | - /** | ||
19 | - * | ||
20 | - * @var string | ||
21 | - * @Column(type="string", length=255, nullable=true) | ||
22 | - */ | ||
23 | - public $name; | ||
24 | - | ||
25 | - /** | ||
26 | - * | ||
27 | - * @var string | ||
28 | - * @Column(type="string", length=255, nullable=true) | ||
29 | - */ | ||
30 | - public $pass; | ||
31 | - | ||
32 | - /** | ||
33 | - * | ||
34 | - * @var string | ||
35 | - * @Column(type="string", length=255, nullable=true) | ||
36 | - */ | ||
37 | - public $email; | ||
38 | - | ||
39 | - /** | ||
40 | - * | ||
41 | - * @var string | ||
42 | - * @Column(type="string", length=255, nullable=true) | ||
43 | - */ | ||
44 | - public $role; | ||
45 | - | ||
46 | - /** | ||
47 | - * | ||
48 | - * @var string | ||
49 | - * @Column(type="string", nullable=true) | ||
50 | - */ | ||
51 | - public $created_at; | ||
52 | - | ||
53 | - /** | ||
54 | - * | ||
55 | - * @var string | ||
56 | - * @Column(type="string", nullable=true) | ||
57 | - */ | ||
58 | - public $updated_at; | ||
59 | - | ||
60 | - /** | ||
61 | - * Validations and business logic | ||
62 | - * | ||
63 | - * @return boolean | ||
64 | - */ | ||
65 | - public function validation() | ||
66 | - { | ||
67 | - $validator = new Validation(); | ||
68 | - | ||
69 | - $validator->add( | ||
70 | - 'email', | ||
71 | - new EmailValidator( | ||
72 | - [ | ||
73 | - 'model' => $this, | ||
74 | - 'message' => 'Please enter a correct email address', | ||
75 | - ] | ||
76 | - ) | ||
77 | - ); | ||
78 | - | ||
79 | - return $this->validate($validator); | ||
80 | - } | ||
81 | - | ||
82 | - /** | ||
83 | - * Initialize method for model. | ||
84 | - */ | ||
85 | - public function initialize() | ||
86 | - { | ||
87 | - $this->setSchema("public"); | ||
88 | - } | ||
89 | - | ||
90 | - /** | ||
91 | - * Returns table name mapped in the model. | ||
92 | - * | ||
93 | - * @return string | ||
94 | - */ | ||
95 | - public function getSource() | ||
96 | - { | ||
97 | - return 'users'; | ||
98 | - } | ||
99 | - | ||
100 | - /** | ||
101 | - * Allows to query a set of records that match the specified conditions | ||
102 | - * | ||
103 | - * @param mixed $parameters | ||
104 | - * @return Users[]|Users | ||
105 | - */ | ||
106 | - public static function find($parameters = null) | ||
107 | - { | ||
108 | - return parent::find($parameters); | ||
109 | - } | ||
110 | - | ||
111 | - /** | ||
112 | - * Allows to query the first record that match the specified conditions | ||
113 | - * | ||
114 | - * @param mixed $parameters | ||
115 | - * @return Users | ||
116 | - */ | ||
117 | - public static function findFirst($parameters = null) | ||
118 | - { | ||
119 | - return parent::findFirst($parameters); | ||
120 | - } | ||
121 | - | ||
122 | -} |