Overview

Namespaces

  • intouch
    • ical

Classes

  • Duration
  • Factory
  • Freq
  • iCal
  • Line
  • Parser
  • Query
  • Recurrence
  • VCalendar
  • VEvent
  • VTimeZone
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php // BUILD: Remove line
  2: 
  3: namespace intouch\ical;
  4: 
  5: /**
  6:  * The wrapper for vevents. Will reveal a unified and simple api for
  7:  * the events, which include always finding a start and end (except
  8:  * when no end or duration is given) and checking if the event is
  9:  * blocking or similar.
 10:  *
 11:  * Will apply the specified timezone to timestamps if a tzid is
 12:  * specified
 13:  *
 14:  * @author Morten Fangel (C) 2008
 15:  * @author Michael Kahn (C) 2013
 16:  * @license http://creativecommons.org/licenses/by-sa/2.5/dk/deed.en_GB CC-BY-SA-DK
 17:  */
 18: class VEvent {
 19:     const DEFAULT_CONFIRMED = true;
 20: 
 21:     protected $uid;
 22: 
 23:     protected $start;
 24:     protected $end;
 25: 
 26:     protected $summary;
 27:     protected $description;
 28:     protected $location;
 29: 
 30:     protected $laststart;
 31:     protected $lastend;
 32: 
 33:     public $recurrence; //RRULE
 34:     public $recurex;    //EXRULE
 35:     public $excluded;   //EXDATE(s)
 36:     public $added;      //RDATE(s)
 37: 
 38:     public $freq; //getFrequency() intouch\ical\Freq
 39: 
 40:     public $data;
 41: 
 42:     /**
 43:      * Constructs a new intouch\ical\VEvent. Needs the iCal
 44:      * supplied so it can query for timezones.
 45:      * @param intouch\ical\Line[] $data
 46:      * @param intouch\ical\iCal $ical
 47:      */
 48:     public function __construct($data, iCal $ical) {
 49: 
 50:         $this->uid = $data['uid']->getData();
 51:         unset($data['uid']);
 52: 
 53:         if ( isset($data['rrule']) ) {
 54:             $this->recurrence = new Recurrence($data['rrule']);
 55:             unset($data['rrule']);
 56:         }
 57: 
 58:         if ( isset($data['exrule']) ) {
 59:             $this->recurex = new Recurrence($data['exrule']);
 60:             unset($data['exrule']);
 61:         }
 62: 
 63:         if( isset($data['dtstart']) ) {
 64:             $this->start = $this->getTimestamp($data['dtstart'], $ical);
 65:             unset($data['dtstart']);
 66:         }
 67: 
 68:         if( isset($data['dtend']) ) {
 69:             $this->end = $this->getTimestamp($data['dtend'], $ical);
 70:             unset($data['dtend']);
 71:         } elseif( isset($data['duration']) ) {
 72:             $dur = new Duration( $data['duration']->getData() );
 73:             $this->end = $this->start + $dur->getDuration();
 74:             unset($data['duration']);
 75:         }
 76: 
 77:         //google cal set dtend as end of initial event (duration)
 78:         if ( isset($this->recurrence) ) {
 79:             //if there is a recurrence rule
 80: 
 81:             //exclusions
 82:             if ( isset($data['exdate']) ) {
 83:                 foreach ($data['exdate'] as $exdate) {
 84:                     foreach ($exdate->getDataAsArray() as $ts) {
 85:                         $this->excluded[] = strtotime($ts);
 86:                     }
 87:                 }
 88:                 unset($data['exdate']);
 89:             }
 90:             //additions
 91:             if ( isset($data['rdate']) ) {
 92:                 foreach ($data['rdate'] as $rdate) {
 93:                     foreach ($rdate->getDataAsArray() as $ts) {
 94:                         $this->added[] = strtotime($ts);
 95:                     }
 96:                 }
 97:                 unset($data['rdate']);
 98:             }
 99: 
100:             $until = $this->recurrence->getUntil();
101:             $count = $this->recurrence->getCount();
102:             //check if there is either 'until' or 'count' set
103:             if ( $until ) {
104:                 //ok..
105:             } elseif ($count) {
106:                 //if count is set, then figure out the last occurrence and set that as the end date
107:                 $this->getFrequency();
108:                 $until = $this->freq->lastOccurrence($this->start);
109:             } else {
110:                 //forever... limit to 3 years
111:                 $this->recurrence->setUntil('+3 years');
112:                 $until = $this->recurrence->getUntil();
113:             }
114:             //date_default_timezone_set( xx ) needed ?;
115:             $this->laststart = strtotime($until);
116:             $this->lastend = $this->laststart + $this->getDuration();
117:         }
118: 
119:         $imports = array('summary','description','location');
120:         foreach( $imports AS $import ) {
121:             if( isset($data[$import]) ) {
122:                 $this->$import = $data[$import]->getData();
123:                 unset($data[$import]);
124:             }
125:         }
126: 
127:         if( isset($this->previous_tz) ) {
128:             date_default_timezone_set($this->previous_tz);
129:         }
130: 
131:         $this->data = Line::Remove_Line($data);
132:     }
133: 
134: 
135:     /**
136:      * Returns the Event Occurrences Iterator (if recurrence set)
137:      * @return intouch\ical\Freq
138:      */
139:     public function getFrequency() {
140:         if (! isset($this->freq)) {
141:             if ( isset($this->recurrence) ) {
142:                 $this->freq = new Freq($this->recurrence->rrule, $this->start, $this->excluded, $this->added);
143:             }
144:         }
145:         return $this->freq;
146:     }
147: 
148:     /**
149:      * Returns the UID of the event
150:      * @return string
151:      */
152:     public function getUID() {
153:         return $this->uid;
154:     }
155: 
156:     /**
157:      * Returns the summary (or null if none is given) of the event
158:      * @return string
159:      */
160:     public function getSummary() {
161:         return $this->summary;
162:     }
163: 
164:     /**
165:      * Returns the description (or null if none is given) of the event
166:      * @return string
167:      */
168:     public function getDescription() {
169:         return $this->description;
170:     }
171: 
172:     /**
173:      * Returns the location (or null if none is given) of the event
174:      * @return string
175:      */
176:     public function getLocation() {
177:         return $this->location;
178:     }
179: 
180:     /**
181:      * Returns true if the event is blocking (ie not transparent)
182:      * @return bool
183:      */
184:     public function isBlocking() {
185:         return !(isset($this->data['transp']) && $this->data['transp'] == 'TRANSPARENT');
186:     }
187: 
188:     /**
189:      * Returns true if the event is confirmed
190:      * @return bool
191:      */
192:     public function isConfirmed() {
193:         if( !isset($this->data['status']) ) {
194:             return self::DEFAULT_CONFIRMED;
195:         } else {
196:             return $this->data['status'] == 'CONFIRMED';
197:         }
198:     }
199: 
200:     /**
201:      * Returns true if duration is multiple of 86400
202:      * @return bool
203:      */
204:     public function isWholeDay() {
205:         $dur = $this->getDuration();
206:         if ($dur > 0 && ($dur % 86400) == 0) {
207:             return true;
208:         }
209:         return false;
210:     }
211: 
212:     /**
213:      * Returns the timestamp for the beginning of the event
214:      * @return int
215:      */
216:     public function getStart() {
217:         return $this->start;
218:     }
219: 
220:     /**
221:      * Returns the timestamp for the end of the event
222:      * @return int
223:      */
224:     public function getEnd() {
225:         return $this->end;
226:     }
227: 
228:     /**
229:      * Returns the timestamp for the end of the last event
230:      * @return int
231:      */
232:     public function getRangeEnd() {
233:         return max($this->end,$this->lastend);
234:     }
235: 
236:     /**
237:      * Returns the duration of this event in seconds
238:      * @return int
239:      */
240:     public function getDuration() {
241:         return $this->end - $this->start;
242:     }
243: 
244:     /**
245:      * Returns the given property of the event.
246:      * @param string $prop
247:      * @return string
248:      */
249:     public function getProperty( $prop ) {
250:         if( isset($this->$prop) ) {
251:             return $this->$prop;
252:         } elseif( isset($this->data[$prop]) ) {
253:             return $this->data[$prop];
254:         } else {
255:             return null;
256:         }
257:     }
258: 
259: 
260: 
261:     /**
262:      * Set default timezone (temporary) to get timestamps
263:      * @return string
264:      */
265:     protected function setLineTimeZone( Line $line ) {
266:         if( isset($line['tzid']) ) {
267:             if (!isset($this->previous_tz)) {
268:                 $this->previous_tz = @ date_default_timezone_get();
269:             }
270:             $this->tzid = $line['tzid'];
271:             date_default_timezone_set($this->tzid);
272:             return true;
273:         }
274:         return false;
275:     }
276: 
277:     /**
278:      * Calculates the timestamp from a DT line.
279:      * @param $line intouch\ical\Line
280:      * @return int
281:      */
282:     protected function getTimestamp( Line $line, iCal $ical ) {
283: 
284:         if( isset($line['tzid']) ) {
285:             $this->setLineTimeZone($line);
286:             //$tz = $ical->getTimeZoneInfo($line['tzid']);
287:             //$offset = $tz->getOffset($ts);
288:             //$ts = strtotime(date('D, d M Y H:i:s', $ts) . ' ' . $offset);
289:         }
290:         $ts = strtotime($line->getData());
291: 
292:         return $ts;
293:     }
294: }
295: 
intouch-iCalendar API documentation generated by ApiGen 2.8.0