diff --git a/application/controllers/users.php b/application/controllers/users.php index a2eae6b..e5acb80 100644 --- a/application/controllers/users.php +++ b/application/controllers/users.php @@ -13,6 +13,7 @@ class Users extends MY_Controller { */ public function __construct() { parent::__construct(); + $this->load->library('form_validation'); $this->load->model('user'); } @@ -28,6 +29,139 @@ class Users extends MY_Controller { * Allows admins to create a new user. */ public function create() { + $config = array( + array( + 'field' => 'username', + 'label' => 'lang:field_username', + 'rules' => 'trim|required|min_length[4]|max_length[20]|unique[users.username]', + ), + array( + 'field' => 'password', + 'label' => 'lang:field_password', + 'rules' => 'required|min_length[6]|matches[password_confirm]', + ), + array( + 'field' => 'password_confirm', + 'label' => 'lang:field_password_confirm', + ), + array( + 'field' => 'firstname', + 'label' => 'lang:field_firstname', + 'rules' => 'trim|required|max_length[50]', + ), + array( + 'field' => 'lastname', + 'label' => 'lang:field_lastname', + 'rules' => 'trim|required|max_length[50]', + ), + array( + 'field' => 'email', + 'label' => 'lang:field_email', + 'rules' => 'trim|required|valid_email', + ), + array( + 'field' => 'institution', + 'label' => 'lang:field_institution', + 'rules' => 'trim|max_length[100]', + ), + array( + 'field' => 'phone', + 'label' => 'lang:field_phone', + 'rules' => 'trim|regex_match[/^\+\d{2,4}\w\d{2,4}\w\d{3,10}+$/i]', + ) + ); + $this->form_validation->set_rules($config); + + if ($this->form_validation->run() === true) { + $username = $this->input->post('username'); + + $data = array( + 'firstname' => $this->input->post('firstname'), + 'lastname' => $this->input->post('lastname'), + 'institution' => $this->input->post('institution'), + 'phone' => $this->input->post('phone') + ); + + if ($this->user->register($username, $this->input->post('password'), $this->input->post('email'), $data)) { + $this->messages->add("The user '" . $username . "' was created", 'success'); + redirect('users', 201); + } + } + $this->load->view('admin/users/create'); } + + /** + * Allows admins to edit the specified user. + * + * @param integer $id + */ + public function edit($id = '') { + $user = $this->user->getUserByID($id); + + if (!isset($user) || !is_array($user)){ + show_404(); + } + + $config = array( + array( + 'field' => 'firstname', + 'label' => 'lang:field_firstname', + 'rules' => 'trim|required|max_length[50]', + ), + array( + 'field' => 'lastname', + 'label' => 'lang:field_lastname', + 'rules' => 'trim|required|max_length[50]', + ), + array( + 'field' => 'email', + 'label' => 'lang:field_email', + 'rules' => 'trim|required|valid_email', + ), + array( + 'field' => 'institution', + 'label' => 'lang:field_institution', + 'rules' => 'trim|max_length[100]', + ), + array( + 'field' => 'phone', + 'label' => 'lang:field_phone', + //'rules' => 'trim|regex_match[/^\+\d{2,4}\w\d{2,4}\w\d{3,10}+$/i]', + ) + ); + $this->form_validation->set_rules($config); + + if ($this->form_validation->run() === true) { + $data = array( + 'email' => $this->input->post('email'), + 'firstname' => $this->input->post('firstname'), + 'lastname' => $this->input->post('lastname'), + 'institution' => $this->input->post('institution'), + 'phone' => $this->input->post('phone') + ); + + if ($this->user->update($user['id'], $data)) { + $this->messages->add("The user '" . $user['username'] . "' was updated", 'success'); + redirect('users', 200); + } + } + + $this->load->view('admin/users/edit', array('user' => $user)); + } + + /** + * Allows admins to delete the specified user. + * + * @param integer $id + */ + public function delete($id = '') { + if (!is_array($this->user->getUserByID())) { + show_404(); + } + + $this->user->delete($id); + $this->messages->add('The selected user was deleted', 'success'); + redirect('users', 200); + } } \ No newline at end of file diff --git a/application/language/english/users_lang.php b/application/language/english/users_lang.php index d9ee277..829211e 100644 --- a/application/language/english/users_lang.php +++ b/application/language/english/users_lang.php @@ -10,6 +10,16 @@ $lang['user_create'] = "Create new user"; $lang['user_delete'] = "Delete"; $lang['create_user'] = "Create a new user"; +$lang['edit_user'] = "Edit user"; + +$lang['field_username'] = "Username"; +$lang['field_password'] = "Password"; +$lang['field_password_confirm'] = "Confirm password"; +$lang['field_firstname'] = "First name"; +$lang['field_lastname'] = "Last name"; +$lang['field_email'] = "Email address"; +$lang['field_institution'] = "Institution"; +$lang['field_phone'] = "Phone number"; /* End of file users_lang.php */ /* Location: ./application/language/english/users_lang.php */ diff --git a/application/models/user.php b/application/models/user.php index 28fea9b..adc5024 100644 --- a/application/models/user.php +++ b/application/models/user.php @@ -350,9 +350,8 @@ class User extends CI_Model { * @return array */ public function getUserByID($id = false) { - // if no ID was passed use the current users ID if (empty($id)) { - $id = $this->session->userdata('user_id'); + return false; } $this->db->where('users.id', $id); diff --git a/application/views/admin/users/create.php b/application/views/admin/users/create.php index aa7810a..72fabd5 100644 --- a/application/views/admin/users/create.php +++ b/application/views/admin/users/create.php @@ -11,35 +11,43 @@

Required information