kxc
kxc

Reputation: 1457

CalDavClient add new record

How to add a new record into Calendar using CalDAV ? I can get all my current recors from Thunderbird Lightning calendar as array, by that code:

require_once 'class.CalDavClient.php';

$cal = new CalDAVClient(
'###',
'###',
'###'
);
if ( isset($options["PROPFIND"]) ) {
    $cal->SetDepth(1);
    $folder_xml = $cal->DoXMLRequest(
    "PROPFIND",
    '<?xml version="1.0" encoding="utf-8" ?>
        <propfind xmlns="DAV:"><prop><getcontentlength/><getcontenttype/>
<resourcetype/><getetag/></prop></propfind>'

    );
}

$date_from = date('Ymd', mktime(0, 0, 0, date('m')-3, date('d'), date('Y')));
$calendar_events = CalDAVClient::normalize_events($cal->GetEvents($date_from, 
"20991212"), $row['calendar_Title']);

print_r($calendar_events);

But how i can add new event ? I looked for documentation, but cant find anything about that.

Upvotes: 0

Views: 161

Answers (2)

kxc
kxc

Reputation: 1457

Thanks to hnh link i found a solution for me, i create that method in class CalDavCliet that generate data for request

public function create_event($title, $desc, $tstart, $tend) {

    $event_id = $this->generate_eventID();
    $tstamp = gmdate("Ymd\THis\Z");
    $xml = "BEGIN:VCALENDAR\n".
            "VERSION:2.0\n".
            "BEGIN:VEVENT\n".
            "DTSTAMP:$tstamp\n".
            "DTSTART:$tstart\n".
            "DTEND:$tend\n".
            "UID:$event_id\n".
            "DESCRIPTION:$desc\n".
            "LOCATION:Office\n".
            "SUMMARY:$title\n".
            "END:VEVENT\n".
            "END:VCALENDAR";
    $etag = $this->DoPUTRequest($this->base_url . $event_id . '.ics', $xml);

    return $etag;
}

and now i can use that method to add new record

$etag = $cal->create_event('тест1', 'описание...', '20141220T173000Z', '20141220T180000Z');
if (!empty($etag)) {
    echo "Событие успешно добавлено !";
}

How i understood, there's no method to automatically generate eID for new record, so i wrote and use that method, to generate "simple" eID like it make Thunderbird client

 protected function generate_eventID() {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $charactersLength = strlen($characters);
    $eventID = '';
    for ($i = 0; $i < 8; $i++) {$eventID .= $characters[rand(0, $charactersLength - 1)];}
    for ($i = 0; $i < 3; $i++) {
        $eventID .= '-';
        for ($j = 0; $j < 4; $j++) {$eventID .= $characters[rand(0, $charactersLength - 1)];}
    }
    $eventID .= '-';
    for ($i = 0; $i < 12; $i++) {$eventID .= $characters[rand(0, $charactersLength - 1)];}

    return $eventID;
}

Upvotes: 0

hnh
hnh

Reputation: 14795

To create a new event you use the HTTP PUT operation with the iCalendar entity representing the new event.

This is a nice introduction: http://sabre.io/dav/building-a-caldav-client/

Upvotes: 1

Related Questions