[Phphtmllib-devel] SF.net SVN: phphtmllib:[3127] branches/BRANCH_2_X/phphtmllib/form/ form_elements
Status: Beta
Brought to you by:
hemna
From: <mpw...@us...> - 2008-08-02 01:15:33
|
Revision: 3127 http://phphtmllib.svn.sourceforge.net/phphtmllib/?rev=3127&view=rev Author: mpwalsh8 Date: 2008-08-02 01:15:40 +0000 (Sat, 02 Aug 2008) Log Message: ----------- Added FEDay class to select day and month pair. Modified Paths: -------------- branches/BRANCH_2_X/phphtmllib/form/form_elements/FEListBox.inc Modified: branches/BRANCH_2_X/phphtmllib/form/form_elements/FEListBox.inc =================================================================== --- branches/BRANCH_2_X/phphtmllib/form/form_elements/FEListBox.inc 2008-08-02 01:14:40 UTC (rev 3126) +++ branches/BRANCH_2_X/phphtmllib/form/form_elements/FEListBox.inc 2008-08-02 01:15:40 UTC (rev 3127) @@ -1562,8 +1562,332 @@ } +/** + * This class builds a widget that shows a group of select boxes (FEMonths, FEDays) representing a date. + * + * FEDate will display two drop down lists representing a date. You can set + * the order in which these elements are displayed. + * + * Like in FEMonths you should use the built in php {@link http://www.php.net/manual/en/function.setlocale.php setlocale} + * function to affect the language used for the month list. + * + * Example as it would appear in FormContent::form_init_elements(): + * <code> + * // set the locale to dutch + * setlocale(LC_TIME, 'nl_NL'); + * $date_element = new FEDay("FEDay label", false, null, null, 'Fd'); + * </code> + * + * the $format parameter conforms the the php {@link http://www.php.net/manual/en/function.setlocale.php date} function + * format argument specification (for years, months and days only). + * + * @author Culley Harrelson <cu...@fa...> + * @author Suren Markosian <su...@em...> + * @author Mike Walsh <mik...@mi...> + * @see FEMonths + * @see FEDays + * + */ +class FEDay extends FEBoxElement { + /** + * The order in which to show the elements. This variable must be 3 + * characters long and contain only one m only one d and only one y. + * + * @var string + * @access private + */ + var $_format = 'md'; + + /** + * A printf style format string used to add punctuation to the confirmation + * display. Defaults to space separated. The placeholders are filled + * according to the order set in $_format + * + * @var string + * @access private + */ + var $_text_format = '%s %s'; + + /** + * The month form element + * + * @var FEMonths + * @access private + */ + var $_month; + + /** + * The day form element + * + * @var FEDays + * @access private + */ + var $_day; + + /** + * The constructor + * + * @param string text label for the element + * @param boolean is this a required element? + * @param int element width in characters, pixels (px), percentage (%) or elements (em) + * @param int element height in px + * @param string date format string. M m F d D are valid. 2 characters max. + * @see FEDay for an example + * @todo we need to blow up somehow if the format string is bogus + * @access public + * + */ + + function FEDay($label, $required = TRUE, $width = NULL, $height = NULL, + $format = 'md'){ + + $this->_set_format($format); + + //call the parent constructor first + //so the element_name is built. + parent::FEBoxElement($label, $required, $width, $height); + + //now create the child elements. + $this->_month = new FEMonths($this->_element_name . '_months', $required, null, null, preg_replace('/[d]/i', '', $this->_format)); + $this->_day = new FEDays($this->_element_name . '_days', $required, null, null); + } + + /** + * We need to override this so we get + * the form name set in the child elements + * so the id attributes are set correctly. + */ + function set_form_name($name) { + $this->_form_name = $name; + $this->_month->set_form_name($name); + $this->_day->set_form_name($name); + } + + /** + * This function builds and returns the + * form element object + * + * @return object + * @access public + */ + function get_element(){ + + $container = new Container(); + + // add the elements in the order specified. + $chars = preg_split('//', $this->_format, -1, PREG_SPLIT_NO_EMPTY); + foreach ($chars as $char){ + switch ($char) { + case 'm': + case 'F': + $container->add($this->_month->get_element()); + break; + case 'd': + $container->add($this->_day->get_element()); + break; + } + } + + return $container; + } + + /** + * This function will return the elements value as an array or month, day and year + * + * @return array + * @access public + */ + function get_value(){ + $value= array("day"=>$this->_day->get_value(), + "month"=>$this->_month->get_value()); + + return $value; + + + } + + /** + * Set the value of the element + * + * This function sets the default values for the date element The + * parameter should be a string representation of the date in ISO 8601 + * format. + * + * @param string + * @access public + */ + function set_value($value){ + if ($value) { + if (is_array($value)) { + $this->_month->set_value($value['month']); + $this->_day->set_value($value['day']); + } else { + $date_parts = explode('-', $value); + $this->_month->set_value($date_parts[1]); + $this->_day->set_value($date_parts[2]); + } + } + + + } + + /** + * This returns a formatted string used for the confirmation display (and possibly elsewhere) + * + * @return string + * @access public + */ + function get_value_text(){ + + // loop through the characters in $_format to properly set the placeholders + // determined in $_text_format + $chars = preg_split('//', $this->_format, -1, PREG_SPLIT_NO_EMPTY); + $i = 1; + foreach ($chars as $char){ + + switch ($char) { + case 'm': + case 'F': + $value = $this->_month->get_value_text(); + break; + case 'd': + $value = $this->_day->get_value_text(); + break; + } + + switch ($i) { + case 1: + $one = $value; + break; + case 2: + $two = $value; + break; + } + + $i++; + } + + return sprintf($this->_text_format, $one, $two); + } + + /** + * + * This function is responsible for performing complete + * validation and setting the appropriate error message + * in case of a failed validation + * + * @param FormValidation + * @access public + * @return boolean success or failure + */ + function validate(&$_FormValidation){ + $value = $this->get_value(); + + //we make sure that the date is valid, spoof the year + //this may break on February 29th .... + if (!checkdate($value["month"], $value["day"], date("Y"))) { + $this->set_error_message("Invalid date"); + return FALSE; + } + return TRUE; + } + + /** + * this method sets the display order for the elements in the widget + * + * @param string + * @return bool success or failure + * @access private + */ + function _set_format($format){ + + // must be 2 or 3 characters + if (strlen($format) != 2) { + return FALSE; + } + + // month can be represented by F m or M + if (strstr($format, 'F')) { + $month = 'f'; + } else { + $month = 'm'; + } + + // compare the characters sent with the characters needed. only set + // the property if one of each is present + $search_for = array ($month, 'd'); + $chars = preg_split('//', strtolower($format), -1, PREG_SPLIT_NO_EMPTY); + + if (count(array_diff($search_for, $chars)) > 0) { + return FALSE; + } + + $this->_format = $format; + return TRUE; + + + } + + /** + * Set the text format for confirmation + * + * this method sets the format string used in get_value_text(). Use this + * method to set special punctuation for the confirmation display. It is + * fed through sprintf + * + * Examples: + * <code> + * $date_element->set_text_format("%s %s"); + * $date_element->set_text_format("%02d-%02d"); + * </code> + * + * @param string + * @access public + * @link http://www.php.net/manual/en/function.sprintf.php + * + */ + function set_text_format($format){ + + $this->_text_format = $format; + } + + /** + * This method returns the hidden version of this element for a confirmation page. + * + * NOTE: This is called by the FormProcessor only. + * It shouldn't be called manually. + * + * @return container + * @access public + */ + function get_confirm_element(){ + $element_name = $this->get_element_name(); + + $c = container(); + $c->add(form_hidden($this->_month->get_element_name(), $this->_month->get_value())); + $c->add(form_hidden($this->_day->get_element_name(), $this->_day->get_value())); + return $c; + } + + + /** + * Sets the disabled element flag + * + * @param bool disabled + */ + function set_disabled($flag) { + $this->_is_disabled = $flag; + $this->_month->set_disabled($flag); + $this->_day->set_disabled($flag); + } + +} + + + + /** * this class is used for building a listbox for * displaying Hours. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |