diff --git a/src/application/controllers/backend.php b/src/application/controllers/backend.php index 8f9f0a82..63c0b940 100644 --- a/src/application/controllers/backend.php +++ b/src/application/controllers/backend.php @@ -112,18 +112,6 @@ class Backend extends CI_Controller { if (isset($_POST['appointment_data'])) { $appointment_data = json_decode(stripcslashes($_POST['appointment_data']), true); $this->Appointments_Model->add($appointment_data); - - if ($appointment_data['id_google_calendar'] != NULL) { - $this->load->model('Providers_Model'); - $provider_settings = json_decode($this->Providers_Model - ->get_setting('google_token', $appointment_data['id_users_provider'])); - - if ($provider_settings->google_sync == TRUE) { - $this->load->library('Google_Sync'); - $this->google_sync->refresh_token($provider_settings->refresh_token); - $this->google_sync->update_appointment($appointment_data['id']); - } - } } // :: SAVE CUSTOMER CHANGES TO DATABASE @@ -132,35 +120,38 @@ class Backend extends CI_Controller { $this->Customers_Model->add($customer_data); } + $appointment_data = $this->Appointments_Model->get_row($appointment_data['id']); + $provider_data = $this->Providers_Model->get_row($appointment_data['id_users_provider']); + $customer_data = $this->Customers_Model->get_row($appointment_data['id_users_customer']); + $service_data = $this->Services_Model->get_row($appointment_data['id_services']); + $company_settings = array( + 'company_name' => $this->Settings_Model->get_setting('company_name') + ); + // :: SYNC APPOINTMENT CHANGES WITH GOOGLE CALENDAR if ($appointment_data['id_google_calendar'] != NULL) { $google_sync = $this->Providers_Model ->get_setting('google_sync', $appointment_data['id_users_provider']); if ($google_sync == TRUE) { - $google_token = $this->Providers_Model - ->get_setting('google_token', $appointment_data['id_users_provider']); + $google_token = json_decode($this->Providers_Model + ->get_setting('google_token', $appointment_data['id_users_provider'])); $this->load->library('Google_Sync'); $this->google_sync->refresh_token($google_token->refresh_token); $this->google_sync->update_appointment($appointment_data, $provider_data, - $service_data, $customer_data); + $service_data, $customer_data, $company_settings); } } // :: SEND EMAIL NOTIFICATIONS TO PROVIDER AND CUSTOMER $this->load->library('Notifications'); - try { - $customer_title = 'Appointment Changes Saved Successfully!'; - $provider_title = 'Appointment Details Have Changed'; - - $this->notifications->send_book_success( - $customer_data, $appointment_data, $customer_title); - $this->notifications->send_new_appointment( - $customer_data, $appointment_data, $provider_title); - - } catch(NotificationException $nt_exc) { - // @task Display message to the user that the notification messages could - // not be sent. - } + + $customer_title = 'Appointment Changes Saved Successfully!'; + $provider_title = 'Appointment Details Have Changed'; + + $this->notifications->send_book_success( + $customer_data, $appointment_data, $customer_title); + $this->notifications->send_new_appointment( + $customer_data, $appointment_data, $provider_title); echo json_encode('SUCCESS'); @@ -201,29 +192,33 @@ class Backend extends CI_Controller { $company_settings = array( 'company_name' => $this->Settings_Model->get_setting('company_name'), 'company_email' => $this->Settings_Model->get_setting('company_email'), - 'company_link' => $this->Settings_Model->get_setting('company_link') + 'company_link' => $this->Settings_Model->get_setting('company_link') ); // :: DELETE APPOINTMENT RECORD FROM DATABASE. $this->Appointments_Model->delete($_POST['appointment_id']); - // :: SYNC CHANGE TO GOOGLE CALENDAR - $google_sync = $this->Providers_Model->get_setting('google_sync', - $provider_data['id']); - - if ($google_sync == TRUE) { - $google_token = json_decode($this->Providers_Model->get_setting('google_token', - $provider_data['id'])); - - $this->load->library('Google_Sync'); - $this->google_sync->refresh_token($google_token->refresh_token); - $this->google_sync->delete_appointment($appointment_data['id_google_calendar']); + // :: SYNC DELETE WITH GOOGLE CALENDAR + if ($appointment_data['id_google_calendar'] != NULL) { + $google_sync = $this->Providers_Model->get_setting('google_sync', + $provider_data['id']); + + if ($google_sync == TRUE) { + $google_token = json_decode($this->Providers_Model->get_setting('google_token', + $provider_data['id'])); + + $this->load->library('Google_Sync'); + $this->google_sync->refresh_token($google_token->refresh_token); + $this->google_sync->delete_appointment($appointment_data['id_google_calendar']); + } } // :: SEND NOTIFICATION EMAILS TO PROVIDER AND CUSTOMER. $this->load->library('Notifications'); - $this->notification->send_remove_appointment($appointment_data, $provider_data, - $service_data, $customer_data, $company_settings); + $this->notifications->send_remove_appointment($appointment_data, $provider_data, + $service_data, $customer_data, $company_settings, $provider_data['email']); + $this->notifications->send_remove_appointment($appointment_data, $provider_data, + $service_data, $customer_data, $company_settings, $customer_data['email']); echo json_encode('SUCCESS'); diff --git a/src/application/libraries/google_sync.php b/src/application/libraries/google_sync.php index 2c571dcb..68d75140 100644 --- a/src/application/libraries/google_sync.php +++ b/src/application/libraries/google_sync.php @@ -31,11 +31,14 @@ class Google_Sync { // Initialize google client and calendar service. $this->client = new Google_Client(); + $this->client->setUseObjects(true); + $this->client->setApplicationName(SystemConfiguration::$google_product_name); $this->client->setClientId(SystemConfiguration::$google_client_id); $this->client->setClientSecret(SystemConfiguration::$google_client_secret); $this->client->setDeveloperKey(SystemConfiguration::$google_api_key); $this->client->setRedirectUri($this->CI->config->item('base_url') . 'google/oauth_callback'); + $this->service = new Google_CalendarService($this->client); } @@ -165,11 +168,49 @@ class Google_Sync { * @param array $provider_data Contains the provider record data. * @param array $service_data Contains the service record data. * @param array $customer_data 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_data, $provider_data, - $service_data, $customer_data) { + $service_data, $customer_data, $company_settings) { $this->CI->load->helper('general'); + $event = $this->service->events->get('primary', $appointment_data['id_google_calendar']); + + // Convert event to object + + $event->setSummary($service_data['name']); + $event->setLocation($company_settings['company_name']); + + $start = new Google_EventDateTime(); + $start->setDateTime(date3339(strtotime($appointment_data['start_datetime']))); + $event->setStart($start); + + $end = new Google_EventDateTime(); + $end->setDateTime(date3339(strtotime($appointment_data['end_datetime']))); + $event->setEnd($end); + + $event_provider = new Google_EventAttendee(); + $event_provider->setDisplayName($provider_data['first_name'] . ' ' + . $provider_data['last_name']); + $event_provider->setEmail($provider_data['email']); + + $event_customer = new Google_EventAttendee(); + $event_customer->setDisplayName($customer_data['first_name'] . ' ' + . $customer_data['last_name']); + $event_customer->setEmail($customer_data['email']); + + $event->attendees = array( + $event_provider, + $event_customer + ); + + $updated_event = $this->service->events->update('primary', $event->getId(), $event); + + return $updated_event; } /** diff --git a/src/application/libraries/notifications.php b/src/application/libraries/notifications.php index 82885aac..2b91f460 100644 --- a/src/application/libraries/notifications.php +++ b/src/application/libraries/notifications.php @@ -183,18 +183,18 @@ class Notifications { '$appointment_provider' => $provider_data['first_name'] . ' ' . $provider_data['last_name'], '$appointment_date' => date('d/m/Y H:i', strtotime($appointment_data['start_datetime'])), '$appointment_duration' => $service_data['duration'] . ' minutes', - '$company_link' => $company_settings('company_link'), - '$company_name' => $company_settings('company_name') + '$company_link' => $company_settings['company_link'], + '$company_name' => $company_settings['company_name'] ); $email_html = file_get_contents(dirname(dirname(__FILE__)) - . '/views/emails/cancel_appointment.php'); + . '/views/emails/remove_appointment.php'); $email_html = $this->replace_template_variables($replace_array, $email_html); // :: SETUP EMAIL OBJECT AND SEND NOTIFICATION $mail = new PHPMailer(); $mail->From = $company_settings['company_email']; - $mail->FromName = $company_settings['$company_name']; + $mail->FromName = $company_settings['company_name']; $mail->AddAddress($to_address); // "Name" argument crushes the phpmailer class. $mail->IsHTML(true); $mail->CharSet = 'UTF-8'; diff --git a/src/application/views/backend/calendar.php b/src/application/views/backend/calendar.php index 3576dca7..03b51d7a 100644 --- a/src/application/views/backend/calendar.php +++ b/src/application/views/backend/calendar.php @@ -42,6 +42,11 @@ Enable Sync + +