[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * Class representing vFreebusy components. 4 * 5 * $Horde: framework/iCalendar/iCalendar/vfreebusy.php,v 1.16.10.9 2006/02/06 17:53:39 mrubinsk Exp $ 6 * 7 * Copyright 2003-2006 Mike Cochrane <mike@graftonhall.co.nz> 8 * 9 * See the enclosed file COPYING for license information (LGPL). If you did 10 * not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 11 * 12 * @author Mike Cochrane <mike@graftonhall.co.nz> 13 * @since Horde 3.0 14 * @package Horde_iCalendar 15 */ 16 class Horde_iCalendar_vfreebusy extends Horde_iCalendar { 17 18 var $_busyPeriods = array(); 19 20 function getType() 21 { 22 return 'vFreebusy'; 23 } 24 25 /** 26 * Parses a string containing vFreebusy data. 27 * 28 * @param string $data The data to parse. 29 */ 30 function parsevCalendar($data) 31 { 32 parent::parsevCalendar($data, 'VFREEBUSY'); 33 34 // Do something with all the busy periods. 35 foreach ($this->_attributes as $key => $attribute) { 36 if ($attribute['name'] == 'FREEBUSY') { 37 foreach ($attribute['values'] as $value) { 38 if (isset($value['duration'])) { 39 $this->addBusyPeriod('BUSY', $value['start'], null, $value['duration']); 40 } else { 41 $this->addBusyPeriod('BUSY', $value['start'], $value['end']); 42 } 43 } 44 unset($this->_attributes[$key]); 45 } 46 } 47 } 48 49 function exportvCalendar() 50 { 51 foreach ($this->_busyPeriods as $start => $end) { 52 $periods = array(array('start' => $start, 'end' => $end)); 53 $this->setAttribute('FREEBUSY', $periods); 54 } 55 56 $res = parent::_exportvData('VFREEBUSY'); 57 58 foreach ($this->_attributes as $key => $attribute) { 59 if ($attribute['name'] == 'FREEBUSY') { 60 unset($this->_attributes[$key]); 61 } 62 } 63 64 return $res; 65 } 66 67 /** 68 * Returns a display name for this object. 69 * 70 * @return string A clear text name for displaying this object. 71 */ 72 function getName() 73 { 74 $name = ''; 75 $method = !empty($this->_container) ? 76 $this->_container->getAttribute('METHOD') : 'PUBLISH'; 77 78 if (is_a($method, 'PEAR_Error') || $method == 'PUBLISH') { 79 $attr = 'ORGANIZER'; 80 } elseif ($method == 'REPLY') { 81 $attr = 'ATTENDEE'; 82 } 83 84 $name = $this->getAttribute($attr, true); 85 if (isset($name[0]['CN'])) { 86 return $name[0]['CN']; 87 } 88 89 $name = $this->getAttribute($attr); 90 if (is_a($name, 'PEAR_Error')) { 91 return ''; 92 } else { 93 $name = parse_url($name); 94 return $name['path']; 95 } 96 } 97 98 /** 99 * Returns the email address for this object. 100 * 101 * @return string The email address of this object's owner. 102 */ 103 function getEmail() 104 { 105 $name = ''; 106 $method = !empty($this->_container) 107 ? $this->_container->getAttribute('METHOD') : 'PUBLISH'; 108 109 if (is_a($method, 'PEAR_Error') || $method == 'PUBLISH') { 110 $attr = 'ORGANIZER'; 111 } elseif ($method == 'REPLY') { 112 $attr = 'ATTENDEE'; 113 } 114 115 $name = $this->getAttribute($attr); 116 if (is_a($name, 'PEAR_Error')) { 117 return ''; 118 } else { 119 $name = parse_url($name); 120 return $name['path']; 121 } 122 } 123 124 function getBusyPeriods() 125 { 126 return $this->_busyPeriods; 127 } 128 129 /** 130 * Returns all the free periods of time in a given period. 131 * 132 * @param integer $startStamp The start timestamp. 133 * @param integer $endStamp The end timestamp. 134 * 135 * @return array A hash with free time periods, the start times as the 136 * keys and the end times as the values. 137 */ 138 function getFreePeriods($startStamp, $endStamp) 139 { 140 $this->simplify(); 141 $periods = array(); 142 143 // Check that we have data for some part of this period. 144 if ($this->getEnd() < $startStamp || $this->getStart() > $endStamp) { 145 return $periods; 146 } 147 148 // Locate the first time in the requested period we have data for. 149 $nextstart = max($startStamp, $this->getStart()); 150 151 // Check each busy period and add free periods in between. 152 foreach ($this->_busyPeriods as $start => $end) { 153 if ($start <= $endStamp && $end >= $nextstart) { 154 if ($nextstart <= $start) { 155 $periods[$nextstart] = min($start, $endStamp); 156 } 157 $nextstart = min($end, $endStamp); 158 } 159 } 160 161 // If we didn't read the end of the requested period but still have 162 // data then mark as free to the end of the period or available data. 163 if ($nextstart < $endStamp && $nextstart < $this->getEnd()) { 164 $periods[$nextstart] = min($this->getEnd(), $endStamp); 165 } 166 167 return $periods; 168 } 169 170 /** 171 * Adds a busy period to the info. 172 * 173 * @param string $type The type of the period. Either 'FREE' or 174 * 'BUSY'; only 'BUSY' supported at the moment. 175 * @param integer $start The start timestamp of the period. 176 * @param integer $end The end timestamp of the period. 177 * @param integer $duration The duration of the period. If specified, the 178 * $end parameter will be ignored. 179 */ 180 function addBusyPeriod($type, $start, $end = null, $duration = null) 181 { 182 if ($type == 'FREE') { 183 // Make sure this period is not marked as busy. 184 return false; 185 } 186 187 // Calculate the end time if duration was specified. 188 $tempEnd = is_null($duration) ? $end : $start + $duration; 189 190 // Make sure the period length is always positive. 191 $end = max($start, $tempEnd); 192 $start = min($start, $tempEnd); 193 194 if (isset($this->_busyPeriods[$start])) { 195 // Already a period starting at this time. Extend to the length of 196 // the longest of the two. 197 $this->_busyPeriods[$start] = max($end, $this->_busyPeriods[$start]); 198 } else { 199 // Add a new busy period. 200 $this->_busyPeriods[$start] = $end; 201 } 202 203 return true; 204 } 205 206 /** 207 * Returns the timestamp of the start of the time period this free busy 208 * information covers. 209 * 210 * @return integer A timestamp. 211 */ 212 function getStart() 213 { 214 if (!is_a($this->getAttribute('DTSTART'), 'PEAR_Error')) { 215 return $this->getAttribute('DTSTART'); 216 } elseif (count($this->_busyPeriods)) { 217 return min(array_keys($this->_busyPeriods)); 218 } else { 219 return false; 220 } 221 } 222 223 /** 224 * Returns the timestamp of the end of the time period this free busy 225 * information covers. 226 * 227 * @return integer A timestamp. 228 */ 229 function getEnd() 230 { 231 if (!is_a($this->getAttribute('DTEND'), 'PEAR_Error')) { 232 return $this->getAttribute('DTEND'); 233 } elseif (count($this->_busyPeriods)) { 234 return max(array_values($this->_busyPeriods)); 235 } else { 236 return false; 237 } 238 } 239 240 /** 241 * Merges the busy periods of another Horde_iCalendar_vfreebusy object into 242 * this one. 243 * 244 * @param Horde_iCalendar_vfreebusy $freebusy A freebusy object. 245 * @param boolean $simplify If true, simplify() will 246 * called after the merge. 247 */ 248 function merge($freebusy, $simplify = true) 249 { 250 if (!is_a($freebusy, 'Horde_iCalendar_vfreebusy')) { 251 return false; 252 } 253 254 foreach ($freebusy->getBusyPeriods() as $start => $end) { 255 $this->addBusyPeriod('BUSY', $start, $end); 256 } 257 258 $thisattr = $this->getAttribute('DTSTART'); 259 $thatattr = $freebusy->getAttribute('DTSTART'); 260 if (is_a($thisattr, 'PEAR_Error') && !is_a($thatattr, 'PEAR_Error')) { 261 $this->setAttribute('DTSTART', $thatattr); 262 } elseif (!is_a($thatattr, 'PEAR_Error')) { 263 if ($thatattr > $thisattr) { 264 $this->setAttribute('DTSTART', $thatattr); 265 } 266 } 267 268 $thisattr = $this->getAttribute('DTEND'); 269 $thatattr = $freebusy->getAttribute('DTEND'); 270 if (is_a($thisattr, 'PEAR_Error') && !is_a($thatattr, 'PEAR_Error')) { 271 $this->setAttribute('DTEND', $thatattr); 272 } elseif (!is_a($thatattr, 'PEAR_Error')) { 273 if ($thatattr < $thisattr) { 274 $this->setAttribute('DTEND', $thatattr); 275 } 276 } 277 278 if ($simplify) { 279 $this->simplify(); 280 } 281 282 return true; 283 } 284 285 /** 286 * Removes all overlaps and simplifies the busy periods array as much as 287 * possible. 288 */ 289 function simplify() 290 { 291 $checked = array(); 292 $checkedEmpty = true; 293 294 foreach ($this->_busyPeriods as $start => $end) { 295 if ($checkedEmpty) { 296 $checked[$start] = $end; 297 $checkedEmpty = false; 298 } else { 299 $added = false; 300 foreach ($checked as $testStart => $testEnd) { 301 if ($start == $testStart) { 302 $checked[$testStart] = max($testEnd, $end); 303 $added = true; 304 } elseif ($end <= $testEnd && $end >= $testStart) { 305 unset($checked[$testStart]); 306 $checked[min($testStart, $start)] = max($testEnd, $end); 307 $added = true; 308 } 309 if ($added) { 310 break; 311 } 312 } 313 if (!$added) { 314 $checked[$start] = $end; 315 } 316 } 317 } 318 319 ksort($checked, SORT_NUMERIC); 320 $this->_busyPeriods = $checked; 321 } 322 323 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |