Files
dbois/application/controllers/beheer/UsersController.php
T
2018-02-08 17:01:22 +01:00

109 lines
2.6 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 25/12/2017
* Time: 10:11
*/
class usersController extends MY_Controller {
public function __construct()
{
parent::__construct();
if( AUTHENTICATED == FALSE ) die('NO ACCES');
$models = [];
$libraries = [];
$this->load->model( $models );
$this->load->library( $libraries );
}
public function index()
{
}
public function getAllUsers( $archive = 'F' )
{
$users = User::getAllUsers( $archive );
$this->json( $users );
}
public function getUniqueprojectEmployees()
{
$projectEmployees = User::getUniqueProjectUsers();
$this->json( $projectEmployees );
}
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();
}
}