Location: PHPKode > projects > PhpiCalLib > event.php
<?php
/**
 * event.php: iCalendar event component class
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @copyright Copyright (C) 2008 Nigel Swinson, hide@address.com 
 * @author  Nigel Swinson
 * @package PhpiCalLib
 * @version 1.0
 */

require_once 'components.php';
require_once 'propertyfactory.php';

/**
 * An eventc iCalendar component
 * */
class PhpiCalLib_Event extends PhpiCalLib_Component {
	public function __construct() {
		parent::__construct();
		$this->SetType(PHPICALLIB_COMPONENT_VEVENT);

		// Establish the property rules
		$this->aPermittedProperties = array(
					//                   ; the following are REQUIRED,
					//                   ; but MUST NOT occur more than once
					//                   dtstamp / dtstart / uid /
					PHPICALLIB_PROPERTY_DTSTAMP => array(1,1),
					PHPICALLIB_PROPERTY_DTSTART => array(1,1),
					PHPICALLIB_PROPERTY_UID => array(1,1),
					//                   ; the following are OPTIONAL,
					//                   ; but MUST NOT occur more than once
					//                   class / created / description / geo /
					PHPICALLIB_PROPERTY_CLASS => array(0,1),
					PHPICALLIB_PROPERTY_CREATED => array(0,1),
					PHPICALLIB_PROPERTY_DESCRIPTION => array(0,1),
					PHPICALLIB_PROPERTY_GEO => array(0,1),
					//                   last-mod / location / organizer / priority /
					PHPICALLIB_PROPERTY_LASTMODIFIED => array(0,1),
					PHPICALLIB_PROPERTY_LOCATION => array(0,1),
					PHPICALLIB_PROPERTY_ORGANIZER => array(0,1),
					PHPICALLIB_PROPERTY_PRIORITY => array(0,1),
					//                   seq / status / summary / transp /
					PHPICALLIB_PROPERTY_SEQUENCE => array(0,1),
					PHPICALLIB_PROPERTY_STATUS => array(0,1),
					PHPICALLIB_PROPERTY_SUMMARY => array(0,1),
					PHPICALLIB_PROPERTY_TRANSP => array(0,1),
					//                   url / recurid /
					PHPICALLIB_PROPERTY_URL => array(0,1),
					PHPICALLIB_PROPERTY_RECURRENCE_ID => array(0,1),
					//                   ; the following is OPTIONAL,
					//                   ; but SHOULD NOT occur more than once
					//                   rrule /
					PHPICALLIB_PROPERTY_RRULE => array(0,1),
					//                   ; either 'dtend' or 'duration' MAY appear in
					//                   ; a 'eventprop', but 'dtend' and 'duration'
					//                   ; MUST NOT occur in the same 'eventprop'
					//                   dtend / duration /
					PHPICALLIB_PROPERTY_DTEND => array(0,1),
					PHPICALLIB_PROPERTY_DURATION => array(0,1),
					//                   ; the following are OPTIONAL,
					//                   ; and MAY occur more than once
					//                   attach / attendee / categories / comment /
					PHPICALLIB_PROPERTY_ATTACH => array(0,-1),
					PHPICALLIB_PROPERTY_ATTENDEE => array(0,-1),
					PHPICALLIB_PROPERTY_CATEGORIES => array(0,-1),
					PHPICALLIB_PROPERTY_COMMENT => array(0,-1),
					//                   contact / exdate / rstatus / related /
					PHPICALLIB_PROPERTY_CONTACT => array(0,-1),
					PHPICALLIB_PROPERTY_EXDATE => array(0,-1),
					PHPICALLIB_PROPERTY_REQUEST_STATUS => array(0,-1),
					PHPICALLIB_PROPERTY_RELATED_TO => array(0,-1),
					//                   resources / rdate / x-prop / iana-prop
					PHPICALLIB_PROPERTY_RESOURCES => array(0,-1),
					PHPICALLIB_PROPERTY_RDATE => array(0,-1),
					PHPICALLIB_PROPERTY_XPROP => array(0,-1),
					PHPICALLIB_PROPERTY_IANAPROP => array(0,-1));
		
		//http://tools.ietf.org/html/draft-ietf-calsify-rfc2445bis-08#section-3.6.1
		//                   ; the following are REQUIRED,
		//                   ; but MUST NOT occur more than once
		//                   dtstamp / dtstart / uid /
		$this->aContentLines = array();
		$this->SetProperty(new PhpiCalLib_Properties_UidProperty());
		// We must have a DtStart too
		$DtStart = new PhpiCalLib_Properties_DtStartProperty();
		$Now = time();
		$DtStart->SetDateTimeFromTimeStamp($Now, true);
		$this->SetProperty($DtStart);
		// And a DtStamp
		$DtStamp = new PhpiCalLib_Properties_DtStampProperty();
		$this->SetProperty($DtStamp);
	}

	/**
	 * Determine if it's an all day event
	 *
	 * @return bool True if the event lasts all day, else false.
	 */
	public function AllDay() {
		//   Value Type:  The default value type is DATE-TIME.  The time value
		//      MUST be one of the forms defined for the DATE-TIME value type.
		//      The value type can be set to a DATE value type.
		$DtStart = $this->GetProperty(PHPICALLIB_PROPERTY_DTSTART);
		$ValueParameter = $DtStart->GetParameter(PHPICALLIB_PARAMETER_VALUE);
		if (!$ValueParameter) return false;
		return ($ValueParameter->ToDataType() == PHPICALLIB_DATATYPE_DATE);
	}
	
	/**
	 * Overridden because events have further rules to validate
	 *
	 * @param PhpiCalLib_ContentLine $ContentLine
	 */
	protected function SetPropertyPermitted($ContentLine) {
		// Call the default version, giving it a chance to throw an exception
		parent::SetPropertyPermitted($ContentLine);
		$this->_AddOrSetPropertyPermitted($ContentLine);
	}
	
	/**
	 * Overridden because events have further rules to validate
	 *
	 * @param PhpiCalLib_ContentLine $ContentLine
	 */
	protected function AddPropertyPermitted($ContentLine) {
		// Call the default version, giving it a chance to throw an exception
		parent::AddPropertyPermitted($ContentLine);
		$this->_AddOrSetPropertyPermitted($ContentLine);
	}
	
	/**
	 * Check that adding this property wouldn't result in having both dtend and duration set.
	 * 
	 * <pre>
	 *                   ; either 'dtend' or 'duration' MAY appear in
	 *                   ; a 'eventprop', but 'dtend' and 'duration'
	 *                   ; MUST NOT occur in the same 'eventprop'
	 *                   dtend / duration /
	 * </pre>
	 * @link http://tools.ietf.org/html/draft-ietf-calsify-rfc2445bis-08#section-3.6.1
	 * @param PhpiCalLib_ContentLine $ContentLine
	 */
	private function _AddOrSetPropertyPermitted($ContentLine) {
		switch ($ContentLine->GetType()) {
			case PHPICALLIB_PROPERTY_DTEND:
				if ($this->GetProperty(PHPICALLIB_PROPERTY_DURATION)) {
					throw new PhpiCalLib_Exception("Event objects can't have both an end time and a duration.");
				}
				break;
			case PHPICALLIB_PROPERTY_DURATION:
				if ($this->GetProperty(PHPICALLIB_PROPERTY_DTEND)) {
					throw new PhpiCalLib_Exception("Event objects can't have both an end time and a duration.");
				}
		}
	}
}

?>
Return current item: PhpiCalLib