[Pieforms-commit] SF.net SVN: pieforms: [122] pieforms-php5/trunk/src/pieform/elements/expiry .php
Status: Alpha
Brought to you by:
oracleshinoda
|
From: <ora...@us...> - 2006-12-23 04:07:30
|
Revision: 122
http://svn.sourceforge.net/pieforms/?rev=122&view=rev
Author: oracleshinoda
Date: 2006-12-22 20:07:30 -0800 (Fri, 22 Dec 2006)
Log Message:
-----------
Updated to use the new APIs. Also behaves better when the required rule is set
Modified Paths:
--------------
pieforms-php5/trunk/src/pieform/elements/expiry.php
Modified: pieforms-php5/trunk/src/pieform/elements/expiry.php
===================================================================
--- pieforms-php5/trunk/src/pieform/elements/expiry.php 2006-12-23 04:06:32 UTC (rev 121)
+++ pieforms-php5/trunk/src/pieform/elements/expiry.php 2006-12-23 04:07:30 UTC (rev 122)
@@ -28,33 +28,33 @@
* Provides a duration chooser, with a text box for a number and a
* select box to choose the units, in days, weeks, months, years, or 'no end date'.
*
- * @param array $element The element to render
- * @param Pieform $form The form to render the element for
- * @return string The HTML for the element
+ * @param Pieform $form The form to render the element for
+ * @param array $element The element to render
+ * @return string The HTML for the element
*/
-function pieform_render_expiry($element, Pieform $form) {
+function pieform_element_expiry(Pieform $form, $element) {
$result = '';
$name = $element['name'];
if (!isset($element['defaultvalue'])) {
$element['defaultvalue'] = null;
}
- $global = ($form->get_method() == 'get') ? $_GET : $_POST;
+ $global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
// Get the value of the element for rendering.
if (isset($element['value'])) {
$seconds = $element['value'];
- $values = pieform_render_expiry_get_expiry_from_seconds($element['value']);
+ $values = pieform_element_expiry_get_expiry_from_seconds($element['value']);
}
else if (isset($global[$element['name'] . '_number'])
&& isset($global[$element['name'] . '_units'])) {
$values = array('number' => $global[$element['name'] . '_number'],
'units' => $global[$element['name'] . '_units']);
- $seconds = $values['number'] * pieform_render_expiry_seconds_in($values['units']);
+ $seconds = $values['number'] * pieform_element_expiry_seconds_in($values['units']);
}
else if (isset($element['defaultvalue'])) {
$seconds = $element['defaultvalue'];
- $values = pieform_render_expiry_get_expiry_from_seconds($seconds);
+ $values = pieform_element_expiry_get_expiry_from_seconds($seconds);
}
else {
$values = array('number' => '', 'units' => 'noenddate');
@@ -64,14 +64,20 @@
// @todo probably create with an actual input element, as tabindex doesn't work here for one thing
// Same with the select. And do the events using mochikit signal instead of dom events
$numberinput = '<input';
- $numberinput .= $values['units'] == 'noenddate' ? ' disabled="disabled"' : '';
+ $numberinput .= ($values['units'] == 'noenddate' && empty($element['rules']['required'])) ? ' disabled="disabled"' : '';
$numberinput .= ' type="text" size="4" name="' . $name . '_number"';
$numberinput .= ' id="' . $name . '_number" value="' . $values['number'] . '" tabindex="' . $element['tabindex'] . "\">\n";
$uselect = '<select onchange="' . $name . '_change()" ';
$uselect .= 'name="' . $name . '_units" id="' . $name . '_units"' . ' tabindex="' . $element['tabindex'] . "\">\n";
- foreach (pieform_render_expire_get_expiry_units() as $u) {
- $uselect .= "\t<option value=\"$u\"" . (($values['units'] == $u) ? ' selected="selected"' : '') . '>' . $form->i18n($u) . "</option>\n";
+ foreach (pieform_element_expire_get_expiry_units() as $u) {
+ // Don't allow 'no end date' if the element is required
+ if ($u == 'noenddate' && !empty($element['rules']['required'])) {
+ continue;
+ }
+
+ $uselect .= "\t<option value=\"$u\"" . (($values['units'] == $u) ? ' selected="selected"' : '') . '>'
+ . $form->i18n('element', 'expiry', $u, $element) . "</option>\n";
}
$uselect .= "</select>\n";
@@ -92,71 +98,78 @@
return $numberinput . $uselect . $script;
}
-function pieform_render_expire_get_expiry_units() {
+/**
+ * Gets the value of the expiry element and converts it to a time in seconds.
+ *
+ * @param Pieform $form The form the element is attached to
+ * @param array $element The element to get the value for
+ * @return int The number of seconds until expiry
+ */
+function pieform_element_expiry_get_value(Pieform $form, $element) {
+ $name = $element['name'];
+ $global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
+ $unit = $global[$name . '_units'];
+ if ($unit == 'noenddate') {
+ return null;
+ }
+ $allunits = pieform_element_expire_get_expiry_units();
+ $number = $global[$name . '_number'];
+ if (!in_array($unit,$allunits) || $number < 0) {
+ return null;
+ }
+ return $number * pieform_element_expiry_seconds_in($unit);
+}
+
+function pieform_element_expiry_i18n() {
+ return array(
+ 'en.utf8' => array(
+ 'days' => 'Days',
+ 'weeks' => 'Weeks',
+ 'months' => 'Months',
+ 'years' => 'Years',
+ 'noenddate' => 'No end date'
+ )
+ );
+}
+
+function pieform_element_expire_get_expiry_units() {
return array('days', 'weeks', 'months', 'years', 'noenddate');
}
-function pieform_render_expiry_seconds_in($unit) {
+function pieform_element_expiry_seconds_in($unit) {
$dayseconds = 60 * 60 * 24;
switch ($unit) {
- case 'days' : return $dayseconds;
- case 'weeks' : return $dayseconds * 7;
- case 'months' : return $dayseconds * 30;
- case 'years' : return $dayseconds * 365;
- default : return null;
+ case 'days' : return $dayseconds;
+ case 'weeks' : return $dayseconds * 7;
+ case 'months' : return $dayseconds * 30;
+ case 'years' : return $dayseconds * 365;
+ default : return null;
}
}
-function pieform_render_expiry_get_expiry_from_seconds($seconds) {
+function pieform_element_expiry_get_expiry_from_seconds($seconds) {
if ($seconds == null) {
return array('number' => '', 'units' => 'noenddate');
}
// This needs work to produce sensible values; at the moment it will convert
// 60 days into 2 months; 70 days into 7 weeks, etc.
- $yearseconds = pieform_render_expiry_seconds_in('years');
+ $yearseconds = pieform_element_expiry_seconds_in('years');
if ($seconds % $yearseconds == 0 && $seconds > 0) {
return array('number' => (int) ($seconds / $yearseconds), 'units' => 'years');
}
- $monthseconds = pieform_render_expiry_seconds_in('months');
+ $monthseconds = pieform_element_expiry_seconds_in('months');
if ($seconds % $monthseconds == 0 && $seconds > 0) {
return array('number' => (int) ($seconds / $monthseconds), 'units' => 'months');
}
- $weekseconds = pieform_render_expiry_seconds_in('weeks');
+ $weekseconds = pieform_element_expiry_seconds_in('weeks');
if ($seconds % $weekseconds == 0 && $seconds > 0) {
return array('number' => (int) ($seconds / $weekseconds), 'units' => 'weeks');
}
- $dayseconds = pieform_render_expiry_seconds_in('days');
+ $dayseconds = pieform_element_expiry_seconds_in('days');
if ($seconds % $dayseconds == 0) {
return array('number' => (int) ($seconds / $dayseconds), 'units' => 'days');
}
return null;
}
-// /** gets the value explicitly from the request */
-function pieform_get_value_expiry($element, Pieform $form) {
- $name = $element['name'];
- $global = ($form->get_method() == 'get') ? $_GET : $_POST;
- //return $global[$name];
- $unit = $global[$name . '_units'];
- if ($unit == 'noenddate') {
- return null;
- }
- $allunits = pieform_render_expire_get_expiry_units();
- $number = $global[$name . '_number'];
- if (!in_array($unit,$allunits) || $number < 0) {
- return null;
- }
- return $number * pieform_render_expiry_seconds_in($unit);
-}
-
-function pieform_get_value_js_expiry($element, Pieform $form) {
- $formname = $form->get_name();
- $name = $element['name'];
- return <<<EOF
- data['{$name}_number'] = $('{$name}_number').value;
- data['{$name}_units'] = $('{$name}_units').value;
-
-EOF;
-}
-
?>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|