MailSuite Pro 7 documentation

Creating events in calendar

This documentation provides samples for listing events in calendar as well as importing and exporting events data using ICS files. This article demonstrates creating new events and adding those into a calendar.

The idea of the code is quite straightforward: upon obtaining list of calendars for specific user, we locate one of the calendars by its name, create Event object instance, populate its properties and add the event to the calendar.

<?php
include_once '/var/www/webmail/libraries/afterlogic/api.php';
$email_address = "username@domain.com";
$calendar_name = "Calendar Two";
/* converting date/time into native format of iCalendar */
function getIcalDate($str)
{
    $datetime = new DateTime($str);
    $timestamp = $datetime->getTimestamp();
    return date('Ymd\THis', $timestamp);
}
if (class_exists('CApi') && CApi::IsValid()) {
    $oApiUsersManager = CApi::Manager('users');
    $oAccount = $oApiUsersManager->getAccountByEmail($email_address);
    $oApiCalendarManager = CApi::Manager('calendar');
    $sCalendarId="";
    $oCals = $oApiCalendarManager->getCalendars($oAccount);
    foreach ($oCals as $oCal) {
        if ($oCal["Name"]==$calendar_name)
            $sCalendarId=$oCal["Id"];
    }
    if ($sCalendarId!="") {
        $oEvent = new CEvent();
        $oEvent->IdCalendar = $sCalendarId;
        $MYTZ = new DateTimeZone('Etc/GMT+3');
        $oEvent->Start = getIcalDate("2016-10-29 02:00:00");
        $oEvent->End = getIcalDate("2016-10-29 04:00:00");
        $oEvent->AllDay = 0;
        $oEvent->Name = "Name";
        $oEvent->Description = "Description";
        $oEvent->Location = "Location";
        $oEvent->Alarms = array(180,60);
        $oApiCalendarManager->createEvent($oAccount,$oEvent);
    } else {
        echo 'Calendar not found';
    }    
} else {
    echo 'AfterLogic API isn\'t available';
}

A couple of items worth clarifying:

  • $email_address stands for email address of MailSuite Pro user, one they use to log into MailSuite Pro installation.

  • Event will be created in a calendar with name supplied as $calendar_name.

  • If AllDay property is set to 1, the event is created as all-day one, only start date value is used; start time, end date and time are discarded.

  • Alarms property holds an array with offsets for reminder alerts, in minutes. Setting it to array(180,60) means reminding 3 hours and 1 hour prior to the event start.

  • To prevent from misplacing the event on the calendar grid, be sure to supply correct timezone for $MYTZ value.