1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3 class Google extends CI_Controller {
4 public function __construct() {
5 parent::__construct();
6 }
7
8 9 10 11 12 13 14 15 16
17 public function oauth($provider_id) {
18
19 if (!isset($_SESSION)) {
20 @session_start();
21 }
22 $_SESSION['oauth_provider_id'] = $provider_id;
23
24
25 $this->load->library('Google_Sync');
26 header('Location: ' . $this->google_sync->get_auth_url());
27 }
28
29 30 31 32 33 34 35 36 37 38 39 40 41 42
43 public function oauth_callback() {
44 if (isset($_GET['code'])) {
45 $this->load->library('Google_Sync');
46 $token = $this->google_sync->authenticate($_GET['code']);
47
48
49 if (!isset($_SESSION)) {
50 @session_start();
51 }
52
53 if (isset($_SESSION['oauth_provider_id'])) {
54 $this->load->model('providers_model');
55 $this->providers_model->set_setting('google_sync', TRUE,
56 $_SESSION['oauth_provider_id']);
57 $this->providers_model->set_setting('google_token', $token,
58 $_SESSION['oauth_provider_id']);
59 $this->providers_model->set_setting('google_calendar', 'primary',
60 $_SESSION['oauth_provider_id']);
61 } else {
62 echo '<h1>Sync provider id not specified!</h1>';
63 }
64 } else {
65 echo '<h1>Authorization Failed!</h1>';
66 }
67 }
68
69 70 71 72 73 74 75 76 77
78 public function sync($provider_id = NULL) {
79 try {
80
81 $this->load->library('session');
82 if ($this->session->userdata('user_id') == FALSE) return;
83
84 if ($provider_id === NULL) {
85 throw new Exception('Provider id not specified.');
86 }
87
88 $this->load->model('appointments_model');
89 $this->load->model('providers_model');
90 $this->load->model('services_model');
91 $this->load->model('customers_model');
92 $this->load->model('settings_model');
93
94 $provider = $this->providers_model->get_row($provider_id);
95
96
97 $google_sync = $this->providers_model->get_setting('google_sync', $provider['id']);
98 if (!$google_sync) {
99 throw new Exception('The selected provider has not the google synchronization '
100 . 'setting enabled.');
101 }
102
103 $google_token = json_decode($this->providers_model->get_setting('google_token', $provider['id']));
104 $this->load->library('google_sync');
105 $this->google_sync->refresh_token($google_token->refresh_token);
106
107
108 $sync_past_days = $this->providers_model->get_setting('sync_past_days', $provider['id']);
109 $sync_future_days = $this->providers_model->get_setting('sync_future_days', $provider['id']);
110 $start = strtotime('-' . $sync_past_days . ' days', strtotime(date('Y-m-d')));
111 $end = strtotime('+' . $sync_future_days . ' days', strtotime(date('Y-m-d')));
112
113 $where_clause = array(
114 'start_datetime >=' => date('Y-m-d H:i:s', $start),
115 'end_datetime <=' => date('Y-m-d H:i:s', $end),
116 'id_users_provider' => $provider['id']
117 );
118
119 $appointments = $this->appointments_model->get_batch($where_clause);
120
121 $company_settings = array(
122 'company_name' => $this->settings_model->get_setting('company_name'),
123 'company_link' => $this->settings_model->get_setting('company_link'),
124 'company_email' => $this->settings_model->get_setting('company_email')
125 );
126
127
128
129 foreach($appointments as $appointment) {
130 if ($appointment['is_unavailable'] == FALSE) {
131 $service = $this->services_model->get_row($appointment['id_services']);
132 $customer = $this->customers_model->get_row($appointment['id_users_customer']);
133 } else {
134 $service = NULL;
135 $customer = NULL;
136 }
137
138
139 if ($appointment['id_google_calendar'] == NULL) {
140 $google_event = $this->google_sync->add_appointment($appointment, $provider,
141 $service, $customer, $company_settings);
142 $appointment['id_google_calendar'] = $google_event->id;
143 $this->appointments_model->add($appointment);
144 } else {
145
146 try {
147 $google_event = $this->google_sync->get_event($provider, $appointment['id_google_calendar']);
148
149 if ($google_event->status == 'cancelled') {
150 throw new Exception('Event is cancelled, remove the record from Easy!Appointments.');
151 }
152
153
154 $is_different = FALSE;
155 $appt_start = strtotime($appointment['start_datetime']);
156 $appt_end = strtotime($appointment['end_datetime']);
157 $event_start = strtotime($google_event->getStart()->getDateTime());
158 $event_end = strtotime($google_event->getEnd()->getDateTime());
159
160 if ($appt_start != $event_start || $appt_end != $event_end) {
161 $is_different = TRUE;
162 }
163
164 if ($is_different) {
165 $appointment['start_datetime'] = date('Y-m-d H:i:s', $event_start);
166 $appointment['end_datetime'] = date('Y-m-d H:i:s', $event_end);
167 $this->appointments_model->add($appointment);
168 }
169
170 } catch(Exception $exc) {
171
172 $this->appointments_model->delete($appointment['id']);
173 $appointment['id_google_calendar'] = NULL;
174 }
175 }
176 }
177
178
179 $google_calendar = $provider['settings']['google_calendar'];
180 $events = $this->google_sync->get_sync_events($google_calendar, $start, $end);
181
182 foreach($events->getItems() as $event) {
183 $results = $this->appointments_model->get_batch(array('id_google_calendar' => $event->getId()));
184 if (count($results) == 0) {
185
186 $appointment = array(
187 'start_datetime' => date('Y-m-d H:i:s', strtotime($event->start->getDateTime())),
188 'end_datetime' => date('Y-m-d H:i:s', strtotime($event->end->getDateTime())),
189 'is_unavailable' => TRUE,
190 'notes' => $event->getSummary() . ' ' . $event->getDescription(),
191 'id_users_provider' => $provider_id,
192 'id_google_calendar' => $event->getId(),
193 'id_users_customer' => NULL,
194 'id_services' => NULL,
195 );
196
197 $this->appointments_model->add($appointment);
198 }
199 }
200
201 echo json_encode(AJAX_SUCCESS);
202
203 } catch(Exception $exc) {
204 echo json_encode(array(
205 'exceptions' => array($exc)
206 ));
207 }
208 }
209 }
210
211
212