Google Calendar integration for booking notifications
- New GoogleCalendar library: createEvent/updateEvent/deleteEvent via service account, all wrapped in try/catch so failures never break booking flow - booking_process: creates worker + owner calendar events on new booking - manage_booking_process: deletes old events, creates new ones on modify - manage_booking_cancel: deletes events before cancellation - Service_model: updateBookingCalEvents() stores gcal event IDs - Admin worker form: Google Calendar ID field added - PHPMailer: enabled exceptions (was silently swallowing SMTP errors) - Config: application/config/google_calendar.php for service account path + Evelin calendar ID DB migration required: ALTER TABLE workers ADD COLUMN google_calendar_id VARCHAR(255) NULL DEFAULT NULL; ALTER TABLE bookings ADD COLUMN gcal_event_id_worker VARCHAR(255) NULL DEFAULT NULL; ALTER TABLE bookings ADD COLUMN gcal_event_id_owner VARCHAR(255) NULL DEFAULT NULL; Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -437,7 +437,29 @@ class Pages extends CI_Controller {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Google Calendar: create events for worker and owner
|
||||
$this->load->library('GoogleCalendar');
|
||||
$this->config->load('google_calendar');
|
||||
$newBooking = $this->Service_model->getBookingByToken($manageToken);
|
||||
if($newBooking){
|
||||
$gcalServiceNames = implode(', ', array_map(function($s){ return $s->service_name_no; }, $serviceArray));
|
||||
$gcalTitle = $worker->worker_name . ' — ' . $gcalServiceNames . ' — ' . $bookingArray['guest_name'];
|
||||
$gcalDescription = 'Guest: ' . $bookingArray['guest_name'] . "\nPhone: " . $bookingArray['guest_phone'] . "\nEmail: " . $bookingArray['guest_email'];
|
||||
$gcalStart = $bookingArray['booking_date'] . 'T' . $bookingArray['booking_start_time'];
|
||||
$gcalEnd = $bookingArray['booking_date'] . 'T' . $bookingArray['booking_finish_time'];
|
||||
$workerEventId = null;
|
||||
$ownerEventId = null;
|
||||
if(!empty($worker->google_calendar_id)){
|
||||
$workerEventId = $this->googlecalendar->createEvent($worker->google_calendar_id, $gcalTitle, $gcalDescription, $gcalStart, $gcalEnd);
|
||||
}
|
||||
$evelinCalId = $this->config->item('gcal_evelin_calendar_id');
|
||||
if(!empty($evelinCalId)){
|
||||
$ownerEventId = $this->googlecalendar->createEvent($evelinCalId, $gcalTitle, $gcalDescription, $gcalStart, $gcalEnd);
|
||||
}
|
||||
$this->Service_model->updateBookingCalEvents($newBooking->booking_id, $workerEventId, $ownerEventId);
|
||||
}
|
||||
|
||||
$content = '';
|
||||
|
||||
switch($_POST['lang']){
|
||||
@@ -832,6 +854,32 @@ class Pages extends CI_Controller {
|
||||
if(is_object($sel)) $serviceArray[] = $sel;
|
||||
}
|
||||
|
||||
// Google Calendar: delete old events, create new ones
|
||||
$this->load->library('GoogleCalendar');
|
||||
$this->config->load('google_calendar');
|
||||
$evelinCalId = $this->config->item('gcal_evelin_calendar_id');
|
||||
$oldWorker = $this->Service_model->getWorkerById($booking->worker_id);
|
||||
if(!empty($booking->gcal_event_id_worker) && !empty($oldWorker->google_calendar_id)){
|
||||
$this->googlecalendar->deleteEvent($oldWorker->google_calendar_id, $booking->gcal_event_id_worker);
|
||||
}
|
||||
if(!empty($booking->gcal_event_id_owner) && !empty($evelinCalId)){
|
||||
$this->googlecalendar->deleteEvent($evelinCalId, $booking->gcal_event_id_owner);
|
||||
}
|
||||
$gcalServiceNames = implode(', ', array_map(function($s){ return $s->service_name_no; }, $serviceArray));
|
||||
$gcalTitle = $worker->worker_name . ' — ' . $gcalServiceNames . ' — ' . $booking->guest_name;
|
||||
$gcalDescription = 'Guest: ' . $booking->guest_name . "\nPhone: " . $booking->guest_phone . "\nEmail: " . $booking->guest_email;
|
||||
$gcalStart = $newBookingDate . 'T' . $newStartTime;
|
||||
$gcalEnd = $newBookingDate . 'T' . $finishTime;
|
||||
$newWorkerEventId = null;
|
||||
$newOwnerEventId = null;
|
||||
if(!empty($worker->google_calendar_id)){
|
||||
$newWorkerEventId = $this->googlecalendar->createEvent($worker->google_calendar_id, $gcalTitle, $gcalDescription, $gcalStart, $gcalEnd);
|
||||
}
|
||||
if(!empty($evelinCalId)){
|
||||
$newOwnerEventId = $this->googlecalendar->createEvent($evelinCalId, $gcalTitle, $gcalDescription, $gcalStart, $gcalEnd);
|
||||
}
|
||||
$this->Service_model->updateBookingCalEvents($booking->booking_id, $newWorkerEventId, $newOwnerEventId);
|
||||
|
||||
$manageLink = SITEURL.$lang.'/manage-booking/'.$token;
|
||||
$totalServicePrice = 0;
|
||||
foreach($serviceArray as $s){ $totalServicePrice += $s->service_price; }
|
||||
@@ -941,6 +989,17 @@ class Pages extends CI_Controller {
|
||||
));
|
||||
$this->User_model->sendEmail($emailArray, $lang, '');
|
||||
|
||||
// Google Calendar: delete events on cancellation
|
||||
$this->load->library('GoogleCalendar');
|
||||
$this->config->load('google_calendar');
|
||||
if(!empty($booking->gcal_event_id_worker) && !empty($worker->google_calendar_id)){
|
||||
$this->googlecalendar->deleteEvent($worker->google_calendar_id, $booking->gcal_event_id_worker);
|
||||
}
|
||||
$evelinCalId = $this->config->item('gcal_evelin_calendar_id');
|
||||
if(!empty($booking->gcal_event_id_owner) && !empty($evelinCalId)){
|
||||
$this->googlecalendar->deleteEvent($evelinCalId, $booking->gcal_event_id_owner);
|
||||
}
|
||||
|
||||
$this->Service_model->deleteBooking($booking->booking_id);
|
||||
|
||||
$data['selectedLang'] = $lang;
|
||||
|
||||
Reference in New Issue
Block a user