Structures_Form_Gtk2
[ class tree: Structures_Form_Gtk2 ] [ index: Structures_Form_Gtk2 ] [ all elements ]

Source for file DateTime.php

Documentation is available at DateTime.php

  1. <?php
  2. /**
  3.  * A Structures_Form element for a set of date and time dropdown boxes.
  4.  *
  5.  * This element is a composite element made of several Gtk2TextSelect elements.
  6.  *
  7.  * This class implement Structures_Form_ElementInterface.
  8.  *
  9.  * @author    Scott Mattocks
  10.  * @package   Structures_Form_Gtk2
  11.  * @license   PHP License
  12.  * @version   0.8.0devel
  13.  * @copyright Copyright 2006 Scott Mattocks
  14.  */
  15. class Structures_Form_Element_Gtk2_DateTime extends GtkHBox implements Structures_Form_ElementInterface {
  16.  
  17.     /**
  18.      * Constants for the element segments.
  19.      *
  20.      * @const
  21.      */
  22.     const ELEMENT_MONTH  = 'month';
  23.     const ELEMENT_DAY    = 'day';
  24.     const ELEMENT_YEAR   = 'year';
  25.     const ELEMENT_HOUR   = 'hour';
  26.     const ELEMENT_MINUTE = 'minute';
  27.     const ELEMENT_SECOND = 'seconds';
  28.     const ELEMENT_AMPM   = 'ampm';
  29.  
  30.     /**
  31.      * Default values
  32.      *
  33.      * @const
  34.      */
  35.     const DEFAULT_DURATION   = 10;
  36.     const DEFAULT_INCREMENTS = 10;
  37.  
  38.     /**
  39.      * The Structures_Form object.
  40.      *
  41.      * @access public
  42.      * @var    object 
  43.      */
  44.     public $form;
  45.  
  46.     /**
  47.      * The month element.
  48.      * 
  49.      * @access public
  50.      * @var    object 
  51.      */
  52.     public $month;
  53.  
  54.     /**
  55.      * The day element.
  56.      * 
  57.      * @access public
  58.      * @var    object 
  59.      */
  60.     public $day;
  61.  
  62.     /**
  63.      * The year element.
  64.      * 
  65.      * @access public
  66.      * @var    object 
  67.      */
  68.     public $year;
  69.     /**
  70.      * The hour element.
  71.      * 
  72.      * @access public
  73.      * @var    object 
  74.      */
  75.     public $hour;
  76.  
  77.     /**
  78.      * The minute element.
  79.      * 
  80.      * @access public
  81.      * @var    object 
  82.      */
  83.     public $minute;
  84.  
  85.     /**
  86.      * The second element.
  87.      * 
  88.      * @access public
  89.      * @var    object 
  90.      */
  91.     public $second;
  92.  
  93.     /**
  94.      * The AM/PM element.
  95.      * 
  96.      * @access public
  97.      * @var    object 
  98.      */
  99.     public $ampm;
  100.  
  101.     /**
  102.      * The increments to display for minutes.
  103.      *
  104.      * @access public
  105.      * @var    integer 
  106.      */
  107.     public $minuteIncrements;
  108.  
  109.     /**
  110.      * Constructor.
  111.      *
  112.      * @access public
  113.      * @param  object  $form      The Structures_Form.
  114.      * @param  string  $label     The string to be used as the label.
  115.      * @param  string  $date      The default date (optional).
  116.      * @param  array   $order     The elements to display and the order to
  117.      *                             display them. (optional).
  118.      * @param  integer $yearStart The first year for the year element
  119.      *                             (optional).
  120.      * @param  integer $duration  The number of years to show (optional).
  121.      * @param  array   $size      The height and width of the entry (optional).
  122.      * @param  object  $style     The GtkStyle to apply to the elements
  123.      *                             (optional).
  124.      * @return void 
  125.      */
  126.     public function __construct(Structures_Form $form$label$date = null,
  127.                                 $order = null$yearStart = null,
  128.                                 $duration = null$minuteIncrements = null,
  129.                                 $size = null$style = null)
  130.     {
  131.         // Call the parent constructor.
  132.         parent::__construct();
  133.  
  134.         // Set the form object.
  135.         $this->form = $form;
  136.  
  137.         // Set the label.
  138.         $this->setLabel($label);
  139.         
  140.         // Set the size if it was given.
  141.         if (is_array($size)) {
  142.             $this->set_size_request($size[0]$size[1]);
  143.         }
  144.  
  145.         // Set the style if it was given.
  146.         if ($style instanceof GtkStyle{
  147.             $this->set_style($style);
  148.         }
  149.  
  150.         // Set the display elements and order.
  151.         if (!is_array($order)) {
  152.             $order = array(self::ELEMENT_MONTH
  153.                            self::ELEMENT_DAY
  154.                            self::ELEMENT_YEAR,
  155.                            self::ELEMENT_HOUR,
  156.                            self::ELEMENT_MINUTE,
  157.                            self::ELEMENT_SECOND,
  158.                            self::ELEMENT_AMPM
  159.                            );
  160.         }
  161.  
  162.         // Create the individual elements.
  163.         if (is_null($yearStart)) {
  164.             $yearStart date('Y');
  165.         }
  166.  
  167.         if (is_null($duration|| !is_numeric($duration)) {
  168.             $duration = self::DEFAULT_DURATION;
  169.         }
  170.  
  171.         if (is_null($minuteIncrements|| !is_numeric($minuteIncrements||
  172.             $minuteIncrements < 1{
  173.             $minuteIncrements = self::DEFAULT_INCREMENTS;
  174.         }
  175.         $this->minuteIncrements = $minuteIncrements;
  176.             
  177.         $this->createElements($yearStart$durationin_array(self::ELEMENT_AMPM$order));
  178.  
  179.         // Set the date. Default to now.
  180.         if (is_null($date)) {
  181.             $date time();
  182.         }
  183.         $this->setValue($date);
  184.  
  185.         // Pack the elements in the order they should appear.
  186.         foreach ($order as $element{
  187.             $this->pack_start($this->$elementfalsefalse3);
  188.         }
  189.     }
  190.  
  191.     /**
  192.      * Creates the individual date elements.
  193.      * 
  194.      * This element is made up of month, day and year elements.
  195.      *
  196.      * @access protected
  197.      * @param  integer   $yearStart  The first year to show.
  198.      * @param  integer   $duration   The number of years to show.
  199.      * @param  boolean   $twelveHour true to use 12 hour format.
  200.      * @return void 
  201.      */
  202.     protected function createElements($yearStart$duration$twelveHour = false)
  203.     {
  204.         require_once 'Structures/Form/Element/Gtk2/TextSelect.php';
  205.  
  206.         // Create the month element.
  207.         $this->month = new Structures_Form_Element_Gtk2_TextSelect($this->form,
  208.                                                                   null
  209.                                                                   );
  210.         
  211.         // Add the months.
  212.         for ($i = 1; $i <= 12; ++$i{
  213.             // Use date to get local month names.
  214.             $this->month->addOption($idate('F'mktime(0,0,0,$i,1,2000)));
  215.         }
  216.  
  217.         // Create the day element.
  218.         $this->day = new Structures_Form_Element_Gtk2_TextSelect($this->form,
  219.                                                                 null
  220.                                                                 );
  221.         
  222.         // Add the days.
  223.         for ($i = 1; $i <= 31; ++$i{
  224.             $this->day->addOption($i$i);
  225.         }
  226.  
  227.         // Create the year element.
  228.         $this->year = new Structures_Form_Element_Gtk2_TextSelect($this->form,
  229.                                                                  null
  230.                                                                  );
  231.         
  232.         // Add the years.
  233.         for ($i = 0; $i $duration; ++$i{
  234.             $this->year->addOption($yearStart $i$yearStart $i);
  235.         }
  236.  
  237.         // Create the hour element.
  238.         $this->hour = new Structures_Form_Element_Gtk2_TextSelect($this->form,
  239.                                                                   null
  240.                                                                   );
  241.         
  242.         // Add the hours.
  243.         $start = 0;
  244.         $limit = 23;
  245.         if ($twelveHour{
  246.             $start = 1;
  247.             $limit = 12;
  248.         }
  249.  
  250.         for ($i $start$i <= $limit; ++$i{
  251.             // Figure out what to show.
  252.             $this->hour->addOption(str_pad($i2'0'STR_PAD_LEFT)$i);
  253.         }
  254.  
  255.         // Create the minute element.
  256.         $this->minute = new Structures_Form_Element_Gtk2_TextSelect($this->form,
  257.                                                                     null
  258.                                                                     );
  259.         
  260.         // Add the days.
  261.         for ($i = 0; $i <= 59; $i += $this->minuteIncrements{
  262.             $mins str_pad($i2'0'STR_PAD_LEFT);
  263.             $this->minute->addOption($mins$mins);
  264.         }
  265.  
  266.         // Create the seconds element.
  267.         $this->second = new Structures_Form_Element_Gtk2_TextSelect($this->form,
  268.                                                                      null
  269.                                                                      );
  270.         
  271.         // Add the seconds.
  272.         for ($i = 0; $i <= 59; ++$i{
  273.             $this->second->addOption(str_pad($i2'0'STR_PAD_LEFT)$i);
  274.         }
  275.  
  276.         // Create the AMPM element.
  277.         $this->ampm = new Structures_Form_Element_Gtk2_TextSelect($this->form,
  278.                                                                   null
  279.                                                                   );
  280.  
  281.         // Add the options.
  282.         $this->ampm->addOption(0'AM');
  283.         $this->ampm->addOption(1'PM');
  284.     }
  285.  
  286.     /**
  287.      * Sets an element's value.
  288.      *
  289.      * This method should set the value of the widget not just set some data
  290.      * that is retrieved later. If the widget is a GtkEntry, this method should
  291.      * call set_text(). If the widget is a GtkComboBox, this method should set
  292.      * the active row.
  293.      *
  294.      * To set the value of this element, we must set the value of the three
  295.      * composite elements. To do this, we first turn the timestamp into a date
  296.      * string and then explode on '-'. This is faster than calling date() three
  297.      * different times.
  298.      *
  299.      * @access public
  300.      * @param  string  $value The text to put in the entry.
  301.      * @return boolean true if the value was changed.
  302.      */
  303.     public function setValue($value)
  304.     {
  305.         // Make sure the value is a number
  306.         if (!is_numeric($value)) {
  307.             $value strtotime($value);
  308.         }
  309.  
  310.         $success = true;
  311.  
  312.         // Turn the value into a string representation.
  313.         $date date('Y-m-d'$value);
  314.         
  315.         // Now break the date string up into pieces.
  316.         list($year$month$dayexplode('-'$date);
  317.  
  318.         // Finally set the three elements.
  319.         $success $success && $this->month->setValue($month);
  320.         $success $success && $this->day->setValue($day);
  321.         $success $success && $this->year->setValue($year);
  322.  
  323.         // Turn the value into a string representation for the time part.
  324.         $time date('H-i-s'$value);
  325.         
  326.         // Now break the date string up into pieces.
  327.         list($hour$minute$secondexplode('-'$time);
  328.  
  329.         // Finally set the four time elements.
  330.         $success $success && $this->hour->setValue($hour);
  331.         $success $success && $this->minute->setValue($minute);
  332.         $success $success && $this->second->setValue($second);
  333.         $success $success && $this->ampm->setValue((int)($hour > 11));
  334.  
  335.         return $success;
  336.     }
  337.  
  338.     /**
  339.      * Returns element's value.
  340.      *
  341.      * This method should return the widget's value not just some data from the
  342.      * widget (i.e. set with set_data()). For example if the widget is a
  343.      * GtkEntry, this method should call get_text(). If the widget is a
  344.      * GtkComboBox, this method should return the value of the column
  345.      * identified when the element was constructed for the given row.
  346.      *
  347.      * @access public
  348.      * @return integer 
  349.      */
  350.     public function getValue()
  351.     {
  352.         return mktime($this->hour->getValue(),
  353.                       $this->minute->getValue(),
  354.                       $this->second->getValue(),
  355.                       $this->month->getValue(),
  356.                       $this->day->getValue(),
  357.                       $this->year->getValue()
  358.                       );
  359.     }
  360.  
  361.     /**
  362.      * Clears the current value of the element.
  363.      *
  364.      * This method should clear the current value if possible. For example, if
  365.      * the widget is a GtkEntry, this method should pass null to set_text(). If
  366.      * the value could not be cleared for some reason (the item is frozen or it
  367.      * is not possible to clear the value (selection type = browse)) this
  368.      * method should return false.
  369.      *
  370.      * It may not be possible to clear this element type. If the children
  371.      * cannot be cleared, this element cannot be cleared.
  372.      *
  373.      * @access public
  374.      * @return boolean true if the value was cleared.
  375.      */
  376.     public function clearValue()
  377.     {
  378.         return ($this->month->clearValue()  &&
  379.                 $this->day->clearValue()    &&
  380.                 $this->year->clearValue()   &&
  381.                 $this->hour->clearValue()   &&
  382.                 $this->minute->clearValue(&&
  383.                 $this->second->clearValue()
  384.                 );
  385.     }
  386.  
  387.     /**
  388.      * Returns the element type.
  389.      * 
  390.      * This method must return a string identifying the element type, such as
  391.      * text, password, submit, etc.
  392.      *
  393.      * @access public
  394.      * @return string The element type.
  395.      */
  396.     public function getType()
  397.     {
  398.         return 'datetime';
  399.     }
  400.  
  401.     /**
  402.      * Sets the element name.
  403.      *
  404.      * This method exists to maintain consistency in the interface. It should
  405.      * simply call set_name which is a GtkWidget method and should be avialable
  406.      * to all elements.
  407.      *
  408.      * @access public
  409.      * @param  string $name 
  410.      * @return void 
  411.      */
  412.     public function setName($name)
  413.     {
  414.         $this->set_name($name);
  415.     }
  416.  
  417.     /**
  418.      * Returns the element's name.
  419.      *
  420.      * This method exists to maintain consistency in the interface. It should
  421.      * simply call get_name which is a GtkWidget method and should be available
  422.      * to all elements.
  423.      *
  424.      * @access public
  425.      * @return string 
  426.      */
  427.     public function getName()
  428.     {
  429.         return $this->get_name();
  430.     }
  431.  
  432.     /**
  433.      * Freezes the element so that its value may not be changed.
  434.      *
  435.      * Again this method exists only to maintain consistency in the interface.
  436.      * It should just pass false to set_sensitive().
  437.      *
  438.      * To make life easier down the road this method should also call
  439.      * set_data('frozen', true);
  440.      *
  441.      * @access public
  442.      * @return void 
  443.      */
  444.     public function freeze()
  445.     {
  446.         // Make the widget insensitive.
  447.         $this->set_sensitive(false);
  448.  
  449.         // Set a data value for ease of programming later.
  450.         $this->set_data('frozen'true);
  451.     }
  452.  
  453.     /**
  454.      * Unfreezes the element so that its value may not be changed.
  455.      *
  456.      * Again this method exists only to maintain consistency in the interface.
  457.      * It should just pass true to set_sensitive().
  458.      *
  459.      * To make life easier down the road this method should also call
  460.      * set_data('frozen', false);
  461.      *
  462.      * @access public
  463.      * @return void 
  464.      */
  465.     public function unfreeze()
  466.     {
  467.         // Make the widget sensitive.
  468.         $this->set_sensitive(true);
  469.                                                 
  470.         // Set a data value for ease of programming later.
  471.         $this->set_data('frozen'false);
  472.     }
  473.  
  474.     /**
  475.      * Returns whether or not the element is currently frozen.
  476.      * 
  477.      * This method should just return the value from get_data('frozen')
  478.      * @access public
  479.      * @return boolean 
  480.      */
  481.     public function isFrozen()
  482.     {
  483.         return (bool) $this->get_data('frozen');
  484.     }
  485.  
  486.     /**
  487.      * Sets the GtkLabel that identifies the element.
  488.      *
  489.      * @access public
  490.      * @param  string $label 
  491.      * @return void 
  492.      */
  493.     public function setLabel($label)
  494.     {
  495.         $this->set_data('label'$label);
  496.     }
  497.  
  498.     /**
  499.      * Returns the GtkLabel that identifies the element.
  500.      *
  501.      * @access public
  502.      * @return string 
  503.      */
  504.     public function getLabel()
  505.     {
  506.         return $this->get_data('label');
  507.     }
  508.  
  509.     /**
  510.      * Adds an event handler for the element.
  511.      *
  512.      * @access public
  513.      * @param  string  $eventName The name of the event.
  514.      * @param  mixed   $callback  The callback to call when the event occurs.
  515.      * @return array   An array of identifiers, one for each piece.
  516.      */
  517.     public function addEventHandler($eventName$callback)
  518.     {
  519.         return array($this->month->addEventHandler($eventName,  $callback),
  520.                      $this->day->addEventHandler($eventName,    $callback),
  521.                      $this->year->addEventHandler($eventName,   $callback),
  522.                      $this->hour->addEventHandler($eventName,   $callback),
  523.                      $this->minute->addEventHandler($eventName$callback),
  524.                      $this->second->addEventHandler($eventName$callback)
  525.                      );
  526.     }
  527. }
  528. ?>

Documentation generated on Mon, 11 Mar 2019 14:44:12 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.