Source for file instrument.php

Documentation is available at instrument.php

  1. <?php
  2. /**
  3. * Instrument object (extends dbo), with extra customisations for other links
  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 DBObjects
  11. */
  12.  
  13. /** Load ancillary functions */
  14. require_once 'inc/typeinfo.php';
  15.  
  16. require_once 'inc/bb/configreader.php';
  17.  
  18. /** parent object */
  19. require_once 'inc/formslib/dbrow.php';
  20. require_once 'inc/formslib/idfield.php';
  21. require_once 'inc/formslib/textfield.php';
  22. require_once 'inc/formslib/textarea.php';
  23. require_once 'inc/formslib/commentfield.php';
  24. require_once 'inc/formslib/checkbox.php';
  25. require_once 'inc/formslib/radiolist.php';
  26. require_once 'inc/formslib/exampleentries.php';
  27. require_once 'inc/bookings/timeslotrule.php';
  28.  
  29. /**
  30. * Instrument object (extends dbo), with extra customisations for other links
  31. *
  32. * @package    Bumblebee
  33. * @subpackage DBObjects
  34. */
  35. class Instrument extends DBRow {
  36.  
  37.   var $_slotrule;
  38.  
  39.   function Instrument($id) {
  40.     $conf = ConfigReader::getInstance();
  41.     //$this->DEBUG=10;
  42.     $this->DBRow('instruments', $id);
  43.     $this->editable = 1;
  44.     $this->deleteFromTable = 0;
  45.     $f = new IdField('id', T_('Instrument ID'));
  46.     $f->editable = 0;
  47.     $this->addElement($f);
  48.     $f = new TextField('name', T_('Name'));
  49.     $attrs = array('size' => '48');
  50.     $f->required = 1;
  51.     $f->isValidTest = 'is_nonempty_string';
  52.     $f->setAttr($attrs);
  53.     $this->addElement($f);
  54.     $f = new TextField('longname', T_('Description'));
  55.     $f->required = 1;
  56.     $f->isValidTest = 'is_nonempty_string';
  57.     $f->setAttr($attrs);
  58.     $this->addElement($f);
  59.     $f = new TextField('location', T_('Location'));
  60.     $f->required = 1;
  61.     $f->isValidTest = 'is_nonempty_string';
  62.     $f->setAttr($attrs);
  63.     $this->addElement($f);
  64.     $f = new TextField('usualopen', T_('Calendar start time (HH:MM)'));
  65.     $f->required = 1;
  66.     $f->defaultValue = $conf->value('instruments', 'usualopen');
  67.     $f->isValidTest = 'is_valid_time';
  68.     $f->setAttr($attrs);
  69.     $this->addElement($f);
  70.     $f = new TextField('usualclose', T_('Calendar end time (HH:MM)'));
  71.     $f->required = 1;
  72.     $f->defaultValue = $conf->value('instruments', 'usualclose');
  73.     $f->isValidTest = 'is_valid_time';
  74.     $f->setAttr($attrs);
  75.     $this->addElement($f);
  76.     $f = new TextField('calprecision', T_('Precision of calendar display (seconds)'));
  77.     $f->required = 1;
  78.     $f->defaultValue = $conf->value('instruments', 'usualprecision');
  79.     $f->isValidTest = 'is_number';
  80.     $f->setAttr($attrs);
  81.     $this->addElement($f);
  82.     $f = new TextField('caltimemarks', T_('Time-periods per HH:MM displayed'));
  83.     $f->required = 1;
  84.     $f->defaultValue = $conf->value('instruments', 'usualtimemarks');
  85.     $f->isValidTest = 'is_number';
  86.     $f->setAttr($attrs);
  87.     $this->addElement($f);
  88.     $f = new TextField('callength', T_('Number of weeks displayed in calendar'));
  89.     $f->required = 1;
  90.     $f->defaultValue = $conf->value('instruments', 'usualcallength');
  91.     $f->isValidTest = 'is_number';
  92.     $f->setAttr($attrs);
  93.     $this->addElement($f);
  94.     $f = new TextField('calhistory', T_('Number of weeks history shown'));
  95.     $f->required = 1;
  96.     $f->defaultValue = $conf->value('instruments', 'usualcalhistory');
  97.     $f->isValidTest = 'is_number';
  98.     $f->setAttr($attrs);
  99.     $this->addElement($f);
  100.     $f = new TextField('calfuture', T_('Number of days into the future'));
  101.     $f->required = 1;
  102.     $f->defaultValue = $conf->value('instruments', 'usualcalfuture');
  103.     $f->isValidTest = 'is_number';
  104.     $f->setAttr($attrs);
  105.     $this->addElement($f);
  106.     $f = new TextArea('calendarcomment', T_('Advisory text'),
  107.           T_('Displayed at the bottom of instrument calendar and on booking form. HTML permitted.'));
  108.     $f->setAttr(array('rows' =>5, 'cols' => 30));
  109.     $f->required = 0;
  110.     $this->addElement($f);
  111.  
  112.     // associate with a charging class
  113.     $f = new RadioList('class', T_('Charging class'));
  114.     $f->connectDB('instrumentclass', array('id', 'name'));
  115.     $classexample = new ExampleEntries('id','instruments','class','name',3);
  116.     $classexample->separator = '; ';
  117.     $f->setFormat('id', '%s', array('name'), ' (%40.40s)', $classexample);
  118.     $newclassname = new TextField('name','');
  119.     $newclassname->namebase = 'newclass-';
  120.     $newclassname->setAttr(array('size' => 24));
  121.     $newclassname->isValidTest = 'is_nonempty_string';
  122.     $newclassname->suppressValidation = 0;
  123.     $f->list->append(array('-1',T_('Create new:').' '), $newclassname);
  124.     $f->setAttr($attrs);
  125.     $f->extendable = 1;
  126.     $f->required = 1;
  127.     $f->isValidTest = 'is_valid_radiochoice';
  128.     $this->addElement($f);
  129.     $f = new TextField('halfdaylength', T_('Hours in a half-day'));
  130.     $f->required = 1;
  131.     $f->defaultValue = $conf->value('instruments', 'usualhalfdaylength');
  132.     $f->isValidTest = 'is_number';
  133.     $f->setAttr($attrs);
  134.     $this->addElement($f);
  135.     $f = new TextField('fulldaylength', T_('Hours in a full-day'));
  136.     $f->required = 1;
  137.     $f->defaultValue = $conf->value('instruments', 'usualfulldaylength');
  138.     $f->isValidTest = 'is_number';
  139.     $f->setAttr($attrs);
  140.     $this->addElement($f);
  141.  
  142.     // create the timeslot rule information required
  143.     $f = new CommentField('timeslotpicturecomment', T_('Instrument timeslots'),
  144.                               T_('These fields describe when the instrument is available.'));
  145.     $f->value = T_('Format: HH:MM-HH:MM/n-x%,comment');
  146.     $this->addElement($f);
  147.     $f = new TextField('timeslotpicture', T_('Time slot picture'));
  148.     $f->required = 1;
  149.     $f->hidden = 1;
  150.     $f->defaultValue = $conf->value('instruments', 'usualtimeslotpicture');
  151.     $f->isValidTest = 'is_set';
  152.     $f->setAttr($attrs);
  153.     $this->addElement($f);
  154.     $weekstart = new SimpleDate(time());
  155.     $weekstart->weekRound();
  156.     for ($day=0; $day<7; $day++) {
  157.       $today = clone($weekstart);
  158.       $today->addDays($day);
  159.       $f = new TextArea('tsr-'.$day, $today->dowStr(), T_('Slots in day, one per line'));
  160.       $f->sqlHidden = 1;
  161.       $f->setAttr(array('rows' =>3, 'cols' => 30));
  162.       $f->required = 1;
  163.       $this->addElement($f);
  164.     }
  165.  
  166.     $f = new TextField('mindatechange', T_('Minimum notice for booking change (hours)'));
  167.     $f->required = 1;
  168.     $f->defaultValue = $conf->value('instruments', 'usualmindatechange');
  169.     $f->isValidTest = 'is_number';
  170.     $f->setAttr($attrs);
  171.     $this->addElement($f);
  172.     $f = new TextField('supervisors', T_('Instrument supervisors'), T_('comma separated list of usernames'));
  173.     $f->required = 0;
  174.     $f->setAttr($attrs);
  175.     $this->addElement($f);
  176.     $f = new CheckBox('emailonbooking', T_('Email supervisors when booking'));
  177.     $f->defaultValue = 0;
  178.     $f->setAttr($attrs);
  179.     $this->addElement($f);
  180.  
  181.     $this->fill();
  182.     $this->dumpheader = 'Instrument object';
  183.   }
  184.  
  185.   function fill() {
  186.     parent::fill();
  187.     //now edit the time slot representation fields
  188.     $this->_calcSlotRepresentation();
  189.   }
  190.  
  191.   function sync() {
  192.     //first construct a timeslot field from the submitted data, then do the sync
  193.     $newslotrule = $this->_calcNewSlotRule();
  194.     if ($this->fields['timeslotpicture']->value != $newslotrule /*&& $this->id > -1*/) {
  195.       $this->log('Instrument::sync(): indulging in timeslotrule munging: <br />'.
  196.                     $newslotrule .'<br/>'.$this->fields['timeslotpicture']->value);
  197.       $this->fields['timeslotpicture']->set($newslotrule);
  198.       $this->fields['timeslotpicture']->changed = 1;
  199.       $this->changed = $this->changed || ($this->id > -1);
  200.       // reflect back the data, this is good for checking that it's right, as TimeSlotRule
  201.       // will drop bits it doesn't understand or doesn't like.
  202.       //$this->_calcSlotRepresentation();
  203.     } else {
  204.       //?
  205.     }
  206.    //$this->DEBUG=10;
  207.    return parent::sync();
  208.   }
  209.  
  210.   function _calcSlotRepresentation() {
  211.     $this->_slotrule = new TimeSlotRule($this->fields['timeslotpicture']->getValue());
  212.     for ($day=0; $day<7; $day++) {
  213.       $this->fields['tsr-'.$day]->value = '';
  214.       //preDump($this->_slotrule->slots[$day]);
  215.       $prevpicture = '';
  216.       foreach ($this->_slotrule->slots[$day] as $key => $slot) {
  217.         if (is_numeric($key) && $slot->picture != $prevpicture) {
  218.           $prevpicture = $slot->picture;
  219.           //preDump($slot);
  220.           $this->log('Added picture '. $slot->picture);
  221.           $this->fields['tsr-'.$day]->value .= $slot->picture."\n";
  222.         }
  223.       }
  224.     }
  225.   }
  226.  
  227.   function _calcNewSlotRule() {
  228.     $newslot = '';
  229.     for ($day=0; $day<7; $day++) {
  230.       //preDump($this->fields['tsr-'.$day]->value);
  231.       $lines = preg_split('/[\n\r]+/', $this->fields['tsr-'.$day]->value);
  232.       // get rid of blanks
  233.       $lines = preg_grep('/^\s*$/', $lines, PREG_GREP_INVERT);
  234.       $rejects = preg_grep('{^\d\d:\d\d\-\d\d:\d\d/(\d+|\*)(\-\d+%)?(,.+)?$}', $lines,PREG_GREP_INVERT);
  235.       if (count($rejects) > 0) {
  236.         //then this input is invalid
  237.         $this->fields['tsr-'.$day]->isValid = 0;
  238.         $this->isValid = 0;
  239.         //preDump($rejects);
  240.       }
  241.       $newslot .= '['.$day.']<'.join($lines,';').'>';
  242.       $this->log('Calculated picture '. $newslot);
  243.     }
  244.     return $newslot;
  245.   }
  246.  
  247.   function display() {
  248.     return $this->displayAsTable();
  249.   }
  250.  

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