From f0b8ab45b5a5674bac6b75351710730f302eded5 Mon Sep 17 00:00:00 2001 From: "alextselegidis@gmail.com" Date: Tue, 16 Jul 2013 14:21:33 +0000 Subject: [PATCH] * Added more functions to services_model.php * Added Unit Testing methods for the services model. --- src/application/controllers/backend_api.php | 24 + .../drivers/Unit_tests_services_model.php | 537 ++++++++++++++++-- src/application/models/services_model.php | 272 ++++++++- src/application/views/backend/services.php | 7 - 4 files changed, 797 insertions(+), 43 deletions(-) diff --git a/src/application/controllers/backend_api.php b/src/application/controllers/backend_api.php index 08c02f42..135b536f 100644 --- a/src/application/controllers/backend_api.php +++ b/src/application/controllers/backend_api.php @@ -485,6 +485,30 @@ class Backend_api extends CI_Controller { )); } } + + public function ajax_save_service() { + + } + + public function ajax_delete_service() { + + } + + public function ajax_filter_services() { + + } + + public function ajax_save_service_category() { + + } + + public function ajax_delete_service_category() { + + } + + public function ajax_filter_service_categories() { + + } } /* End of file backend_api.php */ diff --git a/src/application/libraries/Unit_tests/drivers/Unit_tests_services_model.php b/src/application/libraries/Unit_tests/drivers/Unit_tests_services_model.php index 4d910602..eb323a75 100644 --- a/src/application/libraries/Unit_tests/drivers/Unit_tests_services_model.php +++ b/src/application/libraries/Unit_tests/drivers/Unit_tests_services_model.php @@ -1,15 +1,15 @@ CI =& get_instance(); - $this->CI->load->library('Unit_test'); - $this->CI->load->model('services_model'); + $this->ci =& get_instance(); + $this->ci->load->library('Unit_test'); + $this->ci->load->model('services_model'); } /** @@ -31,14 +31,297 @@ class Unit_tests_services_model extends CI_Driver { // UNIT TESTS ///////////////////////////////////////////////////////////////////////// + // TEST ADD METHOD ----------------------------------------------------- + private function test_add_insert() { + $service = array( + 'name' => 'Test Service', + 'duration' => 30, + 'price' => '20.00', + 'currency' => 'Euro', + 'description' => 'Some description here ...', + 'id_service_categories' => NULL + ); + + $service['id'] = $this->ci->services_model->add($service); + $this->ci->unit->run($service['id'], 'is_int', 'Test if add() - insert operation has ' + . 'returned a valid record id.'); + + $db_record = $this->ci->db->get_where('ea_services', array('id' => $service['id']))->row_array(); + $this->ci->unit->run($service, $db_record, 'Test if add() - insert operation, has ' + . 'successfully inserted a new record.'); + + $this->ci->db->delete('ea_services', array('id' => $service['id'])); + } + + private function test_add_update() { + $service = array( + 'name' => 'Test Service', + 'duration' => 30, + 'price' => '20.00', + 'currency' => 'Euro', + 'description' => 'Some description here ...', + 'id_service_categories' => NULL + ); + + $this->ci->db->insert('ea_services', $service); + $service['id'] = $this->ci->db->insert_id(); + + // Update record data + $service['name'] = 'Updated Name'; + $service['duration'] = 60; + $service['price'] = '60.45'; + $service['description'] = 'Updated description ...'; + + $update_result = $this->ci->services_model->add($service); + $this->ci->unit->run($update_result, 'is_int', 'Test if add() - update operation, has ' + . 'returned the record id.'); + + $db_record = $this->ci->db->get_where('ea_services', array('id' => $service['id']))->row_array(); + $this->ci->unit->run($service, $db_record, 'Test if add() - update operation, has ' + . 'successfully updated a record.'); + + $this->ci->db->delete('ea_services', array('id' => $service['id'])); + } + + private function test_add_invalid_data() { + $service = array( + 'name' => '', // invalid + 'duration' => 'invalid', + 'price' => 'invalid', + 'currency' => 'Euro', + 'description' => 'Some description here ...', + 'id_service_categories' => NULL + ); + + $has_thrown_exc = FALSE; + try { + $this->ci->services_model->add($service); + } catch(Exception $exc) { + $has_thrown_exc = TRUE; + } + + $this->ci->unit->run($has_thrown_exc, TRUE, 'Check if add() with invalid data has ' + . 'thrown exception.'); + } + + // TEST EXISTS METHOD -------------------------------------------------- + private function test_exists_record_exists() { + // insert record + $service = array( + 'name' => 'Test Service', + 'duration' => 30, + 'price' => '20.00', + 'currency' => 'Euro', + 'description' => 'Some description here ...', + 'id_service_categories' => NULL + ); + + $this->ci->db->insert('ea_services', $service); + $service['id'] = $this->ci->db->insert_id(); + + // check exists + $exists = $this->ci->services_model->exists($service); + $this->ci->unit->run($exists, TRUE, 'Check if exists() returned true on existing record.'); + + // delete record + $this->ci->db->delete('ea_services', array('id' => $service['id'])); + } + + private function test_exists_record_does_not_exist() { + $service = array( + 'name' => 'Random Name', + 'duration' => 'Random Duration', + 'price' => 'Random Price' + ); + + $exists = $this->ci->services_model->exists($service); + $this->ci->unit->run($exists, FALSE, 'Check if exists() returned false on non-existing record.'); + } + + private function test_exists_invalid_arguments() { + $invalid_arg = ''; + + $has_thrown_exc = FALSE; + try { + $exists = $this->ci->services_model->exists($invalid_arg); + } catch(Exception $exc) { + $has_thrown_exc = TRUE; + } + $this->ci->unit->run($has_thrown_exc, TRUE, 'Check if exists() with invalid argument has ' + . 'thrown an exception.'); + } + + // TEST VALIDATE METHOD ------------------------------------------------- + private function test_validate() { + $service = array( + 'name' => 'Test Service', + 'duration' => 30, + 'price' => '20.00', + 'currency' => 'Euro', + 'description' => 'Some description here ...', + 'id_service_categories' => NULL + ); + + $is_valid = $this->ci->services_model->validate($service); + $this->ci->unit->run($is_valid, TRUE, 'Test validate() method with valid data.'); + } + + private function test_validate_invalid_record_id() { + $service = array( + 'id' => 'THIS IS INVALID', + 'name' => 'Test Service', + 'duration' => 30, + 'price' => '20.00', + 'currency' => 'Euro', + 'description' => 'Some description here ...', + 'id_service_categories' => NULL + ); + + $is_valid = $this->ci->services_model->validate($service); + $this->ci->unit->run($is_valid, FALSE, 'Test validate() method with invalid record id.'); + } + + private function test_validate_invalid_service_category_id() { + $service = array( + 'name' => 'Test Service', + 'duration' => 30, + 'price' => '20.00', + 'currency' => 'Euro', + 'description' => 'Some description here ...', + 'id_service_categories' => 'THIS IS INVALID' + ); + + $is_valid = $this->ci->services_model->validate($service); + $this->ci->unit->run($is_valid, FALSE, 'Test validate() method with invalid service ' + . 'category id.'); + } + + private function test_validate_invalid_service_name() { + $service = array( + 'name' => '', // invalid + 'duration' => 30, + 'price' => '20.00', + 'currency' => 'Euro', + 'description' => 'Some description here ...', + 'id_service_categories' => 'THIS IS INVALID' + ); + + $is_valid = $this->ci->services_model->validate($service); + $this->ci->unit->run($is_valid, FALSE, 'Test validate() method with invalid service name.'); + } + + // TEST FIND RECORD ID -------------------------------------------------- + private function test_find_record_id() { + // insert record + $service = array( + 'name' => 'Test Service', + 'duration' => 30, + 'price' => '20.00', + 'currency' => 'Euro', + 'description' => 'Some description here ...', + 'id_service_categories' => NULL + ); + + $this->ci->db->insert('ea_services', $service); + $service['id'] = intval($this->ci->db->insert_id()); + + // find record id + $service_id = $this->ci->services_model->find_record_id($service); + $this->ci->unit->run($service_id, $service['id'], 'Test find_record_id() with record ' + . 'that exist.'); + + // delete record + $this->ci->db->delete('ea_services', array('id' => $service['id'])); + } + + private function test_find_record_id_invalid_service_data() { + $service = array( + // name, duration and price are not provided + 'currency' => 'Euro', + 'description' => 'Some description here ...', + 'id_service_categories' => NULL + ); + + $has_thrown_exc = FALSE; + try { + $this->ci->services_model->find_record_id($service); + } catch(Exception $exc) { + $has_thrown_exc = TRUE; + } + $this->ci->unit->run($has_thrown_exc, TRUE, 'Test find_record_id() with invalid service data.'); + } + + private function test_find_record_id_record_does_not_exist() { + $service = array( + 'name' => 'Does not exist', + 'duration' => 'Does not exist', + 'price' => 'Does not exist' + ); + + $has_thrown_exc = FALSE; + try { + $this->ci->services_model->find_record_id($service); + } catch(Exception $exc) { + $has_thrown_exc = TRUE; + } + $this->ci->unit->run($has_thrown_exc, TRUE, 'Test find_record_id() with record that ' + . 'does not exist.'); + } + + // TEST DELETE METHOD --------------------------------------------------- + private function test_delete() { + // insert record + $service = array( + 'name' => 'Test Service', + 'duration' => 30, + 'price' => '20.00', + 'currency' => 'Euro', + 'description' => 'Some description here ...', + 'id_service_categories' => NULL + ); + + $this->ci->db->insert('ea_services', $service); + $service['id'] = intval($this->ci->db->insert_id()); + + // delete record + $delete_result = $this->ci->services_model->delete($service['id']); + $this->ci->unit->run($delete_result, TRUE, 'Test delete() method result.'); + + // check if record was actually deleted + $num_rows = $this->ci->db->get_where('ea_services', array('name' => $service['name'])) + ->num_rows(); + $this->ci->unit->run($num_rows, 0, 'Test if delete() method has actually deleted the record.'); + } + + private function test_delete_invalid_argument() { + $invalid_arg = 'THIS IS INVALID'; + $has_thrown_exc = FALSE; + try { + $this->ci->services_model->delete($invalid_arg); + } catch(Exception $exc) { + $has_thrown_exc = TRUE; + } + $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() method has thrown ' + . 'exception on invalid argument.'); + } + + private function test_delete_record_does_not_exist() { + $random_id = 1029800987; + $has_thrown_exc = FALSE; + $delete_result = $this->ci->services_model->delete($random_id); + $this->ci->unit->run($delete_result, FALSE, 'Test if delete() method result on record ' + . 'that does not exist.'); + } + + // TEST GET BATCH METHOD ----------------------------------------- private function test_get_batch() { // Get all the service rows without using the model. - $db_data = $this->CI->db->get('ea_services')->result_array(); + $db_data = $this->ci->db->get('ea_services')->result_array(); // Get all the service rows by using the model. - $model_data = $this->CI->services_model->get_batch(); + $model_data = $this->ci->services_model->get_batch(); // 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() { @@ -51,21 +334,21 @@ class Unit_tests_services_model extends CI_Driver { 'description' => 'This is some service description.' ); - $this->CI->db->insert('ea_services', $service); - $service['id'] = intval($this->CI->db->insert_id()); + $this->ci->db->insert('ea_services', $service); + $service['id'] = intval($this->ci->db->insert_id()); // Get data without using the model. - $no_model_data = $this->CI->db->get_where('ea_services', array('id' => $service['id'])) + $no_model_data = $this->ci->db->get_where('ea_services', array('id' => $service['id'])) ->result_array(); // Get data by using the model. - $model_data = $this->CI->services_model->get_batch(array('id' => $service['id'])); + $model_data = $this->ci->services_model->get_batch(array('id' => $service['id'])); // Check that the data arrays are the same. - $this->CI->unit->run($no_model_data, $model_data, 'Test get_batch() with where clause.'); + $this->ci->unit->run($no_model_data, $model_data, 'Test get_batch() with where clause.'); // Delete inserted record from database. - $this->CI->db->delete('ea_services', array('id' => $service['id'])); + $this->ci->db->delete('ea_services', array('id' => $service['id'])); } private function unabled_test_get_batch_with_invalid_where_clause() { @@ -83,25 +366,25 @@ class Unit_tests_services_model extends CI_Driver { 'description' => 'This is some service description.' ); - $this->CI->db->insert('ea_services', $service); - $service['id'] = intval($this->CI->db->insert_id()); + $this->ci->db->insert('ea_services', $service); + $service['id'] = intval($this->ci->db->insert_id()); // Get the new service record from db. - $no_model_data = $this->CI->db->get_where('ea_services', array('id' => $service['id'])) + $no_model_data = $this->ci->db->get_where('ea_services', array('id' => $service['id'])) ->row_array(); - $model_data = $this->CI->services_model->get_row($service['id']); + $model_data = $this->ci->services_model->get_row($service['id']); // Check that the row is the correct one. - $this->CI->unit->run($no_model_data, $model_data, 'Test get_row() method'); + $this->ci->unit->run($no_model_data, $model_data, 'Test get_row() method'); // Delete inserted service record. - $this->CI->db->delete('ea_services', array('id' => $service['id'])); + $this->ci->db->delete('ea_services', array('id' => $service['id'])); } private function test_get_row_that_does_not_exist() { $random_record_id = 486868412; - $row_data = $this->CI->services_model->get_row($random_record_id); - $this->CI->unit->run($row_data, NULL, 'Test get_row() with record id that does ' + $row_data = $this->ci->services_model->get_row($random_record_id); + $this->ci->unit->run($row_data, NULL, 'Test get_row() with record id that does ' . 'not exist in the database.'); } @@ -110,12 +393,12 @@ class Unit_tests_services_model extends CI_Driver { $has_thrown_exception = FALSE; try { - $this->CI->services_model->get_row($invalid_id); + $this->ci->services_model->get_row($invalid_id); } catch (Exception $exc) { $has_thrown_exception = TRUE; } - $this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_row() with wrong argument.'); + $this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_row() with wrong argument.'); } // TEST GET VALUE METHOD ----------------------------------------- @@ -128,17 +411,17 @@ class Unit_tests_services_model extends CI_Driver { 'currency' => 'euro', 'description' => 'This is some service description.' ); - $this->CI->db->insert('ea_services', $service); - $service['id'] = intval($this->CI->db->insert_id()); + $this->ci->db->insert('ea_services', $service); + $service['id'] = intval($this->ci->db->insert_id()); // Get a specific value from the database. - $model_value = $this->CI->services_model->get_value('name', $service['id']); + $model_value = $this->ci->services_model->get_value('name', $service['id']); // Check if the value was correctly fetched from the database. - $this->CI->unit->run($model_value, $service['name'], 'Test get_value() method.'); + $this->ci->unit->run($model_value, $service['name'], 'Test get_value() method.'); // Delete inserted appointment record. - $this->CI->db->delete('ea_services', array('id' => $service['id'])); + $this->ci->db->delete('ea_services', array('id' => $service['id'])); } private function test_get_value_record_does_not_exist() { @@ -147,12 +430,12 @@ class Unit_tests_services_model extends CI_Driver { $has_thrown_exception = FALSE; try { - $this->CI->services_model->get_value('name', $random_record_id); + $this->ci->services_model->get_value('name', $random_record_id); } catch (Exception $exc) { $has_thrown_exception = TRUE; } - $this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that ' + $this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that ' . 'does not exist.'); } @@ -165,24 +448,208 @@ class Unit_tests_services_model extends CI_Driver { 'currency' => 'euro', 'description' => 'This is some service description.' ); - $this->CI->db->insert('ea_services', $service); - $service['id'] = intval($this->CI->db->insert_id()); + $this->ci->db->insert('ea_services', $service); + $service['id'] = intval($this->ci->db->insert_id()); // Try to get record value with wrong field name. $wrong_field_name = 'THIS IS WRONG'; $has_thrown_exception = FALSE; try { - $this->CI->services_model->get_value($wrong_field_name, $service['id']); + $this->ci->services_model->get_value($wrong_field_name, $service['id']); } catch (Exception $exc) { $has_thrown_exception = TRUE; } - $this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that ' + $this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that ' . 'does not exist.'); // Delete inserted service record. - $this->CI->db->delete('ea_services', array('id' => $service['id'])); + $this->ci->db->delete('ea_services', array('id' => $service['id'])); + } + + // TEST ADD SERVICE CATEGORY METHOD --------------------------------------- + private function test_add_category_insert() { + $category = array( + 'name' => 'Test Category', + 'description' => 'Test Description ...' + ); + + $category['id'] = $this->ci->services_model->add_category($category); + $this->ci->unit->run($category['id'], 'is_int', 'Test if add_category() - insert ' + . 'operation has returned a valid record id'); + + $db_record = $this->ci->db->get_where('ea_service_categories', array('id' => $category['id']))->row_array(); + $this->ci->unit->run($category, $db_record, 'Test if add_category() - insert ' + . 'operation has successfully inserted the record.'); + + $this->ci->db->delete('ea_service_categories', array('id' => $category['id'])); + } + + private function test_add_category_update() { + $category = array( + 'name' => 'Test Category', + 'description' => 'Test Description ...' + ); + + $this->ci->db->insert('ea_service_categories', $category); + $category['id'] = intval($this->ci->db->insert_id()); + + // update record + $category['name'] = 'new name'; + $category['description'] = 'new description'; + + $update_result = $this->ci->services_model->add_category($category); + $this->ci->unit->run($update_result, 'is_int', 'Check if add_category() - update ' + . 'operation has returned a valid record id.'); + + $db_record = $this->ci->db->get_where('ea_service_categories', + array('id' => $category['id']))->row_array(); + $this->ci->unit->run($category, $db_record, 'Test if add_category() - update operation ' + . 'has successfully updated a record.'); + + $this->ci->db->delete('ea_service_categories', array('id' => $category['id'])); + } + + private function test_add_category_invalid_data() { + $category = array( + 'name' => '', // invalid + 'descrption' => '' + ); + + $has_thrown_exc = FALSE; + try { + $this->ci->services_model->add_category($category); + } catch(Exception $exc) { + $has_thrown_exc = TRUE; + } + $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if add_category() with invalid data ' + . 'has thrown an exception.'); + } + + // TEST DELETE SERVICE CATEGORY METHOD ------------------------------------- + private function test_delete_category() { + // insert record + $category = array( + 'name' => 'Test Category', + 'description' => 'Test Description ...' + ); + + $this->ci->db->insert('ea_service_categories', $category); + $category['id'] = intval($this->ci->db->insert_id()); + + // delete record + $delete_result = $this->ci->services_model->delete_category($category['id']); + $this->ci->unit->run($delete_result, TRUE, 'Test if delete_category() method has ' + . 'returned a valid result.'); + + // check if record was actually deleted. + $num_rows = $this->ci->db->get_where('ea_service_categories', + array('id' => $category['id']))->num_rows(); + $this->ci->unit->run($num_rows, 0, 'Check if delete_category() has actually deleted ' + . 'the record.'); + + if ($num_rows > 0) { + $this->ci->db->delete('ea_service_categories', array('id' => $category['id'])); + } + } + + private function test_delete_category_invalid_argument() { + $invalid_arg = 'This is invalid'; + $has_thrown_exc = TRUE; + try { + $this->ci->services_model->delete_category($invalid_arg); + } catch(Exception $exc) { + $has_thrown_exc = TRUE; + } + $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete_category() with invalid ' + . 'argument has thrown an exception.'); + } + + private function test_delete_category_record_does_not_exist() { + $random_id = 09182093; + $has_thrown_exc = TRUE; + try { + $this->ci->services_model->delete_category($random_id); + } catch(Exception $exc) { + $has_thrown_exc = TRUE; + } + $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete_category() with random id ' + . 'has thrown an exception.'); + } + + // TEST GET SERVICE CATEGORY METHOD --------------------------------------- + private function test_get_category() { + // insert record + $category = array( + 'name' => 'Test Category', + 'description' => 'Test Description ...' + ); + + $this->ci->db->insert('ea_service_categories', $category); + $category['id'] = intval($this->ci->db->insert_id()); + + // get record + $db_record = $this->ci->services_model->get_category($category['id']); + $this->ci->unit->run($db_record, $category, 'Test if get_category() has successfully ' + . 'returned the record.'); + + $this->ci->db->delete('ea_service_categories', array('id' => $category['id'])); + } + + private function test_get_category_invalid_argument() { + $invalid_arg = 'THIS IS INVALID'; + $has_thrown_exc = FALSE; + try { + $this->ci->services_model->get_category($invalid_arg); + } catch(Exception $exc) { + $has_thrown_exc = TRUE; + } + $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_category() with invalid argument ' + . 'has thrown an exception.'); + } + + private function test_get_category_record_does_not_exist() { + $random_id = 123412343; + $has_thrown_exc = TRUE; + try { + $this->ci->services_model->get_category($random_id); + } catch(Exception $exc) { + $has_thrown_exc = TRUE; + } + $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete_category() with random id ' + . 'has thrown an exception.'); + } + + // TEST GET ALL SERVICE CATEGORIES METHOD ---------------------------------- + private function test_get_all_categories() { + $all_categories = $this->ci->services_model->get_all_categories(); + $db_categories = $this->ci->db->get('ea_service_categories')->result_array(); + $this->ci->unit->run($all_categories, $db_categories, 'Test if get_all_categories() method ' + . 'has successfully returned all the services categories.'); + } + + // TEST VALIDATE SERVICE CATEGORY METHOD -------------------------------- + private function test_validate_category() { + $category = array( + 'name' => 'Test Name', + 'description' => 'Test Description ...' + ); + + $is_valid = $this->ci->services_model->validate_category($category); + $this->ci->unit->run($is_valid, TRUE, 'Test if validate_category() has returned true ' + . 'with valid data.'); + } + + private function test_validate_category_invalid_name() { + $category = array( + 'name' => '', // invalid + 'description' => 'Test Description ...' + ); + + $is_valid = $this->ci->services_model->validate_category($category); + $this->ci->unit->run($is_valid, FALSE, 'Test if validate_category() has returned false ' + . 'with invalid data.'); } } diff --git a/src/application/models/services_model.php b/src/application/models/services_model.php index 5ff035b2..31c6b852 100644 --- a/src/application/models/services_model.php +++ b/src/application/models/services_model.php @@ -7,6 +7,178 @@ class Services_Model extends CI_Model { parent::__construct(); } + /** + * Add (insert or update) a service record on the database + * + * @param array $service Contains the service data. If an 'id' value is provided then + * the record will be updated. + * @return numeric Returns the record id. + */ + public function add($service) { + if (!$this->validate($service)) { + throw new Exception('Service data are invalid.'); + } + + if (!isset($service['id'])) { + $service['id'] = $this->insert($service); + } else { + $this->update($service); + } + + return intval($service['id']); + } + + /** + * Insert service record into database. + * + * @param array $service Contains the service record data. + * @return int Returns the new service record id. + */ + public function insert($service) { + if (!$this->db->insert('ea_services', $service)) { + throw new Exception('Could not insert service record.'); + } + return intval($this->db->insert_id()); + } + + /** + * Update service record. + * + * @param array $service Contains the service data. The record id needs to be included in + * the array. + */ + public function update($service) { + $this->db->where('id', $service['id']); + if (!$this->db->update('ea_services', $service)) { + throw new Exception('Could not update service record'); + } + } + + /** + * Checks whether an service record already exists in the database. + * + * @param array $service Contains the service data. Name, duration and price values + * are mandatory in order to perform the checks. + */ + public function exists($service) { + if (!isset($service['name']) + || !isset($service['duration']) + || !isset($service['price'])) { + throw new Exception('Not all service fields are provided in order to check whether ' + . 'a service record already exists: ' . print_r($service, TRUE)); + } + + $num_rows = $this->db->get_where('ea_services', array( + 'name' => $service['name'], + 'duration' => $service['duration'], + 'price' => $service['price'] + ))->num_rows(); + + return ($num_rows > 0) ? TRUE : FALSE; + } + + /** + * Validate a service record data. + * + * @param array $service Contains the service data. + * @return bool Returns the validation result. + */ + public function validate($service) { + $this->load->helper('data_validation'); + + try { + // If record id is provided we need to check whether the record exists + // in the database. + if (isset($service['id'])) { + $num_rows = $this->db->get_where('ea_services', array('id' => $service['id'])) + ->num_rows(); + if ($num_rows == 0) { + throw new Exception('Provided service id does not exist in the database.'); + } + } + + // Check if service category id is valid (only when present) + if ($service['id_service_categories'] != NULL) { + $num_rows = $this->db->get_where('ea_service_categories', + array('id' => $service['id_service_categories']))->num_rows(); + if ($num_rows == 0) { + throw new Exception('Provided service category id does not exist in database.'); + } + } + + // Check for required fields + if ($service['name'] == '') { + throw new Exception('Not all required service fields where provided: ' + . print_r($service, TRUE)); + } + + // Duration must be numeric + if ($service['duration'] !== NULL) { + if (!is_numeric($service['duration'])) { + throw new Exception('Service duration is not numeric.'); + } + } + + if ($service['price'] !== NULL) { + if (!is_numeric($service['price'])) { + throw new Exception('Service price is not numeric.'); + } + } + + return TRUE; + } catch(Exception $exc) { + return FALSE; + } + } + + /** + * Get the record id of an existing record. + * + * NOTICE! The record must exist, otherwise an exeption will be raised. + * + * @param array $service Contains the service record data. Name, duration and price values + * are mandatory for this method to complete. + */ + public function find_record_id($service) { + if (!isset($service['name']) + || !isset($service['duration']) + || !isset($service['price'])) { + throw new Exception('Not all required fields where provided in order to find the ' + . 'service record id.'); + } + + $result = $this->db->get_where('ea_services', array( + 'name' => $service['name'], + 'duration' => $service['duration'], + 'price' => $service['price'] + )); + + if ($result->num_rows() == 0) { + throw new Exception('Cound not find service record id'); + } + + return $result->row()->id; + } + + /** + * Delete a service record from database. + * + * @param numeric $service_id Record id to be deleted. + * @return bool Returns the delete operation result. + */ + public function delete($service_id) { + if (!is_numeric($service_id)) { + throw new Exception('Invalid argument type $service_id (value:"' . $service_id . '"'); + } + + $num_rows = $this->db->get_where('ea_services', array('id' => $service_id))->num_rows(); + if ($num_rows == 0) { + return FALSE; // Record does not exist + } + + return $this->db->delete('ea_services', array('id' => $service_id)); + } + /** * Get a specific row from the services db table. * @@ -74,7 +246,7 @@ class Services_Model extends CI_Model { * @return array Returns an object array with all the * database services. */ - function get_available_services() { + public function get_available_services() { $this->db->distinct(); return $this->db ->select('ea_services.*') @@ -83,6 +255,104 @@ class Services_Model extends CI_Model { 'ea_services_providers.id_services = ea_services.id', 'inner') ->get()->result_array(); } + + /** + * Add (insert or update) a service category record into database. + * + * @param array $category Containst the service category data. + * @return int Returns the record id.s + */ + public function add_category($category) { + if (!$this->validate_category($category)) { + throw new Exception('Service category data are invalid.'); + } + + if (!isset($category['id'])) { + $this->db->insert('ea_service_categories', $category); + $category['id'] = $this->db->insert_id(); + } else { + $this->db->where('id', $category['id']); + $this->db->update('ea_service_categories', $category); + } + + return intval($category['id']); + } + + /** + * Delete a service category record from the database. + * + * @param numeric $category_id Record id to be deleted. + * @return bool Returns the delete operation result. + */ + public function delete_category($category_id) { + if (!is_numeric($category_id)) { + throw new Exception('Invalid argument given for $category_id: ' . $category_id); + } + + $num_rows = $this->db->get_where('ea_service_categories', array('id' => $category_id)) + ->num_rows(); + if ($num_rows == 0) { + throw new Exception('Service category record not found in database.'); + } + + $this->db->where('id', $category_id); + return $this->db->delete('ea_service_categories'); + } + + /** + * Get a service category record data. + * + * @param numeric $category_id Record id to be retrieved. + * @return array Returns the record data from the database. + */ + public function get_category($category_id) { + if (!is_numeric($category_id)) { + throw new Exception('Invalid argument type given $category_id: ' . $category_id); + } + + $result = $this->db->get_where('ea_service_categories', array('id' => $category_id)); + + if ($result->num_rows() == 0) { + throw new Exception('Service category record does not exist.'); + } + + return $result->row_array(); + } + + /** + * Get all service category records from database. + * + * @return array Returns an array that contains all the service category records. + */ + public function get_all_categories() { + return $this->db->get('ea_service_categories')->result_array(); + } + + /** + * Validate a service category record data. This method must be used before adding + * a service category record into database in order to secure the record integrity. + * + * @param array $category Contains the service category data. + * @return bool Returns the validation result. + */ + public function validate_category($category) { + try { + // Required Fields + if (!isset($category['name'])) { + throw new Exception('Not all required fields where provided '); + } + + if ($category['name'] == '' || $category['name'] == NULL) { + throw new Exception('Required fields cannot be empty or null ($category: ' + . print_r($category, TRUE) . ')'); + } + + return TRUE; + } catch(Exception $exc) { + return FALSE; + } + + } } /* End of file services_model.php */ diff --git a/src/application/views/backend/services.php b/src/application/views/backend/services.php index b6eddc13..e69de29b 100644 --- a/src/application/views/backend/services.php +++ b/src/application/views/backend/services.php @@ -1,7 +0,0 @@ -