[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpgwapi/inc/horde/Horde/iCalendar/ -> vfreebusy.php (source)

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


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7