diff --git a/src/application/libraries/google_sync.php b/src/application/libraries/google_sync.php index 966b04c5..ce3c236a 100644 --- a/src/application/libraries/google_sync.php +++ b/src/application/libraries/google_sync.php @@ -1,340 +1,340 @@ - - * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis - * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 - * @link http://easyappointments.org - * @since v1.0.0 - * ---------------------------------------------------------------------------- */ - -// Google API PHP Client is necessary to perform sync operations. -require_once __DIR__ . '/external/google-api-php-client/Google_Client.php'; -require_once __DIR__ . '/external/google-api-php-client/contrib/Google_CalendarService.php'; - -/** - * Google Synchronization Class - * - * This class implements all the core synchronization between the Google Calendar - * and the Easy!Appointments system. Do not place any model handling inside this - * library. - * - * @package Libraries - */ -class Google_Sync { - private $CI; - private $client; - private $service; - - /** - * Class Constructor - * - * This method initializes the Google client class and the Calendar service - * class so that they can be used by the other methods. - */ - public function __construct() { - $this->CI =& get_instance(); - - if (!isset($_SESSION)) { - @session_start(); - } - - // Initialize google client and calendar service. - $this->client = new Google_Client(); - $this->client->setUseObjects(true); - - $this->client->setApplicationName(Config::GOOGLE_PRODUCT_NAME); - $this->client->setClientId(Config::GOOGLE_CLIENT_ID); - $this->client->setClientSecret(Config::GOOGLE_CLIENT_SECRET); - $this->client->setDeveloperKey(Config::GOOGLE_API_KEY); - $this->client->setRedirectUri($this->CI->config->item('base_url') . '/index.php/google/oauth_callback'); - - $this->service = new Google_CalendarService($this->client); - } - - /** - * Get Google OAuth authorization url. - * - * This url must be used to redirect the user to the Google user consent page, - * where the user grants access to his data for the Easy!Appointments app. - */ - public function get_auth_url() { - // "max_auth_age" is needed because the user needs to always log in - // and not use an existing session. - return $this->client->createAuthUrl() . '&max_auth_age=0'; - } - - /** - * Authenticate the Google API usage. - * - * When the user grants consent for his data usage, google is going to redirect - * the browser back to the given redirect url. There a authentication code is - * provided. Using this code, we can authenticate the API usage and store the - * token information to the database. - * - * @see Google Controller - */ - public function authenticate($auth_code) { - $this->client->authenticate($auth_code); - return $this->client->getAccessToken(); - } - - /** - * Refresh the Google Client access token. - * - * This method must be executed every time we need to make actions on a - * provider's Google Calendar account. A new token is necessary and the - * only way to get it is to use the stored refresh token that was provided - * when the provider granted consent to Easy!Appointments for use his - * Google Calendar account. - * - * @param string $refresh_token The provider's refresh token. This value is - * stored in the database and used every time we need to make actions to his - * Google Caledar account. - */ - public function refresh_token($refresh_token) { - $this->client->refreshToken($refresh_token); - } - - /** - * Add an appointment record to its providers Google Calendar account. - * - * This method checks whether the appointment's provider has enabled the Google - * Sync utility of Easy!Appointments and the stored access token is still valid. - * If yes, the selected appointment record is going to be added to the Google - * Calendar account. - * - * @param array $appointment Contains the appointment record data. - * @param array $provider Contains the provider record data. - * @param array $service Contains the service record data. - * @param array $customer Contains the customer recod data. - * @parma array $company_settings Contains some company settings that are used - * by this method. By the time the following values must be in the array: - * 'company_name'. - * @return Google_Event Returns the Google_Event class object. - */ - public function add_appointment($appointment, $provider, $service, $customer, $company_settings) { - $this->CI->load->helper('general'); - - $event = new Google_Event(); - $event->setSummary(($service != NULL) ? $service['name'] : 'Unavailable'); - $event->setLocation($company_settings['company_name']); - - $start = new Google_EventDateTime(); - $start->setDateTime(date3339(strtotime($appointment['start_datetime']))); - $event->setStart($start); - - $end = new Google_EventDateTime(); - $end->setDateTime(date3339(strtotime($appointment['end_datetime']))); - $event->setEnd($end); - - $event->attendees = array(); - - $event_provider = new Google_EventAttendee(); - $event_provider->setDisplayName($provider['first_name'] . ' ' - . $provider['last_name']); - $event_provider->setEmail($provider['email']); - $event->attendees[] = $event_provider; - - if ($customer != NULL) { - $event_customer = new Google_EventAttendee(); - $event_customer->setDisplayName($customer['first_name'] . ' ' - . $customer['last_name']); - $event_customer->setEmail($customer['email']); - $event->attendees[] = $event_customer; - } - - // Add the new event to the google calendar. - $created_event = $this->service->events->insert($provider['settings']['google_calendar'], $event); - - return $created_event; - } - - /** - * Update an existing appointment that is already synced with Google Calendar. - * - * This method updates the google calendar event item that is connected with the - * provided appointment record of Easy!Appointments. - * - * @param array $appointment Contains the appointment record data. - * @param array $provider Contains the provider record data. - * @param array $service Contains the service record data. - * @param array $customer Contains the customer recod data. - * @parma array $company_settings Contains some company settings that are used - * by this method. By the time the following values must be in the array: - * 'company_name'. - * @return Google_Event Returns the Google_Event class object. - */ - public function update_appointment($appointment, $provider, $service, $customer, $company_settings) { - $this->CI->load->helper('general'); - - $event = $this->service->events->get($provider['settings']['google_calendar'], $appointment['id_google_calendar']); - - $event->setSummary($service['name']); - $event->setLocation($company_settings['company_name']); - - $start = new Google_EventDateTime(); - $start->setDateTime(date3339(strtotime($appointment['start_datetime']))); - $event->setStart($start); - - $end = new Google_EventDateTime(); - $end->setDateTime(date3339(strtotime($appointment['end_datetime']))); - $event->setEnd($end); - - $event->attendees = array(); - - $event_provider = new Google_EventAttendee(); - $event_provider->setDisplayName($provider['first_name'] . ' ' - . $provider['last_name']); - $event_provider->setEmail($provider['email']); - $event->attendees[] = $event_provider; - - if ($customer != NULL) { - $event_customer = new Google_EventAttendee(); - $event_customer->setDisplayName($customer['first_name'] . ' ' - . $customer['last_name']); - $event_customer->setEmail($customer['email']); - $event->attendees[] = $event_customer; - } - - $updated_event = $this->service->events->update($provider['settings']['google_calendar'], - $event->getId(), $event); - - return $updated_event; - } - - /** - * Delete an existing appointment from Google Calendar. - * - * @param array $provider Contains the provider record data. - * @param string $google_event_id The Google Calendar event id to - * be deleted. - */ - public function delete_appointment($provider, $google_event_id) { - $this->service->events->delete($provider['settings']['google_calendar'], $google_event_id); - } - - /** - * Add unavailable period event to Google Calendar. - * - * @param array $provider Contains the provider record data. - * @param array $unavailable Contains unavailable period's data. - * @return Google_Event Returns the google event's object. - */ - public function add_unavailable($provider, $unavailable) { - $this->CI->load->helper('general'); - - $event = new Google_Event(); - $event->setSummary('Unavailable'); - $event->setDescription($unavailable['notes']); - - $start = new Google_EventDateTime(); - $start->setDateTime(date3339(strtotime($unavailable['start_datetime']))); - $event->setStart($start); - - $end = new Google_EventDateTime(); - $end->setDateTime(date3339(strtotime($unavailable['end_datetime']))); - $event->setEnd($end); - - // Add the new event to the google calendar. - $created_event = $this->service->events->insert($provider['settings']['google_calendar'], $event); - - return $created_event; - - } - - /** - * Update Google Calendar unavailable period event. - * - * @param array $provider Contains the provider record data. - * @param array $unavailable Contains the unavailable period data. - * @return Google_Event Returns the Google_Event object. - */ - public function update_unavailable($provider, $unavailable) { - $this->CI->load->helper('general'); - - $event = $this->service->events->get($provider['settings']['google_calendar'], $unavailable['id_google_calendar']); - $event->setDescription($unavailable['notes']); - - $start = new Google_EventDateTime(); - $start->setDateTime(date3339(strtotime($unavailable['start_datetime']))); - $event->setStart($start); - - $end = new Google_EventDateTime(); - $end->setDateTime(date3339(strtotime($unavailable['end_datetime']))); - $event->setEnd($end); - - $updated_event = $this->service->events->update($provider['settings']['google_calendar'], - $event->getId(), $event); - - return $updated_event; - } - - /** - * Delete unavailable period event from Google Calendar. - * - * @param array $provider Contains the provider record data. - * @param string $google_event_id Google Calendar event id to be deleted. - */ - public function delete_unavailable($provider, $google_event_id) { - $this->service->events->delete($provider['settings']['google_calendar'], $google_event_id); - } - - /** - * Get an event object from gcal - * - * @param array $provider Contains the provider record data. - * @param string $google_event_id Id of the google calendar event - * @return Google_Event Returns the google event object. - */ - public function get_event($provider, $google_event_id) { - return $this->service->events->get($provider['settings']['google_calendar'], $google_event_id); - } - - /** - * Get all the events between the sync period. - * - * @param string $google_calendar The name of the google calendar to be used. - * @param date $start The start date of sync period. - * @param date $end The end date of sync period. - * @return object Returns an array with Google_Event objects that belong on the given - * sync period (start, end). - */ - public function get_sync_events($google_calendar, $start, $end) { - $this->CI->load->helper('general'); - - $params = array( - 'timeMin' => date3339($start), - 'timeMax' => date3339($end) - ); - - return $this->service->events->listEvents($google_calendar, $params); - } - - /** - * Return available google calendars for specific user. - * - * The given user's token must already exist in db in order to get access to his - * Google Calendar account. - * - * @param string $google_token The user's token will be used to grant access to google calendar. - * @return array Returns an array with the available calendars. - */ - public function get_google_calendars() { - $calendarList = $this->service->calendarList->listCalendarList(); - $calendars = array(); - foreach ($calendarList->items as $google_calendar) { - $calendars[] = array( - 'id' => $google_calendar->id, - 'summary' => $google_calendar->summary - ); - } - return $calendars; - } -} - -/* End of file google_sync.php */ -/* Location: ./application/libraries/google_sync.php */ \ No newline at end of file + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +// Google API PHP Client is necessary to perform sync operations. +require_once __DIR__ . '/external/google-api-php-client/Google_Client.php'; +require_once __DIR__ . '/external/google-api-php-client/contrib/Google_CalendarService.php'; + +/** + * Google Synchronization Class + * + * This class implements all the core synchronization between the Google Calendar + * and the Easy!Appointments system. Do not place any model handling inside this + * library. + * + * @package Libraries + */ +class Google_Sync { + private $CI; + private $client; + private $service; + + /** + * Class Constructor + * + * This method initializes the Google client class and the Calendar service + * class so that they can be used by the other methods. + */ + public function __construct() { + $this->CI =& get_instance(); + + if (!isset($_SESSION)) { + @session_start(); + } + + // Initialize google client and calendar service. + $this->client = new Google_Client(); + $this->client->setUseObjects(true); + + $this->client->setApplicationName(Config::GOOGLE_PRODUCT_NAME); + $this->client->setClientId(Config::GOOGLE_CLIENT_ID); + $this->client->setClientSecret(Config::GOOGLE_CLIENT_SECRET); + $this->client->setDeveloperKey(Config::GOOGLE_API_KEY); + $this->client->setRedirectUri($this->CI->config->item('base_url') . '/index.php/google/oauth_callback'); + + $this->service = new Google_CalendarService($this->client); + } + + /** + * Get Google OAuth authorization url. + * + * This url must be used to redirect the user to the Google user consent page, + * where the user grants access to his data for the Easy!Appointments app. + */ + public function get_auth_url() { + // "max_auth_age" is needed because the user needs to always log in + // and not use an existing session. + return $this->client->createAuthUrl() . '&max_auth_age=0'; + } + + /** + * Authenticate the Google API usage. + * + * When the user grants consent for his data usage, google is going to redirect + * the browser back to the given redirect url. There a authentication code is + * provided. Using this code, we can authenticate the API usage and store the + * token information to the database. + * + * @see Google Controller + */ + public function authenticate($auth_code) { + $this->client->authenticate($auth_code); + return $this->client->getAccessToken(); + } + + /** + * Refresh the Google Client access token. + * + * This method must be executed every time we need to make actions on a + * provider's Google Calendar account. A new token is necessary and the + * only way to get it is to use the stored refresh token that was provided + * when the provider granted consent to Easy!Appointments for use his + * Google Calendar account. + * + * @param string $refresh_token The provider's refresh token. This value is + * stored in the database and used every time we need to make actions to his + * Google Caledar account. + */ + public function refresh_token($refresh_token) { + $this->client->refreshToken($refresh_token); + } + + /** + * Add an appointment record to its providers Google Calendar account. + * + * This method checks whether the appointment's provider has enabled the Google + * Sync utility of Easy!Appointments and the stored access token is still valid. + * If yes, the selected appointment record is going to be added to the Google + * Calendar account. + * + * @param array $appointment Contains the appointment record data. + * @param array $provider Contains the provider record data. + * @param array $service Contains the service record data. + * @param array $customer Contains the customer recod data. + * @parma array $company_settings Contains some company settings that are used + * by this method. By the time the following values must be in the array: + * 'company_name'. + * @return Google_Event Returns the Google_Event class object. + */ + public function add_appointment($appointment, $provider, $service, $customer, $company_settings) { + $this->CI->load->helper('general'); + + $event = new Google_Event(); + $event->setSummary(($service != NULL) ? $service['name'] : 'Unavailable'); + $event->setLocation($company_settings['company_name']); + + $start = new Google_EventDateTime(); + $start->setDateTime(date3339(strtotime($appointment['start_datetime']))); + $event->setStart($start); + + $end = new Google_EventDateTime(); + $end->setDateTime(date3339(strtotime($appointment['end_datetime']))); + $event->setEnd($end); + + $event->attendees = array(); + + $event_provider = new Google_EventAttendee(); + $event_provider->setDisplayName($provider['first_name'] . ' ' + . $provider['last_name']); + $event_provider->setEmail($provider['email']); + $event->attendees[] = $event_provider; + + if ($customer != NULL) { + $event_customer = new Google_EventAttendee(); + $event_customer->setDisplayName($customer['first_name'] . ' ' + . $customer['last_name']); + $event_customer->setEmail($customer['email']); + $event->attendees[] = $event_customer; + } + + // Add the new event to the google calendar. + $created_event = $this->service->events->insert($provider['settings']['google_calendar'], $event); + + return $created_event; + } + + /** + * Update an existing appointment that is already synced with Google Calendar. + * + * This method updates the google calendar event item that is connected with the + * provided appointment record of Easy!Appointments. + * + * @param array $appointment Contains the appointment record data. + * @param array $provider Contains the provider record data. + * @param array $service Contains the service record data. + * @param array $customer Contains the customer recod data. + * @parma array $company_settings Contains some company settings that are used + * by this method. By the time the following values must be in the array: + * 'company_name'. + * @return Google_Event Returns the Google_Event class object. + */ + public function update_appointment($appointment, $provider, $service, $customer, $company_settings) { + $this->CI->load->helper('general'); + + $event = $this->service->events->get($provider['settings']['google_calendar'], $appointment['id_google_calendar']); + + $event->setSummary($service['name']); + $event->setLocation($company_settings['company_name']); + + $start = new Google_EventDateTime(); + $start->setDateTime(date3339(strtotime($appointment['start_datetime']))); + $event->setStart($start); + + $end = new Google_EventDateTime(); + $end->setDateTime(date3339(strtotime($appointment['end_datetime']))); + $event->setEnd($end); + + $event->attendees = array(); + + $event_provider = new Google_EventAttendee(); + $event_provider->setDisplayName($provider['first_name'] . ' ' + . $provider['last_name']); + $event_provider->setEmail($provider['email']); + $event->attendees[] = $event_provider; + + if ($customer != NULL) { + $event_customer = new Google_EventAttendee(); + $event_customer->setDisplayName($customer['first_name'] . ' ' + . $customer['last_name']); + $event_customer->setEmail($customer['email']); + $event->attendees[] = $event_customer; + } + + $updated_event = $this->service->events->update($provider['settings']['google_calendar'], + $event->getId(), $event); + + return $updated_event; + } + + /** + * Delete an existing appointment from Google Calendar. + * + * @param array $provider Contains the provider record data. + * @param string $google_event_id The Google Calendar event id to + * be deleted. + */ + public function delete_appointment($provider, $google_event_id) { + $this->service->events->delete($provider['settings']['google_calendar'], $google_event_id); + } + + /** + * Add unavailable period event to Google Calendar. + * + * @param array $provider Contains the provider record data. + * @param array $unavailable Contains unavailable period's data. + * @return Google_Event Returns the google event's object. + */ + public function add_unavailable($provider, $unavailable) { + $this->CI->load->helper('general'); + + $event = new Google_Event(); + $event->setSummary('Unavailable'); + $event->setDescription($unavailable['notes']); + + $start = new Google_EventDateTime(); + $start->setDateTime(date3339(strtotime($unavailable['start_datetime']))); + $event->setStart($start); + + $end = new Google_EventDateTime(); + $end->setDateTime(date3339(strtotime($unavailable['end_datetime']))); + $event->setEnd($end); + + // Add the new event to the google calendar. + $created_event = $this->service->events->insert($provider['settings']['google_calendar'], $event); + + return $created_event; + + } + + /** + * Update Google Calendar unavailable period event. + * + * @param array $provider Contains the provider record data. + * @param array $unavailable Contains the unavailable period data. + * @return Google_Event Returns the Google_Event object. + */ + public function update_unavailable($provider, $unavailable) { + $this->CI->load->helper('general'); + + $event = $this->service->events->get($provider['settings']['google_calendar'], $unavailable['id_google_calendar']); + $event->setDescription($unavailable['notes']); + + $start = new Google_EventDateTime(); + $start->setDateTime(date3339(strtotime($unavailable['start_datetime']))); + $event->setStart($start); + + $end = new Google_EventDateTime(); + $end->setDateTime(date3339(strtotime($unavailable['end_datetime']))); + $event->setEnd($end); + + $updated_event = $this->service->events->update($provider['settings']['google_calendar'], + $event->getId(), $event); + + return $updated_event; + } + + /** + * Delete unavailable period event from Google Calendar. + * + * @param array $provider Contains the provider record data. + * @param string $google_event_id Google Calendar event id to be deleted. + */ + public function delete_unavailable($provider, $google_event_id) { + $this->service->events->delete($provider['settings']['google_calendar'], $google_event_id); + } + + /** + * Get an event object from gcal + * + * @param array $provider Contains the provider record data. + * @param string $google_event_id Id of the google calendar event + * @return Google_Event Returns the google event object. + */ + public function get_event($provider, $google_event_id) { + return $this->service->events->get($provider['settings']['google_calendar'], $google_event_id); + } + + /** + * Get all the events between the sync period. + * + * @param string $google_calendar The name of the google calendar to be used. + * @param date $start The start date of sync period. + * @param date $end The end date of sync period. + * @return object Returns an array with Google_Event objects that belong on the given + * sync period (start, end). + */ + public function get_sync_events($google_calendar, $start, $end) { + $this->CI->load->helper('general'); + + $params = array( + 'timeMin' => date3339($start), + 'timeMax' => date3339($end) + ); + + return $this->service->events->listEvents($google_calendar, $params); + } + + /** + * Return available google calendars for specific user. + * + * The given user's token must already exist in db in order to get access to his + * Google Calendar account. + * + * @param string $google_token The user's token will be used to grant access to google calendar. + * @return array Returns an array with the available calendars. + */ + public function get_google_calendars() { + $calendarList = $this->service->calendarList->listCalendarList(); + $calendars = array(); + foreach ($calendarList->items as $google_calendar) { + $calendars[] = array( + 'id' => $google_calendar->id, + 'summary' => $google_calendar->summary + ); + } + return $calendars; + } +} + +/* End of file google_sync.php */ +/* Location: ./application/libraries/google_sync.php */ diff --git a/src/application/models/appointments_model.php b/src/application/models/appointments_model.php index 4cc6dba8..2e3e5678 100644 --- a/src/application/models/appointments_model.php +++ b/src/application/models/appointments_model.php @@ -1,406 +1,406 @@ - - * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis - * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 - * @link http://easyappointments.org - * @since v1.0.0 - * ---------------------------------------------------------------------------- */ - -/** - * Appointments Model - * - * @package Models - */ -class Appointments_Model extends CI_Model { - /** - * Class Constructor - */ - public function __construct() { - parent::__construct(); - } - - /** - * Add an appointment record to the database. - * - * This method adds a new appointment to the database. If the - * appointment doesn't exists it is going to be inserted, otherwise - * the record is going to be updated. - * - * @param array $appointment Associative array with the appointment - * data. Each key has the same name with the database fields. - * @return int Returns the appointments id. - */ - public function add($appointment) { - // Validate the appointment data before doing anything. - $this->validate($appointment); - - // Perform insert() or update() operation. - if (!isset($appointment['id'])) { - $appointment['id'] = $this->insert($appointment); - } else { - $this->update($appointment); - } - - return $appointment['id']; - } - - /** - * Check if a particular appointment record already exists. - * - * This method checks wether the given appointment already exists - * in the database. It doesn't search with the id, but by using the - * following fields: "start_datetime", "end_datetime", "id_users_provider", - * "id_users_customer", "id_services". - * - * @param array $appointment Associative array with the appointment's - * data. Each key has the same name with the database fields. - * @return bool Returns wether the record exists or not. - */ - public function exists($appointment) { - if (!isset($appointment['start_datetime']) - || !isset($appointment['end_datetime']) - || !isset($appointment['id_users_provider']) - || !isset($appointment['id_users_customer']) - || !isset($appointment['id_services'])) { - throw new Exception('Not all appointment field values ' - . 'are provided : ' . print_r($appointment, TRUE)); - } - - $num_rows = $this->db->get_where('ea_appointments', array( - 'start_datetime' => $appointment['start_datetime'], - 'end_datetime' => $appointment['end_datetime'], - 'id_users_provider' => $appointment['id_users_provider'], - 'id_users_customer' => $appointment['id_users_customer'], - 'id_services' => $appointment['id_services'],)) - ->num_rows(); - - return ($num_rows > 0) ? TRUE : FALSE; - } - - /** - * Insert a new appointment record to the database. - * - * @param array $appointment Associative array with the appointment's - * data. Each key has the same name with the database fields. - * @return int Returns the id of the new record. - */ - private function insert($appointment) { - $appointment['book_datetime'] = date('Y-m-d H:i:s'); - $appointment['hash'] = $this->generate_hash(); - - if (!$this->db->insert('ea_appointments', $appointment)) { - throw new Exception('Could not insert appointment record.'); - } - - return intval($this->db->insert_id()); - } - - /** - * Update an existing appointment record in the database. - * - * The appointment data argument should already include the record - * id in order to process the update operation. - * - * @expectedException DatabaseException Raises when the update operation - * failes to complete successfully. - * - * @param array $appointment Associative array with the appointment's - * data. Each key has the same name with the database fields. - */ - private function update($appointment) { - $this->db->where('id', $appointment['id']); - if (!$this->db->update('ea_appointments', $appointment)) { - throw new Exception('Could not update appointment record.'); - } - } - - /** - * Find the database id of an appointment record. - * - * The appointment data should include the following fields in order - * to get the unique id from the database: "start_datetime", "end_datetime", - * "id_users_provider", "id_users_customer", "id_services". - * - * IMPORTANT! The record must already exists in the - * database, otherwise an exception is raised. - * - * @param array $appointment Array with the appointment data. The - * keys of the array should have the same names as the db fields. - * @return int Returns the db id of the record that matches the apppointment - * data. - */ - public function find_record_id($appointment) { - $this->db->where(array( - 'start_datetime' => $appointment['start_datetime'], - 'end_datetime' => $appointment['end_datetime'], - 'id_users_provider' => $appointment['id_users_provider'], - 'id_users_customer' => $appointment['id_users_customer'], - 'id_services' => $appointment['id_services'] - )); - - $result = $this->db->get('ea_appointments'); - - if ($result->num_rows() == 0) { - throw new Exception('Could not find appointment record id.'); - } - - return $result->row()->id; - } - - /** - * Validate appointment data before the insert or update operations - * are executed. - * - * @param array $appointment Contains the appointment data. - * @return bool Returns the validation result. - */ - public function validate($appointment) { - $this->load->helper('data_validation'); - - // If a appointment id is given, check wether the record exists - // in the database. - if (isset($appointment['id'])) { - $num_rows = $this->db->get_where('ea_appointments', - array('id' => $appointment['id']))->num_rows(); - if ($num_rows == 0) { - throw new Exception('Provided appointment id does not ' - . 'exist in the database.'); - } - } - - // Check if appointment dates are valid. - if (!validate_mysql_datetime($appointment['start_datetime'])) { - throw new Exception('Appointment start datetime is invalid.'); - } - - if (!validate_mysql_datetime($appointment['end_datetime'])) { - throw new Exception('Appointment end datetime is invalid.'); - } - - // Check if the provider's id is valid. - $num_rows = $this->db - ->select('*') - ->from('ea_users') - ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner') - ->where('ea_users.id', $appointment['id_users_provider']) - ->where('ea_roles.slug', DB_SLUG_PROVIDER) - ->get()->num_rows(); - if ($num_rows == 0) { - throw new Exception('Appointment provider id is invalid.'); - } - - if ($appointment['is_unavailable'] == FALSE) { - // Check if the customer's id is valid. - $num_rows = $this->db - ->select('*') - ->from('ea_users') - ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner') - ->where('ea_users.id', $appointment['id_users_customer']) - ->where('ea_roles.slug', DB_SLUG_CUSTOMER) - ->get()->num_rows(); - if ($num_rows == 0) { - throw new Exception('Appointment customer id is invalid.'); - } - - // Check if the service id is valid. - $num_rows = $this->db->get_where('ea_services', - array('id' => $appointment['id_services']))->num_rows(); - if ($num_rows == 0) { - throw new Exception('Appointment customer id is invalid.'); - } - } - - return TRUE; - } - - /** - * Delete an existing appointment record from the database. - * - * @expectedException InvalidArgumentException Raises when the $appointment_id - * is not an integer. - * - * @param numeric $appointment_id The record id to be deleted. - * @return bool Returns the delete operation result. - */ - public function delete($appointment_id) { - if (!is_numeric($appointment_id)) { - throw new Exception('Invalid argument type $appointment_id (value:"' . $appointment_id . '")'); - } - - $num_rows = $this->db->get_where('ea_appointments', array('id' => $appointment_id))->num_rows(); - - if ($num_rows == 0) { - return FALSE; // Record does not exist. - } - - $this->db->where('id', $appointment_id); - return $this->db->delete('ea_appointments'); - } - - /** - * Get a specific row from the appointments table. - * - * @param numeric $appointment_id The record's id to be returned. - * @return array Returns an associative array with the selected - * record's data. Each key has the same name as the database - * field names. - */ - public function get_row($appointment_id) { - if (!is_numeric($appointment_id)) { - throw new Exception('Invalid argument given. Expected ' - . 'integer for the $appointment_id : ' . $appointment_id); - } - return $this->db->get_where('ea_appointments', - array('id' => $appointment_id))->row_array(); - } - - /** - * Get a specific field value from the database. - * - * @param string $field_name The field name of the value to be returned. - * @param numeric $appointment_id The selected record's id. - * @return string Returns the records value from the database. - */ - public function get_value($field_name, $appointment_id) { - if (!is_numeric($appointment_id)) { - throw new Exception('Invalid argument given, expected ' - . 'integer for the $appointment_id : ' . $appointment_id); - } - - if (!is_string($field_name)) { - throw new Exception('Invalid argument given, expected ' - . 'string for the $field_name : ' . $field_name); - } - - if ($this->db->get_where('ea_appointments', - array('id' => $appointment_id))->num_rows() == 0) { - throw new Exception('The record with the provided id ' - . 'does not exist in the database : ' . $appointment_id); - } - - $row_data = $this->db->get_where('ea_appointments', - array('id' => $appointment_id))->row_array(); - - if (!isset($row_data[$field_name])) { - throw new Exception('The given field name does not ' - . 'exist in the database : ' . $field_name); - } - - return $row_data[$field_name]; - } - - /** - * Get all, or specific records from appointment's table. - * - * @example $this->Model->getBatch('id = ' . $recordId); - * - * @param string $where_clause (OPTIONAL) The WHERE clause of - * the query to be executed. DO NOT INCLUDE 'WHERE' KEYWORD. - * @return array Returns the rows from the database. - */ - public function get_batch($where_clause = '') { - if ($where_clause != '') { - $this->db->where($where_clause); - } - - return $this->db->get('ea_appointments')->result_array(); - } - - /** - * Generate a unique hash for the given appointment data. - * - * This method uses the current date-time to generate a unique - * hash string that is later used to identify this appointment. - * Hash is needed when the email is send to the user with an - * edit link. - * - * @return string Returns the unique appointment hash. - */ - public function generate_hash() { - $current_date = new DateTime(); - return md5($current_date->getTimestamp()); - } - - /** - * Inserts or updates an unavailable period record in the database. - * - * @param array $unavailable Contains the unavaible data. - * @return int Returns the record id. - */ - public function add_unavailable($unavailable) { - // Validate period - $start = strtotime($unavailable['start_datetime']); - $end = strtotime($unavailable['end_datetime']); - if ($start > $end) { - throw new Exception('Unavailable period start must be prior to end.'); - } - - // Validate provider record - $where_clause = array( - 'id' => $unavailable['id_users_provider'], - 'id_roles' => $this->db->get_where('ea_roles', array('slug' => DB_SLUG_PROVIDER))->row()->id - ); - - if ($this->db->get_where('ea_users', $where_clause)->num_rows() == 0) { - throw new Exception('Provider id was not found in database.'); - } - - // Add record to database (insert or update). - if (!isset($unavailable['id'])) { - $unavailable['book_datetime'] = date('Y-m-d H:i:s'); - $unavailable['is_unavailable'] = true; - - $this->db->insert('ea_appointments', $unavailable); - $unavailable['id'] = $this->db->insert_id(); - } else { - $this->db->where(array('id' => $unavailable['id'])); - $this->db->update('ea_appointments', $unavailable); - } - - return $unavailable['id']; - } - - /** - * Delete an unavailable period. - * - * @param numeric $unavailable_id Record id to be deleted. - */ - public function delete_unavailable($unavailable_id) { - if (!is_numeric($unavailable_id)) { - throw new Exception('Invalid argument type $unavailable_id (value:"' . - $unavailable_id . '")'); - } - - $num_rows = $this->db->get_where('ea_appointments', array('id' => $unavailable_id)) - ->num_rows(); - if ($num_rows == 0) { - return FALSE; // Record does not exist. - } - - $this->db->where('id', $unavailable_id); - return $this->db->delete('ea_appointments'); - } - - /** - * Clear google sync IDs from appointment record. - * - * @param numeric $provider_id The appointment provider record id. - */ - public function clear_google_sync_ids($provider_id) { - if (!is_numeric($provider_id)) { - throw new Exception('Invalid argument type $provider_id (value: "' - . $provider_id . '")'); - } - - $this->db->update('ea_appointments', array('id_google_calendar' => NULL), - array('id_users_provider' => $provider_id)); - } -} - -/* End of file appointments_model.php */ -/* Location: ./application/models/appointments_model.php */ \ No newline at end of file + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Appointments Model + * + * @package Models + */ +class Appointments_Model extends CI_Model { + /** + * Class Constructor + */ + public function __construct() { + parent::__construct(); + } + + /** + * Add an appointment record to the database. + * + * This method adds a new appointment to the database. If the + * appointment doesn't exists it is going to be inserted, otherwise + * the record is going to be updated. + * + * @param array $appointment Associative array with the appointment + * data. Each key has the same name with the database fields. + * @return int Returns the appointments id. + */ + public function add($appointment) { + // Validate the appointment data before doing anything. + $this->validate($appointment); + + // Perform insert() or update() operation. + if (!isset($appointment['id'])) { + $appointment['id'] = $this->insert($appointment); + } else { + $this->update($appointment); + } + + return $appointment['id']; + } + + /** + * Check if a particular appointment record already exists. + * + * This method checks wether the given appointment already exists + * in the database. It doesn't search with the id, but by using the + * following fields: "start_datetime", "end_datetime", "id_users_provider", + * "id_users_customer", "id_services". + * + * @param array $appointment Associative array with the appointment's + * data. Each key has the same name with the database fields. + * @return bool Returns wether the record exists or not. + */ + public function exists($appointment) { + if (!isset($appointment['start_datetime']) + || !isset($appointment['end_datetime']) + || !isset($appointment['id_users_provider']) + || !isset($appointment['id_users_customer']) + || !isset($appointment['id_services'])) { + throw new Exception('Not all appointment field values ' + . 'are provided : ' . print_r($appointment, TRUE)); + } + + $num_rows = $this->db->get_where('ea_appointments', array( + 'start_datetime' => $appointment['start_datetime'], + 'end_datetime' => $appointment['end_datetime'], + 'id_users_provider' => $appointment['id_users_provider'], + 'id_users_customer' => $appointment['id_users_customer'], + 'id_services' => $appointment['id_services'],)) + ->num_rows(); + + return ($num_rows > 0) ? TRUE : FALSE; + } + + /** + * Insert a new appointment record to the database. + * + * @param array $appointment Associative array with the appointment's + * data. Each key has the same name with the database fields. + * @return int Returns the id of the new record. + */ + private function insert($appointment) { + $appointment['book_datetime'] = date('Y-m-d H:i:s'); + $appointment['hash'] = $this->generate_hash(); + + if (!$this->db->insert('ea_appointments', $appointment)) { + throw new Exception('Could not insert appointment record.'); + } + + return intval($this->db->insert_id()); + } + + /** + * Update an existing appointment record in the database. + * + * The appointment data argument should already include the record + * id in order to process the update operation. + * + * @expectedException DatabaseException Raises when the update operation + * failes to complete successfully. + * + * @param array $appointment Associative array with the appointment's + * data. Each key has the same name with the database fields. + */ + private function update($appointment) { + $this->db->where('id', $appointment['id']); + if (!$this->db->update('ea_appointments', $appointment)) { + throw new Exception('Could not update appointment record.'); + } + } + + /** + * Find the database id of an appointment record. + * + * The appointment data should include the following fields in order + * to get the unique id from the database: "start_datetime", "end_datetime", + * "id_users_provider", "id_users_customer", "id_services". + * + * IMPORTANT! The record must already exists in the + * database, otherwise an exception is raised. + * + * @param array $appointment Array with the appointment data. The + * keys of the array should have the same names as the db fields. + * @return int Returns the db id of the record that matches the apppointment + * data. + */ + public function find_record_id($appointment) { + $this->db->where(array( + 'start_datetime' => $appointment['start_datetime'], + 'end_datetime' => $appointment['end_datetime'], + 'id_users_provider' => $appointment['id_users_provider'], + 'id_users_customer' => $appointment['id_users_customer'], + 'id_services' => $appointment['id_services'] + )); + + $result = $this->db->get('ea_appointments'); + + if ($result->num_rows() == 0) { + throw new Exception('Could not find appointment record id.'); + } + + return $result->row()->id; + } + + /** + * Validate appointment data before the insert or update operations + * are executed. + * + * @param array $appointment Contains the appointment data. + * @return bool Returns the validation result. + */ + public function validate($appointment) { + $this->load->helper('data_validation'); + + // If a appointment id is given, check wether the record exists + // in the database. + if (isset($appointment['id'])) { + $num_rows = $this->db->get_where('ea_appointments', + array('id' => $appointment['id']))->num_rows(); + if ($num_rows == 0) { + throw new Exception('Provided appointment id does not ' + . 'exist in the database.'); + } + } + + // Check if appointment dates are valid. + if (!validate_mysql_datetime($appointment['start_datetime'])) { + throw new Exception('Appointment start datetime is invalid.'); + } + + if (!validate_mysql_datetime($appointment['end_datetime'])) { + throw new Exception('Appointment end datetime is invalid.'); + } + + // Check if the provider's id is valid. + $num_rows = $this->db + ->select('*') + ->from('ea_users') + ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner') + ->where('ea_users.id', $appointment['id_users_provider']) + ->where('ea_roles.slug', DB_SLUG_PROVIDER) + ->get()->num_rows(); + if ($num_rows == 0) { + throw new Exception('Appointment provider id is invalid.'); + } + + if ($appointment['is_unavailable'] == FALSE) { + // Check if the customer's id is valid. + $num_rows = $this->db + ->select('*') + ->from('ea_users') + ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner') + ->where('ea_users.id', $appointment['id_users_customer']) + ->where('ea_roles.slug', DB_SLUG_CUSTOMER) + ->get()->num_rows(); + if ($num_rows == 0) { + throw new Exception('Appointment customer id is invalid.'); + } + + // Check if the service id is valid. + $num_rows = $this->db->get_where('ea_services', + array('id' => $appointment['id_services']))->num_rows(); + if ($num_rows == 0) { + throw new Exception('Appointment customer id is invalid.'); + } + } + + return TRUE; + } + + /** + * Delete an existing appointment record from the database. + * + * @expectedException InvalidArgumentException Raises when the $appointment_id + * is not an integer. + * + * @param numeric $appointment_id The record id to be deleted. + * @return bool Returns the delete operation result. + */ + public function delete($appointment_id) { + if (!is_numeric($appointment_id)) { + throw new Exception('Invalid argument type $appointment_id (value:"' . $appointment_id . '")'); + } + + $num_rows = $this->db->get_where('ea_appointments', array('id' => $appointment_id))->num_rows(); + + if ($num_rows == 0) { + return FALSE; // Record does not exist. + } + + $this->db->where('id', $appointment_id); + return $this->db->delete('ea_appointments'); + } + + /** + * Get a specific row from the appointments table. + * + * @param numeric $appointment_id The record's id to be returned. + * @return array Returns an associative array with the selected + * record's data. Each key has the same name as the database + * field names. + */ + public function get_row($appointment_id) { + if (!is_numeric($appointment_id)) { + throw new Exception('Invalid argument given. Expected ' + . 'integer for the $appointment_id : ' . $appointment_id); + } + return $this->db->get_where('ea_appointments', + array('id' => $appointment_id))->row_array(); + } + + /** + * Get a specific field value from the database. + * + * @param string $field_name The field name of the value to be returned. + * @param numeric $appointment_id The selected record's id. + * @return string Returns the records value from the database. + */ + public function get_value($field_name, $appointment_id) { + if (!is_numeric($appointment_id)) { + throw new Exception('Invalid argument given, expected ' + . 'integer for the $appointment_id : ' . $appointment_id); + } + + if (!is_string($field_name)) { + throw new Exception('Invalid argument given, expected ' + . 'string for the $field_name : ' . $field_name); + } + + if ($this->db->get_where('ea_appointments', + array('id' => $appointment_id))->num_rows() == 0) { + throw new Exception('The record with the provided id ' + . 'does not exist in the database : ' . $appointment_id); + } + + $row_data = $this->db->get_where('ea_appointments', + array('id' => $appointment_id))->row_array(); + + if (!isset($row_data[$field_name])) { + throw new Exception('The given field name does not ' + . 'exist in the database : ' . $field_name); + } + + return $row_data[$field_name]; + } + + /** + * Get all, or specific records from appointment's table. + * + * @example $this->Model->getBatch('id = ' . $recordId); + * + * @param string $where_clause (OPTIONAL) The WHERE clause of + * the query to be executed. DO NOT INCLUDE 'WHERE' KEYWORD. + * @return array Returns the rows from the database. + */ + public function get_batch($where_clause = '') { + if ($where_clause != '') { + $this->db->where($where_clause); + } + + return $this->db->get('ea_appointments')->result_array(); + } + + /** + * Generate a unique hash for the given appointment data. + * + * This method uses the current date-time to generate a unique + * hash string that is later used to identify this appointment. + * Hash is needed when the email is send to the user with an + * edit link. + * + * @return string Returns the unique appointment hash. + */ + public function generate_hash() { + $current_date = new DateTime(); + return md5($current_date->getTimestamp()); + } + + /** + * Inserts or updates an unavailable period record in the database. + * + * @param array $unavailable Contains the unavaible data. + * @return int Returns the record id. + */ + public function add_unavailable($unavailable) { + // Validate period + $start = strtotime($unavailable['start_datetime']); + $end = strtotime($unavailable['end_datetime']); + if ($start > $end) { + throw new Exception('Unavailable period start must be prior to end.'); + } + + // Validate provider record + $where_clause = array( + 'id' => $unavailable['id_users_provider'], + 'id_roles' => $this->db->get_where('ea_roles', array('slug' => DB_SLUG_PROVIDER))->row()->id + ); + + if ($this->db->get_where('ea_users', $where_clause)->num_rows() == 0) { + throw new Exception('Provider id was not found in database.'); + } + + // Add record to database (insert or update). + if (!isset($unavailable['id'])) { + $unavailable['book_datetime'] = date('Y-m-d H:i:s'); + $unavailable['is_unavailable'] = true; + + $this->db->insert('ea_appointments', $unavailable); + $unavailable['id'] = $this->db->insert_id(); + } else { + $this->db->where(array('id' => $unavailable['id'])); + $this->db->update('ea_appointments', $unavailable); + } + + return $unavailable['id']; + } + + /** + * Delete an unavailable period. + * + * @param numeric $unavailable_id Record id to be deleted. + */ + public function delete_unavailable($unavailable_id) { + if (!is_numeric($unavailable_id)) { + throw new Exception('Invalid argument type $unavailable_id (value:"' . + $unavailable_id . '")'); + } + + $num_rows = $this->db->get_where('ea_appointments', array('id' => $unavailable_id)) + ->num_rows(); + if ($num_rows == 0) { + return FALSE; // Record does not exist. + } + + $this->db->where('id', $unavailable_id); + return $this->db->delete('ea_appointments'); + } + + /** + * Clear google sync IDs from appointment record. + * + * @param numeric $provider_id The appointment provider record id. + */ + public function clear_google_sync_ids($provider_id) { + if (!is_numeric($provider_id)) { + throw new Exception('Invalid argument type $provider_id (value: "' + . $provider_id . '")'); + } + + $this->db->update('ea_appointments', array('id_google_calendar' => NULL), + array('id_users_provider' => $provider_id)); + } +} + +/* End of file appointments_model.php */ +/* Location: ./application/models/appointments_model.php */ diff --git a/src/application/models/customers_model.php b/src/application/models/customers_model.php index 04dd0925..4f93ac99 100644 --- a/src/application/models/customers_model.php +++ b/src/application/models/customers_model.php @@ -1,327 +1,327 @@ - - * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis - * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 - * @link http://easyappointments.org - * @since v1.0.0 - * ---------------------------------------------------------------------------- */ - -/** - * Customers Model - * - * @package Models - */ -class Customers_Model extends CI_Model { - /** - * Class Constructor - */ - public function __construct() { - parent::__construct(); - } - - /** - * Add a customer record to the database. - * - * This method adds a customer to the database. If the customer - * doesn't exists it is going to be inserted, otherwise the - * record is going to be updated. - * - * @param array $customer Associative array with the customer's - * data. Each key has the same name with the database fields. - * @return int Returns the customer id. - */ - public function add($customer) { - // Validate the customer data before doing anything. - $this->validate($customer); - - // :: CHECK IF CUSTOMER ALREADY EXIST (FROM EMAIL). - if ($this->exists($customer) && !isset($customer['id'])) { - // Find the customer id from the database. - $customer['id'] = $this->find_record_id($customer); - } - - // :: INSERT OR UPDATE CUSTOMER RECORD - if (!isset($customer['id'])) { - $customer['id'] = $this->insert($customer); - } else { - $this->update($customer); - } - - return $customer['id']; - } - - /** - * Check if a particular customer record already exists. - * - * This method checks wether the given customer already exists in - * the database. It doesn't search with the id, but with the following - * fields: "email" - * - * @param array $customer Associative array with the customer's - * data. Each key has the same name with the database fields. - * @return bool Returns wether the record exists or not. - */ - public function exists($customer) { - if (!isset($customer['email'])) { - throw new Exception('Customer\'s email is not provided.'); - } - - // 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', $customer['email']) - ->where('ea_roles.slug', DB_SLUG_CUSTOMER) - ->get()->num_rows(); - - return ($num_rows > 0) ? TRUE : FALSE; - } - - /** - * Insert a new customer record to the database. - * - * @param array $customer Associative array with the customer's - * data. Each key has the same name with the database fields. - * @return int Returns the id of the new record. - */ - private function insert($customer) { - // Before inserting the customer we need to get the customer's role id - // from the database and assign it to the new record as a foreign key. - $customer_role_id = $this->db - ->select('id') - ->from('ea_roles') - ->where('slug', DB_SLUG_CUSTOMER) - ->get()->row()->id; - - $customer['id_roles'] = $customer_role_id; - - if (!$this->db->insert('ea_users', $customer)) { - throw new Exception('Could not insert customer to the database.'); - } - - return intval($this->db->insert_id()); - } - - /** - * Update an existing customer record in the database. - * - * The customer data argument should already include the record - * id in order to process the update operation. - * - * @param array $customer Associative array with the customer's - * data. Each key has the same name with the database fields. - * @return int Returns the updated record id. - */ - private function update($customer) { - // Do not update empty string values. - foreach ($customer as $key => $value) { - if ($value === '') - unset($customer[$key]); - } - - $this->db->where('id', $customer['id']); - if (!$this->db->update('ea_users', $customer)) { - throw new Exception('Could not update customer to the database.'); - } - - return intval($customer['id']); - } - - /** - * Find the database id of a customer record. - * - * The customer data should include the following fields in order to - * get the unique id from the database: "email" - * - * IMPORTANT! The record must already exists in the - * database, otherwise an exception is raised. - * - * @param array $customer Array with the customer data. The - * keys of the array should have the same names as the db fields. - * @return int Returns the id. - */ - public function find_record_id($customer) { - if (!isset($customer['email'])) { - throw new Exception('Customer\'s email was not provided : ' - . print_r($customer, TRUE)); - } - - // Get customer's role id - $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', $customer['email']) - ->where('ea_roles.slug', DB_SLUG_CUSTOMER) - ->get(); - - if ($result->num_rows() == 0) { - throw new Exception('Could not find customer record id.'); - } - - return $result->row()->id; - } - - /** - * Validate customer data before the insert or update operation is executed. - * - * @param array $customer Contains the customer data. - * @return bool Returns the validation result. - */ - public function validate($customer) { - $this->load->helper('data_validation'); - - // If a customer id is provided, check whether the record - // exist in the database. - if (isset($customer['id'])) { - $num_rows = $this->db->get_where('ea_users', - array('id' => $customer['id']))->num_rows(); - if ($num_rows == 0) { - throw new Exception('Provided customer id does not ' - . 'exist in the database.'); - } - } - // Validate required fields - if (!isset($customer['last_name']) - || !isset($customer['email']) - || !isset($customer['phone_number'])) { - throw new Exception('Not all required fields are provided : ' - . print_r($customer, TRUE)); - } - - // Validate email address - if (!filter_var($customer['email'], FILTER_VALIDATE_EMAIL)) { - throw new Exception('Invalid email address provided : ' - . $customer['email']); - } - - // When inserting a record the email address must be unique. - $customer_id = (isset($customer['id'])) ? $customer['id'] : ''; - - $num_rows = $this->db - ->select('*') - ->from('ea_users') - ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner') - ->where('ea_roles.slug', DB_SLUG_CUSTOMER) - ->where('ea_users.email', $customer['email']) - ->where('ea_users.id <>', $customer_id) - ->get() - ->num_rows(); - - if ($num_rows > 0) { - throw new Exception('Given email address belongs to another customer record. ' - . 'Please use a different email.'); - } - - return TRUE; - } - - /** - * Delete an existing customer record from the database. - * - * @param numeric $customer_id The record id to be deleted. - * @return bool Returns the delete operation result. - */ - public function delete($customer_id) { - if (!is_numeric($customer_id)) { - throw new Exception('Invalid argument type $customer_id : ' . $customer_id); - } - - $num_rows = $this->db->get_where('ea_users', array('id' => $customer_id))->num_rows(); - if ($num_rows == 0) { - return FALSE; - } - - return $this->db->delete('ea_users', array('id' => $customer_id)); - } - - /** - * Get a specific row from the appointments table. - * - * @param numeric $customer_id The record's id to be returned. - * @return array Returns an associative array with the selected - * record's data. Each key has the same name as the database - * field names. - */ - public function get_row($customer_id) { - if (!is_numeric($customer_id)) { - throw new Exception('Invalid argument provided as $customer_id : ' . $customer_id); - } - return $this->db->get_where('ea_users', array('id' => $customer_id))->row_array(); - } - - /** - * Get a specific field value from the database. - * - * @param string $field_name The field name of the value to be - * returned. - * @param int $customer_id The selected record's id. - * @return string Returns the records value from the database. - */ - public function get_value($field_name, $customer_id) { - if (!is_numeric($customer_id)) { - throw new Exception('Invalid argument provided as $customer_id : ' - . $customer_id); - } - - if (!is_string($field_name)) { - throw new Exception('$field_name argument is not a string : ' - . $field_name); - } - - if ($this->db->get_where('ea_users', array('id' => $customer_id))->num_rows() == 0) { - throw new Exception('The record with the $customer_id argument ' - . 'does not exist in the database : ' . $customer_id); - } - - $row_data = $this->db->get_where('ea_users', array('id' => $customer_id) - )->row_array(); - if (!isset($row_data[$field_name])) { - throw new Exception('The given $field_name argument does not' - . 'exist in the database : ' . $field_name); - } - - $customer = $this->db->get_where('ea_users', array('id' => $customer_id))->row_array(); - - return $customer[$field_name]; - } - - /** - * Get all, or specific records from appointment's table. - * - * @example $this->Model->getBatch('id = ' . $recordId); - * - * @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. - */ - public function get_batch($where_clause = '') { - $customers_role_id = $this->get_customers_role_id(); - - if ($where_clause != '') { - $this->db->where($where_clause); - } - - $this->db->where('id_roles', $customers_role_id); - - return $this->db->get('ea_users')->result_array(); - } - - /** - * Get the customers role id from the database. - * - * @return int Returns the role id for the customer records. - */ - public function get_customers_role_id() { - return $this->db->get_where('ea_roles', array('slug' => DB_SLUG_CUSTOMER))->row()->id; - } -} - -/* End of file customers_model.php */ -/* Location: ./application/models/customers_model.php */ \ No newline at end of file + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Customers Model + * + * @package Models + */ +class Customers_Model extends CI_Model { + /** + * Class Constructor + */ + public function __construct() { + parent::__construct(); + } + + /** + * Add a customer record to the database. + * + * This method adds a customer to the database. If the customer + * doesn't exists it is going to be inserted, otherwise the + * record is going to be updated. + * + * @param array $customer Associative array with the customer's + * data. Each key has the same name with the database fields. + * @return int Returns the customer id. + */ + public function add($customer) { + // Validate the customer data before doing anything. + $this->validate($customer); + + // :: CHECK IF CUSTOMER ALREADY EXIST (FROM EMAIL). + if ($this->exists($customer) && !isset($customer['id'])) { + // Find the customer id from the database. + $customer['id'] = $this->find_record_id($customer); + } + + // :: INSERT OR UPDATE CUSTOMER RECORD + if (!isset($customer['id'])) { + $customer['id'] = $this->insert($customer); + } else { + $this->update($customer); + } + + return $customer['id']; + } + + /** + * Check if a particular customer record already exists. + * + * This method checks wether the given customer already exists in + * the database. It doesn't search with the id, but with the following + * fields: "email" + * + * @param array $customer Associative array with the customer's + * data. Each key has the same name with the database fields. + * @return bool Returns wether the record exists or not. + */ + public function exists($customer) { + if (!isset($customer['email'])) { + throw new Exception('Customer\'s email is not provided.'); + } + + // 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', $customer['email']) + ->where('ea_roles.slug', DB_SLUG_CUSTOMER) + ->get()->num_rows(); + + return ($num_rows > 0) ? TRUE : FALSE; + } + + /** + * Insert a new customer record to the database. + * + * @param array $customer Associative array with the customer's + * data. Each key has the same name with the database fields. + * @return int Returns the id of the new record. + */ + private function insert($customer) { + // Before inserting the customer we need to get the customer's role id + // from the database and assign it to the new record as a foreign key. + $customer_role_id = $this->db + ->select('id') + ->from('ea_roles') + ->where('slug', DB_SLUG_CUSTOMER) + ->get()->row()->id; + + $customer['id_roles'] = $customer_role_id; + + if (!$this->db->insert('ea_users', $customer)) { + throw new Exception('Could not insert customer to the database.'); + } + + return intval($this->db->insert_id()); + } + + /** + * Update an existing customer record in the database. + * + * The customer data argument should already include the record + * id in order to process the update operation. + * + * @param array $customer Associative array with the customer's + * data. Each key has the same name with the database fields. + * @return int Returns the updated record id. + */ + private function update($customer) { + // Do not update empty string values. + foreach ($customer as $key => $value) { + if ($value === '') + unset($customer[$key]); + } + + $this->db->where('id', $customer['id']); + if (!$this->db->update('ea_users', $customer)) { + throw new Exception('Could not update customer to the database.'); + } + + return intval($customer['id']); + } + + /** + * Find the database id of a customer record. + * + * The customer data should include the following fields in order to + * get the unique id from the database: "email" + * + * IMPORTANT! The record must already exists in the + * database, otherwise an exception is raised. + * + * @param array $customer Array with the customer data. The + * keys of the array should have the same names as the db fields. + * @return int Returns the id. + */ + public function find_record_id($customer) { + if (!isset($customer['email'])) { + throw new Exception('Customer\'s email was not provided : ' + . print_r($customer, TRUE)); + } + + // Get customer's role id + $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', $customer['email']) + ->where('ea_roles.slug', DB_SLUG_CUSTOMER) + ->get(); + + if ($result->num_rows() == 0) { + throw new Exception('Could not find customer record id.'); + } + + return $result->row()->id; + } + + /** + * Validate customer data before the insert or update operation is executed. + * + * @param array $customer Contains the customer data. + * @return bool Returns the validation result. + */ + public function validate($customer) { + $this->load->helper('data_validation'); + + // If a customer id is provided, check whether the record + // exist in the database. + if (isset($customer['id'])) { + $num_rows = $this->db->get_where('ea_users', + array('id' => $customer['id']))->num_rows(); + if ($num_rows == 0) { + throw new Exception('Provided customer id does not ' + . 'exist in the database.'); + } + } + // Validate required fields + if (!isset($customer['last_name']) + || !isset($customer['email']) + || !isset($customer['phone_number'])) { + throw new Exception('Not all required fields are provided : ' + . print_r($customer, TRUE)); + } + + // Validate email address + if (!filter_var($customer['email'], FILTER_VALIDATE_EMAIL)) { + throw new Exception('Invalid email address provided : ' + . $customer['email']); + } + + // When inserting a record the email address must be unique. + $customer_id = (isset($customer['id'])) ? $customer['id'] : ''; + + $num_rows = $this->db + ->select('*') + ->from('ea_users') + ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner') + ->where('ea_roles.slug', DB_SLUG_CUSTOMER) + ->where('ea_users.email', $customer['email']) + ->where('ea_users.id <>', $customer_id) + ->get() + ->num_rows(); + + if ($num_rows > 0) { + throw new Exception('Given email address belongs to another customer record. ' + . 'Please use a different email.'); + } + + return TRUE; + } + + /** + * Delete an existing customer record from the database. + * + * @param numeric $customer_id The record id to be deleted. + * @return bool Returns the delete operation result. + */ + public function delete($customer_id) { + if (!is_numeric($customer_id)) { + throw new Exception('Invalid argument type $customer_id : ' . $customer_id); + } + + $num_rows = $this->db->get_where('ea_users', array('id' => $customer_id))->num_rows(); + if ($num_rows == 0) { + return FALSE; + } + + return $this->db->delete('ea_users', array('id' => $customer_id)); + } + + /** + * Get a specific row from the appointments table. + * + * @param numeric $customer_id The record's id to be returned. + * @return array Returns an associative array with the selected + * record's data. Each key has the same name as the database + * field names. + */ + public function get_row($customer_id) { + if (!is_numeric($customer_id)) { + throw new Exception('Invalid argument provided as $customer_id : ' . $customer_id); + } + return $this->db->get_where('ea_users', array('id' => $customer_id))->row_array(); + } + + /** + * Get a specific field value from the database. + * + * @param string $field_name The field name of the value to be + * returned. + * @param int $customer_id The selected record's id. + * @return string Returns the records value from the database. + */ + public function get_value($field_name, $customer_id) { + if (!is_numeric($customer_id)) { + throw new Exception('Invalid argument provided as $customer_id : ' + . $customer_id); + } + + if (!is_string($field_name)) { + throw new Exception('$field_name argument is not a string : ' + . $field_name); + } + + if ($this->db->get_where('ea_users', array('id' => $customer_id))->num_rows() == 0) { + throw new Exception('The record with the $customer_id argument ' + . 'does not exist in the database : ' . $customer_id); + } + + $row_data = $this->db->get_where('ea_users', array('id' => $customer_id) + )->row_array(); + if (!isset($row_data[$field_name])) { + throw new Exception('The given $field_name argument does not' + . 'exist in the database : ' . $field_name); + } + + $customer = $this->db->get_where('ea_users', array('id' => $customer_id))->row_array(); + + return $customer[$field_name]; + } + + /** + * Get all, or specific records from appointment's table. + * + * @example $this->Model->getBatch('id = ' . $recordId); + * + * @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. + */ + public function get_batch($where_clause = '') { + $customers_role_id = $this->get_customers_role_id(); + + if ($where_clause != '') { + $this->db->where($where_clause); + } + + $this->db->where('id_roles', $customers_role_id); + + return $this->db->get('ea_users')->result_array(); + } + + /** + * Get the customers role id from the database. + * + * @return int Returns the role id for the customer records. + */ + public function get_customers_role_id() { + return $this->db->get_where('ea_roles', array('slug' => DB_SLUG_CUSTOMER))->row()->id; + } +} + +/* End of file customers_model.php */ +/* Location: ./application/models/customers_model.php */ diff --git a/src/application/models/roles_model.php b/src/application/models/roles_model.php index 8f4671af..9079c8fe 100644 --- a/src/application/models/roles_model.php +++ b/src/application/models/roles_model.php @@ -1,89 +1,89 @@ - - * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis - * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 - * @link http://easyappointments.org - * @since v1.0.0 - * ---------------------------------------------------------------------------- */ - -/** - * Roles Model - * - * @package Models - */ -class Roles_Model extends CI_Model { - /** - * Class Constructor - */ - public function __construct() { - parent::__construct(); - } - - /** - * Get the record id of a particular role. - * - * @param string $role_slug The selected role slug. Slugs are - * defined in the "application/config/constants.php" file. - * @return int Returns the database id of the roles record. - */ - public function get_role_id($role_slug) { - return $this->db->get_where('ea_roles', array('slug' => $role_slug))->row()->id; - } - - /** - * Returns all the privileges (bool values) of a role slug. - * - * The privilege numbers are converted into bool values of the four main actions (view, - * add, edit, delete). By checking each value you can know if the user is able to perform - * this action. - * - * @param string $slug The role slug. - * @return array Returns the privilege value. - */ - public function get_privileges($slug) { - $privileges = $this->db->get_where('ea_roles', array('slug' => $slug))->row_array(); - unset($privileges['id'], $privileges['name'], $privileges['slug'], $privileges['is_admin']); - - // Convert the numeric values to bool so that is easier to check whether a - // user has the required privileges for a specific action. - foreach($privileges as &$value) { - $privileges_number = $value; - - $value = array( - 'view' => FALSE, - 'add' => FALSE, - 'edit' => FALSE, - 'delete' => FALSE - ); - - if ($privileges_number > 0) { - if (intval($privileges_number / PRIV_DELETE) == 1) { - $value['delete'] = TRUE; - $privileges_number -= PRIV_DELETE; - } - - if (intval($privileges_number / PRIV_EDIT) == 1) { - $value['edit'] = TRUE; - $privileges_number -= PRIV_EDIT; - } - - if (intval($privileges_number / PRIV_ADD) == 1) { - $value['add'] = TRUE; - $privileges_number -= PRIV_ADD; - } - - $value['view'] = TRUE; - } - } - - return $privileges; - } -} - -/* End of file roles_model.php */ -/* Location: ./application/models/roles_model.php */ \ No newline at end of file + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Roles Model + * + * @package Models + */ +class Roles_Model extends CI_Model { + /** + * Class Constructor + */ + public function __construct() { + parent::__construct(); + } + + /** + * Get the record id of a particular role. + * + * @param string $role_slug The selected role slug. Slugs are + * defined in the "application/config/constants.php" file. + * @return int Returns the database id of the roles record. + */ + public function get_role_id($role_slug) { + return $this->db->get_where('ea_roles', array('slug' => $role_slug))->row()->id; + } + + /** + * Returns all the privileges (bool values) of a role slug. + * + * The privilege numbers are converted into bool values of the four main actions (view, + * add, edit, delete). By checking each value you can know if the user is able to perform + * this action. + * + * @param string $slug The role slug. + * @return array Returns the privilege value. + */ + public function get_privileges($slug) { + $privileges = $this->db->get_where('ea_roles', array('slug' => $slug))->row_array(); + unset($privileges['id'], $privileges['name'], $privileges['slug'], $privileges['is_admin']); + + // Convert the numeric values to bool so that is easier to check whether a + // user has the required privileges for a specific action. + foreach($privileges as &$value) { + $privileges_number = $value; + + $value = array( + 'view' => FALSE, + 'add' => FALSE, + 'edit' => FALSE, + 'delete' => FALSE + ); + + if ($privileges_number > 0) { + if (intval($privileges_number / PRIV_DELETE) == 1) { + $value['delete'] = TRUE; + $privileges_number -= PRIV_DELETE; + } + + if (intval($privileges_number / PRIV_EDIT) == 1) { + $value['edit'] = TRUE; + $privileges_number -= PRIV_EDIT; + } + + if (intval($privileges_number / PRIV_ADD) == 1) { + $value['add'] = TRUE; + $privileges_number -= PRIV_ADD; + } + + $value['view'] = TRUE; + } + } + + return $privileges; + } +} + +/* End of file roles_model.php */ +/* Location: ./application/models/roles_model.php */ diff --git a/src/application/models/services_model.php b/src/application/models/services_model.php index 8e79c225..2f1538ed 100644 --- a/src/application/models/services_model.php +++ b/src/application/models/services_model.php @@ -1,375 +1,375 @@ - - * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis - * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 - * @link http://easyappointments.org - * @since v1.0.0 - * ---------------------------------------------------------------------------- */ - -/** - * Services Model - * - * @package Models - */ -class Services_Model extends CI_Model { - /** - * Class Constructor - */ - function __construct() { - 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) { - $this->validate($service); - - 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'); - - // 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; - } - - /** - * 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. - * - * @param numeric $service_id The record's id to be returned. - * @return array Returns an associative array with the selected - * record's data. Each key has the same name as the database - * field names. - */ - public function get_row($service_id) { - if (!is_numeric($service_id)) { - throw new Exception('$service_id argument is not an numeric (value: "' . $service_id . '")'); - } - return $this->db->get_where('ea_services', array('id' => $service_id))->row_array(); - } - - /** - * Get a specific field value from the database. - * - * @param string $field_name The field name of the value to be - * returned. - * @param int $service_id The selected record's id. - * @return string Returns the records value from the database. - */ - public function get_value($field_name, $service_id) { - if (!is_numeric($service_id)) { - throw new Exception('Invalid argument provided as $service_id : ' . $service_id); - } - - if (!is_string($field_name)) { - throw new Exception('$field_name argument is not a string : ' . $field_name); - } - - if ($this->db->get_where('ea_services', array('id' => $service_id))->num_rows() == 0) { - throw new Exception('The record with the $service_id argument does not exist in the database : ' . $service_id); - } - - $row_data = $this->db->get_where('ea_services', array('id' => $service_id))->row_array(); - if (!isset($row_data[$field_name])) { - throw new Exception('The given $field_name argument does not exist in the database : ' . $field_name); - } - - $setting = $this->db->get_where('ea_services', array('id' => $service_id))->row_array(); - return $setting[$field_name]; - } - - /** - * Get all, or specific records from service's table. - * - * @example $this->Model->getBatch('id = ' . $recordId); - * - * @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. - */ - public function get_batch($where_clause = NULL) { - if ($where_clause != NULL) { - $this->db->where($where_clause); - } - - return $this->db->get('ea_services')->result_array(); - } - - /** - * This method returns all the services from the database. - * - * @return array Returns an object array with all the - * database services. - */ - public function get_available_services() { - $this->db->distinct(); - return $this->db - ->select('ea_services.*, ea_service_categories.name AS category_name, ' - . 'ea_service_categories.id AS category_id') - ->from('ea_services') - ->join('ea_services_providers', - 'ea_services_providers.id_services = ea_services.id', 'inner') - ->join('ea_service_categories', - 'ea_service_categories.id = ea_services.id_service_categories', 'left') - ->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($where = '') { - if ($where !== '') $this->db->where($where); - 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 */ -/* Location: ./application/models/services_model.php */ \ No newline at end of file + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Services Model + * + * @package Models + */ +class Services_Model extends CI_Model { + /** + * Class Constructor + */ + function __construct() { + 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) { + $this->validate($service); + + 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'); + + // 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; + } + + /** + * 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. + * + * @param numeric $service_id The record's id to be returned. + * @return array Returns an associative array with the selected + * record's data. Each key has the same name as the database + * field names. + */ + public function get_row($service_id) { + if (!is_numeric($service_id)) { + throw new Exception('$service_id argument is not an numeric (value: "' . $service_id . '")'); + } + return $this->db->get_where('ea_services', array('id' => $service_id))->row_array(); + } + + /** + * Get a specific field value from the database. + * + * @param string $field_name The field name of the value to be + * returned. + * @param int $service_id The selected record's id. + * @return string Returns the records value from the database. + */ + public function get_value($field_name, $service_id) { + if (!is_numeric($service_id)) { + throw new Exception('Invalid argument provided as $service_id : ' . $service_id); + } + + if (!is_string($field_name)) { + throw new Exception('$field_name argument is not a string : ' . $field_name); + } + + if ($this->db->get_where('ea_services', array('id' => $service_id))->num_rows() == 0) { + throw new Exception('The record with the $service_id argument does not exist in the database : ' . $service_id); + } + + $row_data = $this->db->get_where('ea_services', array('id' => $service_id))->row_array(); + if (!isset($row_data[$field_name])) { + throw new Exception('The given $field_name argument does not exist in the database : ' . $field_name); + } + + $setting = $this->db->get_where('ea_services', array('id' => $service_id))->row_array(); + return $setting[$field_name]; + } + + /** + * Get all, or specific records from service's table. + * + * @example $this->Model->getBatch('id = ' . $recordId); + * + * @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. + */ + public function get_batch($where_clause = NULL) { + if ($where_clause != NULL) { + $this->db->where($where_clause); + } + + return $this->db->get('ea_services')->result_array(); + } + + /** + * This method returns all the services from the database. + * + * @return array Returns an object array with all the + * database services. + */ + public function get_available_services() { + $this->db->distinct(); + return $this->db + ->select('ea_services.*, ea_service_categories.name AS category_name, ' + . 'ea_service_categories.id AS category_id') + ->from('ea_services') + ->join('ea_services_providers', + 'ea_services_providers.id_services = ea_services.id', 'inner') + ->join('ea_service_categories', + 'ea_service_categories.id = ea_services.id_service_categories', 'left') + ->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($where = '') { + if ($where !== '') $this->db->where($where); + 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 */ +/* Location: ./application/models/services_model.php */ diff --git a/src/application/models/settings_model.php b/src/application/models/settings_model.php index 7e17290e..284684f4 100644 --- a/src/application/models/settings_model.php +++ b/src/application/models/settings_model.php @@ -1,149 +1,149 @@ - - * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis - * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 - * @link http://easyappointments.org - * @since v1.0.0 - * ---------------------------------------------------------------------------- */ - -/** - * Settings Model - * - * @package Models - */ -class Settings_Model extends CI_Model { - /** - * Class Constructor - */ - function __construct() { - parent::__construct(); - } - - /** - * Get setting value from database. - * - * This method returns a system setting from the - * database. - * - * @expectedException Exception - * - * @param string $name The database setting name. - * @return string Returns the database value for - * the selected setting. - */ - function get_setting($name) { - if (!is_string($name)) { // Check argument type. - throw new Exception('$name argument is not a string : ' . $name); - } - - if ($this->db->get_where('ea_settings', array('name' => $name))->num_rows() == 0) { // Check if setting exists in db. - throw new Exception('$name setting does not exist in database : ' . $name); - } - - $query = $this->db->get_where('ea_settings', array('name' => $name)); - $setting = ($query->num_rows() > 0) ? $query->row() : ''; - return $setting->value; - } - - /** - * This method sets the value for a specific setting - * on the database. If the setting doesn't exist, it - * is going to be created, otherwise updated. - * - * @expectedException Exception - * - * @param string $name The setting name. - * @param type $value The setting value. - * @return int Returns the setting database id. - */ - function set_setting($name, $value) { - if (!is_string($name)) { - throw new Exception('$name argument is not a string : ' . $name); - } - - $query = $this->db->get_where('ea_settings', array('name' => $name)); - if ($query->num_rows() > 0) { - // Update setting - if (!$this->db->update('ea_settings', array('value' => $value), array('name' => $name))) { - throw new Exception('Could not update database setting.'); - } - $setting_id = intval($this->db->get_where('ea_settings', array('name' => $name))->row()->id); - } else { - // Insert setting - $insert_data = array( - 'name' => $name, - 'value' => $value - ); - if (!$this->db->insert('ea_settings', $insert_data)) { - throw new Exception('Could not insert database setting'); - } - $setting_id = intval($this->db->insert_id()); - } - - return $setting_id; - } - - /** - * Remove a setting from the database. - * - * @expectedException Exception - * - * @param string $name The setting name to be removed. - * @return bool Returns the delete operation result. - */ - function remove_setting($name) { - if (!is_string($name)) { - throw new Exception('$name is not a string : ' . $name); - } - - if ($this->db->get_where('ea_settings', array('name' => $name))->num_rows() == 0) { - return FALSE; // There is no such setting. - } - - return $this->db->delete('ea_settings', array('name' => $name)); - } - - /** - * Saves all the system settings into the database. - * - * This method is usefull when trying to save all the system settings at once instead of - * saving them one by one. - * - * @param array $settings Contains all the system settings. - * @return bool Returns the save operation result. - * - * @throws Exception When the update operation won't work for a specific setting. - */ - public function save_settings($settings) { - if (!is_array($settings)) { - throw new Exception('$settings argument is invalid: '. print_r($settings, TRUE)); - } - - foreach($settings as $setting) { - $this->db->where('name', $setting['name']); - if (!$this->db->update('ea_settings', array('value' => $setting['value']))) { - throw new Exception('Could not save setting (' . $setting['name'] - . ' - ' . $setting['value'] . ')'); - } - } - - return TRUE; - } - - /** - * Returns all the system settings at once. - * - * @return array Array of all the system settings stored in the 'ea_settings' table. - */ - public function get_settings() { - return $this->db->get('ea_settings')->result_array(); - } -} - -/* End of file settings_model.php */ -/* Location: ./application/models/settings_model.php */ \ No newline at end of file + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Settings Model + * + * @package Models + */ +class Settings_Model extends CI_Model { + /** + * Class Constructor + */ + function __construct() { + parent::__construct(); + } + + /** + * Get setting value from database. + * + * This method returns a system setting from the + * database. + * + * @expectedException Exception + * + * @param string $name The database setting name. + * @return string Returns the database value for + * the selected setting. + */ + function get_setting($name) { + if (!is_string($name)) { // Check argument type. + throw new Exception('$name argument is not a string : ' . $name); + } + + if ($this->db->get_where('ea_settings', array('name' => $name))->num_rows() == 0) { // Check if setting exists in db. + throw new Exception('$name setting does not exist in database : ' . $name); + } + + $query = $this->db->get_where('ea_settings', array('name' => $name)); + $setting = ($query->num_rows() > 0) ? $query->row() : ''; + return $setting->value; + } + + /** + * This method sets the value for a specific setting + * on the database. If the setting doesn't exist, it + * is going to be created, otherwise updated. + * + * @expectedException Exception + * + * @param string $name The setting name. + * @param type $value The setting value. + * @return int Returns the setting database id. + */ + function set_setting($name, $value) { + if (!is_string($name)) { + throw new Exception('$name argument is not a string : ' . $name); + } + + $query = $this->db->get_where('ea_settings', array('name' => $name)); + if ($query->num_rows() > 0) { + // Update setting + if (!$this->db->update('ea_settings', array('value' => $value), array('name' => $name))) { + throw new Exception('Could not update database setting.'); + } + $setting_id = intval($this->db->get_where('ea_settings', array('name' => $name))->row()->id); + } else { + // Insert setting + $insert_data = array( + 'name' => $name, + 'value' => $value + ); + if (!$this->db->insert('ea_settings', $insert_data)) { + throw new Exception('Could not insert database setting'); + } + $setting_id = intval($this->db->insert_id()); + } + + return $setting_id; + } + + /** + * Remove a setting from the database. + * + * @expectedException Exception + * + * @param string $name The setting name to be removed. + * @return bool Returns the delete operation result. + */ + function remove_setting($name) { + if (!is_string($name)) { + throw new Exception('$name is not a string : ' . $name); + } + + if ($this->db->get_where('ea_settings', array('name' => $name))->num_rows() == 0) { + return FALSE; // There is no such setting. + } + + return $this->db->delete('ea_settings', array('name' => $name)); + } + + /** + * Saves all the system settings into the database. + * + * This method is usefull when trying to save all the system settings at once instead of + * saving them one by one. + * + * @param array $settings Contains all the system settings. + * @return bool Returns the save operation result. + * + * @throws Exception When the update operation won't work for a specific setting. + */ + public function save_settings($settings) { + if (!is_array($settings)) { + throw new Exception('$settings argument is invalid: '. print_r($settings, TRUE)); + } + + foreach($settings as $setting) { + $this->db->where('name', $setting['name']); + if (!$this->db->update('ea_settings', array('value' => $setting['value']))) { + throw new Exception('Could not save setting (' . $setting['name'] + . ' - ' . $setting['value'] . ')'); + } + } + + return TRUE; + } + + /** + * Returns all the system settings at once. + * + * @return array Array of all the system settings stored in the 'ea_settings' table. + */ + public function get_settings() { + return $this->db->get('ea_settings')->result_array(); + } +} + +/* End of file settings_model.php */ +/* Location: ./application/models/settings_model.php */ diff --git a/src/application/models/user_model.php b/src/application/models/user_model.php index 1f506ea1..ea90c3d5 100644 --- a/src/application/models/user_model.php +++ b/src/application/models/user_model.php @@ -1,155 +1,155 @@ - - * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis - * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 - * @link http://easyappointments.org - * @since v1.0.0 - * ---------------------------------------------------------------------------- */ - -/** - * User Model - * - * Contains current user's methods. - * - * @package Model - */ -class User_Model extends CI_Model { - /** - * Class Constructor - */ - public function __construct() { - parent::__construct(); - } - - /** - * Returns the user settings from the database. - * - * @param numeric $user_id User record id of which the settings will be returned. - * @return array Returns an array with user settings. - */ - public function get_settings($user_id) { - $user = $this->db->get_where('ea_users', array('id' => $user_id))->row_array(); - $user['settings'] = $this->db->get_where('ea_user_settings', array('id_users' => $user_id))->row_array(); - unset($user['settings']['id_users']); - return $user; - } - - /** - * This method saves the user settings into the database. - * - * @param array $user Contains the current users settings. - * @return bool Returns the operation result. - */ - public function save_settings($user) { - $user_settings = $user['settings']; - $user_settings['id_users'] = $user['id']; - unset($user['settings']); - - // Prepare user password (hash). - if (isset($user_settings['password'])) { - $this->load->helper('general'); - $salt = $this->db->get_where('ea_user_settings', array('id_users' => $user['id']))->row()->salt; - $user_settings['password'] = hash_password($salt, $user_settings['password']); - } - - if (!$this->db->update('ea_users', $user, array('id' => $user['id']))) { - return FALSE; - } - - if (!$this->db->update('ea_user_settings', $user_settings, array('id_users' => $user['id']))) { - return FALSE; - } - - return TRUE; - } - - /** - * Retrieve user's salt from database. - * - * @param string $username This will be used to find the user record. - * @return string Returns the salt db value. - */ - public function get_salt($username) { - $user = $this->db->get_where('ea_user_settings', array('username' => $username))->row_array(); - return ($user) ? $user['salt'] : ''; - } - - /** - * Performs the check of the given user credentials. - * - * @param string $username Given user's name. - * @param type $password Given user's password (not hashed yet). - * @return array|null Returns the session data of the logged in user or null on - * failure. - */ - public function check_login($username, $password) { - $this->load->helper('general'); - $salt = $this->user_model->get_salt($username); - $password = hash_password($salt, $password); - - $user_data = $this->db - ->select('ea_users.id AS user_id, ea_users.email AS user_email, ' - . 'ea_roles.slug AS role_slug, ea_user_settings.username') - ->from('ea_users') - ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner') - ->join('ea_user_settings', 'ea_user_settings.id_users = ea_users.id') - ->where('ea_user_settings.username', $username) - ->where('ea_user_settings.password', $password) - ->get()->row_array(); - - return ($user_data) ? $user_data : NULL; - } - - /** - * Get the given user's display name (first + last name). - * - * @param numeric $user_id The given user record id. - * @return string Returns the user display name. - */ - public function get_user_display_name($user_id) { - if (!is_numeric($user_id)) - throw new Exception ('Invalid argument given ($user_id = "' . $user_id . '").'); - $user = $this->db->get_where('ea_users', array('id' => $user_id))->row_array(); - return $user['first_name'] . ' ' . $user['last_name']; - } - - /** - * If the given arguments correspond to an existing user record, generate a new - * password and send it with an email. - * - * @param string $username - * @param string $email - * @return string|bool Returns the new password on success or FALSE on failure. - */ - public function regenerate_password($username, $email) { - $this->load->helper('general'); - - $result = $this->db - ->select('ea_users.id') - ->from('ea_users') - ->join('ea_user_settings', 'ea_user_settings.id_users = ea_users.id', 'inner') - ->where('ea_users.email', $email) - ->where('ea_user_settings.username', $username) - ->get(); - - if ($result->num_rows() == 0) return FALSE; - - $user_id = $result->row()->id; - - // Create a new password and send it with an email to the given email address. - $new_password = generate_random_string(); - $salt = $this->db->get_where('ea_user_settings', array('id_users' => $user_id))->row()->salt; - $hash_password = hash_password($salt, $new_password); - $this->db->update('ea_user_settings', array('password' => $hash_password), array('id_users' => $user_id)); - - return $new_password; - } -} - -/* End of file user_model.php */ -/* Location: ./application/models/user_model.php */ \ No newline at end of file + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * User Model + * + * Contains current user's methods. + * + * @package Model + */ +class User_Model extends CI_Model { + /** + * Class Constructor + */ + public function __construct() { + parent::__construct(); + } + + /** + * Returns the user settings from the database. + * + * @param numeric $user_id User record id of which the settings will be returned. + * @return array Returns an array with user settings. + */ + public function get_settings($user_id) { + $user = $this->db->get_where('ea_users', array('id' => $user_id))->row_array(); + $user['settings'] = $this->db->get_where('ea_user_settings', array('id_users' => $user_id))->row_array(); + unset($user['settings']['id_users']); + return $user; + } + + /** + * This method saves the user settings into the database. + * + * @param array $user Contains the current users settings. + * @return bool Returns the operation result. + */ + public function save_settings($user) { + $user_settings = $user['settings']; + $user_settings['id_users'] = $user['id']; + unset($user['settings']); + + // Prepare user password (hash). + if (isset($user_settings['password'])) { + $this->load->helper('general'); + $salt = $this->db->get_where('ea_user_settings', array('id_users' => $user['id']))->row()->salt; + $user_settings['password'] = hash_password($salt, $user_settings['password']); + } + + if (!$this->db->update('ea_users', $user, array('id' => $user['id']))) { + return FALSE; + } + + if (!$this->db->update('ea_user_settings', $user_settings, array('id_users' => $user['id']))) { + return FALSE; + } + + return TRUE; + } + + /** + * Retrieve user's salt from database. + * + * @param string $username This will be used to find the user record. + * @return string Returns the salt db value. + */ + public function get_salt($username) { + $user = $this->db->get_where('ea_user_settings', array('username' => $username))->row_array(); + return ($user) ? $user['salt'] : ''; + } + + /** + * Performs the check of the given user credentials. + * + * @param string $username Given user's name. + * @param type $password Given user's password (not hashed yet). + * @return array|null Returns the session data of the logged in user or null on + * failure. + */ + public function check_login($username, $password) { + $this->load->helper('general'); + $salt = $this->user_model->get_salt($username); + $password = hash_password($salt, $password); + + $user_data = $this->db + ->select('ea_users.id AS user_id, ea_users.email AS user_email, ' + . 'ea_roles.slug AS role_slug, ea_user_settings.username') + ->from('ea_users') + ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner') + ->join('ea_user_settings', 'ea_user_settings.id_users = ea_users.id') + ->where('ea_user_settings.username', $username) + ->where('ea_user_settings.password', $password) + ->get()->row_array(); + + return ($user_data) ? $user_data : NULL; + } + + /** + * Get the given user's display name (first + last name). + * + * @param numeric $user_id The given user record id. + * @return string Returns the user display name. + */ + public function get_user_display_name($user_id) { + if (!is_numeric($user_id)) + throw new Exception ('Invalid argument given ($user_id = "' . $user_id . '").'); + $user = $this->db->get_where('ea_users', array('id' => $user_id))->row_array(); + return $user['first_name'] . ' ' . $user['last_name']; + } + + /** + * If the given arguments correspond to an existing user record, generate a new + * password and send it with an email. + * + * @param string $username + * @param string $email + * @return string|bool Returns the new password on success or FALSE on failure. + */ + public function regenerate_password($username, $email) { + $this->load->helper('general'); + + $result = $this->db + ->select('ea_users.id') + ->from('ea_users') + ->join('ea_user_settings', 'ea_user_settings.id_users = ea_users.id', 'inner') + ->where('ea_users.email', $email) + ->where('ea_user_settings.username', $username) + ->get(); + + if ($result->num_rows() == 0) return FALSE; + + $user_id = $result->row()->id; + + // Create a new password and send it with an email to the given email address. + $new_password = generate_random_string(); + $salt = $this->db->get_where('ea_user_settings', array('id_users' => $user_id))->row()->salt; + $hash_password = hash_password($salt, $new_password); + $this->db->update('ea_user_settings', array('password' => $hash_password), array('id_users' => $user_id)); + + return $new_password; + } +} + +/* End of file user_model.php */ +/* Location: ./application/models/user_model.php */ diff --git a/src/application/views/backend/header.php b/src/application/views/backend/header.php index 22f70e0c..fbb4fc49 100644 --- a/src/application/views/backend/header.php +++ b/src/application/views/backend/header.php @@ -1,140 +1,140 @@ - - - - <?php echo $company_name; ?> | Easy!Appointments - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + + + + <?php echo $company_name; ?> | Easy!Appointments + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/application/views/emails/appointment_details.php b/src/application/views/emails/appointment_details.php index 79120a90..229fff7b 100644 --- a/src/application/views/emails/appointment_details.php +++ b/src/application/views/emails/appointment_details.php @@ -1,71 +1,71 @@ - - - Appointment Details - - -
- - -
-

$email_title

-

$email_message

- -

Appointment Details

- - - - - - - - - - - - - - - - - -
Service$appointment_service
Provider$appointment_provider
Start$appointment_start_date
End$appointment_end_date
- -

Customer Details

- - - - - - - - - - - - - - - - - -
Name$customer_name
Email$customer_email
Phone$customer_phone
Address$customer_address
- -

Appointment Link

- $appointment_link -
- - -
- - \ No newline at end of file + + + Appointment Details + + +
+ + +
+

$email_title

+

$email_message

+ +

Appointment Details

+ + + + + + + + + + + + + + + + + +
Service$appointment_service
Provider$appointment_provider
Start$appointment_start_date
End$appointment_end_date
+ +

Customer Details

+ + + + + + + + + + + + + + + + + +
Name$customer_name
Email$customer_email
Phone$customer_phone
Address$customer_address
+ +

Appointment Link

+ $appointment_link +
+ + +
+ + diff --git a/src/application/views/emails/delete_appointment.php b/src/application/views/emails/delete_appointment.php index c1da5563..0079aab6 100644 --- a/src/application/views/emails/delete_appointment.php +++ b/src/application/views/emails/delete_appointment.php @@ -1,71 +1,71 @@ - - - $email_title - - -
- - -
-

$email_title

-

$email_message

- -

Appointment Details

- - - - - - - - - - - - - - - - - -
Service$appointment_service
Provider$appointment_provider
Date$appointment_date
Duration$appointment_duration
- -

Customer Details

- - - - - - - - - - - - - - - - - -
Name$customer_name
Email$customer_email
Phone$customer_phone
Address$customer_address
- -

Reason

-

$reason

-
- - -
- - \ No newline at end of file + + + $email_title + + +
+ + +
+

$email_title

+

$email_message

+ +

Appointment Details

+ + + + + + + + + + + + + + + + + +
Service$appointment_service
Provider$appointment_provider
Date$appointment_date
Duration$appointment_duration
+ +

Customer Details

+ + + + + + + + + + + + + + + + + +
Name$customer_name
Email$customer_email
Phone$customer_phone
Address$customer_address
+ +

Reason

+

$reason

+
+ + +
+ + diff --git a/src/application/views/emails/new_password.php b/src/application/views/emails/new_password.php index b1d7a0c3..09a1b217 100644 --- a/src/application/views/emails/new_password.php +++ b/src/application/views/emails/new_password.php @@ -1,28 +1,28 @@ - - - New Account Password - - -
- - -
-

$email_title

-

$email_message

-
- - -
- - \ No newline at end of file + + + New Account Password + + +
+ + +
+

$email_title

+

$email_message

+
+ + +
+ + diff --git a/src/application/views/general/test.php b/src/application/views/general/test.php index 16467583..498e4fa9 100644 --- a/src/application/views/general/test.php +++ b/src/application/views/general/test.php @@ -1,45 +1,45 @@ - - - - - -
- Easy!Appointments Unit Testing - -
\ No newline at end of file + + + + + +
+ Easy!Appointments Unit Testing + +
diff --git a/src/application/views/user/forgot_password.php b/src/application/views/user/forgot_password.php index 4bbad350..5d290705 100644 --- a/src/application/views/user/forgot_password.php +++ b/src/application/views/user/forgot_password.php @@ -1,149 +1,149 @@ - - - - - - - - <?php echo $this->lang->line('forgot_your_password') . ' - ' . $company_name; ?> - - - - - - - - - - - - - - - - - - - -
-

lang->line('forgot_your_password'); ?>

-

lang->line('type_username_and_email_for_new_password'); ?>

-
- -
-
- - -
-
- - -
- -
- - - - -
-
- - - \ No newline at end of file + + + + + + + + <?php echo $this->lang->line('forgot_your_password') . ' - ' . $company_name; ?> + + + + + + + + + + + + + + + + + + + +
+

lang->line('forgot_your_password'); ?>

+

lang->line('type_username_and_email_for_new_password'); ?>

+
+ +
+
+ + +
+
+ + +
+ +
+ + + + +
+
+ + + diff --git a/src/application/views/user/login.php b/src/application/views/user/login.php index 066dea8e..c2b7da38 100644 --- a/src/application/views/user/login.php +++ b/src/application/views/user/login.php @@ -1,156 +1,156 @@ - - - - - - - - <?php echo $this->lang->line('login') . ' - ' . $company_name; ?> - - - - - - - - - - - - - - - - - - - -
-

lang->line('backend_section'); ?>

-

lang->line('you_need_to_login'); ?>

-
- -
-
- - -
-
- - -
-
- - - -

- - - lang->line('forgot_your_password'); ?> - | - - config->item('language')); ?> - -
-
- - - - \ No newline at end of file + + + + + + + + <?php echo $this->lang->line('login') . ' - ' . $company_name; ?> + + + + + + + + + + + + + + + + + + + +
+

lang->line('backend_section'); ?>

+

lang->line('you_need_to_login'); ?>

+
+ +
+
+ + +
+
+ + +
+
+ + + +

+ + + lang->line('forgot_your_password'); ?> + | + + config->item('language')); ?> + +
+
+ + + + diff --git a/src/application/views/user/logout.php b/src/application/views/user/logout.php index c204151d..8754f20c 100644 --- a/src/application/views/user/logout.php +++ b/src/application/views/user/logout.php @@ -1,85 +1,85 @@ - - - - - - - - <?php echo $this->lang->line('log_out') . ' - ' . $company_name; ?> - - - - - - - - - - - - - - - - -
-

lang->line('log_out'); ?>

-

- lang->line('logout_success'); ?> -

- -
- - - - lang->line('book_appointment_title'); ?> - - - - - lang->line('backend_section'); ?> - -
- - \ No newline at end of file + + + + + + + + <?php echo $this->lang->line('log_out') . ' - ' . $company_name; ?> + + + + + + + + + + + + + + + + +
+

lang->line('log_out'); ?>

+

+ lang->line('logout_success'); ?> +

+ +
+ + + + lang->line('book_appointment_title'); ?> + + + + + lang->line('backend_section'); ?> + +
+ + diff --git a/src/application/views/user/no_privileges.php b/src/application/views/user/no_privileges.php index 66470009..3c486559 100644 --- a/src/application/views/user/no_privileges.php +++ b/src/application/views/user/no_privileges.php @@ -1,79 +1,79 @@ - - - - - - - - <?php echo $this->lang->line('no_privileges') . ' - ' . $company_name; ?> - - - - - - - - - - - - - - -
-

lang->line('no_privileges'); ?>

-

- lang->line('no_provileges_message'); ?> -

- -
- - - - lang->line('backend_calendar'); ?> - -
- - \ No newline at end of file + + + + + + + + <?php echo $this->lang->line('no_privileges') . ' - ' . $company_name; ?> + + + + + + + + + + + + + + +
+

lang->line('no_privileges'); ?>

+

+ lang->line('no_provileges_message'); ?> +

+ +
+ + + + lang->line('backend_calendar'); ?> + +
+ +