1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3 // Google API PHP Client is necessary to perform sync operations.
4 require_once dirname(__FILE__) . '/external/google-api-php-client/Google_Client.php';
5 require_once dirname(__FILE__) . '/external/google-api-php-client/contrib/Google_CalendarService.php';
6 require_once dirname(dirname(dirname(__FILE__))) . '/configuration.php';
7
8 /**
9 * Google Synchronization Class
10 *
11 * This class implements all the core synchronization between the Google Calendar
12 * and the Easy!Appointments system. Do not place any model handling inside this
13 * library.
14 */
15 class Google_Sync {
16 private $CI;
17 private $client;
18 private $service;
19
20 /**
21 * Class Constructor
22 *
23 * This method initializes the Google client class and the Calendar service
24 * class so that they can be used by the other methods.
25 */
26 public function __construct() {
27 $this->CI =& get_instance();
28
29 if (!isset($_SESSION)) {
30 @session_start();
31 }
32
33 // Initialize google client and calendar service.
34 $this->client = new Google_Client();
35 $this->client->setUseObjects(true);
36
37 $this->client->setApplicationName(SystemConfiguration::$google_product_name);
38 $this->client->setClientId(SystemConfiguration::$google_client_id);
39 $this->client->setClientSecret(SystemConfiguration::$google_client_secret);
40 $this->client->setDeveloperKey(SystemConfiguration::$google_api_key);
41 $this->client->setRedirectUri($this->CI->config->item('base_url') . '/google/oauth_callback');
42
43 $this->service = new Google_CalendarService($this->client);
44 }
45
46 /**
47 * Get Google OAuth authorization url.
48 *
49 * This url must be used to redirect the user to the Google user consent page,
50 * where the user grants access to his data for the Easy!Appointments app.
51 */
52 public function get_auth_url() {
53 // "max_auth_age" is needed because the user needs to always log in
54 // and not use an existing session.
55 return $this->client->createAuthUrl() . '&max_auth_age=0';
56 }
57
58 /**
59 * Authenticate the Google API usage.
60 *
61 * When the user grants consent for his data usage, google is going to redirect
62 * the browser back to the given redirect url. There a authentication code is
63 * provided. Using this code, we can authenticate the API usage and store the
64 * token information to the database.
65 *
66 * @see Google Controller
67 */
68 public function authenticate($auth_code) {
69 $this->client->authenticate($auth_code);
70 return $this->client->getAccessToken();
71 }
72
73 /**
74 * Refresh the Google Client access token.
75 *
76 * This method must be executed every time we need to make actions on a
77 * provider's Google Calendar account. A new token is necessary and the
78 * only way to get it is to use the stored refresh token that was provided
79 * when the provider granted consent to Easy!Appointments for use his
80 * Google Calendar account.
81 *
82 * @param string $refresh_token The provider's refresh token. This value is
83 * stored in the database and used every time we need to make actions to his
84 * Google Caledar account.
85 */
86 public function refresh_token($refresh_token) {
87 $this->client->refreshToken($refresh_token);
88 }
89
90 /**
91 * Add an appointment record to its providers Google Calendar account.
92 *
93 * This method checks whether the appointment's provider has enabled the Google
94 * Sync utility of Easy!Appointments and the stored access token is still valid.
95 * If yes, the selected appointment record is going to be added to the Google
96 * Calendar account.
97 *
98 * @param array $appointment Contains the appointment record data.
99 * @param array $provider Contains the provider record data.
100 * @param array $service Contains the service record data.
101 * @param array $customer Contains the customer recod data.
102 * @parma array $company_settings Contains some company settings that are used
103 * by this method. By the time the following values must be in the array:
104 * 'company_name'.
105 * @return Google_Event Returns the Google_Event class object.
106 */
107 public function add_appointment($appointment, $provider, $service, $customer, $company_settings) {
108 $this->CI->load->helper('general');
109
110 $event = new Google_Event();
111 $event->setSummary(($service != NULL) ? $service['name'] : 'Unavailable');
112 $event->setLocation($company_settings['company_name']);
113
114 $start = new Google_EventDateTime();
115 $start->setDateTime(date3339(strtotime($appointment['start_datetime'])));
116 $event->setStart($start);
117
118 $end = new Google_EventDateTime();
119 $end->setDateTime(date3339(strtotime($appointment['end_datetime'])));
120 $event->setEnd($end);
121
122 $event->attendees = array();
123
124 $event_provider = new Google_EventAttendee();
125 $event_provider->setDisplayName($provider['first_name'] . ' '
126 . $provider['last_name']);
127 $event_provider->setEmail($provider['email']);
128 $event->attendees[] = $event_provider;
129
130 if ($customer != NULL) {
131 $event_customer = new Google_EventAttendee();
132 $event_customer->setDisplayName($customer['first_name'] . ' '
133 . $customer['last_name']);
134 $event_customer->setEmail($customer['email']);
135 $event->attendees[] = $event_customer;
136 }
137
138 // Add the new event to the google calendar.
139 $created_event = $this->service->events->insert($provider['settings']['google_calendar'], $event);
140
141 return $created_event;
142 }
143
144 /**
145 * Update an existing appointment that is already synced with Google Calendar.
146 *
147 * This method updates the google calendar event item that is connected with the
148 * provided appointment record of Easy!Appointments.
149 *
150 * @param array $appointment Contains the appointment record data.
151 * @param array $provider Contains the provider record data.
152 * @param array $service Contains the service record data.
153 * @param array $customer Contains the customer recod data.
154 * @parma array $company_settings Contains some company settings that are used
155 * by this method. By the time the following values must be in the array:
156 * 'company_name'.
157 * @return Google_Event Returns the Google_Event class object.
158 */
159 public function update_appointment($appointment, $provider, $service, $customer, $company_settings) {
160 $this->CI->load->helper('general');
161
162 $event = $this->service->events->get($provider['settings']['google_calendar'], $appointment['id_google_calendar']);
163
164 $event->setSummary($service['name']);
165 $event->setLocation($company_settings['company_name']);
166
167 $start = new Google_EventDateTime();
168 $start->setDateTime(date3339(strtotime($appointment['start_datetime'])));
169 $event->setStart($start);
170
171 $end = new Google_EventDateTime();
172 $end->setDateTime(date3339(strtotime($appointment['end_datetime'])));
173 $event->setEnd($end);
174
175 $event->attendees = array();
176
177 $event_provider = new Google_EventAttendee();
178 $event_provider->setDisplayName($provider['first_name'] . ' '
179 . $provider['last_name']);
180 $event_provider->setEmail($provider['email']);
181 $event->attendees[] = $event_provider;
182
183 if ($customer != NULL) {
184 $event_customer = new Google_EventAttendee();
185 $event_customer->setDisplayName($customer['first_name'] . ' '
186 . $customer['last_name']);
187 $event_customer->setEmail($customer['email']);
188 $event->attendees[] = $event_customer;
189 }
190
191 $updated_event = $this->service->events->update($provider['settings']['google_calendar'],
192 $event->getId(), $event);
193
194 return $updated_event;
195 }
196
197 /**
198 * Delete an existing appointment from Google Calendar.
199 *
200 * @param array $provider Contains the provider record data.
201 * @param string $google_event_id The Google Calendar event id to
202 * be deleted.
203 */
204 public function delete_appointment($provider, $google_event_id) {
205 $this->service->events->delete($provider['settings']['google_calendar'], $google_event_id);
206 }
207
208 /**
209 * Add unavailable period event to Google Calendar.
210 *
211 * @param array $provider Contains the provider record data.
212 * @param array $unavailable Contains unavailable period's data.
213 * @return Google_Event Returns the google event's object.
214 */
215 public function add_unavailable($provider, $unavailable) {
216 $this->CI->load->helper('general');
217
218 $event = new Google_Event();
219 $event->setSummary('Unavailable');
220 $event->setDescription($unavailable['notes']);
221
222 $start = new Google_EventDateTime();
223 $start->setDateTime(date3339(strtotime($unavailable['start_datetime'])));
224 $event->setStart($start);
225
226 $end = new Google_EventDateTime();
227 $end->setDateTime(date3339(strtotime($unavailable['end_datetime'])));
228 $event->setEnd($end);
229
230 // Add the new event to the google calendar.
231 $created_event = $this->service->events->insert($provider['settings']['google_calendar'], $event);
232
233 return $created_event;
234
235 }
236
237 /**
238 * Update Google Calendar unavailable period event.
239 *
240 * @param array $provider Contains the provider record data.
241 * @param array $unavailable Contains the unavailable period data.
242 * @return Google_Event Returns the Google_Event object.
243 */
244 public function update_unavailable($provider, $unavailable) {
245 $this->CI->load->helper('general');
246
247 $event = $this->service->events->get($provider['settings']['google_calendar'], $unavailable['id_google_calendar']);
248 $event->setDescription($unavailable['notes']);
249
250 $start = new Google_EventDateTime();
251 $start->setDateTime(date3339(strtotime($unavailable['start_datetime'])));
252 $event->setStart($start);
253
254 $end = new Google_EventDateTime();
255 $end->setDateTime(date3339(strtotime($unavailable['end_datetime'])));
256 $event->setEnd($end);
257
258 $updated_event = $this->service->events->update($provider['settings']['google_calendar'],
259 $event->getId(), $event);
260
261 return $updated_event;
262 }
263
264 /**
265 * Delete unavailable period event from Google Calendar.
266 *
267 * @param array $provider Contains the provider record data.
268 * @param string $google_event_id Google Calendar event id to be deleted.
269 */
270 public function delete_unavailable($provider, $google_event_id) {
271 $this->service->events->delete($provider['settings']['google_calendar'], $google_event_id);
272 }
273
274 /**
275 * Get an event object from gcal
276 *
277 * @param array $provider Contains the provider record data.
278 * @param string $google_event_id Id of the google calendar event
279 * @return Google_Event Returns the google event object.
280 */
281 public function get_event($provider, $google_event_id) {
282 return $this->service->events->get($provider['settings']['google_calendar'], $google_event_id);
283 }
284
285 /**
286 * Get all the events between the sync period.
287 *
288 * @param string $google_calendar The name of the google calendar to be used.
289 * @param date $start The start date of sync period.
290 * @param date $end The end date of sync period.
291 * @return object Returns an array with Google_Event objects that belong on the given
292 * sync period (start, end).
293 */
294 public function get_sync_events($google_calendar, $start, $end) {
295 $this->CI->load->helper('general');
296
297 $params = array(
298 'timeMin' => date3339($start),
299 'timeMax' => date3339($end)
300 );
301
302 return $this->service->events->listEvents($google_calendar, $params);
303 }
304
305 /**
306 * Return available google calendars for specific user.
307 *
308 * The given user's token must already exist in db in order to get access to his
309 * Google Calendar account.
310 *
311 * @param string $google_token The user's token will be used to grant access to google calendar.
312 * @return array Returns an array with the available calendars.
313 */
314 public function get_google_calendars() {
315 $calendarList = $this->service->calendarList->listCalendarList();
316 $calendars = array();
317 foreach ($calendarList->items as $google_calendar) {
318 $calendars[] = array(
319 'id' => $google_calendar->id,
320 'summary' => $google_calendar->summary
321 );
322 }
323 return $calendars;
324 }
325 }
326
327 /* End of file google_sync.php */
328 /* Location: ./application/libraries/google_sync.php */