* Added provider services support to providers_model.php

* Finished unit testing for providers model
* Completed admins_model.php
This commit is contained in:
alextselegidis@gmail.com 2013-07-27 22:48:19 +00:00
parent c56e5ddf07
commit cc084e002f
4 changed files with 997 additions and 126 deletions

View file

@ -52,11 +52,12 @@ class Unit_tests extends CI_Driver_Library {
* report will be outputted. * report will be outputted.
*/ */
public function run_model_tests($output_report = true) { public function run_model_tests($output_report = true) {
$this->appointments_model->run_all(); // @task Reenable all model tests.
$this->customers_model->run_all(); //$this->appointments_model->run_all();
$this->settings_model->run_all(); //$this->customers_model->run_all();
//$this->settings_model->run_all();
$this->providers_model->run_all(); $this->providers_model->run_all();
$this->services_model->run_all(); //$this->services_model->run_all();
if ($output_report) { if ($output_report) {
$this->CI->output->append_output($this->CI->unit->report()); $this->CI->output->append_output($this->CI->unit->report());

View file

@ -3,6 +3,7 @@
class Unit_tests_providers_model extends CI_Driver { class Unit_tests_providers_model extends CI_Driver {
private $ci; private $ci;
private $provider_role_id; private $provider_role_id;
private $default_working_plan;
/** /**
* Class Constructor * Class Constructor
@ -14,13 +15,16 @@ class Unit_tests_providers_model extends CI_Driver {
$this->provider_role_id = $this->ci->db->get_where('ea_roles', $this->provider_role_id = $this->ci->db->get_where('ea_roles',
array('slug' => DB_SLUG_PROVIDER))->row()->id; array('slug' => DB_SLUG_PROVIDER))->row()->id;
$this->default_working_plan = $this->ci->db->get_where('ea_settings',
array('name' => 'company_working_plan'))->row()->value;
} }
/** /**
* Run all the available tests * Run all the available tests
*/ */
public function run_all() { public function run_all() {
// All the methods whose names start with "test" are going to be // All the methods whose names start with "test" are going to be
// executed. If you want a method to not be executed remove the // executed. If you want a method to not be executed remove the
// "test" keyword from the beginning. // "test" keyword from the beginning.
$class_methods = get_class_methods('Unit_tests_providers_model'); $class_methods = get_class_methods('Unit_tests_providers_model');
@ -39,6 +43,208 @@ class Unit_tests_providers_model extends CI_Driver {
$provider = array( $provider = array(
'first_name' => 'John', 'first_name' => 'John',
'last_name' => 'Doe', 'last_name' => 'Doe',
'email' => 'test@test.com',
'mobile_number' => '000000',
'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id,
'settings' => array(
'username' => 'test_prov',
'password' => 'test_prov',
'working_plan' => $this->default_working_plan,
'notifications' => TRUE,
'google_sync' => FALSE,
'google_token' => NULL,
'sync_past_days' => '5',
'sync_future_days' => '5'
)
);
// Insert new provider.
$provider['id'] = $this->ci->providers_model->add($provider);
$this->ci->unit->run($provider['id'], 'is_int', 'Check if add (insert) result is integer.');
// Check if record was inserted successfully.
$db_provider = $this->ci->db->get_where('ea_users', array('id' => $provider['id']))->row_array();
$db_provider['settings'] = $this->ci->db->get_where('ea_user_settings',
array('id_users' => $provider['id']))->row_array();
unset($db_provider['settings']['id_users']); // not needed
$this->ci->unit->run($provider, $db_provider, 'Check if add(insert) has successfully '
. 'inserted a provider record.');
// Delete provider record (the relative ea_user_settings record will be deleted
// automatically because of the cascade on delete setting.
if ($this->ci->db->get_where('ea_users', array('id' => $provider['id']))->num_rows() > 0) {
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
}
}
private function test_add_update() {
// Insert new provider.
$provider = array(
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'test@test.com',
'mobile_number' => '000000',
'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id
);
$this->ci->db->insert('ea_users', $provider);
$provider['id'] = $this->ci->db->insert_id();
// Insert provider settings.
$settings = array(
'id_users' => $provider['id'],
'username' => 'test_prov',
'password' => 'test_prov',
'working_plan' => $this->default_working_plan,
'notifications' => TRUE,
'google_sync' => FALSE,
'google_token' => NULL,
'sync_past_days' => '5',
'sync_future_days' => '5'
);
$this->ci->db->insert('ea_user_settings', $settings);
// Update provider and check whether the operation completed successfully.
$provider = array(
'id' => $provider['id'], // Keep the old ID, everything else changes.
'first_name' => 'CHANGED',
'last_name' => 'CHANGED',
'email' => 'changed@changed.com',
'mobile_number' => 'CHANGED',
'phone_number' => 'CHANGED',
'address' => 'CHANGED',
'city' => 'CHANGED',
'state' => 'CHANGED',
'zip_code' => 'CHANGED',
'notes' => 'CHANGED',
'id_roles' => $this->provider_role_id,
'settings' => array(
'username' => 'CHANGED',
'password' => 'CHANGED',
'working_plan' => $this->default_working_plan,
'notifications' => TRUE,
'google_sync' => FALSE,
'google_token' => NULL,
'sync_past_days' => '9', // changed
'sync_future_days' => '8' // changed
)
);
$update_result = $this->ci->providers_model->add($provider);
$this->ci->unit->run($update_result, 'is_int', 'Check if add (update) result is integer.');
// Check if record was updated successfully
$db_provider = $this->ci->db->get_where('ea_users', array('id' => $provider['id']))->row_array();
$db_provider['settings'] = $this->ci->db->get_where('ea_user_settings',
array('id_users' => $provider['id']))->row_array();
unset($db_provider['settings']['id_users']);
$this->ci->unit->run($provider, $db_provider, 'Check if add(update) has successfully '
. 'updated a provider record.');
// Delete provider record (the relative ea_user_settings record will be deleted
// automatically because of the cascade on delete setting.
if ($this->ci->db->get_where('ea_users', array('id' => $provider['id']))->num_rows() > 0) {
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
}
}
private function test_add_with_invalid_data() {
// Provider's data are missing required fields. That means that an excpetion
// is expected.
$provider = array(
'first_name' => 'some name',
'last_name' => 'some name',
);
$has_thrown_exc = FALSE;
try {
$this->ci->providers_model->add($provider);
} catch(Exception $exc) {
$has_thrown_exc = TRUE;
}
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if add() with invalid data has '
. 'thrown an exception');
}
// TEST EXISTS METHOD
private function test_exists_with_record_that_exists() {
// Insert new provider record.
$provider = array(
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'test@test.com',
'mobile_number' => '000000',
'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id
);
$this->ci->db->insert('ea_users', $provider);
$provider['id'] = $this->ci->db->insert_id();
// Check whether exists() detects the record.
$exists = $this->ci->providers_model->exists($provider);
$this->ci->unit->run($exists, TRUE, 'Test exists() with record that exists.');
// Delete provider record.
if ($this->ci->db->get_where('ea_users', array('id' => $provider['id']))->num_rows() > 0) {
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
}
}
private function test_exists_with_invalid_data() {
// Insert new provider record.
$provider = array(
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'test@test.com',
'mobile_number' => '000000',
'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id
);
$this->ci->db->insert('ea_users', $provider);
$provider['id'] = $this->ci->db->insert_id();
// Email is not provided, an exception is expected.
$has_thrown_exc = FALSE;
try {
unset($provider['email']);
$this->ci->providers_model->exists($provider);
} catch(Exception $exc) {
$has_thrown_exc = TRUE;
}
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test exists() with invalid record data.');
// Delete provider record.
if ($this->ci->db->get_where('ea_users', array('id' => $provider['id']))->num_rows() > 0) {
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
}
}
private function test_exists_with_record_that_does_not_exist() {
$provider = array(
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'test@test.com',
'mobile_number' => '000000', 'mobile_number' => '000000',
'phone_number' => '111111', 'phone_number' => '111111',
'address' => 'Some Str', 'address' => 'Some Str',
@ -49,83 +255,317 @@ class Unit_tests_providers_model extends CI_Driver {
'id_roles' => $this->provider_role_id 'id_roles' => $this->provider_role_id
); );
$provider['id'] = $this->ci->providers_model->add($provider); $exists = $this->ci->providers_model->exists($provider);
$this->ci->unit->run($provider['id'], 'is_int', 'Check if add (insert) result is integer.'); $this->ci->unit->run($exists, FALSE, 'Test exists() with record that does not exist.');
$db_record = $this->ci->db->get_where('ea_users', array('id' => $provider['id']))->row_array();
$this->ci->unit->run($provider, $db_record, 'Check if add(insert) has successfully '
. 'inserted a provider record.');
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
}
private function test_add_update() {
}
private function test_add_with_invalid_data() {
}
// TEST EXISTS METHOD
private function test_exists_with_record_that_exists() {
}
private function test_exists_with_invalid_data() {
}
private function test_exists_with_record_that_does_not_exist() {
} }
// TEST FIND RECORD ID METHOD // TEST FIND RECORD ID METHOD
private function test_find_record_id() { private function test_find_record_id() {
// Insert new provider record.
$provider = array(
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'test@test.com',
'mobile_number' => '000000',
'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id
);
$this->ci->db->insert('ea_users', $provider);
$insert_id = $this->ci->db->insert_id();
// Check if the record id will be retrieved.
$provider['id'] = $this->ci->providers_model->find_record_id($provider);
$this->ci->unit->run($provider['id'], $insert_id, 'Test if find_record_id() has '
. 'successfully found the inserted record id.');
// Delete provider record.
if ($this->ci->db->get_where('ea_users', array('id' => $insert_id))->num_rows() > 0) {
$this->ci->db->delete('ea_users', array('id' => $insert_id));
}
} }
private function test_find_record_id_with_invalid_data() { private function test_find_record_id_with_invalid_data() {
$provider = array(
'first_name' => 'John',
'last_name' => 'Doe',
//'email' => 'test@test.com', // Email is not provided
'mobile_number' => '000000',
'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id
);
// An exception is expected.
$has_thrown_exc = FALSE;
try {
$this->ci->providers_model->find_record_id($provider);
} catch(Exception $exc) {
$has_thrown_exc = TRUE;
}
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if find_record_id() has thrown '
. 'exception on invalid data.');
} }
private function test_find_record_id_with_record_that_does_not_exist() { private function test_find_record_id_with_record_that_does_not_exist() {
// The following provider record does not exist on database. Therefore an
// exception is expected.
$provider = array(
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'test@test.com',
'mobile_number' => '000000',
'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id
);
$has_thrown_exc = FALSE;
try {
$this->ci->providers_model->find_record_id($provider);
} catch(Exception $exc) {
$has_thrown_exc = TRUE;
}
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if find_record_id() has thrown '
. 'exception on record that does not exist.');
} }
// TEST VALIDATE RECORD METHOD // TEST VALIDATE RECORD METHOD
private function test_validate() { private function test_validate() {
$provider = array(
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'test@test.com',
'mobile_number' => '000000',
'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id,
'settings' => array(
'username' => 'test_prov',
'password' => 'test_prov',
'working_plan' => $this->default_working_plan,
'notifications' => TRUE,
'google_sync' => FALSE,
'google_token' => NULL,
'sync_past_days' => '5',
'sync_future_days' => '5'
)
);
$validation_result = $this->ci->providers_model->validate($provider);
$this->ci->unit->run($validation_result, TRUE, 'Test if validate() returned TRUE '
. 'with valid data');
} }
private function test_validate_with_record_that_does_not_exist() { private function test_validate_with_record_that_does_not_exist() {
$provider = array(
'id' => '23209348092', // This id does not exists in db.
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'test@test.com',
'mobile_number' => '000000',
'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id,
'settings' => array(
'username' => 'test_prov',
'password' => 'test_prov',
'working_plan' => $this->default_working_plan,
'notifications' => TRUE,
'google_sync' => FALSE,
'google_token' => NULL,
'sync_past_days' => '5',
'sync_future_days' => '5'
)
);
$validation_result = $this->ci->providers_model->validate($provider);
$this->ci->unit->run($validation_result, FALSE, 'Test if validate() has returned FALSE '
. 'with record that does not exist.');
} }
private function test_validate_with_missing_required_fields() { private function test_validate_with_missing_required_fields() {
$provider = array(
'first_name' => 'John',
//'last_name' => 'Doe',
//'email' => 'test@test.com',
'mobile_number' => '000000',
//'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id,
'settings' => array(
'username' => 'test_prov',
'password' => 'test_prov',
'working_plan' => $this->default_working_plan,
'notifications' => TRUE,
'google_sync' => FALSE,
'google_token' => NULL,
'sync_past_days' => '5',
'sync_future_days' => '5'
)
);
$validation_result = $this->ci->providers_model->validate($provider);
$this->ci->unit->run($validation_result, FALSE, 'Test if validate() has returned FALSE '
. 'with data that is missing required fields.');
} }
private function test_validate_with_invalid_email_address() { private function test_validate_with_invalid_email_address() {
$provider = array(
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'THIS IS INVALID',
'mobile_number' => '000000',
'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id,
'settings' => array(
'username' => 'test_prov',
'password' => 'test_prov',
'working_plan' => $this->default_working_plan,
'notifications' => TRUE,
'google_sync' => FALSE,
'google_token' => NULL,
'sync_past_days' => '5',
'sync_future_days' => '5'
)
);
$validation_result = $this->ci->providers_model->validate($provider);
$this->ci->unit->run($validation_result, FALSE, 'Test if validate() has returned FALSE '
. 'with data that has invalid email.');
}
private function test_validate_with_no_settings() {
$provider = array(
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'THIS IS INVALID',
'mobile_number' => '000000',
'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id
);
$validation_result = $this->ci->providers_model->validate($provider);
$this->ci->unit->run($validation_result, FALSE, 'Test if validate() has returned FALSE '
. 'with data that do not contain the provider settings.');
} }
// TEST DELETE RECORD METHOD // TEST DELETE RECORD METHOD
private function test_delete() { private function test_delete() {
// Insert new provider record with settings.
$provider = array(
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'THIS IS INVALID',
'mobile_number' => '000000',
'phone_number' => '111111',
'address' => 'Some Str',
'city' => 'Some City',
'state' => 'Some State',
'zip_code' => '12345',
'notes' => 'This is a test provider',
'id_roles' => $this->provider_role_id
);
$this->ci->db->insert('ea_users', $provider);
$provider['id'] = intval($this->ci->db->insert_id());
$provider['settings'] = array(
'id_users' => $provider['id'],
'username' => 'test-prov',
'password' => 'test-prov',
'notifications' => FALSE,
'working_plan' => $this->default_working_plan,
'google_sync' => FALSE,
'google_token' => NULL,
'sync_past_days' => '5',
'sync_future_days' => 5
);
$this->ci->db->insert('ea_user_settings', $provider['settings']);
// Delete provider record from database.
$result = $this->ci->providers_model->delete($provider['id']);
$this->ci->unit->run($result, TRUE, 'Test if delete() returned TRUE as result');
// Check if the record has successfully been deleted.
$provider_num_rows = $this->ci->db->get_where('ea_users',
array('id' => $provider['id']))->num_rows();
$this->ci->unit->run($provider_num_rows, 0, 'Test if delete() has successfully '
. 'deleted provider record.');
$settings_num_rows = $this->ci->db->get_where('ea_user_settings',
array('id_users' => $provider['id']))->num_rows();
$this->ci->unit->run($settings_num_rows, 0, 'Test if delete() has successfully '
. 'deleted provider settings record.');
// Delete records (if needed)
if ($provider_num_rows > 0) {
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
}
if ($settings_num_rows > 0) {
$this->ci->db->delete('ea_user_settings', array('id_users' => $provider['id']));
}
} }
private function test_delete_with_invalid_record_id() { private function test_delete_with_invalid_record_id() {
$invalid_id = 'this is invalid';
$has_thrown_exc = FALSE;
try {
$this->ci->providers_model->delete($invalid_id);
} catch(Exception $exc) {
$has_thrown_exc = TRUE;
}
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() with invalid id argument '
. 'has thrown an exception.');
} }
private function test_delete_with_record_that_does_not_exist() { private function test_delete_with_record_that_does_not_exist() {
$provider_id = '234082390'; // This record id does not exist in db.
$result = $this->ci->providers_model->delete($provider_id);
$this->ci->unit->run($result, FALSE, 'Test if delete() with record id that does '
. 'not exist in database, has returned FALSE as result.');
} }
// TEST GET ROW METHOD --------------------------------------------- // TEST GET ROW METHOD
private function test_get_row() { private function test_get_row() {
// Insert a new customer record. // Insert a new provider record with settings.
$provider = array( $provider = array(
'last_name' => 'Doe', 'last_name' => 'Doe',
'first_name' => 'John', 'first_name' => 'John',
@ -137,43 +577,70 @@ class Unit_tests_providers_model extends CI_Driver {
'id_roles' => $this->provider_role_id 'id_roles' => $this->provider_role_id
); );
$this->ci->db->insert('ea_users', $provider); $this->ci->db->insert('ea_users', $provider);
$provider['id'] = intval($this->ci->db->insert_id()); $provider['id'] = $this->ci->db->insert_id();
// Get the new customer record from db. $provider['settings'] = array(
$no_model_data = $this->ci->db->get_where('ea_users', array('id' => $provider['id'])) 'id_users' => $provider['id'],
->row_array(); 'username' => 'testprov',
$model_data = $this->ci->providers_model->get_row($provider['id']); 'password' => 'testprov',
'notifications' => FALSE,
'working_plan' => $this->default_working_plan,
'google_sync' => FALSE,
'google_token' => NULL,
'sync_past_days' => '5',
'sync_future_days' => '5'
);
$this->ci->db->insert('ea_user_settings', $provider['settings']);
unset($provider['settings']['id_users']);
// Check that the row is the correct one. // Check if get_row() will successfully return the record.
$this->ci->unit->run($no_model_data, $model_data, 'Test get_row() method'); $no_model_provider = $this->ci->db->get_where('ea_users', array('id' => $provider['id']))->row_array();
$no_model_provider['settings'] = $this->ci->db->get_where('ea_user_settings',
array('id_users' => $provider['id']))->row_array();
unset($no_model_provider['settings']['id_users']);
// Delete inserted customer record. $model_provider = $this->ci->providers_model->get_row($provider['id']);
$this->ci->unit->run($no_model_provider, $model_provider, 'Test get_row() method successfully '
. 'returned provider record.');
// Delete inserted provider record.
$this->ci->db->delete('ea_users', array('id' => $provider['id'])); $this->ci->db->delete('ea_users', array('id' => $provider['id']));
} }
private function test_get_row_that_does_not_exist() { private function test_get_row_that_does_not_exist() {
$random_record_id = 486868412; $random_record_id = 486868412;
$row_data = $this->ci->providers_model->get_row($random_record_id);
$this->ci->unit->run($row_data, NULL, 'Test get_row() with record id that does ' $has_thrown_exc = FALSE;
. 'not exist in the database.'); try {
$this->ci->providers_model->get_row($random_record_id);
} catch (Exception $exc) {
$has_thrown_exc = TRUE;
}
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_row() with record id that does '
. 'not exist in the database has thrown an exception.');
} }
private function test_get_row_with_invalid_argument() { private function test_get_row_with_invalid_argument() {
$invalid_id = 'THIS IS NOT AN INTEGER'; $invalid_id = 'THIS IS NOT AN INTEGER';
$has_thrown_exception = FALSE; $has_thrown_exc = FALSE;
try { try {
$this->ci->providers_model->get_row($invalid_id); $this->ci->providers_model->get_row($invalid_id);
} catch (Exception $exc) { } catch (Exception $exc) {
$has_thrown_exception = TRUE; $has_thrown_exc = TRUE;
} }
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_row() with wrong argument.'); $this->ci->unit->run($has_thrown_exc, TRUE, 'Test get_row() with wrong argument.');
} }
// TEST GET VALUE METHOD --------------------------------------------- // TEST GET VALUE METHOD ---------------------------------------------
private function test_get_value() { private function test_get_value() {
// Insert new customer record. // Insert new provider record.
$provider = array( $provider = array(
'last_name' => 'Doe', 'last_name' => 'Doe',
'first_name' => 'John', 'first_name' => 'John',
@ -213,7 +680,7 @@ class Unit_tests_providers_model extends CI_Driver {
} }
private function test_get_value_field_does_not_exist() { private function test_get_value_field_does_not_exist() {
// Insert new customer record. // Insert new provider record.
$provider = array( $provider = array(
'last_name' => 'Doe', 'last_name' => 'Doe',
'first_name' => 'John', 'first_name' => 'John',
@ -246,17 +713,23 @@ class Unit_tests_providers_model extends CI_Driver {
// TEST GET BATCH METHOD --------------------------------------------- // TEST GET BATCH METHOD ---------------------------------------------
private function test_get_batch() { private function test_get_batch() {
// Get all the customer rows without using the model. // Get all the provider rows without using the model.
$db_data = $this->ci->db->get_where('ea_users', $db_data = $this->ci->db->get_where('ea_users',
array('id_roles' => $this->provider_role_id))->result_array(); array('id_roles' => $this->provider_role_id))->result_array();
// Get all the customer rows by using the model. foreach($db_data as &$db_provider) {
$db_provider['settings'] = $this->ci->db->get_where('ea_user_settings',
array('id_users' => $db_provider['id']))->row_array();
unset($db_provider['settings']['id_users']);
}
// Get all the provider rows by using the model.
$model_data = $this->ci->providers_model->get_batch(); $model_data = $this->ci->providers_model->get_batch();
// Check that the two arrays are the same. // Check that the two arrays are the same.
$this->ci->unit->run($db_data, $model_data, 'Test get_batch() method.'); $this->ci->unit->run($db_data, $model_data, 'Test get_batch() method.');
} }
private function test_get_batch_with_where_clause() { private function test_get_batch_with_where_clause() {
// Insert new customer record. // Insert new provider record.
$provider = array( $provider = array(
'last_name' => 'Doe', 'last_name' => 'Doe',
'first_name' => 'John', 'first_name' => 'John',
@ -273,6 +746,11 @@ class Unit_tests_providers_model extends CI_Driver {
// Get data without using the model. // Get data without using the model.
$no_model_data = $this->ci->db->get_where('ea_users', array('id' => $provider['id'])) $no_model_data = $this->ci->db->get_where('ea_users', array('id' => $provider['id']))
->result_array(); ->result_array();
foreach($no_model_data as &$no_model_provider) {
$no_model_provider['settings'] = $this->ci->db->get_where('ea_user_settings',
array('id_users' => $no_model_provider['id']))->row_array();
unset($no_model_provider['settings']['id_users']);
}
// Get data by using the model. // Get data by using the model.
$model_data = $this->ci->providers_model->get_batch(array('id' => $provider['id'])); $model_data = $this->ci->providers_model->get_batch(array('id' => $provider['id']));

View file

@ -1,5 +1,23 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.'); <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.');
/**
* Admins_Model Class
*
* Handles the database actions for admin users management.
*
* Data Structure:
* 'fist_name'
* 'last_name' (required)
* 'email' (required)
* 'mobile_number'
* 'phone_number' (required)
* 'address'
* 'city'
* 'state'
* 'zip_code'
* 'notes'
* 'id_roles'
*/
class Admins_Model extends CI_Model { class Admins_Model extends CI_Model {
/** /**
* Class Constructor * Class Constructor
@ -8,47 +26,264 @@ class Admins_Model extends CI_Model {
parent::__construct(); parent::__construct();
} }
/**
* Add (insert or update) a admin user record into database.
*
* @param array $admin Contains the admin user data.
* @return int Returns the record id.
* @throws Exception When the admin data are invalid (see validate() method).
*/
public function add($admin) { public function add($admin) {
if (!$this->validate($admin)) {
throw new Exception('Admin data are invalid: ' . print_r($admin, TRUE));
}
if ($this->exists($admin) && !isset($admin['id'])) {
$admin['id'] = $this->find_record_id($admin);
}
if (!isset($admin['id'])) {
$admin['id'] = $this->insert($admin);
} else {
$admin['id'] = $this->update($admin);
}
return intval($admin);
} }
/**
* Check whether a particular admin record exists in the database.
*
* @param array $admin Contains the admin data. The 'email' value is required to be present
* at the moment.
* @return bool Returns whether the record exists or not.
* @throws Exception When the 'email' value is not present on the $admin argument.
*/
public function exists($admin) { public function exists($admin) {
if (!isset($admin['email'])) {
throw new Exception('Admin email is not provided: ' . print_r($admin, TRUE));
}
// This method shouldn't depend on another method of this class.
$num_rows = $this->db
->select('*')
->from('ea_users')
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
->where('ea_users.email', $provider['email'])
->where('ea_roles.slug', DB_SLUG_ADMIN)
->get()->num_rows();
return ($num_rows > 0) ? TRUE : FALSE;
} }
/**
* Insert a new admin record into the database.
*
* @param array $admin Contains the admin data.
* @return int Returns the new record id.
* @throws Exception When the insert operation fails.
*/
public function insert($admin) { public function insert($admin) {
$admin['id_roles'] = $this->get_admin_role_id();
if (!$this->db->insert('ea_users', $admin)) {
throw new Exception('Could not insert admin into the database.');
}
return intval($this->db->insert_id());
} }
/**
* Update an existing admin record in the database.
*
* @param array $admin Contains the admin record data.
* @return int Retuns the record id.
* @throws Exception When the update operation fails.
*/
public function update($admin) { public function update($admin) {
$this->db->where('id', $admin['id']);
if (!$this->db->update('ea_users', $admin)){
throw new Exception('Could not update admin record.');
}
return intval($admin['id']);
} }
/**
* Find the database record id of a provider.
*
* @param array $admin Contains the admin data. The 'email value is required in order to
* find the record id.
* @return int Returns the record id
* @throws Exception When the 'email' value is not present on the $admin array.
*/
public function find_record_id($admin) { public function find_record_id($admin) {
if (!isset($admin['email'])) {
throw new Exception('Admin email was not provided: ' . print_r($admin, TRUE));
}
$result = $this->db
->select('ea_users.id')
->from('ea_users')
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
->where('ea_users.email', $admin['email'])
->where('ea_roles.slug', DB_SLUG_ADMIN)
->get();
if ($result->num_rows() == 0) {
throw new Exception('Could not find admin record id.');
}
return intval($result->row()->id);
} }
/**
* Validate admin user data before add() operation is executed.
*
* @param array $admin Contains the admin user data.
* @return bool Returns the validation result.
*/
public function validate($admin) { public function validate($admin) {
$this->load->helper('data_validation');
try {
// If a record id is provided then check whether the record exists in the database.
if (isset($admin['id'])) {
$num_rows = $this->db->get_where('ea_users', array('id' => $admin['id']))
->num_rows();
if ($num_rows == 0) {
throw new Exception('Given admin id does not exist in database: ' . $admin['id']);
}
}
// Validate required fields integrity.
if (!isset($admin['last_name'])
|| !isset($admin['email'])
|| !isset($admin['phone_number'])) {
throw new Exception('Not all required fields are provided : ' . print_r($admin, TRUE));
}
// Validate admin email address.
if (!filter_var($admin['email'], FILTER_VALIDATE_EMAIL)) {
throw new Exception('Invalid email address provided : ' . $admin['email']);
}
return TRUE;
} catch (Exception $exc) {
return FALSE;
}
} }
public function delete($admin) { /**
* Delete an existing admin record from the database.
*
* @param numeric $admin_id The admin record id to be deleted.
* @return bool Returns the delete operation result.
* @throws Exception When the $admin_id is not a valid numeric value.
* @throws Exception When the record to be deleted is the only one admin user left on
* the system.
*/
public function delete($admin_id) {
if (!is_numeric($admin_id)) {
throw new Exception('Invalid argument type $admin_id : ' . $admin_id);
}
// There must be always at least one admin user. If this is the only admin
// the system, it cannot be deleted.
$admin_count = $this->db->get_where('ea_users',
array('id_roles' => $this->get_admin_roles_id()))->num_rows();
if ($admin_count == 1) {
throw new Exception('Record could not be deleted. The system requires at least '
. 'one admin user.');
}
$num_rows = $this->db->get_where('ea_users', array('id' => $admin_id))->num_rows();
if ($num_rows == 0) {
return FALSE; // Record does not exist in database.
}
return $this->db->delete('ea_users', array('id' => $admin_id));
} }
/**
* Get a specific admin record from the database.
*
* @param numeric $admin_id The id of the record to be returned.
* @return array Returns an array with the admin user data.
* @throws Exception When the $admin_id is not a valid numeric value.
*/
public function get_row($admin_id) { public function get_row($admin_id) {
if (!is_numeric($admin_id)) {
throw new Exception('$admin_id argument is not a valid numeric value: ' . $admin_id);
}
$admin = $this->db->get_where('ea_users', array('id' => $admin_id))->row_array();
return $admin;
} }
/**
* Get a specific field value from the database.
*
* @param string $field_name The field name of the value to be returned.
* @param numeric $admin_id Record id of the value to be returned.
* @return string Returns the selected record value from the database.
* @throws Exception When the $field_name argument is not a valid string.
* @throws Exception When the $admin_id is not a valid numeric.
* @throws Exception When the admin record does not exist in the database.
* @throws Exception When the selected field value is not present on database.
*/
public function get_value($field_name, $admin_id) { public function get_value($field_name, $admin_id) {
if (!is_string($field_name)) {
throw new Exception('$field_name argument is not a string : ' . $field_name);
}
if (!is_numeric($admin_id)) {
throw new Exception('$admin_id argument is not a valid numeric value: ' . $admin_id);
}
// Check whether the admin record exists.
$result = $this->db->get_where('ea_users', array('id' => $admin_id));
if ($result->num_rows() == 0) {
throw new Exception('The record with the given id does not exist in the '
. 'database : ' . $admin_id);
}
// Check if the required field name exist in database.
$provider = $result->row_array();
if (!isset($provider[$field_name])) {
throw new Exception('The given $field_name argument does not exist in the '
. 'database: ' . $field_name);
}
return $provider[$field_name];
} }
/**
* Get all, or specific admin records from database.
*
* @param string|array $where_clause (OPTIONAL) The WHERE clause of the query to be executed.
* Use this to get specific admin records.
* @return array Returns an array with admin records.
*/
public function get_batch($where_clause = '') { public function get_batch($where_clause = '') {
if ($where_clause != '') {
$this->db->where($where_clause);
}
$this->db->where('id_roles', $this->get_admin_role_id());
$batch = $this->get('ea_users')->result_array();
return $batch;
} }
/**
* Get the admin users role id.
*
* @return int Returns the role record id.
*/
public function get_admin_role_id() {
return intval($this->db->get_where('ea_roles', array('slug' => DB_SLUG_ADMIN))->row()->id);
}
} }
/* End of file admins_model.php */ /* End of file admins_model.php */

View file

@ -1,5 +1,34 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.'); <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.');
/**
* Providers_Model Class
*
* Contains the database operations for the service provider users of
* Easy!Appointmenst.
*
* Data Structure:
* 'fist_name'
* 'last_name' (required)
* 'email' (required)
* 'mobile_number'
* 'phone_number' (required)
* 'address'
* 'city'
* 'state'
* 'zip_code'
* 'notes'
* 'id_roles'
* 'services' >> array that contains the ids that the provider can provide
* 'settings'
* 'username'
* 'password'
* 'notifications'
* 'working_plan'
* 'google_sync'
* 'google_token'
* 'sync_past_days'
* 'sync_future_days'
*/
class Providers_Model extends CI_Model { class Providers_Model extends CI_Model {
/** /**
* Class Constructor * Class Constructor
@ -9,7 +38,7 @@ class Providers_Model extends CI_Model {
} }
/** /**
* Add (insert/update) a service provider record. * Add (insert - update) a service provider record.
* *
* If the record already exists (id value provided) then it is going to be updated, * If the record already exists (id value provided) then it is going to be updated,
* otherwise inserted into the database. * otherwise inserted into the database.
@ -30,10 +59,10 @@ class Providers_Model extends CI_Model {
if (!isset($provider['id'])) { if (!isset($provider['id'])) {
$provider['id'] = $this->insert($provider); $provider['id'] = $this->insert($provider);
} else { } else {
$this->update($provider); $provider['id'] = $this->update($provider);
} }
return $provider['id']; return intval($provider['id']);
} }
/** /**
@ -69,14 +98,26 @@ class Providers_Model extends CI_Model {
* @throws Exception When the insert operation fails. * @throws Exception When the insert operation fails.
*/ */
public function insert($provider) { public function insert($provider) {
// Get provider's role id. // Get provider role id.
$provider['id_roles'] = $this->get_providers_role_id(); $provider['id_roles'] = $this->get_providers_role_id();
// Store provider settings and services (must not be present on the $provider array).
$services = $provider['services'];
unset($provider['services']);
$settings = $provider['settings'];
unset($provider['settings']);
// Insert provider record and save settings.
if (!$this->db->insert('ea_users', $provider)) { if (!$this->db->insert('ea_users', $provider)) {
throw new Exception('Could not insert provider into the database'); throw new Exception('Could not insert provider into the database');
} }
return intval($this->db->insert_id()); $provider['id'] = $this->db->insert_id();
$this->save_settings($settings, $provider['id']);
$this->save_services($services, $provider['id']);
// Return the new record id.
return intval($provider['id']);
} }
/** /**
@ -87,12 +128,22 @@ class Providers_Model extends CI_Model {
* @throws Exception When the update operation fails. * @throws Exception When the update operation fails.
*/ */
public function update($provider) { public function update($provider) {
$this->db->where('id', $provider['id']); // Store service and settings (must not be present on the $provider array).
$services = $provider['services'];
unset($provider['services']);
$settings = $provider['settings'];
unset($provider['settings']);
// Update provider record.
$this->db->where('id', $provider['id']);
if (!$this->db->update('ea_users', $provider)) { if (!$this->db->update('ea_users', $provider)) {
throw new Exception('Could not update provider record.'); throw new Exception('Could not update provider record.');
} }
$this->save_services($services, $provider['id']);
$this->save_settings($settings, $provider['id']);
// Return record id.
return intval($provider['id']); return intval($provider['id']);
} }
@ -109,7 +160,6 @@ class Providers_Model extends CI_Model {
throw new Exception('Provider email was not provided :' . print_r($provider, TRUE)); throw new Exception('Provider email was not provided :' . print_r($provider, TRUE));
} }
// Get customer's role id
$result = $this->db $result = $this->db
->select('ea_users.id') ->select('ea_users.id')
->from('ea_users') ->from('ea_users')
@ -122,7 +172,7 @@ class Providers_Model extends CI_Model {
throw new Exception('Could not find provider record id.'); throw new Exception('Could not find provider record id.');
} }
return $result->row()->id; return intval($result->row()->id);
} }
/** /**
@ -135,8 +185,7 @@ class Providers_Model extends CI_Model {
$this->load->helper('data_validation'); $this->load->helper('data_validation');
try { try {
// If a customer id is provided, check whether the record // If a provider id is present, check whether the record exist in the database.
// exist in the database.
if (isset($provider['id'])) { if (isset($provider['id'])) {
$num_rows = $this->db->get_where('ea_users', $num_rows = $this->db->get_where('ea_users',
array('id' => $provider['id']))->num_rows(); array('id' => $provider['id']))->num_rows();
@ -144,18 +193,37 @@ class Providers_Model extends CI_Model {
throw new Exception('Provided record id does not exist in the database.'); throw new Exception('Provided record id does not exist in the database.');
} }
} }
// Validate required fields
// Validate required fields.
if (!isset($provider['last_name']) if (!isset($provider['last_name'])
|| !isset($provider['email']) || !isset($provider['email'])
|| !isset($provider['phone_number'])) { || !isset($provider['phone_number'])) {
throw new Exception('Not all required fields are provided : ' . print_r($provider, TRUE)); throw new Exception('Not all required fields are provided : ' . print_r($provider, TRUE));
} }
// Validate email address // Validate provider email address.
if (!filter_var($provider['email'], FILTER_VALIDATE_EMAIL)) { if (!filter_var($provider['email'], FILTER_VALIDATE_EMAIL)) {
throw new Exception('Invalid email address provided : ' . $provider['email']); throw new Exception('Invalid email address provided : ' . $provider['email']);
} }
// Validate provider services.
if (!isset($provider['services']) || !is_array($provider['services'])) {
throw new Exception('Invalid provider services given: ' . print_r($provider, TRUE));
} else { // Check if services are valid numeric values.
foreach($provider['services'] as $service_id) {
if (!is_numeric($service_id)) {
throw new Exception('A provider service with invalid id was found: '
. print_r($provider, TRUE));
}
}
}
// Validate provider settings.
if (!isset($provider['settings']) || count($provider['settings']) == 0
|| !is_array($provider['settings'])) {
throw new Exception('Invalid provider settings given: ' . print_r($provider, TRUE));
}
return TRUE; return TRUE;
} catch (Exception $exc) { } catch (Exception $exc) {
return FALSE; return FALSE;
@ -171,86 +239,128 @@ class Providers_Model extends CI_Model {
*/ */
public function delete($provider_id) { public function delete($provider_id) {
if (!is_numeric($provider_id)) { if (!is_numeric($provider_id)) {
throw new Exception('Invalid argument type $customer_id : ' . $provider_id); throw new Exception('Invalid argument type $provider_id : ' . $provider_id);
} }
$num_rows = $this->db->get_where('ea_users', array('id' => $provider_id))->num_rows(); $num_rows = $this->db->get_where('ea_users', array('id' => $provider_id))->num_rows();
if ($num_rows == 0) { if ($num_rows == 0) {
return FALSE; // record does not exist return FALSE; // Record does not exist in database.
} }
return $this->db->delete('ea_users', array('id' => $provider_id)); return $this->db->delete('ea_users', array('id' => $provider_id));
} }
/** /**
* Get a specific row from the providers table. * Get a specific provider record from the database.
* *
* @param int $provider_id The id of the record to be returned. * @param int $provider_id The id of the record to be returned.
* @return array Returns an associative array with the selected * @return array Returns an associative array with the selected record's data. Each key
* record's data. Each key has the same name as the database * has the same name as the database field names.
* field names. * @throws Exception When the selected record does not exist in database.
*/ */
public function get_row($provider_id) { public function get_row($provider_id) {
if (!is_numeric($provider_id)) { if (!is_numeric($provider_id)) {
throw new Exception('$provider_id argument is not an integer : ' . $provider_id); throw new Exception('$provider_id argument is not a valid numeric value: ' . $provider_id);
} }
return $this->db->get_where('ea_users', array('id' => $provider_id))->row_array();
// Check if selected record exists on database.
if ($this->db->get_where('ea_users', array('id' => $provider_id))->num_rows() == 0) {
throw new Exception('Selected record does not exist in the database.');
}
// Get provider data.
$provider = $this->db->get_where('ea_users', array('id' => $provider_id))->row_array();
// Include provider services.
$services = $this->db->get_where('ea_services_providers',
array('id_users' => $provider_id))->result_array();
$provider['services'] = array();
foreach($services as $service) {
$provider['services'][] = $service['id_services'];
}
// Include provider settings.
$provider['settings'] = $this->db->get_where('ea_user_settings',
array('id_users' => $provider_id))->row_array();
unset($provider['settings']['id_users']);
// Return provider data array.
return $provider;
} }
/** /**
* Get a specific field value from the database. * Get a specific field value from the database.
* *
* @param string $field_name The field name of the value to be * @param string $field_name The field name of the value to be returned.
* returned. * @param numeric $provider_id Record id of the value to be returned.
* @param int $provider_id The selected record's id. * @return string Returns the selected record value from the database.
* @return string Returns the records value from the database. * @throws Exception When the $field_name argument is not a valid string.
* @throws Exception When the $admin_id is not a valid numeric.
* @throws Exception When the admin record does not exist in the database.
* @throws Exception When the selected field value is not present on database.
*/ */
public function get_value($field_name, $provider_id) { public function get_value($field_name, $provider_id) {
if (!is_numeric($provider_id)) { if (!is_numeric($provider_id)) {
throw new Exception('Invalid argument provided as $customer_id : ' throw new Exception('Invalid argument provided as $provider_id : ' . $provider_id);
. $provider_id);
} }
if (!is_string($field_name)) { if (!is_string($field_name)) {
throw new Exception('$field_name argument is not a string : ' throw new Exception('$field_name argument is not a string : ' . $field_name);
. $field_name);
} }
if ($this->db->get_where('ea_users', array('id' => $provider_id))->num_rows() == 0) { // Check whether the admin record exists in database.
$result = $this->db->get_where('ea_users', array('id' => $provider_id));
if ($result->num_rows() == 0) {
throw new Exception('The record with the $provider_id argument does not exist in ' throw new Exception('The record with the $provider_id argument does not exist in '
. 'the database : ' . $provider_id); . 'the database : ' . $provider_id);
} }
$row_data = $this->db->get_where('ea_users', array('id' => $provider_id))->row_array(); $provider = $result->row_array();
if (!isset($row_data[$field_name])) { if (!isset($provider[$field_name])) {
throw new Exception('The given $field_name argument does not exist in the ' throw new Exception('The given $field_name argument does not exist in the '
. 'database : ' . $field_name); . 'database : ' . $field_name);
} }
$provider = $this->db->get_where('ea_users', array('id' => $provider_id))->row_array();
return $provider[$field_name]; return $provider[$field_name];
} }
/** /**
* Get all, or specific records from provider's table. * Get all, or specific records from provider's table.
* *
* @example $this->Model->getBatch('id = ' . $recordId); * @example $this->Model->get_batch('id = ' . $recordId);
*
* @param string $where_clause (OPTIONAL) The WHERE clause of the query to be executed.
*
* NOTICE: DO NOT INCLUDE 'WHERE' KEYWORD.
* *
* @param string $whereClause (OPTIONAL) The WHERE clause of
* the query to be executed. DO NOT INCLUDE 'WHERE' KEYWORD.
* @return array Returns the rows from the database. * @return array Returns the rows from the database.
*/ */
public function get_batch($where_clause = '') { public function get_batch($where_clause = '') {
$providers_role_id = $this->get_providers_role_id();
if ($where_clause != '') { if ($where_clause != '') {
$this->db->where($where_clause); $this->db->where($where_clause);
} }
$this->db->where('id_roles', $providers_role_id); $batch = $this->db->get_where('ea_users',
array('id_roles' => $this->get_providers_role_id()))->result_array();
// Include each provider sevices and settings.
foreach($batch as &$provider) {
// Services
$services = $this->db->get_where('ea_services_providers',
array('id_users' => $provider['id']))->result_array();
$provider['services'] = array();
foreach($services as $service) {
$provider['services'][] = $service['id_services'];
}
// Settings
$provider['settings'] = $this->db->get_where('ea_user_settings',
array('id_users' => $provider['id']))->row_array();
unset($provider['settings']['id_users']);
}
return $this->db->get('ea_users')->result_array(); // Return provider records in an array.
return $batch;
} }
/** /**
@ -260,53 +370,43 @@ class Providers_Model extends CI_Model {
* provide. * provide.
* *
* @return array Returns an array with the providers data. * @return array Returns an array with the providers data.
*
* @deprecated since version 0.5 - Use get_batch() instead.
*/ */
public function get_available_providers() { public function get_available_providers() {
// Get provider records from database.
$this->db $this->db
->select('ea_users.*') ->select('ea_users.*')
->from('ea_users') ->from('ea_users')
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner') ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
->where('ea_roles.slug', 'provider'); ->where('ea_roles.slug', DB_SLUG_PROVIDER);
$providers = $this->db->get()->result_array(); $providers = $this->db->get()->result_array();
// :: GET PROVIDER SERVICES // Include each provider services and settings.
// Return also an array with the services that each provider can provide
// to the customers.
foreach($providers as &$provider) { foreach($providers as &$provider) {
$this->db // Services
->select('id_services') $services = $this->db->get_where('ea_services_providers',
->from('ea_services_providers') array('id_users' => $provider['id']))->result_array();
->where('id_users', $provider['id']); $provider['services'] = array();
foreach($services as $service) {
$provider_services = $this->db->get()->result_array();
if (!isset($provider['services'])) {
$provider['services'] = array();
}
foreach($provider_services as $service) {
$provider['services'][] = $service['id_services']; $provider['services'][] = $service['id_services'];
} }
// Settings
$provider['settings'] = $this->db->get_where('ea_user_settings',
array('id_users' => $provider['id']))->row_array();
unset($provider['settings']['id_users']);
} }
// :: GET PROVIDER SETTINGS // Return provider records.
foreach($providers as &$provider) {
$this->db
->select('*')
->from('ea_user_settings')
->where('id_users', $provider['id']);
$provider['settings'] = $this->db->get()->row_array();
unset($provider['settings']['id_users']); // Do not need it.
}
return $providers; return $providers;
} }
/** /**
* Get the providers role id from the database. * Get the providers role id from the database.
* *
* @return int Returns the role id for the customer records. * @return int Returns the role id for the provider records.
*/ */
public function get_providers_role_id() { public function get_providers_role_id() {
return $this->db->get_where('ea_roles', array('slug' => DB_SLUG_PROVIDER))->row()->id; return $this->db->get_where('ea_roles', array('slug' => DB_SLUG_PROVIDER))->row()->id;
@ -329,6 +429,8 @@ class Providers_Model extends CI_Model {
/** /**
* Set a provider's setting value in the database. * 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 $setting_name The setting's name.
* @param string $value The setting's value. * @param string $value The setting's value.
* @param numeric $provider_id The selected provider id. * @param numeric $provider_id The selected provider id.
@ -337,6 +439,61 @@ class Providers_Model extends CI_Model {
$this->db->where(array('id_users' => $provider_id)); $this->db->where(array('id_users' => $provider_id));
return $this->db->update('ea_user_settings', array($setting_name => $value)); return $this->db->update('ea_user_settings', array($setting_name => $value));
} }
/**
* Save the provider settings (used from insert or update operation).
*
* @param array $settings Contains the setting values.
* @param numeric $provider_id Record id of the provider.
*/
private function save_settings($settings, $provider_id) {
if (!is_numeric($provider_id)) {
throw new Exception('Invalid $provider_id argument given :' . $provider_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' => $provider_id))
->num_rows() == 0) {
$this->db->insert('ea_user_settings', array('id_users' => $provider_id));
}
foreach($settings as $name=>$value) {
$this->set_setting($name, $value, $provider_id);
}
}
/**
* Save the provider services in the database (use on both insert and update operation).
*
* @param array $services Contains the service ids that the selected provider can provide.
* @param numeric $provider_id The selected provider record id.
* @throws Exception When the $services argument type is not array.
* @throws Exception When the $provider_id argumetn type is not numeric.
*/
private function save_services($services, $provider_id) {
// Validate method arguments.
if (!is_array($services)) {
throw new Exception('Invalid argument type $services: ' . $services);
}
if (!is_numeric($provider_id)) {
throw new Exception('Invalid argument type $provider_id: ' . $provider_id);
}
// Save provider services in the database (delete old records and add new).
$this->db->delete('ea_services_records', array('id_users' => $provider_id));
foreach($services as $service_id) {
$service_provider = array(
'id_users' => $provider_id,
'id_services' => $service_id
);
$this->db->insert('ea_services_providers', $service_provider);
}
}
} }
/* End of file providers_model.php */ /* End of file providers_model.php */