[dev] Horde_Form: add hourminutesecond, conditionally display title

John Morrissey jwm at horde.net
Wed Jun 25 08:50:54 PDT 2003


hms.patch adds an 'hourminutesecond' type that mirrors the 'monthdayyear'
type for times, giving the user three popups to choose a time from.

Also, I'd like to specify which date/time components are displayed in the
form. Would it be acceptable to add support for boolean type options such as
showMonth or showSeconds?

notitle.patch only displays the form header if a title is set. This avoids
displaying empty header cells if no title is specified.

john
-- 
John Morrissey          _o            /\         ----  __o
jwm at horde.net        _-< \_          /  \       ----  <  \,
www.horde.net/    __(_)/_(_)________/    \_______(_) /_(_)__
-------------- next part --------------
Index: Form.php
===================================================================
RCS file: /repository/horde/lib/Form.php,v
retrieving revision 1.110
diff -u -r1.110 Form.php
--- Form.php	23 Jun 2003 15:01:41 -0000	1.110
+++ Form.php	25 Jun 2003 15:39:40 -0000
@@ -1180,6 +1178,98 @@
 
 }
 
+class Horde_Form_Type_HourMinuteSecond extends Horde_Form_Type {
+
+    function isValid(&$var, &$vars, $value, &$message)
+    {
+        $time = $vars->getVar($var->getVarName());
+        if (!$this->emptyTimeArray($time) && !$this->checktime($time['hour'], $time['minute'], $time['second'])) {
+            $message = _("Please enter a valid time.");
+            return false;
+        } elseif ($this->emptyTimeArray($time) && $var->isRequired()) {
+            $message = _("This field is required.");
+            return false;
+        }
+        return true;
+    }
+
+	function checktime($hour, $minute, $second) {
+		if (isset($hour) && ($hour < 0 || $hour > 23)) {
+			return false;
+		}
+		if (isset($minute) && ($minute < 0 || $minute > 60)) {
+			return false;
+		}
+		if (isset($second) && ($second < 0 || $second > 60)) {
+			return false;
+		}
+		return true;
+	}
+
+    /**
+     * Return the time supplied as a PEAR Date object.
+     *
+     * @param string $time_in  Date in one of the three formats
+     *                         supported by Horde_Form and PEAR's Date
+     *                         class (ISO format YYYY-MM-DD HH:MM:SS,
+     *                         timestamp YYYYMMDDHHMMSS, and UNIX
+     *                         epoch).
+     *
+     * @return object          The time object.
+     */
+    function &getTimeOb($time_in)
+    {
+        require_once 'Date.php';
+
+        /* Fix the time if it is the shortened ISO. */
+        if (is_array($time_in)) {
+            if (!$this->emptyTimeArray($time_in)) {
+                $time_in = sprintf('%02d:%02d:%02d', $time_in['hour'], $time_in['minute'], $time_in['second']);
+            }
+        }
+
+        /* Return the PEAR time object. */
+        return new Date($time_in);
+    }
+
+    /**
+     * Return the time supplied split up into an array.
+     *
+     * @param string $time_in  Time in one of the three formats
+     *                         supported by Horde_Form and PEAR's Date
+     *                         class (ISO format YYYY-MM-DD HH:MM:SS,
+     *                         timestamp YYYYMMDDHHMMSS, and UNIX
+     *                         epoch).
+     *
+     * @return array           Array with three elements - hour, minute
+     *                         and second.
+     */
+    function getTimeParts($time_in)
+    {
+        if (is_array($time_in)) {
+            /* This is probably a failed isValid input
+               so just return the parts as they are. */
+            return $time_in;
+        } elseif (empty($time_in)) {
+            /* This is just an empty field so return
+               empty parts. */
+            return array('hour' => '', 'minute' => '', 'second' => '');
+        }
+        $time = &$this->getTimeOb($time_in);
+        return array('hour' => $time->getHour(),
+                     'minute' => $time->getMinute(),
+                     'second' => $time->getSecond());
+    }
+
+    function emptyTimeArray($time)
+    {
+        if (is_array($time) && empty($time['hour']) && empty($time['minute']) && empty($time['second'])) {
+            return true;
+        }
+        return false;
+    }
+}
+
 class Horde_Form_Type_monthYear extends Horde_Form_Type {
 
     var $startYear;
Index: Form/Renderer.php
===================================================================
RCS file: /repository/horde/lib/Form/Renderer.php,v
retrieving revision 1.73
diff -u -r1.73 Renderer.php
--- Form/Renderer.php	20 Jun 2003 23:04:14 -0000	1.73
+++ Form/Renderer.php	25 Jun 2003 15:39:41 -0000
@@ -331,6 +331,28 @@
 ?><input type="text" size="5" name="<?php echo $var->getVarName() ?>" value="<?php echo $var->getValue($vars, $index) ?>" /><?php
     }
 
+    function _renderVarInput_hourminutesecond(&$form, &$var, &$vars, $index = null)
+    {
+        $hours = array('' => _("HH"));
+        for ($i = 0; $i <= 23; $i++) {
+            $hours[sprintf('%02d', $i)] = $i;
+        }
+        $minutes = array('' => _("MM"));
+        for ($i = 0; $i <= 59; $i++) {
+            $minutes[sprintf('%02d', $i)] = $i;
+        }
+        $seconds = array('' => _("SS"));
+        for ($i = 0; $i <= 59; $i++) {
+            $seconds[sprintf('%02d', $i)] = $i;
+        }
+        $time = $var->type->getTimeParts($var->getValue($vars, $index));
+?>
+<select name="<?php echo $var->getVarName() ?>[hour]" id="<?php echo $var->getVarName() ?>[hour]"><?php echo $this->_selectOptions($hours, $time['hour']) ?></select>
+<select name="<?php echo $var->getVarName() ?>[minute]" id="<?php echo $var->getVarName() ?>[minute]"><?php echo $this->_selectOptions($minutes, $time['minute']) ?></select>
+<select name="<?php echo $var->getVarName() ?>[second]" id="<?php echo $var->getVarName() ?>[second]"><?php echo $this->_selectOptions($seconds, $time['second']) ?></select>
+<?php
+    }
+
     function _renderVarInput_monthyear(&$form, &$var, &$vars, $index = null)
     {
         $months = array('' => _("MM"),
-------------- next part --------------
Index: Renderer.php
===================================================================
RCS file: /repository/horde/lib/Form/Renderer.php,v
retrieving revision 1.73
diff -u -r1.73 Renderer.php
--- Renderer.php	20 Jun 2003 23:04:14 -0000	1.73
+++ Renderer.php	25 Jun 2003 15:45:15 -0000
@@ -796,9 +818,11 @@
 
     function _sectionHeader($title)
     {
+        if (!empty($title)) {
 ?><table border="0" cellpadding="2" cellspacing="0" width="100%">
 <tr><td align="left" class="header"><b><?php echo $title ?></b></td></tr>
 </table><?php
+        }
     }
 
     /**


More information about the dev mailing list