Previous Up Next
Examples Examples File parsing

Object creation

An example of creating an iCalendar file using PhpiCalLib

Table of Contents

Source

The source of the example:

  1. <?php
  2. /**
  3.  * create.php: example of creating an icalendar file
  4.  * 
  5.  * In this example, we try to create an ics file that looks like the following:
  6.  * {@example create.ics}
  7.  * 
  8.  * @copyright Copyright (C) 2008 Nigel Swinson, nigelswinson@users.sourceforge.net
  9.  * @author  Nigel Swinson
  10.  * @package PhpiCalLib
  11.  * @version 1.0
  12.  */
  13.  
  14. // You should only ever have to include this file.
  15. require_once '../icalendar.php';
  16.  
  17. // The error handling model in PhpiCalLib is to throw exceptions, so you really should wrap code
  18. // that calls the library in a try/catch block to handle errors.
  19. try {
  20.     // Create a new event object.  It will automatically get DtStamp, DtStart and Uid properties as 
  21.     // these are compulsory.
  22.     $Event new PhpiCalLib_Event();
  23.     
  24.     // Set the event's attributes.  There are several ways we can set the property values.
  25.     // - Call SetEncodedValue() supplying an encoded string
  26.     // - Call SetValue() supplying a strongly typed object from which we can extract the encoded 
  27.     //   string by calling ToString()
  28.     // - Use a PhpiCalLib_PropertyFactory and call ToContentLines() suppling the encoded value for
  29.     //   the full string of the property
  30.     {
  31.         // Add the Dtstamp property.
  32.         // ### Note this isn't strictly necessary as the Event will auto allocate a DtStamp property.  
  33.         // It is also not normally even correct, as DtStamp is meant to represent the time the ics 
  34.         // object representation was generated, which is exactly what the default will do.  But the premise
  35.         // of the example is to produce an ics file like the example, so we override it.
  36.         $DtStamp new PhpiCalLib_Properties_DtStampProperty();
  37.         {
  38.             // Here we use a DateTime object to create the value
  39.             $DateTime new PhpiCalLib_DataTypes_DateTime();
  40.             $DateTime->Parse("19960704T120000Z");
  41.             $DtStamp->SetValue($DateTime);
  42.         }
  43.         // Note calling SetProperty removes any previous values.  Were we to call AddProperty, then it would
  44.         // add an additional DtStamp to any that were already there.  The Event class chooses a default DtStamp
  45.         // property so we want to remove it and replace it with this, so hence the call to SetProperty.
  46.         // AddProperty is faster, so is a better choice for properties that are not required.
  47.         $Event->SetProperty($DtStamp);
  48.         
  49.         // Add the Uid property.  Note this isn't strictly necessary, as the Event will auto
  50.         // allocate a Uid property
  51.         $Uid new PhpiCalLib_Properties_UidProperty();
  52.         // Here we call SetTextValue() to create the property, which is a method of 
  53.         // PhpiCalLib_Properties_UidProperty that offers extra validation.
  54.         $Uid->SetTextValue('uid1@example.com');
  55.         // A required property, so we call SetProperty to overwrite the default
  56.         $Event->SetProperty($Uid);
  57.     
  58.         // Add the organizer.  Note here we illustrate using the generic ContentLine base class
  59.         // to create the property, which won't offer us any validation, meaning if the coder
  60.         // gets it wrong, we may create an invalid iCalendar file
  61.         $Organiser new PhpiCalLib_ContentLine();
  62.         $Organiser->SetName('ORGANIZER');
  63.         // Because we are using the PhpiCalLib_ContentLine() base class, our only option to set the value is 
  64.         // to call SetEncodedValue()
  65.         $Organiser->SetEncodedValue("mailto:jsmith@example.com");
  66.         $Event->AddProperty($Organiser);
  67.     
  68.         // Add the DtStart property to 19960918T143000Z
  69.         $DtStart new PhpiCalLib_Properties_DtStartProperty();
  70.         {
  71.             // Here we exculively use the API to create the values without needing to do any parsing.
  72.             // Perhaps the highest performance, but a bit laborious, compare this to setting the DtStamp
  73.             $Date new PhpiCalLib_DataTypes_Date();
  74.             $Date->SetFullYear(1996);
  75.             $Date->SetMonth(9);
  76.             $Date->SetMonthDay(18);
  77.             $Time new PhpiCalLib_DataTypes_Time();
  78.             $Time->SetHours(14);
  79.             $Time->SetMinutes(30);
  80.             $Time->SetSeconds(0);
  81.             $Time->SetUtc(true);
  82.             $DateTime new PhpiCalLib_DataTypes_DateTime();
  83.             $DateTime->SetDate($Date);
  84.             $DateTime->SetTime($Time);
  85.             $DtStart->SetDateTimeValue($DateTime);
  86.         }
  87.         // A required property, so we call SetProperty to overwrite the default
  88.         $Event->SetProperty($DtStart);
  89.         
  90.         // Add the DtEnd property to 19960920T220000Z
  91.         $DtEnd new PhpiCalLib_Properties_DtEndProperty();
  92.         {
  93.             // Here we use an alternative method of PhpiCalLib_DataTypes_DateTime to create the object.
  94.             // Compare this to the way we set the DtStart or DtStamp properties
  95.             $DateTime new PhpiCalLib_DataTypes_DateTime();
  96.             // int gmmktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )
  97.             // Note we use gmmktime to make a time WRT to UTC
  98.             $TimeStamp gmmktime(22,00,00,9,20,1996);
  99.             $DateTime->FromTimeStamp($TimeStamptrue);
  100.             $DtEnd->SetDateTimeValue($DateTime);
  101.         }
  102.         $Event->AddProperty($DtEnd);
  103.         
  104.         // Add the Status property.  Here we illustrate the use of the Property factory.  The object
  105.         // returned will be the most specialized PhpiCalLib_ContentLine class available providing the best
  106.         // data validation possible.
  107.         $PropertyFactory new PhpiCalLib_PropertyFactory();
  108.         $Status $PropertyFactory->Create("STATUS:CONFIRMED");
  109.         $Event->AddProperty($Status);
  110.     
  111.         // The intention is that your code for adding properties would look more along these lines.  Create
  112.         // a specialized property, call a custom accessor to set the value, and then add it to the component.
  113.         {
  114.             // Add the Categories
  115.             $Categories new PhpiCalLib_Properties_CategoriesProperty();
  116.             $Categories->SetTextValue('CONFERENCE');
  117.             $Event->AddProperty($Categories);
  118.             // Add the Summary
  119.             $Summary new PhpiCalLib_Properties_SummaryProperty();
  120.             $Summary->SetTextValue('Networld+Interop Conference');
  121.             $Event->AddProperty($Summary);
  122.         }
  123.             
  124.         // Add the description property
  125.         $Description new PhpiCalLib_Properties_DescriptionProperty();
  126.         // Note here we add a multiline text property, so we use " to indicate the \n means newline.  
  127.         // Were we to call SetEnocededValue, it would throw an exception as raw newlines aren't allowed in 
  128.         // property values.
  129.         $Description->SetTextValue("Networld+Interop Conference and Exhibit\nAtlanta World Congress Center\n Atlanta, Georgia");
  130.         $Event->AddProperty($Description);
  131.     }
  132.     // Add the completed event to an iCalendar object
  133.     $iCalFile new PhpiCalLib_iCalendar();
  134.     $iCalFile->AddComponent($Event);
  135. catch (PhpiCalLib_Exception $E{
  136.     // A failure in the API, but perhaps the default exception handler is sufficient? 
  137.     throw $E;
  138. }
  139. ?>
  140. <html>
  141. <body>
  142.  
  143. <table>
  144.     <tr>
  145.         <th>Contents of <a href="create.ics">create.ics</a></th>
  146.         <th>Contents of manually created ics file</th>
  147.     </tr>
  148.     <tr>
  149.         <td>
  150.             <pre><?
  151.             echo htmlspecialchars(file_get_contents('create.ics'));
  152.             ?>
  153.             </pre>
  154.         </td>
  155.         <td>
  156.             <pre><?
  157.                 // Echo it's results
  158.                 echo $iCalFile->ToString();
  159.             ?>
  160.             </pre>
  161.         </td>
  162.     </tr>
  163. </table>
  164. </body>
  165. </html>

Input

The sample input file:

  1. BEGIN:VCALENDAR
  2. PRODID:-//xyz Corp//NONSGML PDA Calendar Version 1.0//EN
  3. VERSION:2.0
  4. BEGIN:VEVENT
  5. DTSTAMP:19960704T120000Z
  6. UID:uid1@example.com
  7. ORGANIZER:mailto:jsmith@example.com
  8. DTSTART:19960918T143000Z
  9. DTEND:19960920T220000Z
  10. STATUS:CONFIRMED
  11. CATEGORIES:CONFERENCE
  12. SUMMARY:Networld+Interop Conference
  13. DESCRIPTION:Networld+Interop Conference
  14.   and ExhibitnAtlanta World Congress Centern
  15.  AtlantaGeorgia
  16. END:VEVENT
  17. END:VCALENDAR

Output

Typical output can be seen by running the example http://scripts.carrubbers.org/scripts/php/icallib/examples/create.php

Previous Up Next
Examples Examples File parsing

Documentation generated on Tue, 01 Apr 2008 00:19:51 +0100 by phpDocumentor 1.4.0