From 1496c44505c3f352ad394430949c928c92a787f3 Mon Sep 17 00:00:00 2001 From: Eike Foken Date: Thu, 15 Sep 2011 18:28:45 +0200 Subject: [PATCH] Implement user settings --- application/config/form_validation.php | 10 ++++++++++ application/controllers/auth.php | 11 ++++++++++- application/libraries/Access.php | 16 ++++++++++++++++ application/models/user.php | 26 ++++++++++++++++++++++++++ application/views/auth/settings.php | 12 ++++++++++-- 5 files changed, 72 insertions(+), 3 deletions(-) diff --git a/application/config/form_validation.php b/application/config/form_validation.php index 38f1eed..f16ddd0 100644 --- a/application/config/form_validation.php +++ b/application/config/form_validation.php @@ -72,6 +72,16 @@ $config['auth/settings'] = array( 'label' => _('New password'), 'rules' => 'min_length[6]|matches[new_password_confirm]', ), + array( + 'field' => 'job_check_interval', + 'label' => _('Job checking interval'), + 'rules' => 'greater_than[4]', + ), + array( + 'field' => 'projects_sort_recently', + 'label' => _('Sort projects by last access'), + 'rules' => 'integer', + ), ); /** diff --git a/application/controllers/auth.php b/application/controllers/auth.php index 57c5bf8..fcedc58 100644 --- a/application/controllers/auth.php +++ b/application/controllers/auth.php @@ -128,8 +128,17 @@ class Auth extends CI_Controller { 'phone' => $this->input->post('phone') ); + // update the users settings + $settings = array( + 'projects_sort_recently' => $this->input->post('projects_sort_recently'), + 'jobs_check_interval' => $this->input->post('jobs_check_interval'), + ); + + $this->user->updateSettings($settings, $user['id']); + $this->session->unset_userdata('settings'); // clear saved settings + if ($this->user->update($user['id'], $data)) { - $this->messages->add(_("Settings saved successfully"), 'success'); + $this->messages->add(_("Changes saved successfully"), 'success'); redirect('auth/settings', 303); } } diff --git a/application/libraries/Access.php b/application/libraries/Access.php index a19b876..6e3a395 100644 --- a/application/libraries/Access.php +++ b/application/libraries/Access.php @@ -273,6 +273,22 @@ class Access { return $this->cache['profile']; } + /** + * Gets the settings of the current user. + * + * @return array + */ + public function settings($key) { + if ((boolean) $this->CI->session->userdata('settings')) { + $settings = $this->CI->session->userdata('settings'); + } else { + $settings = $this->CI->user->getSettings($this->CI->session->userdata('user_id')); + $this->CI->session->set_userdata('settings', $settings); + } + + return isset($settings[$key]) ? $settings[$key] : false; + } + } /* End of file Access.php */ diff --git a/application/models/user.php b/application/models/user.php index c719851..c5dc56c 100644 --- a/application/models/user.php +++ b/application/models/user.php @@ -454,6 +454,17 @@ class User extends CI_Model { ->where('id', $user->group_id)->get('groups')->row_array(); } + /** + * Gets a users settings. + * + * @param string $userId + * @return array + */ + public function getSettings($userId) { + $query = $this->db->get_where('users_settings', array('user_id' => $userId)); + return $query->row_array(); + } + /** * Updates a user. * @@ -480,6 +491,21 @@ class User extends CI_Model { return $this->db->affected_rows() > 0; } + /** + * + */ + public function updateSettings($data, $userId) { + foreach ($data as $key => $value) { + $data[$key] = $this->db->escape($value); + } + + $query = $this->db->query("REPLACE INTO `users_settings` (`user_id`, " + . implode(", ", array_keys($data)) . ") VALUES ('" . $userId . "', " + . implode(", ", array_values($data)) . ")"); + + return $this->db->affected_rows() > 0; + } + /** * Deletes a specified user. * diff --git a/application/views/auth/settings.php b/application/views/auth/settings.php index dcfb390..106f767 100644 --- a/application/views/auth/settings.php +++ b/application/views/auth/settings.php @@ -57,10 +57,18 @@