diff --git a/app/library/App/Controllers/UsersController.php b/app/library/App/Controllers/UsersController.php new file mode 100644 index 0000000..6816080 --- /dev/null +++ b/app/library/App/Controllers/UsersController.php @@ -0,0 +1,245 @@ +persistent->parameters = null; + } + + /** + * Searches for users + */ + public function searchAction() + { + $numberPage = 1; + if ($this->request->isPost()) { + $query = Criteria::fromInput($this->di, 'Users', $_POST); + $this->persistent->parameters = $query->getParams(); + } else { + $numberPage = $this->request->getQuery("page", "int"); + } + + $parameters = $this->persistent->parameters; + if (!is_array($parameters)) { + $parameters = []; + } + $parameters["order"] = "id"; + + $users = Users::find($parameters); + if (count($users) == 0) { + $this->flash->notice("The search did not find any users"); + + $this->dispatcher->forward([ + "controller" => "users", + "action" => "index" + ]); + + return; + } + + $paginator = new Paginator([ + 'data' => $users, + 'limit'=> 10, + 'page' => $numberPage + ]); + + $this->view->page = $paginator->getPaginate(); + } + + /** + * Displays the creation form + */ + public function newAction() + { + + } + + /** + * Edits a user + * + * @param string $id + */ + public function editAction($id) + { + if (!$this->request->isPost()) { + + $user = Users::findFirstByid($id); + if (!$user) { + $this->flash->error("user was not found"); + + $this->dispatcher->forward([ + 'controller' => "users", + 'action' => 'index' + ]); + + return; + } + + $this->view->id = $user->id; + + $this->tag->setDefault("id", $user->id); + $this->tag->setDefault("name", $user->name); + $this->tag->setDefault("pass", $user->pass); + $this->tag->setDefault("email", $user->email); + $this->tag->setDefault("role", $user->role); + $this->tag->setDefault("created_at", $user->created_at); + $this->tag->setDefault("updated_at", $user->updated_at); + + } + } + + /** + * Creates a new user + */ + public function createAction() + { + if (!$this->request->isPost()) { + $this->dispatcher->forward([ + 'controller' => "users", + 'action' => 'index' + ]); + + return; + } + + $user = new Users(); + $user->name = $this->request->getPost("name"); + $user->pass = $this->request->getPost("pass"); + $user->email = $this->request->getPost("email", "email"); + $user->role = $this->request->getPost("role"); + $user->created_at = $this->request->getPost("created_at"); + $user->updated_at = $this->request->getPost("updated_at"); + + + if (!$user->save()) { + foreach ($user->getMessages() as $message) { + $this->flash->error($message); + } + + $this->dispatcher->forward([ + 'controller' => "users", + 'action' => 'new' + ]); + + return; + } + + $this->flash->success("user was created successfully"); + + $this->dispatcher->forward([ + 'controller' => "users", + 'action' => 'index' + ]); + } + + /** + * Saves a user edited + * + */ + public function saveAction() + { + + if (!$this->request->isPost()) { + $this->dispatcher->forward([ + 'controller' => "users", + 'action' => 'index' + ]); + + return; + } + + $id = $this->request->getPost("id"); + $user = Users::findFirstByid($id); + + if (!$user) { + $this->flash->error("user does not exist " . $id); + + $this->dispatcher->forward([ + 'controller' => "users", + 'action' => 'index' + ]); + + return; + } + + $user->name = $this->request->getPost("name"); + $user->pass = $this->request->getPost("pass"); + $user->email = $this->request->getPost("email", "email"); + $user->role = $this->request->getPost("role"); + $user->created_at = $this->request->getPost("created_at"); + $user->updated_at = $this->request->getPost("updated_at"); + + + if (!$user->save()) { + + foreach ($user->getMessages() as $message) { + $this->flash->error($message); + } + + $this->dispatcher->forward([ + 'controller' => "users", + 'action' => 'edit', + 'params' => [$user->id] + ]); + + return; + } + + $this->flash->success("user was updated successfully"); + + $this->dispatcher->forward([ + 'controller' => "users", + 'action' => 'index' + ]); + } + + /** + * Deletes a user + * + * @param string $id + */ + public function deleteAction($id) + { + $user = Users::findFirstByid($id); + if (!$user) { + $this->flash->error("user was not found"); + + $this->dispatcher->forward([ + 'controller' => "users", + 'action' => 'index' + ]); + + return; + } + + if (!$user->delete()) { + + foreach ($user->getMessages() as $message) { + $this->flash->error($message); + } + + $this->dispatcher->forward([ + 'controller' => "users", + 'action' => 'search' + ]); + + return; + } + + $this->flash->success("user was deleted successfully"); + + $this->dispatcher->forward([ + 'controller' => "users", + 'action' => "index" + ]); + } + +} -- libgit2 0.21.4