Source for file bookingdata.php

Documentation is available at bookingdata.php

  1. <?php
  2. /**
  3. * Obtains booking data from the database
  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. /** loads data into Booking objects */
  17. require_once 'booking.php';
  18.  
  19. /**
  20. * Obtains booking data from the database
  21. *
  22. * Can be used either to find all bookings that are within a time period
  23. * or to look for a specific booking id. Note that when using time periods
  24. * to define the query, a booking is within the query time if *any* part of
  25. * the booking period overlaps with *any* part of the query period.
  26. *
  27. * Can optionally exclude deleted bookings from the list.
  28. *
  29. @package    Bumblebee
  30. @subpackage Bookings
  31. */
  32. class BookingData {
  33.   /** @var string   start of the query (SQL date-time string)  */
  34.   var $start;
  35.   /** @var string   stop of the query (SQL date-time string)  */
  36.   var $stop;
  37.   /** @var integer  instrument id number of interest  */
  38.   var $instrument;
  39.   /** @var integer  specific booking that is of interest */
  40.   var $id;
  41.   /** @var array    list of Booking objects returned for time period */
  42.   var $bookinglist;
  43.   /** @var Booking  single Booking object returned for id match */
  44.   var $booking;
  45.   /** @var boolean  sql errors are fatal */
  46.   var $fatal_sql = 1;
  47.   /** @var boolean  include deleted bookings in listing */
  48.   var $includeDeleted = 0;
  49.  
  50.   /**
  51.   * Obtain the booking listing within defined parameters
  52.   *
  53.   * @param array  $arr    data => value pairs where data can be
  54.   *                        start, stop, instrument, id
  55.   */
  56.   function BookingData($arr$includeDeleted=false{
  57.     if ($start issetSet($arr,'start')) {
  58.       $this->start = $start->dateTimeString();
  59.     }
  60.     if ($stop issetSet($arr,'stop')) {
  61.       $this->stop = $stop->dateTimeString();
  62.     }
  63.     $this->instrument  = issetSet($arr,'instrument');
  64.     $this->id  = issetSet($arr,'id');
  65.     $this->includeDeleted = $includeDeleted;
  66.     $this->_fill();
  67.   }
  68.  
  69.   /**
  70.   * Interrogate the database to get the bookings
  71.   *
  72.   * @global string  prefix prepended to all table names in database
  73.   */
  74.   function _fill({
  75.     global $TABLEPREFIX;
  76.     $q 'SELECT bookings.id AS bookid,bookwhen,duration,'
  77.         .'DATE_ADD(bookwhen, INTERVAL duration HOUR_SECOND) AS stoptime,'
  78.         .'discount,log,comments,projectid,'
  79.         .'userid,'
  80.         .'users.name AS name, '
  81.         .'users.username AS username, '
  82.         .'users.email AS email, '
  83.         .'users.phone AS phone, '
  84.         .'bookedby AS masquserid, '
  85.         .'masq.name AS masquser, '
  86.         .'masq.username AS masqusername, '
  87.         .'masq.email AS masqemail, '
  88.         .'projects.name AS project, '
  89.         .'instruments.name AS instrumentname, '
  90.         .'instruments.longname AS instrumentdescription '
  91.         .'FROM '.$TABLEPREFIX.'bookings AS bookings '
  92.         .'LEFT JOIN '.$TABLEPREFIX.'users AS users ON '
  93.             .'bookings.userid=users.id '
  94.         .'LEFT JOIN '.$TABLEPREFIX.'users AS masq ON '
  95.             .'bookings.bookedby=masq.id '
  96.         .'LEFT JOIN '.$TABLEPREFIX.'projects AS projects ON '
  97.             .'bookings.projectid=projects.id '
  98.         .'LEFT JOIN '.$TABLEPREFIX.'instruments AS instruments ON '
  99.             .'bookings.instrument=instruments.id '
  100.         .'WHERE '.($this->includeDeleted ? '' 'bookings.deleted<>1 AND ');
  101.     if ($this->id{
  102.       $q .= 'bookings.id='.qw($this->id);
  103.     else {
  104.       $q .= 'bookings.userid<>0 AND ';
  105.       if (is_array($this->instrument)) {
  106.         $q .= 'bookings.instrument IN ('.join(array_qw($this->instrument),',').') ';
  107.       else {
  108.         $q .= 'bookings.instrument='.qw($this->instrument).' ';
  109.       }
  110.  
  111.       $q .= 'HAVING (bookwhen <= '.qw($this->start).' AND stoptime > '.qw($this->start).') '
  112.             .'OR (bookwhen < '.qw($this->stop).' AND stoptime >= '.qw($this->stop).') '
  113.             .'OR (bookwhen >= '.qw($this->start).' AND stoptime <= '.qw($this->stop).')'
  114.            .' ORDER BY bookwhen';
  115.       if (is_array($this->instrument)) {
  116.         $q .= ', duration DESC';
  117.       }
  118.     }
  119.  
  120.     if ($this->id{
  121.       $g db_get_single($q$this->fatal_sql);
  122.       $this->booking = new Booking($g);
  123.     else {
  124.       $this->bookinglist = array();
  125.       $sql db_get($q$this->fatal_sql);
  126.       while ($g db_fetch_array($sql)) {
  127.         $this->bookinglist[new Booking($g);
  128.       }
  129.     }
  130.   }
  131.  
  132.   /**
  133.   * obtain the list of bookings
  134.   * @return array  list of Booking objects
  135.   */
  136.   function dataArray({
  137.     return $this->bookinglist;
  138.   }
  139.  
  140.   /**
  141.   * obtain the bookings
  142.   * @return Booking booking object
  143.   */
  144.   function dataEntry({
  145.     return $this->booking;
  146.   }
  147.  
  148. //class BookingData
  149.  
  150. /**
  151. * Find the next booking already in the database
  152. *
  153. @package    Bumblebee
  154. @subpackage Bookings
  155. */
  156. class NextBooking {
  157.   /** @var string   start of the query (SQL date-time string)  */
  158.   var $start;
  159.   /** @var mixed    instrument id number or list of ids of interest  */
  160.   var $instrument;
  161.   /** @var SimpleDate  start of the next booking (null if none) */
  162.   var $booking = null;
  163.   /** @var boolean  sql errors are fatal */
  164.   var $fatal_sql = 1;
  165.   /** @var boolean  include deleted bookings in listing */
  166.   var $includeDeleted = 0;
  167.  
  168.   /**
  169.   * Obtain the booking listing within defined parameters
  170.   *
  171.   * @param SimpleDate  $start       start date for searching for the next booking
  172.   * @param mixed       $instrument  id for the instrument to check or list of ids
  173.   */
  174.   function NextBooking($start$instrument{
  175.     $s new SimpleDate($start);
  176.     $this->start       = $s->dateTimeString();
  177.     $this->instrument  = $instrument;
  178.     $this->_fill();
  179.   }
  180.  
  181.   /**
  182.   * Interrogate the database to get the bookings
  183.   *
  184.   * @global string  prefix prepended to all table names in database
  185.   */
  186.   function _fill({
  187.     global $TABLEPREFIX;
  188.     $q 'SELECT bookings.id AS bookid, '
  189.         .'bookwhen '
  190.         .'FROM '.$TABLEPREFIX.'bookings AS bookings '
  191.         .'WHERE bookings.userid<>0 AND ';
  192.     if (is_array($this->instrument)) {
  193.       $q .= 'bookings.instrument IN ('.join(array_qw($this->instrument),',').') ';
  194.     else {
  195.       $q .= 'bookings.instrument='.qw($this->instrument).' ';
  196.     }
  197.     $q .= 'AND bookwhen > '.qw($this->start).' '
  198.         .'ORDER BY bookwhen '
  199.         .'LIMIT 1';
  200.  
  201.     $g db_get_single($q$this->fatal_sql);
  202.     if (is_array($g)) {
  203.       $this->booking = new SimpleDate($g['bookwhen']);
  204.     }
  205.   }
  206.  

Documentation generated on Tue, 06 Mar 2007 10:00:48 +0000 by phpDocumentor 1.3.0