2013-04-12 19:36:09 +03:00
< ? php if ( ! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );
class Appointments extends CI_Controller {
/**
2013-04-14 22:42:40 +03:00
* This page displays the book appointment wizard
* for the customers .
2013-04-12 19:36:09 +03:00
*/
2013-04-17 00:37:36 +03:00
public function index () {
2013-04-20 20:20:16 +03:00
if ( strtoupper ( $_SERVER [ 'REQUEST_METHOD' ]) != 'POST' ) {
// Display the appointment booking page to the customer.
// Get business name.
$this -> load -> model ( 'Settings_Model' );
2013-05-04 00:26:04 +03:00
$view_data [ 'business_name' ] = $this -> Settings_Model -> get_setting ( 'business_name' );
2013-04-20 20:20:16 +03:00
// Get the available services and providers.
$this -> load -> model ( 'Services_Model' );
2013-05-04 00:26:04 +03:00
$view_data [ 'available_services' ] = $this -> Services_Model -> get_available_services ();
2013-04-20 20:20:16 +03:00
$this -> load -> model ( 'Providers_Model' );
2013-05-04 00:26:04 +03:00
$view_data [ 'available_providers' ] = $this -> Providers_Model -> get_available_providers (); // Provider rows contain an array of which services they can provide.
2013-04-20 20:20:16 +03:00
// Load the book appointment view.
2013-05-04 00:26:04 +03:00
$this -> load -> view ( 'appointments/book' , $view_data );
2013-04-20 20:20:16 +03:00
} else {
2013-05-04 00:26:04 +03:00
$post_data = json_decode ( $_POST [ 'post_data' ], true );
2013-04-20 20:20:16 +03:00
2013-05-04 00:26:04 +03:00
// Add customer
2013-04-20 20:20:16 +03:00
$this -> load -> model ( 'Customers_Model' );
2013-05-04 00:26:04 +03:00
$customer_id = $this -> Customers_Model -> add ( $post_data [ 'customer' ]);
2013-04-20 20:20:16 +03:00
2013-05-04 00:26:04 +03:00
// Add appointment
$post_data [ 'appointment' ][ 'id_users_customer' ] = $customer_id ;
2013-04-20 20:20:16 +03:00
$this -> load -> model ( 'Appointments_Model' );
2013-05-04 00:26:04 +03:00
$view_data [ 'appointment_id' ] = $this -> Appointments_Model -> add ( $post_data [ 'appointment' ]);
2013-04-20 20:20:16 +03:00
// Send an email to the customer with the appointment info.
2013-05-04 00:26:04 +03:00
$this -> load -> library ( 'Notifications' );
$this -> notifications -> send_book_success ( $post_data [ 'customer' ], $post_data [ 'appointment' ]);
$this -> notifications -> send_new_appointment ( $post_data [ 'customer' ], $post_data [ 'appointment' ]);
2013-04-20 20:20:16 +03:00
// Load the book appointment view.
2013-05-04 00:26:04 +03:00
$this -> load -> view ( 'appointments/book_success' , $view_data );
2013-04-20 20:20:16 +03:00
}
2013-04-12 19:36:09 +03:00
}
2013-04-17 00:37:36 +03:00
/**
* This method answers to an AJAX request . It calculates the
* available hours for the given service , provider and date .
*
2013-05-04 00:26:04 +03:00
* @ param array $_POST [ 'post_data' ] An associative array that
2013-04-17 00:37:36 +03:00
* contains the user selected service , provider and date .
*/
2013-05-04 00:26:04 +03:00
public function ajax_get_available_hours () {
2013-04-17 00:37:36 +03:00
/*** CHECK BUSINESS WORKING HOURS ***/
/*** CHECK PROVIDERS WORKING HOURS ***/
/*** CHECK ALREADY BOOKED APPOINTMENTS ***/
/*** RETURN AVAILABLE HOURS ***/
// ------------------------------------------------------------------
// For now we just need to return a sample array. Dynamic calculation
// will be adding in future development.
2013-05-04 00:26:04 +03:00
$start_time = strtotime ( $_POST [ 'selected_date' ] . ' 08:00' ) / 60 ; // Convert to minutes
$end_time = strtotime ( $_POST [ 'selected_date' ] . ' 19:00' ) / 60 ; // Convert to minutes
2013-04-17 00:37:36 +03:00
2013-05-04 00:26:04 +03:00
for ( $i = 0 ; ( $start_time * 60 + $i * 60 ) <= ( $end_time * 60 ); $i += intval ( $_POST [ 'service_duration' ])) {
$available_hours [] = date ( 'H:i' , $start_time * 60 + $i * 60 );
2013-04-17 00:37:36 +03:00
}
2013-04-20 20:20:16 +03:00
// If the selected date is today, remove past hours.
2013-05-04 00:26:04 +03:00
if ( date ( 'm/d/Y' , strtotime ( $_POST [ 'selected_date' ])) == date ( 'm/d/Y' )) {
foreach ( $available_hours as $index => $value ) {
$available_hour = date ( 'm/d/Y H:i' , strtotime ( $value ));
$current_hour = date ( 'm/d/Y H:i' );
2013-04-20 20:20:16 +03:00
2013-05-04 00:26:04 +03:00
if ( $available_hour < $current_hour ) {
unset ( $available_hours [ $index ]);
2013-04-20 20:20:16 +03:00
}
}
}
2013-05-04 00:26:04 +03:00
$available_hours = array_values ( $available_hours );
echo json_encode ( $available_hours );
}
/**
* Synchronize appointment with its ' providers google calendar .
*
* This method syncs the registered appointment with the
* google calendar of the user .
*
* @ task This method needs to be changed . Everytime a customer
* books a new appointment the synchronization process must be
* executed .
*/
public function google_sync ( $appointment_id ) {
try {
$this -> load -> library ( 'Google_Sync' );
$this -> google_sync -> sync_appointment ( $appointment_id );
$view_data [ 'message' ] = 'Your appointment has been successfully added to Google Calendar!' ;
$view_data [ 'image' ] = 'success.png' ;
} catch ( Exception $exc ) {
$view_data [ 'message' ] = 'An unexpected error occured during the sync operation: <br/><pre>' . $exc -> getMessage ()
. '<br/>' . $exc -> getTraceAsString () . '</pre>' ;
$view_data [ 'image' ] = 'error.png' ;
}
2013-04-20 20:20:16 +03:00
2013-05-04 00:26:04 +03:00
$this -> load -> view ( 'appointments/google_sync' , $view_data );
2013-04-17 00:37:36 +03:00
}
2013-04-12 19:36:09 +03:00
}
2013-05-04 00:26:04 +03:00
/* End of file appointments.php */
/* Location: ./application/controllers/appointments.php */