The Kolab_Format package allows you to easily read and write the Kolab format using PHP.
The package is being distributed as a standard PEAR package by the Horde project. As long as you have PEAR installed, installation should be straight forward.
<?php pear channel-discover pear.horde.org pear install --force channel://pear.horde.org/Kolab_Format ?>
pear will probably complain about the library (and its dependencies) not being marked stable yet but the --force option allows to ignore these warnings.
This section will present the construction of a short example script to demonstrate reading/writing an event in the Kolab XML format. The first required statement is the inclusion of the package:
<?php require_once 'Horde/Kolab/Format.php'; ?>
The API provided by the package is very simple. It only provides a load() and a save() function.
In order to have access to these methods it is necessary to create the Horde_Kolab_Format object. The call looks like this:
<?php $format = Horde_Kolab_Format::factory('XML', 'event'); ?>
The function takes three arguments:
The <tt>$format</tt> variable created above now provides the means to save and load events in Kolab XML format. In order to save an event we need to prepare an array with all relevant information about this event:
<?php $object = array( 'uid' => 1, 'summary' => 'test event', 'start-date' => time(), 'end-date' => time() + 24 * 60 * 60, ); ?>
This is an event that has the UID of 1 and carries the title 'test event'. It starts right now (time()) and ends in a day (time() + 24 * 60 * 60).
This event can now be saved using the save() function of the format handler:
$xml = $format->save($object);
The function returns the Kolab XML format as a result. This string can be fed back into the load() function:
<?php $read_object = $format->load($xml); ?>
If we dump the contents of the two variables <tt>$xml</tt> and <tt>$read_object</tt> this will be the result:
<?php var_dump($xml); string(438) "<?xml version="1.0"?> <event version="1.0"> <uid>1</uid> <body></body> <categories></categories> <creation-date>2008-07-10T12:51:51Z</creation-date> <last-modification-date>2008-07-10T12:51:51Z</last-modification-date> <sensitivity>public</sensitivity> <product-id>Horde::Kolab</product-id> <summary>test event</summary> <start-date>2008-07-10T12:51:51Z</start-date> <end-date>2008-07-11T12:51:51Z</end-date> </event> " var_dump($read_object); array(11) { ["uid"]=> string(1) "1" ["body"]=> string(0) "" ["categories"]=> string(0) "" ["creation-date"]=> int(1215694311) ["last-modification-date"]=> int(1215694311) ["sensitivity"]=> string(6) "public" ["product-id"]=> string(12) "Horde::Kolab" ["summary"]=> string(10) "test event" ["start-date"]=> int(1215694311) ["attendee"]=> array(0) { } ["end-date"]=> int(1215780711) } ?>
We see that the format stores a lot more information than we originally provided. The resulting XML string does not only contain the uid, summary, start-date, and end-date. Several additional attributes have been added. These were either calculated or set to a default value.
If we read the XML data back into an array all these new informations are available within that array.
Currently the Horde_Kolab_Format implements the object types contact, distributionslist, event, note, task as they are defined within the [[Kolab Format]] specification. In addition the Horde specific hprefs type is available. It is used for storing Horde user preferences in the IMAP store provided by the Kolab server.
Depending on the web application you might wish to connect with the Kolab server these object types may not be enough. Do not hesitate to define your own new type in that case. If you want it to find wider distribution you should of course discuss it on the Kolab Format mailing list to get some feedback on the new type.
The Horde_Kolab_Format packages makes the definition of a new object type rather straight forward. The following will explain the creation of a very simple new object that only saves a single string value.
This time it will be necessary to load the XML format definition, too. Any new object type will extend this XML definition:
<?php require_once 'Horde/Kolab/Format.php'; require_once 'Horde/Kolab/Format/XML.php'; ?>
A new object type is represented by a class that extends Horde_Kolab_Format_XML:
<?php class Horde_Kolab_Format_XML_string extends Horde_Kolab_Format_XML { var $_fields_specific; function Horde_Kolab_Format_XML_string() { $this->_root_name = 'string'; /** Specific fields of this object type */ $this->_fields_specific = array( 'string' => array( 'type' => HORDE_KOLAB_XML_TYPE_STRING, 'value' => HORDE_KOLAB_XML_VALUE_MAYBE_MISSING, ), ); parent::Horde_Kolab_Format_XML(); } } ?>
The class needs to end with the name of the object type. Here it is just string.
The declaration var $_fields_specific; indicates that the new object type has attributes beyond the basic set required for any Kolab object. So this part may not be missing for a declaration of a new type.
The function creating the class (Horde_Kolab_Format_XML_string()) needs to do three things:
The new format can now be used as demonstrated in the initial event example:
<?php $format = Horde_Kolab_Format::factory('XML', 'string'); $object = array( 'uid' => 1, 'string' => 'test string', ); $xml = $format->save($object); $read_object = $format->load($xml); var_dump($xml); var_dump($read_object); ?>
The result looks like this:
<?php string(347) "<?xml version="1.0"?> <string version="1.0"> <uid>1</uid> <body></body> <categories></categories> <creation-date>2008-07-10T13:28:36Z</creation-date> <last-modification-date>2008-07-10T13:28:36Z</last-modification-date> <sensitivity>public</sensitivity> <product-id>Horde::Kolab</product-id> <string>test string</string> </string> " array(8) { ["uid"]=> string(1) "1" ["body"]=> string(0) "" ["categories"]=> string(0) "" ["creation-date"]=> int(1215696516) ["last-modification-date"]=> int(1215696516) ["sensitivity"]=> string(6) "public" ["product-id"]=> string(12) "Horde::Kolab" ["string"]=> string(11) "test string" } ?>
There are only a number of valid entries available to specify the attributes a new object type may contain.
Each entry in the field list will look like this
<?php 'attribute_name' => array( 'type' => HORDE_KOLAB_XML_TYPE_*, 'value' => HORDE_KOLAB_XML_VALUE_*, ), ?>
attribute_name should be a short name describing the value that should be stored. 'type' must be set to one of the following HORDE_KOLAB_XML_TYPE_* type values:
Examples for HORDE_KOLAB_XML_TYPE_COMPOSITE and HORDE_KOLAB_XML_TYPE_MULTIPLE can be found in the definitions currently provided by the Horde_Kolab_Format package.
The following 'value' settings are allowed:
Examples for HORDE_KOLAB_XML_VALUE_CALCULATE can again be found in the current object types implemented in Horde_Kolab_Format.
A detailed documentation based on the code comments and extracted via phpDocumentor can be found here. Simply select the package Kolab_Format in the package selection box in the upper right corner.