diff --git a/src/application/controllers/appointments.php b/src/application/controllers/appointments.php index 7d27815d..e8ce21fe 100644 --- a/src/application/controllers/appointments.php +++ b/src/application/controllers/appointments.php @@ -371,7 +371,7 @@ class Appointments extends CI_Controller { } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array( exceptionToJavascript($exc) ) + 'exceptions' => array( exceptionToJavaScript($exc) ) )); } } @@ -422,7 +422,7 @@ class Appointments extends CI_Controller { } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } diff --git a/src/application/controllers/backend.php b/src/application/controllers/backend.php index 2c494371..29fc59f4 100644 --- a/src/application/controllers/backend.php +++ b/src/application/controllers/backend.php @@ -109,9 +109,9 @@ class Backend extends CI_Controller { $view['base_url'] = $this->config->item('base_url'); $view['company_name'] = $this->settings_model->get_setting('company_name'); + $view['admins'] = $this->admins_model->get_batch(); $view['providers'] = $this->providers_model->get_batch(); $view['secretaries'] = $this->secretaries_model->get_batch(); - $view['admins'] = $this->admins_model->get_batch(); $view['services'] = $this->services_model->get_batch(); $this->load->view('backend/header', $view); diff --git a/src/application/controllers/backend_api.php b/src/application/controllers/backend_api.php index 9c52cd5c..9c533383 100644 --- a/src/application/controllers/backend_api.php +++ b/src/application/controllers/backend_api.php @@ -60,7 +60,7 @@ class Backend_api extends CI_Controller { } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -135,7 +135,7 @@ class Backend_api extends CI_Controller { } } } catch(Exception $exc) { - $warnings[] = exceptionToJavascript($exc); + $warnings[] = exceptionToJavaScript($exc); } // :: SEND EMAIL NOTIFICATIONS TO PROVIDER AND CUSTOMER @@ -176,7 +176,7 @@ class Backend_api extends CI_Controller { $provider_message, $provider_link, $provider['email']); } catch(Exception $exc) { - $warnings[] = exceptionToJavascript($exc); + $warnings[] = exceptionToJavaScript($exc); } if (!isset($warnings)) { @@ -188,7 +188,7 @@ class Backend_api extends CI_Controller { } } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -243,7 +243,7 @@ class Backend_api extends CI_Controller { $this->google_sync->delete_appointment($appointment['id_google_calendar']); } } catch(Exception $exc) { - $warnings[] = exceptionToJavascript($exc); + $warnings[] = exceptionToJavaScript($exc); } } @@ -257,7 +257,7 @@ class Backend_api extends CI_Controller { $service, $customer, $company_settings, $customer['email'], $_POST['delete_reason']); } catch(Exception $exc) { - $warnings[] = exceptionToJavascript($exc); + $warnings[] = exceptionToJavaScript($exc); } // :: SEND RESPONSE TO CLIENT BROWSER @@ -270,7 +270,7 @@ class Backend_api extends CI_Controller { } } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -298,7 +298,7 @@ class Backend_api extends CI_Controller { } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -347,7 +347,7 @@ class Backend_api extends CI_Controller { } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -402,7 +402,7 @@ class Backend_api extends CI_Controller { } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -446,7 +446,7 @@ class Backend_api extends CI_Controller { } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -464,7 +464,7 @@ class Backend_api extends CI_Controller { echo json_encode(AJAX_SUCCESS); } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -481,7 +481,7 @@ class Backend_api extends CI_Controller { echo json_encode(AJAX_SUCCESS); } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -499,7 +499,7 @@ class Backend_api extends CI_Controller { echo json_encode(AJAX_SUCCESS); } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -516,7 +516,7 @@ class Backend_api extends CI_Controller { echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE); } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -539,7 +539,7 @@ class Backend_api extends CI_Controller { echo json_encode($services); } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -558,7 +558,7 @@ class Backend_api extends CI_Controller { echo json_encode(AJAX_SUCCESS); } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -575,7 +575,7 @@ class Backend_api extends CI_Controller { echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE); } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } @@ -595,7 +595,199 @@ class Backend_api extends CI_Controller { echo json_encode($categories); } catch(Exception $exc) { echo json_encode(array( - 'exceptions' => array(exceptionToJavascript($exc)) + 'exceptions' => array(exceptionToJavaScript($exc)) + )); + } + } + + /** + * [AJAX] Filter admin records with string key. + * + * @param string $_POST['key'] The key string used to filter the records. + * @return array Returns a json encoded array back to client with the admin records. + */ + public function ajax_filter_admins() { + try { + $this->load->model('admins_model'); + $key = $_POST['key']; // @task sql injection + $where = + '(first_name LIKE "%' . $key . '%" OR last_name LIKE "%' . $key . '%" ' . + 'OR email LIKE "%' . $key . '%" OR mobile_number LIKE "%' . $key . '%" ' . + 'OR phone_number LIKE "%' . $key . '%" OR address LIKE "%' . $key . '%" ' . + 'OR city LIKE "%' . $key . '%" OR state LIKE "%' . $key . '%" ' . + 'OR zip_code LIKE "%' . $key . '%" OR notes LIKE "%' . $key . '%")'; + $admins = $this->admins_model->get_batch($where); + echo json_encode($admins); + } catch(Exception $exc) { + echo json_encode(array( + 'exceptions' => array(exceptionToJavaScript($exc)) + )); + } + } + + /** + * [AJAX] Save (insert or update) admin record into database. + * + * @param array $_POST['admin'] A json encoded array that contains the admin data. If an 'id' + * value is provided then the record is going to be updated. + * @return string Returns the success contant 'AJAX_SUCCESS' so javascript knows that + * everything completed successfully. + */ + public function ajax_save_admin() { + try { + $this->load->model('admins_model'); + $admin = json_decode($_POST['admin'], true); + $this->admins_model->add($admin); + echo json_encode(AJAX_SUCCESS); + } catch(Exception $exc) { + echo json_encode(array( + 'exceptions' => array(exceptionToJavaScript($exc)) + )); + } + } + + /** + * [AJAX] Delete an admin record from the database. + * + * @param numeric $_POST['admin_id'] The id of the record to be deleted. + * @return string Returns the operation result constant (AJAX_SUCESS or AJAX_FAILURE). + */ + public function ajax_delete_admin() { + try { + $this->load->model('admins_model'); + $result = $this->admins_model->delete($_POST['admin_id']); + echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE); + } catch(Exception $exc) { + echo json_encode(array( + 'exceptions' => array(exceptionToJavaScript($exc)) + )); + } + } + + /** + * [AJAX] Filter provider records with string key. + * + * @param string $_POST['key'] The key string used to filter the records. + * @return array Returns a json encoded array back to client with the provider records. + */ + public function ajax_filter_providers() { + try { + $this->load->model('providers_model'); + $key = $_POST['key']; // @task sql injection + $where = + '(first_name LIKE "%' . $key . '%" OR last_name LIKE "%' . $key . '%" ' . + 'OR email LIKE "%' . $key . '%" OR mobile_number LIKE "%' . $key . '%" ' . + 'OR phone_number LIKE "%' . $key . '%" OR address LIKE "%' . $key . '%" ' . + 'OR city LIKE "%' . $key . '%" OR state LIKE "%' . $key . '%" ' . + 'OR zip_code LIKE "%' . $key . '%" OR notes LIKE "%' . $key . '%")'; + $providers = $this->providers_model->get_batch($where); + echo json_encode($providers); + } catch(Exception $exc) { + echo json_encode(array( + 'exceptions' => array(exceptionToJavaScript($exc)) + )); + } + } + + /** + * [AJAX] Save (insert or update) a provider record into database. + * + * @param array $_POST['provider'] A json encoded array that contains the provider data. If an 'id' + * value is provided then the record is going to be updated. + * @return string Returns the success contant 'AJAX_SUCCESS' so javascript knows that + * everything completed successfully. + */ + public function ajax_save_provider() { + try { + $this->load->model('providers_model'); + $provider = json_decode($_POST['provider'], true); + $this->providers_model->add($provider); + echo json_encode(AJAX_SUCCESS); + } catch(Exception $exc) { + echo json_encode(array( + 'exceptions' => array(exceptionToJavaScript($exc)) + )); + } + } + + /** + * [AJAX] Delete a provider record from the database. + * + * @param numeric $_POST['provider_id'] The id of the record to be deleted. + * @return string Returns the operation result constant (AJAX_SUCESS or AJAX_FAILURE). + */ + public function ajax_delete_provider() { + try { + $this->load->model('providers_model'); + $result = $this->providers_model->delete($_POST['provider_id']); + echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE); + } catch(Exception $exc) { + echo json_encode(array( + 'exceptions' => array(exceptionToJavaScript($exc)) + )); + } + } + + /** + * [AJAX] Filter secretary records with string key. + * + * @param string $_POST['key'] The key string used to filter the records. + * @return array Returns a json encoded array back to client with the secretary records. + */ + public function ajax_filter_secretaries() { + try { + $this->load->model('secretaries_model'); + $key = $_POST['key']; // @task sql injection + $where = + '(first_name LIKE "%' . $key . '%" OR last_name LIKE "%' . $key . '%" ' . + 'OR email LIKE "%' . $key . '%" OR mobile_number LIKE "%' . $key . '%" ' . + 'OR phone_number LIKE "%' . $key . '%" OR address LIKE "%' . $key . '%" ' . + 'OR city LIKE "%' . $key . '%" OR state LIKE "%' . $key . '%" ' . + 'OR zip_code LIKE "%' . $key . '%" OR notes LIKE "%' . $key . '%")'; + $secretaries = $this->secretaries_model->get_batch($where); + echo json_encode($secretaries); + } catch(Exception $exc) { + echo json_encode(array( + 'exceptions' => array(exceptionToJavaScript($exc)) + )); + } + } + + /** + * [AJAX] Save (insert or update) a secretary record into database. + * + * @param array $_POST['secretary'] A json encoded array that contains the secretary data. + * If an 'id' value is provided then the record is going to be updated. + * @return string Returns the success contant 'AJAX_SUCCESS' so javascript knows that + * everything completed successfully. + */ + public function ajax_save_secretary() { + try { + $this->load->model('secretaries_model'); + $secretary = json_decode($_POST['secretary'], true); + $this->secretaries_model->add($secretary); + echo json_encode(AJAX_SUCCESS); + } catch(Exception $exc) { + echo json_encode(array( + 'exceptions' => array(exceptionToJavaScript($exc)) + )); + } + } + + /** + * [AJAX] Delete a secretary record from the database. + * + * @param numeric $_POST['secretary_id'] The id of the record to be deleted. + * @return string Returns the operation result constant (AJAX_SUCESS or AJAX_FAILURE). + */ + public function ajax_delete_secretary() { + try { + $this->load->model('secretaries_model'); + $result = $this->secretaries_model->delete($_POST['secretary_id']); + echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE); + } catch(Exception $exc) { + echo json_encode(array( + 'exceptions' => array(exceptionToJavaScript($exc)) )); } } diff --git a/src/application/helpers/custom_exceptions_helper.php b/src/application/helpers/custom_exceptions_helper.php index 63e46690..7c7065fe 100644 --- a/src/application/helpers/custom_exceptions_helper.php +++ b/src/application/helpers/custom_exceptions_helper.php @@ -60,7 +60,7 @@ function exceptionToHtml($exc) { * @param Exception $exception The given exception object. * @return string Returns the json encoded object of the exception. */ -function exceptionToJavascript($exception) { +function exceptionToJavaScript($exception) { return json_encode(array( 'code' => $exception->getCode(), 'file' => $exception->getFile(), diff --git a/src/application/libraries/Unit_tests/Unit_tests.php b/src/application/libraries/Unit_tests/Unit_tests.php index 53b036cd..31867a09 100644 --- a/src/application/libraries/Unit_tests/Unit_tests.php +++ b/src/application/libraries/Unit_tests/Unit_tests.php @@ -55,12 +55,12 @@ class Unit_tests extends CI_Driver_Library { */ public function run_model_tests($output_report = true) { // @task Reenable all model tests. -// $this->appointments_model->run_all(); -// $this->customers_model->run_all(); -// $this->settings_model->run_all(); -// $this->providers_model->run_all(); -// $this->services_model->run_all(); -// $this->admins_model->run_all(); + $this->appointments_model->run_all(); + $this->customers_model->run_all(); + $this->settings_model->run_all(); + $this->providers_model->run_all(); + $this->services_model->run_all(); + $this->admins_model->run_all(); $this->secretaries_model->run_all(); if ($output_report) { diff --git a/src/application/libraries/Unit_tests/drivers/Unit_tests_admins_model.php b/src/application/libraries/Unit_tests/drivers/Unit_tests_admins_model.php index 5a6ff65e..d20c9d72 100644 --- a/src/application/libraries/Unit_tests/drivers/Unit_tests_admins_model.php +++ b/src/application/libraries/Unit_tests/drivers/Unit_tests_admins_model.php @@ -4,6 +4,7 @@ class Unit_tests_admins_model extends CI_Driver { private $ci; private $admin_role_id; private $default_admin; // does not contain an 'id' value + private $default_settings; // does not contain 'id_users' value /** * Class Constructor @@ -29,6 +30,17 @@ class Unit_tests_admins_model extends CI_Driver { 'notes' => 'This is a test admin user.', 'id_roles' => $this->admin_role_id ); + + $this->default_settings = array( + 'username' => 'test_admin', + 'password' => 'test_pswd', + 'working_plan' => NULL, + 'notifications' => FALSE, + 'google_sync' => FALSE, + 'google_token' => NULL, + 'sync_past_days' => NULL, + 'sync_future_days' => NULL + ); } /** diff --git a/src/application/libraries/Unit_tests/drivers/Unit_tests_secretaries_model.php b/src/application/libraries/Unit_tests/drivers/Unit_tests_secretaries_model.php index 2c7b4880..da6c3b1b 100644 --- a/src/application/libraries/Unit_tests/drivers/Unit_tests_secretaries_model.php +++ b/src/application/libraries/Unit_tests/drivers/Unit_tests_secretaries_model.php @@ -59,6 +59,7 @@ class Unit_tests_secretaries_model extends CI_Driver { . 'has returned and integer value.'); $db_secretary = $this->ci->db->get_where('ea_users', array('id' => $secretary['id']))->row_array(); + $db_secretary['providers'] = array(); $this->ci->unit->run($secretary, $db_secretary, 'Test if add() - insert operation - ' . 'has successfully inserted a new record.'); @@ -88,6 +89,7 @@ class Unit_tests_secretaries_model extends CI_Driver { . 'returned an integer value.'); $db_secretary = $this->ci->db->get_where('ea_users', array('id' => $secretary['id']))->row_array(); + $db_secretary['providers'] = array(); $this->ci->unit->run($secretary, $db_secretary, 'Test if add() - update operation - has ' . 'successfully updated the secretary record.'); @@ -125,6 +127,7 @@ class Unit_tests_secretaries_model extends CI_Driver { . 'returned and integer value.'); $db_secretary = $this->ci->db->get_where('ea_users', array('id' => $secretary_id))->row_array(); + $db_secretary['providers'] = array(); unset($db_secretary['id']); $this->ci->unit->run($secretary, $db_secretary, 'Test if add() - update operation - has ' . 'successfully updated the secretary record using find_record_id() method ' @@ -323,9 +326,14 @@ class Unit_tests_secretaries_model extends CI_Driver { private function test_get_row_record_does_not_exist() { $random_id = 2309203923; // no record exists with this id. - $model_secretary = $this->ci->secretaries_model->get_row($random_id); - $this->ci->unit->run($model_secretary, array(), 'Test if get_row() has returned an empty ' - . 'array on record that does not exist.'); + $has_thrown_exc = FALSE; + try { + $this->ci->secretaries_model->get_row($random_id); + } catch (Exception $exc) { + $has_thrown_exc = TRUE; + } + $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_row() has thrown an exception ' + . 'when trying to get a record that does not exist in the database.'); } // TEST GET VALUE METHOD -------------------------------------------------- diff --git a/src/application/models/admins_model.php b/src/application/models/admins_model.php index f7635aee..0273faf4 100644 --- a/src/application/models/admins_model.php +++ b/src/application/models/admins_model.php @@ -17,6 +17,7 @@ * 'zip_code' * 'notes' * 'id_roles' + * 'settings' >>> array that contains user settings (username, password etc) */ class Admins_Model extends CI_Model { /** @@ -85,12 +86,27 @@ class Admins_Model extends CI_Model { */ public function insert($admin) { $admin['id_roles'] = $this->get_admin_role_id(); + $settings = $admin['settings']; + unset($admin['settings']); + + $this->db->trans_begin(); if (!$this->db->insert('ea_users', $admin)) { throw new Exception('Could not insert admin into the database.'); } - return intval($this->db->insert_id()); + $admin['id'] = intval($this->db->insert_id()); + $settings['id_users'] = $admin['id']; + + // Insert admin settings. + if (!$this->db->insert('ea_user_settings', $settings)) { + $this->db->trans_rollback(); + throw new Exception('Could not insert admin settings into the database.'); + } + + $this->db->trans_complete(); + + return $admin['id']; } /** @@ -101,11 +117,20 @@ class Admins_Model extends CI_Model { * @throws Exception When the update operation fails. */ public function update($admin) { + $settings = $admin['settings']; + unset($admin['settings']); + $settings['id_users'] = $admin['id']; + $this->db->where('id', $admin['id']); - if (!$this->db->update('ea_users', $admin)){ + if (!$this->db->update('ea_users', $admin)) { throw new Exception('Could not update admin record.'); } + $this->db->where('id_users', $settings['id_users']); + if (!$this->db->update('ea_user_settings', $settings)) { + throw new Exception('Could not update admin settings.'); + } + return intval($admin['id']); } @@ -218,6 +243,10 @@ class Admins_Model extends CI_Model { } $admin = $this->db->get_where('ea_users', array('id' => $admin_id))->row_array(); + $admin['settings'] = $this->db->get_where('ea_user_settings', + array('id_users' => $admin_id))->row_array(); + unset($admin['settings']['id_users']); + return $admin; } @@ -272,8 +301,14 @@ class Admins_Model extends CI_Model { $this->db->where($where_clause); } - $this->db->where('id_roles', $role_id); - $batch = $this->db->get('ea_users')->result_array(); + $batch = $this->db->get_where('ea_users', array('id_roles' => $role_id))->result_array(); + + // Get every admin settings. + foreach ($batch as &$admin) { + $admin['settings'] = $this->db->get_where('ea_user_settings', + array('id_users' => $admin['id']))->row_array(); + unset($admin['settings']['id_users']); + } return $batch; } diff --git a/src/application/models/secretaries_model.php b/src/application/models/secretaries_model.php index 11d8837b..2c1631df 100644 --- a/src/application/models/secretaries_model.php +++ b/src/application/models/secretaries_model.php @@ -17,7 +17,7 @@ * 'zip_code' * 'notes' * 'id_roles' - * 'provders' >> array with provider ids that the secretary handles + * 'providers' >> array with provider ids that the secretary handles */ class Secretaries_Model extends CI_Model { /** @@ -38,7 +38,7 @@ class Secretaries_Model extends CI_Model { if (!$this->validate($secretary)) { throw new Exception('Secretary data are invalid: ' . print_r($secretary, TRUE)); } - + if ($this->exists($secretary) && !isset($secretary['id'])) { $secretary['id'] = $this->find_record_id($secretary); } @@ -87,7 +87,9 @@ class Secretaries_Model extends CI_Model { public function insert($secretary) { $providers = $secretary['providers']; unset($secretary['providers']); - + $settings = $secretary['settings']; + unset($secretary['settings']); + $secretary['id_roles'] = $this->get_secretary_role_id(); if (!$this->db->insert('ea_users', $secretary)) { @@ -96,7 +98,8 @@ class Secretaries_Model extends CI_Model { $secretary['id'] = intval($this->db->insert_id()); - $this->save_providers($providers,$secretary['id']); + $this->save_providers($providers, $secretary['id']); + $this->save_settings($settings, $secretary['id']); return $secretary['id']; } @@ -111,6 +114,8 @@ class Secretaries_Model extends CI_Model { public function update($secretary) { $providers = $secretary['providers']; unset($secretary['providers']); + $settings = $secretary['settings']; + unset($secretary['settings']); $this->db->where('id', $secretary['id']); if (!$this->db->update('ea_users', $secretary)){ @@ -118,6 +123,7 @@ class Secretaries_Model extends CI_Model { } $this->save_providers($providers, $secretary['id']); + $this->save_settings($settings, $secretary['id']); return intval($secretary['id']); } @@ -170,7 +176,7 @@ class Secretaries_Model extends CI_Model { } // Validate 'providers' value datatype (must be array) - if (isset($secretary['provders']) && !is_array($secretary['providers'])) { + if (isset($secretary['providers']) && !is_array($secretary['providers'])) { throw new Exception('Secretary providers value is not an array.'); } @@ -218,15 +224,30 @@ class Secretaries_Model extends CI_Model { * @param numeric $secretary_id The id of the record to be returned. * @return array Returns an array with the secretary user data. * @throws Exception When the $secretary_id is not a valid numeric value. + * @throws Exception When given record id does not exist in the database. */ public function get_row($secretary_id) { if (!is_numeric($secretary_id)) { throw new Exception('$secretary_id argument is not a valid numeric value: ' . $secretary_id); } + // Check if record exists + if ($this->db->get_where('ea_users', array('id' => $secretary_id))->num_rows() == 0) { + throw new Exception('The given secretary id does not match a record in the database.'); + } + + $secretary = $this->db->get_where('ea_users', array('id' => $secretary_id))->row_array(); - $secretary['providers'] = $this->db->get_where('ea_secretaries_providers', - array('id_users_secretary' => $secretary_id))->result_array(); + + $secretary_providers = $this->db->get_where('ea_secretaries_providers', + array('id_users_secretary' => $secretary['id']))->result_array(); + $secretary['providers'] = array(); + foreach($secretary_providers as $secretary_provider) { + $secretary['providers'][] = $secretary_provider['id_users_provider']; + } + + $secretary['settings'] = $this->db->get_where('ea_user_settings', + array('id_users' => $secretary['id']))->row_array(); return $secretary; } @@ -285,10 +306,18 @@ class Secretaries_Model extends CI_Model { $this->db->where('id_roles', $role_id); $batch = $this->db->get('ea_users')->result_array(); - // Include every secretary handling users. + // Include every secretary providers. foreach ($batch as &$secretary) { - $secretary['providers'] = $this->db->get_where('ea_secretaries_providers', + $secretary_providers = $this->db->get_where('ea_secretaries_providers', array('id_users_secretary' => $secretary['id']))->result_array(); + + $secretary['providers'] = array(); + foreach($secretary_providers as $secretary_provider) { + $secretary['providers'][] = $secretary_provider['id_users_provider']; + } + + $secretary['settings'] = $this->db->get_where('ea_user_settings', + array('id_users' => $secretary['id']))->row_array(); } return $batch; @@ -313,6 +342,9 @@ class Secretaries_Model extends CI_Model { throw new Exception('Invalid argument given $providers: ' . print_r($providers, TRUE)); } + // Delete old connections + $this->db->delete('ea_secretaries_providers', array('id_users_secretary' => $secretary_id)); + if (count($providers) > 0) { foreach ($providers as $provider_id) { $this->db->insert('ea_secretaries_providers', array( @@ -322,6 +354,60 @@ class Secretaries_Model extends CI_Model { } } } + + /** + * Save the secretary settings (used from insert or update operation). + * + * @param array $settings Contains the setting values. + * @param numeric $secretary_id Record id of the secretary. + */ + private function save_settings($settings, $secretary_id) { + if (!is_numeric($secretary_id)) { + throw new Exception('Invalid $provider_id argument given :' . $secretary_id); + } + + if (count($settings) == 0 || !is_array($settings)) { + throw new Exception('Invalid $settings argument given:' . print_r($settings, TRUE)); + } + + // Check if the setting record exists in db. + if ($this->db->get_where('ea_user_settings', array('id_users' => $secretary_id)) + ->num_rows() == 0) { + $this->db->insert('ea_user_settings', array('id_users' => $secretary_id)); + } + + foreach($settings as $name=>$value) { + $this->set_setting($name, $value, $secretary_id); + } + } + + /** + * Get a providers setting from the database. + * + * @param string $setting_name The setting name that is going to be + * returned. + * @param int $secretary_id The selected provider id. + * @return string Returs the value of the selected user setting. + */ + public function get_setting($setting_name, $secretary_id) { + $provider_settings = $this->db->get_where('ea_user_settings', + array('id_users' => $secretary_id))->row_array(); + return $provider_settings[$setting_name]; + } + + /** + * Set a provider's setting value in the database. + * + * The provider and settings record must already exist. + * + * @param string $setting_name The setting's name. + * @param string $value The setting's value. + * @param numeric $secretary_id The selected provider id. + */ + public function set_setting($setting_name, $value, $secretary_id) { + $this->db->where(array('id_users' => $secretary_id)); + return $this->db->update('ea_user_settings', array($setting_name => $value)); + } } /* End of file secretaries_model.php */ diff --git a/src/application/views/backend/users.php b/src/application/views/backend/users.php index cd1afe87..94acb48a 100644 --- a/src/application/views/backend/users.php +++ b/src/application/views/backend/users.php @@ -1,4 +1,312 @@ -
- This is something totally random. -
+ + + + +t |
t |