Source for file matrix.php

Documentation is available at matrix.php

  1. <?php
  2. /**
  3. * Booking matrix object for display in a table
  4. *
  5. * @author    Stuart Prescott
  6. * @copyright  Copyright Stuart Prescott
  7. * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  8. * @version    $Id$
  9. * @package    Bumblebee
  10. * @subpackage Bookings
  11. */
  12.  
  13. /** Load ancillary functions */
  14. require_once 'inc/typeinfo.php';
  15.  
  16. /**
  17. * Booking matrix object for display in a table
  18. *
  19. * @package    Bumblebee
  20. * @subpackage Bookings
  21. * @todo //TODO:       documentation
  22. */
  23. class BookingMatrix {
  24.   var $dayStart;
  25.   var $dayStop;
  26.   var $day; 
  27.   var $granularity;
  28.   var $bookings;
  29.   var $numRows;
  30.   var $rows;
  31.   var $_lastnum = 0;
  32.   
  33.   function BookingMatrix($dayStart, $dayStop, $granularity, &$bookings) {
  34.     $this->dayStart    = $dayStart;
  35.     $this->dayStop     = $dayStop;
  36.     $this->granularity = $granularity;
  37.     $this->bookings    = $bookings;
  38.   }
  39.   
  40.   function setDate($date) {
  41.     $this->day = $date;
  42.     $this->rows = array();
  43.   }
  44.   
  45.   function getMatrix() {
  46.     return $this->rows;
  47.   }
  48.  
  49.   function prepareMatrix() {
  50.     $this->numRows = $this->dayStop->subtract($this->dayStart)
  51.                         / $this->granularity;
  52.     $numBookings = count($this->bookings);
  53.  
  54.     #echo "Preparing matrix with $this->numRows rows for date "
  55.         #.$this->day->dateString()."<br/>\n";
  56.  
  57.     // keep track of where we got up to in the calculations (reduces O(n2) to O(n))
  58.     // provides 25% performance boost to this function
  59.     $startnum = ($this->_lastnum - 2 > 0) ? $this->_lastnum : 0;
  60.     $foundFlag = false;
  61.  
  62.     // foreach is much more expensive than for and since this function is called repeatedly,
  63.     // it's best not to do that. (function is 20% faster from this change)
  64.     //foreach ($this->bookings as $booknum => $b) {
  65.     for ($booknum = $startnum; $booknum < $numBookings; $booknum++) {
  66.       $b = $this->bookings[$booknum];
  67.       #echo "Booking $k, ".$b->start->dateTimeString()." - ".$b->stop->dateTimeString()."<br />";
  68.       // the TimeSlot object will cache the dayStart and dayStop calculations.
  69.       // (function is 50% faster with this change)
  70.       $bookDay = clone($this->bookings[$booknum]->dayStart());
  71.       $bookStopDay = clone($this->bookings[$booknum]->dayStop());
  72.       #echo "Checking eligibility for booking: ".$this->day->ticks .'='.$bookDay->ticks.'||'.$bookStopDay->ticks.'<br />';
  73.       if ($bookDay->ticks == $this->day->ticks) {
  74.         $foundFlag = true;
  75.         $bookDayStart = clone($bookDay);
  76.         $mystart = clone( isset($b->displayStart) ? $b->displayStart : $b->start );
  77.         $mystop  = clone( isset($b->displayStop)  ? $b->displayStop  : $b->stop  );
  78.         $bookDayStart->setTime($this->dayStart);
  79.         //$starttime = $b->start->subtract($bookDayStart);
  80.         $starttime = $mystart->subtract($bookDayStart);
  81.         if ($starttime > 0) {
  82.           //then the start of the booking is after the start time of the matrix
  83.           $rowstart = floor($starttime/$this->granularity);
  84.         } else {
  85.           //the booking starts before the matrix; starting row adjusted
  86.           $rowstart = 0;
  87.         }
  88.         $bookDayStop = clone($bookDay);
  89.         $bookDayStop->setTime($this->dayStop);
  90.         //$stoptime = $b->stop->subtract($bookDayStop);
  91.         $stoptime = $mystop->subtract($bookDayStop);
  92.         if ($stoptime < 0) {
  93.           //the stop time is before the stop time of the matrix
  94.           //$stoptimestart = $b->stop->subtract($bookDayStart);
  95.           $stoptimestart = $mystop->subtract($bookDayStart);
  96.           $rowstop = floor($stoptimestart/$this->granularity);
  97.         } else {
  98.           //the stop time is after the stop time of the matrix,
  99.           //adjust the duration
  100.           $rowstop = $this->numRows;
  101.         }
  102.         $rowspan = round($rowstop - $rowstart);
  103.  
  104.         // Only add the cell to the matrix if the cell doesn't already have an entry in it.
  105.         // Otherwise, the second part of a booking that is split across multiple slots would
  106.         // overwrite the earlier part.
  107.         if (! isset($this->rows[$rowstart]) && $rowspan > 0) {
  108.           $cell = new BookingCell($this->bookings[$booknum],$this->bookings[$booknum]->isStart,$rowspan);
  109.           $this->rows[$rowstart] = $cell;
  110.         }
  111.       } else {
  112.         // since the list of bookings should be in date order, once we get a negative match
  113.         // we can return
  114.         if ($foundFlag) {
  115.           $this->_lastnum = $booknum;
  116.           return;
  117.         }
  118.       }
  119.     }
  120.     $this->_lastnum = $booknum;
  121.   }
  122.  
  123. } //class BookingMatrix
  124. ?>

Documentation generated on Tue, 06 Mar 2007 10:01:45 +0000 by phpDocumentor 1.3.0