diff --git a/db/easy_appointments.sql b/db/easy_appointments.sql index 8f234e56..da86ab81 100644 --- a/db/easy_appointments.sql +++ b/db/easy_appointments.sql @@ -3,7 +3,7 @@ -- http://www.phpmyadmin.net -- -- Φιλοξενητής: localhost --- Χρόνος δημιουργίας: 14 Απρ 2013 στις 19:41:00 +-- Χρόνος δημιουργίας: 20 Απρ 2013 στις 20:18:59 -- Έκδοση διακομιστή: 5.5.24-log -- Έκδοση PHP: 5.4.3 @@ -30,7 +30,7 @@ CREATE TABLE IF NOT EXISTS `ea_appointments` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `start_datetime` datetime DEFAULT NULL, `end_datetime` datetime DEFAULT NULL, - `comments` text, + `notes` text, `id_users_provider` bigint(20) unsigned NOT NULL, `id_users_customer` bigint(20) unsigned NOT NULL, `id_services` bigint(20) unsigned NOT NULL, @@ -38,7 +38,7 @@ CREATE TABLE IF NOT EXISTS `ea_appointments` ( KEY `id_users_customer` (`id_users_customer`), KEY `id_services` (`id_services`), KEY `id_users_provider` (`id_users_provider`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=18 ; -- -------------------------------------------------------- @@ -49,6 +49,7 @@ CREATE TABLE IF NOT EXISTS `ea_appointments` ( CREATE TABLE IF NOT EXISTS `ea_roles` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(256) DEFAULT NULL, + `slug` varchar(256) DEFAULT NULL, `is_admin` tinyint(4) DEFAULT NULL COMMENT '0', `services` int(4) DEFAULT NULL COMMENT '0', `providers` int(4) DEFAULT NULL COMMENT '0', @@ -56,14 +57,16 @@ CREATE TABLE IF NOT EXISTS `ea_roles` ( `notifications` int(4) DEFAULT NULL COMMENT '0', `appointments` int(4) DEFAULT NULL COMMENT '0', PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ; -- -- Άδειασμα δεδομένων του πίνακα `ea_roles` -- -INSERT INTO `ea_roles` (`id`, `name`, `is_admin`, `services`, `providers`, `customers`, `notifications`, `appointments`) VALUES -(1, 'Administrator', 1, 15, 15, 15, 15, 15); +INSERT INTO `ea_roles` (`id`, `name`, `slug`, `is_admin`, `services`, `providers`, `customers`, `notifications`, `appointments`) VALUES +(1, 'Administrator', 'administrator', 1, 15, 15, 15, 15, 15), +(2, 'Provider', 'provider', 0, 0, 0, 15, 0, 15), +(3, 'Customer', 'customer', 0, 0, 0, 0, 0, 0); -- -------------------------------------------------------- @@ -81,14 +84,43 @@ CREATE TABLE IF NOT EXISTS `ea_services` ( `id_service_categories` bigint(20) unsigned DEFAULT NULL, PRIMARY KEY (`id`), KEY `id_service_categories` (`id_service_categories`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ; -- -- Άδειασμα δεδομένων του πίνακα `ea_services` -- INSERT INTO `ea_services` (`id`, `name`, `duration`, `price`, `currency`, `description`, `id_service_categories`) VALUES -(1, 'General Examination', 20, '50.00', 'euro', 'Sample Description ...', NULL); +(1, 'Γενική Εξέταση', 20, '50.00', 'euro', 'Γενική εξέταση του ασθενή.', NULL), +(2, 'Εξέταση Καρδιάς', 30, '40.00', 'euro', 'Εξέταση του ασθενή για νοσήματα καρδιάς.', NULL), +(3, 'Νευρολογική Εξέταση', 20, '35.00', 'euro', 'Νευρολογική εξέταση του ασθενή.', NULL); + +-- -------------------------------------------------------- + +-- +-- Δομή πίνακα για τον πίνακα `ea_services_providers` +-- + +CREATE TABLE IF NOT EXISTS `ea_services_providers` ( + `id_users` bigint(20) unsigned NOT NULL, + `id_services` bigint(20) unsigned NOT NULL, + PRIMARY KEY (`id_users`,`id_services`), + KEY `id_services` (`id_services`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- +-- Άδειασμα δεδομένων του πίνακα `ea_services_providers` +-- + +INSERT INTO `ea_services_providers` (`id_users`, `id_services`) VALUES +(1, 1), +(2, 1), +(3, 1), +(4, 1), +(3, 2), +(4, 2), +(2, 3), +(3, 3); -- -------------------------------------------------------- @@ -114,14 +146,16 @@ CREATE TABLE IF NOT EXISTS `ea_settings` ( `name` varchar(512) DEFAULT NULL, `value` longtext, PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ; -- -- Άδειασμα δεδομένων του πίνακα `ea_settings` -- INSERT INTO `ea_settings` (`id`, `name`, `value`) VALUES -(1, 'business_name', 'Javation & Co'); +(1, 'business_name', 'Javation & Co'), +(2, 'business_working_hours', '{}'), +(3, NULL, NULL); -- -------------------------------------------------------- @@ -146,14 +180,17 @@ CREATE TABLE IF NOT EXISTS `ea_users` ( `id_roles` bigint(20) unsigned NOT NULL, PRIMARY KEY (`id`), KEY `id_roles` (`id_roles`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=35 ; -- -- Άδειασμα δεδομένων του πίνακα `ea_users` -- INSERT INTO `ea_users` (`id`, `username`, `password`, `first_name`, `last_name`, `email`, `mobile_number`, `phone_number`, `address`, `city`, `state`, `zip_code`, `notes`, `id_roles`) VALUES -(1, 'admin', 'admin', 'Alex', 'Tselegidis', 'alextselegidis@gmail.com', '123456789', '987654321', 'Pantazopoulou 11', 'Thessaloniki', NULL, '56121', 'This is me making Easy!Appointments', 1); +(1, 'admin', 'admin', 'Alex', 'Tselegidis', 'alextselegidis@gmail.com', '123456789', '987654321', 'Pantazopoulou 11', 'Thessaloniki', NULL, '56121', 'This is me making Easy!Appointments', 1), +(2, 'provider_1', 'provider_1', 'Γιώργος', 'Παπαδόπουλος', 'prov1@testing.gr', '1212121212', '2121212121', 'John Doe 23', 'Washington DC', NULL, '12345', 'This is a test provider', 2), +(3, 'provider_2', 'provider_2', 'Νίκος', 'Αναστασίου', 'prov2@test.gr', '1313133113131', '32132165146', 'Some Street 3', NULL, NULL, NULL, NULL, 2), +(4, 'provider_3', 'provider_3', 'Ηρώ', 'Καριοφύλη', 'prov3@test.gr', '239203490', '029340923', 'John Doe 3 ', NULL, NULL, NULL, NULL, 2); -- -- Περιορισμοί για άχρηστους πίνακες @@ -173,6 +210,13 @@ ALTER TABLE `ea_appointments` ALTER TABLE `ea_services` ADD CONSTRAINT `ea_services_ibfk_1` FOREIGN KEY (`id_service_categories`) REFERENCES `ea_service_categories` (`id`) ON DELETE CASCADE ON UPDATE CASCADE; +-- +-- Περιορισμοί για πίνακα `ea_services_providers` +-- +ALTER TABLE `ea_services_providers` + ADD CONSTRAINT `ea_services_providers_ibfk_1` FOREIGN KEY (`id_users`) REFERENCES `ea_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, + ADD CONSTRAINT `ea_services_providers_ibfk_2` FOREIGN KEY (`id_services`) REFERENCES `ea_services` (`id`) ON DELETE CASCADE ON UPDATE CASCADE; + -- -- Περιορισμοί για πίνακα `ea_users` -- diff --git a/src/application/controllers/appointments.php b/src/application/controllers/appointments.php index 3d6a4618..3c8861e5 100644 --- a/src/application/controllers/appointments.php +++ b/src/application/controllers/appointments.php @@ -6,19 +6,62 @@ class Appointments extends CI_Controller { * for the customers. */ public function index() { - // Get business name. - $this->load->model('Settings'); - $viewData['businessName'] = $this->Settings->getSetting('business_name'); - - // Get the available services and providers. - $this->load->model('Services'); - $viewData['availableServices'] = $this->Services->getAvailableServices(); - - $this->load->model('Providers'); - $viewData['availableProviders'] = $this->Providers->getAvailableProviders(); // Provider rows contain an array of which services they can provide. - - // Load the book appointment view. - $this->load->view('appointments/book', $viewData); + if (strtoupper($_SERVER['REQUEST_METHOD']) != 'POST') { + // Display the appointment booking page to the customer. + // Get business name. + $this->load->model('Settings_Model'); + $viewData['businessName'] = $this->Settings_Model->getSetting('business_name'); + + // Get the available services and providers. + $this->load->model('Services_Model'); + $viewData['availableServices'] = $this->Services_Model->getAvailableServices(); + + $this->load->model('Providers_Model'); + $viewData['availableProviders'] = $this->Providers_Model->getAvailableProviders(); // Provider rows contain an array of which services they can provide. + + // Load the book appointment view. + $this->load->view('appointments/book', $viewData); + } else { + // Add the appointment to the database and display the success + // page to the customer. + $customerData = array( + 'first_name' => $_POST['firstName'], + 'last_name' => $_POST['lastName'], + 'email' => $_POST['email'], + 'phone_number' => $_POST['phoneNumber'], + 'address' => $_POST['address'], + 'city' => $_POST['city'], + 'zip_code' => $_POST['zipCode'] + ); + + $this->load->model('Customers_Model'); + $customerId = $this->Customers_Model->add($customerData); + + $appointmentData = array( + 'start_datetime' => $_POST['startDatetime'], + 'end_datetime' => $_POST['endDatetime'], + 'notes' => $_POST['notes'], + 'id_users_provider' => $_POST['providerId'], + 'id_users_customer' => $customerId, + 'id_services' => $_POST['serviceId'], + ); + + $this->load->model('Appointments_Model'); + $this->Appointments_Model->add($appointmentData); + + // Send an email to the customer with the appointment info. + $to = $customerData['email']; + $subject = 'Appointment Book Success'; + $message = 'Hi there! Your appointment has been successfully booked.'; + $headers = 'From: alextselegidis@gmail.com' . "\r\n" . + 'Reply-To: alextselegidis@gmail.com' . "\r\n" . + 'X-Mailer: PHP/' . phpversion(); + + mail($to, $subject, $message, $headers); + + // Load the book appointment view. + $this->load->view('appointments/book-success'); + } } /** @@ -38,12 +81,26 @@ class Appointments extends CI_Controller { // For now we just need to return a sample array. Dynamic calculation // will be adding in future development. $startTime = strtotime($_POST['selectedDate'] . ' 08:00') / 60; // Convert to minutes - $endTime = strtotime($_POST['selectedDate'] . ' 16:00') / 60; // Convert to minutes + $endTime = strtotime($_POST['selectedDate'] . ' 19:00') / 60; // Convert to minutes for ($i=0; ($startTime*60 + $i*60)<=($endTime*60); $i+=intval($_POST['serviceDuration'])) { $availableHours[] = date('H:i', $startTime * 60 + $i * 60); } + // If the selected date is today, remove past hours. + if (date('m/d/Y', strtotime($_POST['selectedDate'])) == date('m/d/Y')) { + foreach($availableHours as $index=>$value) { + $availableHour = date('m/d/Y H:i', strtotime($value)); + $currentHour = date('m/d/Y H:i'); + + if ($availableHour < $currentHour) { + unset($availableHours[$index]); + } + } + } + + $availableHours = array_values($availableHours); + echo json_encode($availableHours); } } diff --git a/src/application/models/appointments.php b/src/application/models/appointments.php deleted file mode 100644 index b6eddc13..00000000 --- a/src/application/models/appointments.php +++ /dev/null @@ -1,7 +0,0 @@ - diff --git a/src/application/models/appointments_model.php b/src/application/models/appointments_model.php new file mode 100644 index 00000000..dc56e58c --- /dev/null +++ b/src/application/models/appointments_model.php @@ -0,0 +1,36 @@ +exists($appointmentData)) { + $this->insert($appointmentData); + } else { + $this->update($appointmentData); + } + } catch (Exception $exc) { + echo $exc->getTraceAsString(); + } + } + + public function exists($appointmentData) { + return false; // @task This check is going to be more complicated than just checking an email (customers model) + } + + private function insert($appointmentData) { + $this->db->insert('ea_appointments', $appointmentData); + } + + private function update($appointmentData) { + $this->db->where('id', $appointmentData['id']); + $this->db->update('ea_appointments', $appointmentData); + } + + public function delete($appointmentId) { + + } +} +?> diff --git a/src/application/models/customers.php b/src/application/models/customers.php deleted file mode 100644 index b6eddc13..00000000 --- a/src/application/models/customers.php +++ /dev/null @@ -1,7 +0,0 @@ - diff --git a/src/application/models/customers_model.php b/src/application/models/customers_model.php new file mode 100644 index 00000000..91a3f7c1 --- /dev/null +++ b/src/application/models/customers_model.php @@ -0,0 +1,84 @@ +exists($customerData['email'])) { + $this->insert($customerData); + } else { + $this->update($customerData); + } + } catch(Exception $exc) { + // Send some info for the exception back to the browser. + } + + return $this->db->insert_id(); + } + + /** + * This method checks if a customer exists on the database. + * The unique identifier for a customer is his email. + * + * @param string $customerEmail The customers email. + * @return bool Returns the operation result. + */ + public function exists($customerEmail) { + return false; // @task This should be implemented soon. + } + + /** + * This method inserts a customer into the database. + * + * @param array $customerData Associative array with the customers + * data. The key of the array should be the same as the database + * field names. + */ + private function insert($customerData) { + $this->db + ->select('id') + ->from('ea_roles') + ->where('slug', 'customer'); + + $customerRoleId = $this->db->get()->row()->id; + + if ($customerRoleId !== NULL) { + $customerData['id_roles'] = $customerRoleId; + } + + if (!$this->db->insert('ea_users', $customerData)) { + throw new Exception('Could not insert customer to the database.'); + } + } + + private function update($customerData) { + $this->db->where('email', $customerData['email']); + if (!$this->db->update('ea_users', $customerData)) { + throw new Exception('Could not update customer to the database.'); + } + } + + /** + * This method deletes a customers from the database. + * All his appointmets will be deleted too (automatically + * through the foreign key constraint). + * + * @param int $customerId The id of the customer to be + * deleted. + */ + public function delete($customerId) { + + } +} + + + +?> diff --git a/src/application/models/providers.php b/src/application/models/providers_model.php similarity index 55% rename from src/application/models/providers.php rename to src/application/models/providers_model.php index 7182fe3e..5468a036 100644 --- a/src/application/models/providers.php +++ b/src/application/models/providers_model.php @@ -1,5 +1,5 @@ db + ->select('ea_users.*') + ->from('ea_users') + ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner') + ->where('ea_roles.slug', 'provider'); - $providers = $this->db->query($sql)->result_array(); + $providers = $this->db->get()->result_array(); foreach($providers as &$provider) { - $sql = ' - SELECT id_services - FROM ea_services_providers - WHERE id_users = ' . $this->db->escape($provider['id']); + $this->db + ->select('id_services') + ->from('ea_services_providers') + ->where('id_users', $provider['id']); - $providerServices = $this->db->query($sql)->result_array(); + $providerServices = $this->db->get()->result_array(); if (!isset($provider['services'])) { $provider['services'] = array(); diff --git a/src/application/models/services.php b/src/application/models/services_model.php similarity index 86% rename from src/application/models/services.php rename to src/application/models/services_model.php index 800acf33..a7aab045 100644 --- a/src/application/models/services.php +++ b/src/application/models/services_model.php @@ -1,5 +1,5 @@ db->get_where('ea_settings', array('name' => $name)); $setting = ($query->num_rows() > 0) ? $query->row() : ''; return $setting->value; @@ -30,8 +28,7 @@ class Settings extends CI_Model { * @return bool Returns the operation success - failure * result. */ - function setSetting($name, $value) - { + function setSetting($name, $value) { $query = $this->db->get_where('ea_settings', array('name' => $name)); if ($query->num_rows() > 0) { // Update setting diff --git a/src/application/views/appointments/book-success.php b/src/application/views/appointments/book-success.php new file mode 100644 index 00000000..65eb7fc7 --- /dev/null +++ b/src/application/views/appointments/book-success.php @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + +
+ +

Your appointment has been successfully registered.

+

An email with the appointment details has been sented to you.

+
+ + \ No newline at end of file diff --git a/src/application/views/appointments/book.php b/src/application/views/appointments/book.php index f4c7d206..eb2a4da5 100644 --- a/src/application/views/appointments/book.php +++ b/src/application/views/appointments/book.php @@ -16,150 +16,25 @@ + + + + + @@ -188,97 +63,120 @@
-

Select Service & Provider

- - +
+

Select Service & Provider

+
+ + - - + + +
+
- +