Source for file daterange.php

Documentation is available at daterange.php

  1. <?php
  2. /**
  3. * DateRange display/data reflection class
  4. *
  5. * A class to handle a simple date range from a form and reflect all other entered data
  6. *
  7. * @author    Stuart Prescott
  8. * @copyright  Copyright Stuart Prescott
  9. * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version    $Id$
  11. * @package    Bumblebee
  12. * @subpackage DBObjects
  13. */
  14.  
  15. /** Load ancillary functions */
  16. require_once 'inc/typeinfo.php';
  17.  
  18. /** date manipulation routines */
  19. require_once 'inc/date.php';
  20. /** quick select javascript object */
  21. require_once 'inc/jsquickwalk.php';
  22. /** parent object */
  23. require_once 'inc/formslib/nondbrow.php';
  24. require_once 'inc/formslib/datefield.php';
  25.  
  26. /** default is current time period */
  27. define('DR_CURRENT',  1);
  28. /** default is next time period */
  29. define('DR_NEXT',     2);
  30. /** default is previous time period */
  31. define('DR_PREVIOUS', 4);
  32.  
  33. /** default time period is one day */
  34. define('DR_DAY',      8);
  35. /** default time period is one week */
  36. define('DR_WEEK',    16);
  37. /** default time period is one month */
  38. define('DR_MONTH',   32);
  39. /** default time period is one quarter (3 months) (alternative) */
  40. define('DR_QTR',     64);
  41. /** default time period is one quarter (3 months) */
  42. define('DR_QUARTER', 64);
  43. /** default time period is one year */
  44. define('DR_YEAR',   128);
  45.  
  46. /**
  47. * DateRange display/data reflection class
  48. *
  49. * A class to handle a simple date range from a form and reflect all other entered data
  50. *
  51. * @author    Stuart Prescott
  52. * @copyright  Copyright Stuart Prescott
  53. * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  54. * @version    $Id$
  55. * @package    Bumblebee
  56. * @subpackage DBObjects
  57. */
  58. class DateRange extends nonDBRow {
  59.   /** @var boolean   include data in hidden fields if not shown  */
  60.   var $reflectData = 1;
  61.   /** @var boolean   include a submit button in the output  */
  62.   var $includeSubmitButton = 1;
  63.  
  64.   function DateRange($name, $longname, $description) {
  65.     parent::nonDBRow($name, $longname, $description);
  66.     //$this->DEBUG=10;
  67.     $startdate = new DateField('startdate', T_('Start period beginning of'),
  68.                           T_('Period starts at the beginning of this day'));
  69.     $startdate->isValidTest = 'is_valid_date';
  70.     $this->addElement($startdate);
  71.     $stopdate  = new DateField('stopdate', T_('Finish period end of'),
  72.                           T_('Period finishes at the end of this day'));
  73.     $stopdate->isValidTest = 'is_valid_date';
  74.     $this->addElement($stopdate);
  75.   }
  76.  
  77.   /**
  78.    * Calculate a sensible range
  79.    *
  80.    * this is calculated from the 'DR_*' data that we have above
  81.    *
  82.    * @param integer $which     DR_NEXT, DR_CURRENT, DR_PREVIOUS
  83.    * @param integer $range     DR_DAY, DR_WEEK, etc...
  84.    * @param integer $basetime  what date is "current" date
  85.    * @return array  (SimpleDate startdate, SimpleDate stopdate)
  86.    */
  87.   function _calcRange($which, $range, $basetime) {
  88.     $start = clone($basetime);
  89.     $stop  = clone($start);
  90.     switch ($range) {
  91.       case DR_YEAR:
  92.         $start->yearRound();
  93.         $stop = clone($start);
  94.         switch ($which) {
  95.           case DR_NEXT:
  96.             $start->addTimeParts(0,0,0,0,0,1);
  97.             $stop->addTimeParts(0,0,0,0,0,2);
  98.             break;
  99.           case DR_PREVIOUS:
  100.             $start->addTimeParts(0,0,0,0,0,-1);
  101.             break;
  102.           case DR_CURRENT:
  103.             $stop->addTimeParts(0,0,0,0,0,1);
  104.         }
  105.         break;
  106.       case DR_QUARTER:
  107.         $start->quarterRound();
  108.         $stop = clone($start);
  109.         switch ($which) {
  110.           case DR_NEXT:
  111.             $start->addTimeParts(0,0,0,0,3);
  112.             $stop->addTimeParts(0,0,0,0,6);
  113.             break;
  114.           case DR_PREVIOUS:
  115.             $start->addTimeParts(0,0,0,0,-3);
  116.             break;
  117.           case DR_CURRENT:
  118.             $stop->addTimeParts(0,0,0,0,3);
  119.         }
  120.         break;
  121.       case DR_MONTH:
  122.         $start->monthRound();
  123.         $stop = clone($start);
  124.         switch ($which) {
  125.           case DR_NEXT:
  126.             $start->addTimeParts(0,0,0,0,1);
  127.             $stop->addTimeParts(0,0,0,0,2);
  128.             break;
  129.           case DR_PREVIOUS:
  130.             $start->addTimeParts(0,0,0,0,-1);
  131.             break;
  132.           case DR_CURRENT:
  133.             $stop->addTimeParts(0,0,0,0,1);
  134.         }
  135.         break;
  136.       case DR_WEEK:
  137.         $start->weekRound();
  138.         $stop = clone($start);
  139.         switch ($which) {
  140.           case DR_NEXT:
  141.             $start->addTimeParts(0,0,0,7);
  142.             $stop->addTimeParts(0,0,0,14);
  143.             break;
  144.           case DR_PREVIOUS:
  145.             $start->addTimeParts(0,0,0,-7);
  146.             break;
  147.           case DR_CURRENT:
  148.             $stop->addTimeParts(0,0,0,0,7);
  149.         }
  150.         break;
  151.       case DR_DAY:
  152.         $start->dayRound();
  153.         $stop = clone($start);
  154.         switch ($which) {
  155.           case DR_NEXT:
  156.             $start->addTimeParts(0,0,0,1);
  157.             $stop->addTimeParts(0,0,0,2);
  158.             break;
  159.           case DR_PREVIOUS:
  160.             $start->addTimeParts(0,0,0,-1);
  161.             break;
  162.           case DR_CURRENT:
  163.             $stop->addTimeParts(0,0,0,0,1);
  164.         }
  165.         break;
  166.     }
  167.     $stop->addDays(-1);
  168.     return array('startdate'=>$start, 'stopdate'=>$stop);
  169.   }
  170.  
  171.   /**
  172.    * what default values should be in the boxes
  173.    *
  174.    * this is calculated from the 'DR_*' data that we have above
  175.    *
  176.    * @param integer $which     DR_NEXT, DR_CURRENT, DR_PREVIOUS
  177.    * @param integer $range     DR_DAY, DR_WEEK, etc...
  178.    * @param integer $basetime  what date is "current" date
  179.    * @return array  (html javascript, forwards-backwards button html)
  180.    */
  181.   function setDefaults($which, $range, $basetime=0) {
  182.     $start = new SimpleDate($basetime ? $basetime : time());
  183.     $daterange = $this->_calcRange($which, $range, $start);
  184.     $this->fields['startdate']->setDate($daterange['startdate']->dateString());
  185.     $this->fields['stopdate']->setDate($daterange['stopdate']->dateString());
  186.     $this->extrarows = array();
  187.     $rangePile = array($daterange);
  188.     $nextrange = $daterange;
  189.     $prevrange = $daterange;
  190.     $maxRanges  = 10;             //FIXME: hardcoded magic number
  191.     for ($i=0; $i<$maxRanges; $i++) {
  192.       $nextrange = $this->_calcRange(DR_NEXT,     $range, $nextrange['startdate']);
  193.       $prevrange = $this->_calcRange(DR_PREVIOUS, $range, $prevrange['startdate']);
  194.       array_unshift($rangePile, $prevrange);
  195.       array_push   ($rangePile, $nextrange);
  196.     }
  197.     //preDump($rangePile);
  198.     $jswalk = new JSQuickWalk($this->namebase,'&laquo; '.T_('Previous'), T_('Next').' &raquo;', array('startdate', 'stopdate'), $rangePile, $maxRanges);
  199.     //preDump($jswalk);
  200.     //$this->extrarows[] = array('', $prevRange[0]->dateString().$prevRange[1]->dateString());
  201.     //$this->extrarows[] = array('', $nextRange[0]->dateString().$nextRange[1]->dateString());
  202.     $this->extrarows[] = array($jswalk->displayJS(),
  203.                         $jswalk->displayBack().' | '.$jswalk->displayFwd());
  204.   }
  205.  
  206.  
  207.   function display($PD=NULL) {
  208.     $t = '';
  209.     if ($this->reflectData) {
  210.       foreach ($PD as $key => $val) {
  211.         $t .= '<input type="hidden" name="'.$key.'" value="'.xssqw($val).'" />';
  212.       }
  213.     }
  214.     $t .= $this->displayInTable(2);
  215.     if ($this->includeSubmitButton) {
  216.       $t .= '<input type="submit" name="submit" value="Go" />';
  217.     }
  218.     return $t;
  219.   }
  220.  
  221.   function getStart() {
  222.     return $this->fields['startdate']->date;
  223.   }
  224.  
  225.   function getStop() {
  226.     return $this->fields['stopdate']->date;
  227.   }
  228. } //dateRange
  229.  
  230. ?>

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