From cf345f6f437336200835e1444c080f6d238c779b Mon Sep 17 00:00:00 2001 From: Theo Weijmer Date: Sun, 31 Dec 2017 10:14:14 +0100 Subject: [PATCH] Ucase 2 --- .../beheer/CustomersController.php | 67 +++++++++++++ .../controllers/beheer/UsersController.php | 99 +++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 application/controllers/beheer/CustomersController.php create mode 100644 application/controllers/beheer/UsersController.php diff --git a/application/controllers/beheer/CustomersController.php b/application/controllers/beheer/CustomersController.php new file mode 100644 index 0000000..fe40206 --- /dev/null +++ b/application/controllers/beheer/CustomersController.php @@ -0,0 +1,67 @@ +load->model( $models ); + $this->load->library( $libraries ); + } + + public function index() + { + + } + + public function getAllCustomers( $archive = 'F' ) + { + $users = Customer::getAllCustomers( $archive ); + + $this->json( $users ); + } + + public function saveCustomer() + { + $userValidation = new CustomerValidation(); + + if ( ! $userValidation->getValidationResult() ) + return $this->json( [ + 'success' => FALSE, 'messages' => $userValidation->getErrorMessages()] ); + + $user = $this->input->post(); + + $userID = $user['id']; + unset( $user['id'] ); + + $userID = Customer::saveCustomer( $userID, $user ); + + $this->json( ['success' => TRUE, 'userID' => $userID] ); + } + + public function archiveCustomer( $userID, $status ) + { + Customer::whereId( $userID )->update( ['archive' => $status] ); + } + + public function removeCustomer( $userID ) + { + $removeCustomer = new RemoveCustomer( $userID ); + + if ( $removeCustomer->getAllowDelete() === FALSE ) return $this->json( ['result' => FALSE] ); + + Customer::removeCustomer( $userID ); + + $this->json( ['result' => TRUE] ); + } +} \ No newline at end of file diff --git a/application/controllers/beheer/UsersController.php b/application/controllers/beheer/UsersController.php new file mode 100644 index 0000000..26da539 --- /dev/null +++ b/application/controllers/beheer/UsersController.php @@ -0,0 +1,99 @@ +load->model( $models ); + $this->load->library( $libraries ); + } + + public function index() + { + + } + + public function getAllUsers( $archive = 'F' ) + { + $users = User::getAllUsers( $archive ); + + $this->json( $users ); + } + + public function saveUser() + { + $userValidation = new UserValidation(); + + if ( ! $userValidation->getValidationResult() ) + return $this->json( [ + 'success' => FALSE, 'messages' => $userValidation->getErrorMessages()] ); + + $user = $this->input->post(); + + $userID = $user['id']; + unset( $user['id'] ); + + if ( $userID == 0 ) $user = $this->newUser( $user ); + + $user['role'] = empty( $user['role'] ) ? NULL : $user['role']; + + $userID = User::saveUser( $userID, $user ); + + $this->json( ['success' => TRUE, 'userID' => $userID] ); + } + + public function archiveUser( $userID, $status ) + { + User::whereId( $userID )->update( ['archive' => $status] ); + } + + public function removeUser( $userID ) + { + $removeUser = new RemoveUser( $userID ); + + if ( $removeUser->getAllowDelete() === FALSE ) return $this->json( ['result' => FALSE] ); + + User::removeUser( $userID ); + + $this->json( ['result' => TRUE] ); + } + + private function newUser( $user ) + { + $userCreation = new UserCreation( $user ); + + $user = $userCreation->getUser(); + + $password = $userCreation->getPassword(); + + $this->sendWelcomeEmail( $user, $password ); + + return $user; + } + + private function sendWelcomeEmail( $user, $password ) + { + $this->load->library( 'email' ); + + $html = $this->load->view( 'emails/welcome_email', [ + 'password' => $password, 'user' => $user], TRUE ); + + $this->email->from( 'noreply@debeleidsonderzoekers.nl', 'DBOIS' ); + $this->email->to( $user['email'] ); + $this->email->subject( 'Wachtwoord DBOIS' ); + $this->email->message( $html ); + + $this->email->send(); + } +} \ No newline at end of file