Alpha Framework alpha--model--types
[ class tree: alpha--model--types ] [ index: alpha--model--types ] [ all elements ]

Source for file Timestamp.inc

Documentation is available at Timestamp.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/exceptions/AlphaException.inc';
  4. require_once $config->get('sysRoot').'alpha/model/types/AlphaType.inc';
  5. require_once $config->get('sysRoot').'alpha/model/types/AlphaTypeInterface.inc';
  6.  
  7. /**
  8.  * The Timestamp complex data type
  9.  * 
  10.  * @package alpha::model::types
  11.  * @since 1.0
  12.  * @author John Collins <dev@alphaframework.org>
  13.  * @version $Id: AlphaDAO.inc 1258 2011-01-15 13:33:42Z johnc $
  14.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  15.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  16.  *  All rights reserved.
  17.  * 
  18.  *  <pre>
  19.  *  Redistribution and use in source and binary forms, with or
  20.  *  without modification, are permitted provided that the
  21.  *  following conditions are met:
  22.  * 
  23.  *  * Redistributions of source code must retain the above
  24.  *    copyright notice, this list of conditions and the
  25.  *    following disclaimer.
  26.  *  * Redistributions in binary form must reproduce the above
  27.  *    copyright notice, this list of conditions and the
  28.  *    following disclaimer in the documentation and/or other
  29.  *    materials provided with the distribution.
  30.  *  * Neither the name of the Alpha Framework nor the names
  31.  *    of its contributors may be used to endorse or promote
  32.  *    products derived from this software without specific
  33.  *    prior written permission.
  34.  *   
  35.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  36.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  40.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  45.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  46.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  47.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48.  *  </pre>
  49.  *  
  50.  */
  51. class Timestamp extends AlphaType implements AlphaTypeInterface {
  52.     /**
  53.      * The year part
  54.      *
  55.      * @var integer 
  56.      * @since 1.0
  57.      */
  58.     private $year;
  59.     
  60.     /**
  61.      * The month part
  62.      *
  63.      * @var integer 
  64.      * @since 1.0
  65.      */
  66.     private $month;
  67.     
  68.     /**
  69.      * The day part
  70.      *
  71.      * @var integer 
  72.      * @since 1.0
  73.      */
  74.     private $day;
  75.     
  76.     /**
  77.      * The hour part
  78.      *
  79.      * @var integer 
  80.      * @since 1.0
  81.      */
  82.     private $hour;
  83.     
  84.     /**
  85.      * The minute part
  86.      *
  87.      * @var integer 
  88.      * @since 1.0
  89.      */
  90.     private $minute;
  91.      
  92.     /**
  93.      * The second part
  94.      *
  95.      * @var integer 
  96.      * @since 1.0
  97.      */
  98.     private $second;
  99.      
  100.      /**
  101.      * The textual version of the day, e.g. Monday
  102.      *
  103.      * @var string 
  104.      * @since 1.0
  105.      */
  106.     private $weekday;
  107.     
  108.      /**
  109.      * The validation rule (reg-ex) applied to Date values
  110.      *
  111.      * @var string 
  112.      * @since 1.0
  113.      */
  114.      private $validationRule;
  115.      
  116.     /**
  117.      * The error message returned for invalid values
  118.      *
  119.      * @var string 
  120.      * @since 1.0
  121.      */
  122.     private $helper'Not a valid timestamp value!  A timestamp should be in the format YYYY-MM-DD HH:MM:SS.';
  123.      
  124.     /**
  125.      * Constructor
  126.      * 
  127.      * @since 1.0
  128.      * @throws IllegalArguementException
  129.      */
  130.      public function __construct($timestamp=''{
  131.          global $config;
  132.          
  133.          $this->validationRule AlphaValidator::ALLOW_ALL;
  134.         
  135.          if(empty($timestamp)) {
  136.              if($config->get('sysDefaultDateTime'== 'now'{
  137.                  $this->year=date('Y');
  138.                  $this->month=date('m');
  139.                  $this->day=date('d');
  140.                  $this->weekday=date('l');
  141.                  $this->hour=date('H');
  142.                  $this->minute=date('i');
  143.                  $this->second=date('s');
  144.              }else{
  145.                  $this->year '0000';
  146.                  $this->month '00';
  147.                  $this->day '00';
  148.                  $this->hour '00';
  149.                  $this->minute '00';
  150.                  $this->second '00';
  151.              }             
  152.          }else{
  153.             if (preg_match($this->validationRule$timestamp)) {
  154.                 $this->populateFromString($timestamp);
  155.             }else{
  156.                 throw new IllegalArguementException($this->helper);
  157.             }
  158.         }
  159.      }
  160.      
  161.     /**
  162.       * Accepts a full date/time string in YYYY-mm-dd hh:ii:ss format
  163.       * 
  164.       * @param string $dateTime 
  165.       * @since 1.0
  166.       */
  167.      public function setValue($dateTime{
  168.          $this->populateFromString($dateTime);
  169.      }
  170.      
  171.      /**
  172.       * Setter for the timestamp value
  173.       *
  174.       * @param integer $year 
  175.       * @param integer $month 
  176.       * @param integer $day 
  177.       * @param integer $hour 
  178.       * @param integer $minute 
  179.       * @param integer $second 
  180.       * @since 1.0
  181.       * @throws IllegalArguementException
  182.       */
  183.      public function setTimestampValue($year$month$day$hour$minute$second{
  184.          $valid null;
  185.          
  186.          if (!preg_match('/^[0-9]{4}$/'$year))
  187.              $valid 'The year value '.$year.' provided is invalid!';
  188.          if (!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$month))
  189.              $valid 'The month value '.$month.' provided is invalid!';
  190.          if (!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$day))
  191.              $valid 'The day value '.$day.' provided is invalid!';
  192.          if (!isset($valid&& !checkdate($month$day$year))
  193.              $valid 'The day value '.$year.'-'.$month.'-'.$day.' provided is invalid!';
  194.          if(!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$hour|| !($hour >= && $hour 24))
  195.              $valid 'The hour value '.$hour.' provided is invalid!';
  196.          if(!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$minute|| !($minute >= && $minute 60))
  197.              $valid 'The minute value '.$minute.' provided is invalid!';
  198.          if(!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$second|| !($second >= && $second 60))
  199.              $valid 'The second value '.$second.' provided is invalid!';
  200.          
  201.          if (isset($valid)){
  202.              throw new IllegalArguementException($valid);
  203.          }else{
  204.              $this->year $year;
  205.              $this->month str_pad($month2'0'STR_PAD_LEFT);
  206.              $this->day str_pad($day2'0'STR_PAD_LEFT);
  207.              $this->hour str_pad($hour2'0'STR_PAD_LEFT);
  208.              $this->minute str_pad($minute2'0'STR_PAD_LEFT);
  209.              $this->second str_pad($second2'0'STR_PAD_LEFT);
  210.              $unixTime mktime($this->hour$this->minute$this->second$this->month$this->day$this->year);             
  211.              $this->weekday=date('l'$unixTime);
  212.          }
  213.      }
  214.      
  215.      /**
  216.       * Getter for the Timestamp value
  217.       *
  218.       * @return string 
  219.       * @since 1.0
  220.       */
  221.      public function getValue({
  222.          return $this->year.'-'.$this->month.'-'.$this->day.' '.$this->hour.':'.$this->minute.':'.$this->second;
  223.      }
  224.      
  225.      /**
  226.       * Return the value in UNIX timestamp format
  227.       * 
  228.       * @return integer 
  229.       * @since 1.0
  230.       */
  231.      public function getUnixValue({
  232.          return mktime($this->hour$this->minute$this->second$this->month$this->day$this->year);
  233.      }
  234.      
  235.      /**
  236.       * Getter for the date part
  237.       *
  238.       * @return string 
  239.       * @since 1.0
  240.       */
  241.      public function getDate({
  242.          return $this->year.'-'.$this->month.'-'.$this->day;
  243.      }
  244.      
  245.     /**
  246.       * Get the date value as a string in the format "DD/MM/YYYY"
  247.       *
  248.       * @return string 
  249.       * @since 1.0
  250.       */
  251.      public function getEuroValue({
  252.          return $this->day.'/'.$this->month.'/'.substr($this->year22);
  253.      }
  254.      
  255.      /**
  256.       * Setter for the date part
  257.       *
  258.       * @param integer $year 
  259.       * @param integer $month 
  260.       * @param integer $day 
  261.       * @since 1.0
  262.       * @throws IllegalArguementException
  263.       */
  264.      public function setDate($year$month$day{
  265.          $valid null;
  266.          
  267.          if (!preg_match('/^[0-9]{4}$/'$year))
  268.              $valid 'The year value '.$year.' provided is invalid!';
  269.          if (!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$month))
  270.              $valid 'The month value '.$month.' provided is invalid!';
  271.          if (!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$day))
  272.              $valid 'The day value '.$day.' provided is invalid!';
  273.          if (!isset($valid&& !checkdate($month$day$year))
  274.              $valid 'The day value '.$year.'/'.$month.'/'.$day.' provided is invalid!';
  275.          
  276.          if (isset($valid)){
  277.              throw new IllegalArguementException($valid);
  278.          }else{
  279.              $this->year $year;
  280.              $this->month str_pad($month2'0'STR_PAD_LEFT);
  281.              $this->day str_pad($day2'0'STR_PAD_LEFT);
  282.              $unixTime mktime(000$this->month$this->day$this->year);             
  283.              $this->weekday=date('l'$unixTime);
  284.          }
  285.      }
  286.      
  287.      /**
  288.       * Getter for the time part
  289.       *
  290.       * @return string 
  291.       * @since 1.0
  292.       */
  293.      public function getTime({
  294.          return $this->hour.':'.$this->minute.':'.$this->second;
  295.      }
  296.      
  297.      /**
  298.       * Getter for the year part
  299.       *
  300.       * @return string 
  301.       * @since 1.0
  302.       */
  303.      public function getYear({
  304.          return $this->year;    
  305.      }
  306.      
  307.      /**
  308.       * Getter for the month part
  309.       *
  310.       * @return string 
  311.       * @since 1.0
  312.       */
  313.      public function getMonth({
  314.          return $this->month;    
  315.      }
  316.      
  317.      /**
  318.       * Getter for the day part
  319.       *
  320.       * @return string 
  321.       * @since 1.0
  322.       */
  323.      public function getDay({
  324.          return $this->day;    
  325.      }
  326.      
  327.     /**
  328.       * Get the textual weekday part, e.g. Monday
  329.       *
  330.       * @return string 
  331.       * @since 1.0
  332.       */
  333.      public function getWeekday({
  334.          return $this->weekday;
  335.      }
  336.      
  337.      /**
  338.       * Getter for the hour part
  339.       *
  340.       * @return string 
  341.       * @since 1.0
  342.       */
  343.      public function getHour({
  344.          return $this->hour;    
  345.      }
  346.      
  347.      /**
  348.       * Getter for the minute part
  349.       *
  350.       * @return string 
  351.       * @since 1.0
  352.       */
  353.      public function getMinute({
  354.          return $this->minute;    
  355.      }
  356.      
  357.      /**
  358.       * Getter for the second part
  359.       *
  360.       * @return string 
  361.       * @since 1.0
  362.       */
  363.      public function getSecond({
  364.          return $this->second;    
  365.      }
  366.      
  367.      /**
  368.       * Setter for the time part
  369.       *
  370.       * @param integer $hour 
  371.       * @param integer $minute 
  372.       * @param integer $second 
  373.       * @since 1.0
  374.       * @throws IllegalArguementException
  375.       */
  376.      function setTime($hour$minute$second{
  377.          $valid null;
  378.          
  379.          if(!isset($valid&& !preg_match('/^[0-9]{2}$/'$hour|| !($hour >= && $hour 24))
  380.              $valid 'The hour value '.$hour.' provided is invalid!';
  381.          if(!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$minute|| !($minute >= && $minute 60))
  382.              $valid 'The minute value '.$minute.' provided is invalid!';
  383.          if(!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$second|| !($second >= && $second 60))
  384.              $valid 'The second value '.$second.' provided is invalid!';
  385.              
  386.          if (isset($valid)) {
  387.              throw new IllegalArguementException($valid);
  388.          }else{
  389.              $this->hour str_pad($hour2'0'STR_PAD_LEFT);
  390.              $this->minute str_pad($minute2'0'STR_PAD_LEFT);
  391.              $this->second str_pad($second2'0'STR_PAD_LEFT);             
  392.          }
  393.      }
  394.      
  395.      /**
  396.       * Accepts a full date/time string in YYYY-mm-dd hh:ii:ss format
  397.       * 
  398.       * @param string $dateTime 
  399.       * @since 1.0
  400.       * @throws IllegalArguementException
  401.       */
  402.      public function populateFromString($dateTime{
  403.          $valid null;
  404.          
  405.          if($dateTime == 'Please select' || $dateTime == '0000-00-00 00:00:00'{
  406.              $this->year '0000';
  407.              $this->month '00';
  408.              $this->day '00';
  409.              $this->hour '00';
  410.              $this->minute '00';
  411.              $this->second '00';
  412.          }else{
  413.              $spilt_by_space explode(" "$dateTime);
  414.              
  415.              if(isset($spilt_by_space[0])) {
  416.                  $date $spilt_by_space[0];
  417.              }else{
  418.                  throw new IllegalArguementException($this->helper);
  419.              }
  420.              
  421.              if(isset($spilt_by_space[1])) {
  422.                  $time $spilt_by_space[1];
  423.              }else{
  424.                  throw new IllegalArguementException($this->helper);
  425.              }
  426.              
  427.              $split_by_dash explode("-"$date);
  428.              
  429.              if(isset($split_by_dash[0])) {
  430.                  $year $split_by_dash[0];
  431.              }else{
  432.                  throw new IllegalArguementException($this->helper);
  433.              }
  434.              
  435.              if(isset($split_by_dash[1])) {
  436.                  $month $split_by_dash[1];
  437.              }else{
  438.                  throw new IllegalArguementException($this->helper);
  439.              }
  440.              
  441.              if(isset($split_by_dash[2])) {
  442.                  $day $split_by_dash[2];
  443.              }else{
  444.                  throw new IllegalArguementException($this->helper);
  445.              }
  446.              
  447.              $split_by_colon explode(":"$time);
  448.              
  449.              if(isset($split_by_colon[0])) {
  450.                  $hour $split_by_colon[0];
  451.              }else{
  452.                  throw new IllegalArguementException($this->helper);
  453.              }
  454.              
  455.              if(isset($split_by_colon[1])) {
  456.                  $minute $split_by_colon[1];
  457.              }else{
  458.                  throw new IllegalArguementException($this->helper);
  459.              }
  460.              
  461.              if(isset($split_by_colon[2])) {
  462.                  $second $split_by_colon[2];
  463.              }else{
  464.                  throw new IllegalArguementException($this->helper);
  465.              }
  466.              
  467.              if(!preg_match('/^[0-9]{4}$/'$year))
  468.                  $valid 'The year value '.$year.' provided is invalid!';
  469.              if(!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$month))
  470.                  $valid 'The month value '.$month.' provided is invalid!';
  471.              if(!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$day))
  472.                  $valid 'The day value '.$day.' provided is invalid!';
  473.              if(!isset($valid&& !checkdate($month$day$year))
  474.                  $valid 'The day value '.$year.'/'.$month.'/'.$day.' provided is invalid!';
  475.              if(!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$hour|| !($hour >= && $hour 24))
  476.                  $valid 'The hour value '.$hour.' provided is invalid!';
  477.              if(!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$minute|| !($minute >= && $minute 60))
  478.                  $valid 'The minute value '.$minute.' provided is invalid!';
  479.              if(!isset($valid&& !preg_match('/^[0-9]{1,2}$/'$second|| !($second >= && $second 60))
  480.                  $valid 'The second value '.$second.' provided is invalid!';
  481.              
  482.              if (isset($valid)){
  483.                  throw new IllegalArguementException($valid);
  484.              }else{
  485.                  $this->year $year;
  486.                  $this->month str_pad($month2'0'STR_PAD_LEFT);
  487.                  $this->day str_pad($day2'0'STR_PAD_LEFT);
  488.                  $this->hour str_pad($hour2'0'STR_PAD_LEFT);
  489.                  $this->minute str_pad($minute2'0'STR_PAD_LEFT);
  490.                  $this->second str_pad($second2'0'STR_PAD_LEFT);
  491.                  $unixTime mktime($this->hour$this->minute$this->second$this->month$this->day$this->year);             
  492.                  $this->weekday=date('l'$unixTime);             
  493.              }
  494.          }
  495.      }
  496.      
  497.     /**
  498.       * Get the validation rule
  499.       *
  500.       * @return string 
  501.       * @since 1.0
  502.       */
  503.      public function getRule({
  504.         return $this->validationRule;
  505.     }
  506.     
  507.     /**
  508.      * Set the validation rule
  509.      *
  510.      * @param string $rule 
  511.      * @since 1.0
  512.      */
  513.     public function setRule($rule{
  514.         $this->validationRule $rule;
  515.     }
  516.     
  517.     /**
  518.      * Get the validation helper text
  519.      *
  520.      * @return string 
  521.      * @since 1.0
  522.      */
  523.     public function getHelper({
  524.         return $this->helper;
  525.     }
  526.     
  527.     /**
  528.      * Set the validation helper text
  529.      *
  530.      * @param string $helper 
  531.      * @since 1.0
  532.      */
  533.     public function setHelper($helper{
  534.         $this->helper $helper;
  535.     }
  536. }
  537.  
  538. ?>

Documentation generated on Thu, 17 Mar 2011 16:45:02 +0000 by phpDocumentor 1.4.3