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