Location: PHPKode > projects > PhpiCalLib > examples/create.php
<?php
/**
 * create.php: example of creating an icalendar file
 * 
 * In this example, we try to create an ics file that looks like the following:
 * {@example create.ics}
 * 
 * @copyright Copyright (C) 2008 Nigel Swinson, hide@address.com 
 * @author  Nigel Swinson
 * @package PhpiCalLib
 * @version 1.0
 */

// You should only ever have to include this file.
require_once '../icalendar.php';

// The error handling model in PhpiCalLib is to throw exceptions, so you really should wrap code
// that calls the library in a try/catch block to handle errors.
try {
	// Create a new event object.  It will automatically get DtStamp, DtStart and Uid properties as 
	// these are compulsory.
	$Event = new PhpiCalLib_Event();
	
	// Set the event's attributes.  There are several ways we can set the property values.
	// - Call SetEncodedValue() supplying an encoded string
	// - Call SetValue() supplying a strongly typed object from which we can extract the encoded 
	//   string by calling ToString()
	// - Use a PhpiCalLib_PropertyFactory and call ToContentLines() suppling the encoded value for
	//   the full string of the property
	{
		// Add the Dtstamp property.
		// ### Note this isn't strictly necessary as the Event will auto allocate a DtStamp property.  
		// It is also not normally even correct, as DtStamp is meant to represent the time the ics 
		// object representation was generated, which is exactly what the default will do.  But the premise
		// of the example is to produce an ics file like the example, so we override it.
		$DtStamp = new PhpiCalLib_Properties_DtStampProperty();
		{
			// Here we use a DateTime object to create the value
			$DateTime = new PhpiCalLib_DataTypes_DateTime();
			$DateTime->Parse("19960704T120000Z");
			$DtStamp->SetValue($DateTime);
		}
		// Note calling SetProperty removes any previous values.  Were we to call AddProperty, then it would
		// add an additional DtStamp to any that were already there.  The Event class chooses a default DtStamp
		// property so we want to remove it and replace it with this, so hence the call to SetProperty.
		// AddProperty is faster, so is a better choice for properties that are not required.
		$Event->SetProperty($DtStamp);
		
		// Add the Uid property.  Note this isn't strictly necessary, as the Event will auto
		// allocate a Uid property
		$Uid = new PhpiCalLib_Properties_UidProperty();
		// Here we call SetTextValue() to create the property, which is a method of 
		// PhpiCalLib_Properties_UidProperty that offers extra validation.
		$Uid->SetTextValue('hide@address.com');
		// A required property, so we call SetProperty to overwrite the default
		$Event->SetProperty($Uid);
	
		// Add the organizer.  Note here we illustrate using the generic ContentLine base class
		// to create the property, which won't offer us any validation, meaning if the coder
		// gets it wrong, we may create an invalid iCalendar file
		$Organiser = new PhpiCalLib_ContentLine();
		$Organiser->SetName('ORGANIZER');
		// Because we are using the PhpiCalLib_ContentLine() base class, our only option to set the value is 
		// to call SetEncodedValue()
		$Organiser->SetEncodedValue("mailto:hide@address.com");
		$Event->AddProperty($Organiser);
	
		// Add the DtStart property to 19960918T143000Z
		$DtStart = new PhpiCalLib_Properties_DtStartProperty();
		{
			// Here we exculively use the API to create the values without needing to do any parsing.
			// Perhaps the highest performance, but a bit laborious, compare this to setting the DtStamp
			$Date = new PhpiCalLib_DataTypes_Date();
			$Date->SetFullYear(1996);
			$Date->SetMonth(9);
			$Date->SetMonthDay(18);
			$Time = new PhpiCalLib_DataTypes_Time();
			$Time->SetHours(14);
			$Time->SetMinutes(30);
			$Time->SetSeconds(0);
			$Time->SetUtc(true);
			$DateTime = new PhpiCalLib_DataTypes_DateTime();
			$DateTime->SetDate($Date);
			$DateTime->SetTime($Time);
			$DtStart->SetDateTimeValue($DateTime);
		}
		// A required property, so we call SetProperty to overwrite the default
		$Event->SetProperty($DtStart);
		
		// Add the DtEnd property to 19960920T220000Z
		$DtEnd = new PhpiCalLib_Properties_DtEndProperty();
		{
			// Here we use an alternative method of PhpiCalLib_DataTypes_DateTime to create the object.
			// Compare this to the way we set the DtStart or DtStamp properties
			$DateTime = new PhpiCalLib_DataTypes_DateTime();
			// int gmmktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )
			// Note we use gmmktime to make a time WRT to UTC
			$TimeStamp = gmmktime(22,00,00,9,20,1996);
			$DateTime->FromTimeStamp($TimeStamp, true);
			$DtEnd->SetDateTimeValue($DateTime);
		}
		$Event->AddProperty($DtEnd);
		
		// Add the Status property.  Here we illustrate the use of the Property factory.  The object
		// returned will be the most specialized PhpiCalLib_ContentLine class available providing the best
		// data validation possible.
		$PropertyFactory = new PhpiCalLib_PropertyFactory();
		$Status = $PropertyFactory->Create("STATUS:CONFIRMED");
		$Event->AddProperty($Status);
	
		// The intention is that your code for adding properties would look more along these lines.  Create
		// a specialized property, call a custom accessor to set the value, and then add it to the component.
		{
			// Add the Categories
			$Categories = new PhpiCalLib_Properties_CategoriesProperty();
			$Categories->SetTextValue('CONFERENCE');
			$Event->AddProperty($Categories);
			// Add the Summary
			$Summary = new PhpiCalLib_Properties_SummaryProperty();
			$Summary->SetTextValue('Networld+Interop Conference');
			$Event->AddProperty($Summary);
		}
			
		// Add the description property
		$Description = new PhpiCalLib_Properties_DescriptionProperty();
		// Note here we add a multiline text property, so we use " to indicate the \n means newline.  
		// Were we to call SetEnocededValue, it would throw an exception as raw newlines aren't allowed in 
		// property values.
		$Description->SetTextValue("Networld+Interop Conference and Exhibit\nAtlanta World Congress Center\n Atlanta, Georgia");
		$Event->AddProperty($Description);
	}
	// Add the completed event to an iCalendar object
	$iCalFile = new PhpiCalLib_iCalendar();
	$iCalFile->AddComponent($Event);
} catch (PhpiCalLib_Exception $E) {
	// A failure in the API, but perhaps the default exception handler is sufficient? 
	throw $E;
}
?>
<html>
<body>

<table>
	<tr>
		<th>Contents of <a href="create.ics">create.ics</a></th>
		<th>Contents of manually created ics file</th>
	</tr>
	<tr>
		<td>
			<pre><?
			echo htmlspecialchars(file_get_contents('create.ics'));
			?>
			</pre>
		</td>
		<td>
			<pre><?
				// Echo it's results
				echo $iCalFile->ToString();
			?>
			</pre>
		</td>
	</tr>
</table>
</body>
</html>
Return current item: PhpiCalLib