iflrandevu/application/libraries/Ics_file.php

186 lines
6.3 KiB
PHP
Raw Permalink Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
2017-11-01 16:24:25 +03:00
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
2017-11-01 16:24:25 +03:00
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.3.0
* ---------------------------------------------------------------------------- */
use Jsvrcek\ICS\CalendarExport;
use Jsvrcek\ICS\CalendarStream;
use Jsvrcek\ICS\Exception\CalendarEventException;
use Jsvrcek\ICS\Model\CalendarAlarm;
2017-11-01 16:24:25 +03:00
use Jsvrcek\ICS\Model\CalendarEvent;
use Jsvrcek\ICS\Model\Description\Location;
2017-11-01 16:24:25 +03:00
use Jsvrcek\ICS\Model\Relationship\Attendee;
use Jsvrcek\ICS\Model\Relationship\Organizer;
use Jsvrcek\ICS\Utility\Formatter;
/**
* Class Ics_file
*
2022-02-23 11:07:21 +03:00
* An ICS file is a calendar file saved in a universal calendar format used by email and calendar clients, including
* Microsoft Outlook, Google Calendar, and Apple Calendar.
2017-11-01 16:24:25 +03:00
*
* Depends on the Jsvrcek\ICS composer package.
2022-02-23 11:07:21 +03:00
*
* Notice: The Ics_calendar and Ics_provider classes are used for PHP 8.1 compatibility.
2017-11-01 16:24:25 +03:00
*/
class Ics_file {
2022-02-23 11:07:21 +03:00
/**
* @var EA_Controller
*/
protected $CI;
/**
* Availability constructor.
*/
public function __construct()
{
$this->CI =& get_instance();
$this->CI->load->library('ics_provider');
$this->CI->load->library('ics_calendar');
}
2017-11-01 16:24:25 +03:00
/**
* Get the ICS file contents for the provided arguments.
*
* @param array $appointment Appointment.
* @param array $service Service.
* @param array $provider Provider.
* @param array $customer Customer.
*
* @return string Returns the contents of the ICS file.
*
* @throws CalendarEventException
* @throws Exception
2017-11-01 16:24:25 +03:00
*/
public function get_stream($appointment, $service, $provider, $customer)
{
2022-02-23 11:07:21 +03:00
$appointment_timezone = new DateTimeZone($provider['timezone']);
$appointment_start = new DateTime($appointment['start_datetime'], $appointment_timezone);
$appointment_end = new DateTime($appointment['end_datetime'], $appointment_timezone);
2017-11-01 16:24:25 +03:00
// Setup the event.
$event = new CalendarEvent();
$event
->setStart($appointment_start)
->setEnd($appointment_end)
->setStatus('CONFIRMED')
2017-11-01 16:24:25 +03:00
->setSummary($service['name'])
->setUid($appointment['id']);
2022-02-23 11:07:21 +03:00
if ( ! empty($service['location']))
{
$location = new Location();
$location->setName((string)$service['location']);
$event->addLocation($location);
}
$description = [
'',
lang('provider'),
'',
lang('name') . ': ' . $provider['first_name'] . ' ' . $provider['last_name'],
2022-02-23 11:07:21 +03:00
lang('email') . ': ' . $provider['email'],
lang('phone_number') . ': ' . $provider['phone_number'],
lang('address') . ': ' . $provider['address'],
lang('city') . ': ' . $provider['city'],
lang('zip_code') . ': ' . $provider['zip_code'],
'',
lang('customer'),
'',
lang('name') . ': ' . $customer['first_name'] . ' ' . $customer['last_name'],
2022-02-23 11:07:21 +03:00
lang('email') . ': ' . $customer['email'],
lang('phone_number') . ': ' . $customer['phone_number'],
lang('address') . ': ' . $customer['address'],
lang('city') . ': ' . $customer['city'],
lang('zip_code') . ': ' . $customer['zip_code'],
'',
lang('notes'),
'',
$appointment['notes'],
];
$event->setDescription(implode("\\n", $description));
2017-11-01 16:24:25 +03:00
$attendee = new Attendee(new Formatter());
if (isset($customer['email']) && ! empty($customer['email']))
{
$attendee->setValue($customer['email']);
}
// Add the event attendees.
2017-11-01 16:24:25 +03:00
$attendee->setName($customer['first_name'] . ' ' . $customer['last_name']);
$attendee->setCalendarUserType('INDIVIDUAL')
->setRole('REQ-PARTICIPANT')
->setParticipationStatus('NEEDS-ACTION')
->setRsvp('TRUE');
$event->addAttendee($attendee);
$alarm = new CalendarAlarm();
$alarm_datetime = clone $appointment_start;
$alarm->setTrigger($alarm_datetime->modify('-15 minutes'));
$alarm->setSummary('Alarm notification');
$alarm->setDescription('This is an event reminder');
$alarm->setAction('EMAIL');
$alarm->addAttendee($attendee);
$event->addAlarm($alarm);
$alarm = new CalendarAlarm();
$alarm_datetime = clone $appointment_start;
$alarm->setTrigger($alarm_datetime->modify('-60 minutes'));
$alarm->setSummary('Alarm notification');
$alarm->setDescription('This is an event reminder');
$alarm->setAction('EMAIL');
$alarm->addAttendee($attendee);
$event->addAlarm($alarm);
$attendee = new Attendee(new Formatter());
if (isset($provider['email']) && ! empty($provider['email']))
{
$attendee->setValue($provider['email']);
}
$attendee->setName($provider['first_name'] . ' ' . $provider['last_name']);
$attendee->setCalendarUserType('INDIVIDUAL')
->setRole('REQ-PARTICIPANT')
->setParticipationStatus('ACCEPTED')
->setRsvp('FALSE');
2017-11-01 16:24:25 +03:00
$event->addAttendee($attendee);
// Set the organizer.
$organizer = new Organizer(new Formatter());
$organizer
->setValue($provider['email'])
->setName($provider['first_name'] . ' ' . $provider['last_name']);
$event->setOrganizer($organizer);
// Setup calendar.
2022-02-23 11:07:21 +03:00
$calendar = new Ics_calendar();
2017-11-01 16:24:25 +03:00
$calendar
->setProdId('-//EasyAppointments//Open Source Web Scheduler//EN')
->setTimezone(new DateTimeZone($provider['timezone']))
2017-11-01 16:24:25 +03:00
->addEvent($event);
// Setup exporter.
$calendarExport = new CalendarExport(new CalendarStream, new Formatter());
$calendarExport->addCalendar($calendar);
return $calendarExport->getStream();
}
}