From 36718c87f9a1e53b593355ce3dece9476531fd09 Mon Sep 17 00:00:00 2001 From: "alextselegidis@gmail.com" Date: Mon, 24 Jun 2013 09:52:23 +0000 Subject: [PATCH] =?UTF-8?q?=CE=A0=CF=81=CE=BF=CF=83=CE=B8=CE=AE=CE=BA?= =?UTF-8?q?=CE=B7=20=CE=B4=CF=85=CE=BD=CE=B1=CF=84=CF=8C=CF=84=CE=B7=CF=84?= =?UTF-8?q?=CE=B1=CF=82=20=CF=83=CF=85=CE=B3=CF=87=CF=81=CE=BF=CE=BD=CE=B9?= =?UTF-8?q?=CF=83=CE=BC=CE=BF=CF=8D=20=CE=BC=CE=B5=CF=84=CE=AC=20=CE=B1?= =?UTF-8?q?=CF=80=CF=8C=20=CE=B1=CE=BB=CE=BB=CE=B1=CE=B3=CE=AE=20=CF=83?= =?UTF-8?q?=CF=84=CE=B1=20=CF=83=CF=84=CE=BF=CF=87=CE=B5=CE=AF=CE=B1=20?= =?UTF-8?q?=CE=B5=CE=BD=CF=8C=CF=82=20=CF=81=CE=B1=CE=BD=CF=84=CE=B5=CE=B2?= =?UTF-8?q?=CE=BF=CF=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/application/controllers/backend.php | 79 ++++++++++----------- src/application/libraries/google_sync.php | 43 ++++++++++- src/application/libraries/notifications.php | 8 +-- src/application/views/backend/calendar.php | 5 ++ src/assets/js/backend_calendar.js | 14 +++- 5 files changed, 100 insertions(+), 49 deletions(-) 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 + +
diff --git a/src/assets/js/backend_calendar.js b/src/assets/js/backend_calendar.js index 275022ad..0d02c220 100644 --- a/src/assets/js/backend_calendar.js +++ b/src/assets/js/backend_calendar.js @@ -133,6 +133,16 @@ var BackendCalendar = { } }); + /** + * Event: Reload Button "Click" + * + * When the user clicks the reload button an the calendar items need to + * be refreshed. + */ + $('#reload-appointments').click(function() { + $('#select-filter-item').trigger('change'); + }); + /** * Event: Popover Close Button "Click" * @@ -513,8 +523,8 @@ var BackendCalendar = { }, error : function(jqXHR, textStatus, errorThrown) { ////////////////////////////////////////////////////////////////// - //console.log('Update Appointment Data Error:', jqXHR, textStatus, - // errorThrown); + console.log('Update Appointment Data Error:', jqXHR, textStatus, + errorThrown); ////////////////////////////////////////////////////////////////// if (errorCallback !== undefined) {