Commit 26deabdf authored by Nigel McNie's avatar Nigel McNie Committed by Nigel McNie
Browse files

New upstream

parent 0c48eff9
......@@ -41,23 +41,20 @@ function pieform_render_expiry($element, Pieform $form) {
$global = ($form->get_method() == 'get') ? $_GET : $_POST;
// Get the value of the element for rendering. The values of the
// two inputs are rendered, and the total time in seconds is
// stored in a hidden input.
// Get the value of the element for rendering.
if (isset($element['value'])) {
$seconds = $element['value'];
$values = get_expiry_from_seconds($element['value']);
$values = pieform_render_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'] * seconds_in($values['units']);
$seconds = $values['number'] * pieform_render_expiry_seconds_in($values['units']);
}
else if (isset($element['defaultvalue'])) {
$seconds = $element['defaultvalue'];
$values = get_expiry_from_seconds($seconds);
$values = pieform_render_expiry_get_expiry_from_seconds($seconds);
}
else {
$values = array('number' => '', 'units' => 'noenddate');
......@@ -65,59 +62,22 @@ function pieform_render_expiry($element, Pieform $form) {
}
// @todo probably create with an actual input element, as tabindex doesn't work here for one thing
$numberinput = '<input ';
if ($form->get_ajaxpost()) {
$numberinput .= 'onchange="' . $name . '_change()"';
}
$numberinput = '<input';
$numberinput .= $values['units'] == 'noenddate' ? ' disabled="disabled"' : '';
$numberinput .= 'type="text" size="4" ' . 'name="' . $name . '_number" ';
$numberinput .= 'id="' . $name . '_number" value="' . $values['number'] . "\">\n";
$allunits = get_expiry_units();
$numberinput .= ' type="text" size="4" name="' . $name . '_number"';
$numberinput .= ' id="' . $name . '_number" value="' . $values['number'] . "\">\n";
$uselect = '<select onchange="' . $name . '_change()" ';
$uselect .= 'name="' . $name . '_units" id="' . $name . '_units"' . ">\n";
foreach ($allunits as $u) {
foreach (pieform_render_expire_get_expiry_units() as $u) {
$uselect .= "\t<option value=\"$u\"" . (($values['units'] == $u) ? ' selected="selected"' : '') . '>' . $form->i18n($u) . "</option>\n";
}
$uselect .= "</select>\n";
// The hidden input contains the value of the expiry in seconds
$hidden = '<input type="hidden" name="' . $name . '" id="' . $name . '" value="' . $seconds . "\">\n";
// Every time one of the two inputs is changed, update the number
// of seconds in the hidden input.
// Make sure the input is disabled if "no end date" is selected
$script = <<<EOJS
<script type="text/javascript" language="javascript">
function {$name}_change() {
EOJS;
/*
if ($form->get_ajaxpost()) {
$script .= <<<EOJS
var seconds = null;
if ($('{$name}_number').value > 0) {
var mult = $('{$name}_number').value * 60 * 60 * 24;
if ($('{$name}_units').value == 'days') {
seconds = mult;
} else if ($('{$name}_units').value == 'weeks') {
seconds = mult * 7;
} else if ($('{$name}_units').value == 'months') {
seconds = mult * 30;
} else if ($('{$name}_units').value == 'years') {
seconds = mult * 365;
}
}
else {
seconds = 0;
}
$('{$name}').value = seconds;
EOJS;
}
*/
$script .= <<<EOJS
if ($('{$name}_units').value == 'noenddate') {
$('{$name}_number').disabled = true;
}
......@@ -128,14 +88,14 @@ EOJS;
</script>
EOJS;
return $numberinput . $uselect . $hidden . $script;
return $numberinput . $uselect . $script;
}
function get_expiry_units() {
return array('days','weeks','months','years','noenddate');
function pieform_render_expire_get_expiry_units() {
return array('days', 'weeks', 'months', 'years', 'noenddate');
}
function seconds_in($unit) {
function pieform_render_expiry_seconds_in($unit) {
$dayseconds = 60 * 60 * 24;
switch ($unit) {
case 'days' : return $dayseconds;
......@@ -146,25 +106,25 @@ function seconds_in($unit) {
}
}
function get_expiry_from_seconds($seconds) {
function pieform_render_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 = seconds_in('years');
$yearseconds = pieform_render_expiry_seconds_in('years');
if ($seconds % $yearseconds == 0 && $seconds > 0) {
return array('number' => (int) ($seconds / $yearseconds), 'units' => 'years');
}
$monthseconds = seconds_in('months');
$monthseconds = pieform_render_expiry_seconds_in('months');
if ($seconds % $monthseconds == 0 && $seconds > 0) {
return array('number' => (int) ($seconds / $monthseconds), 'units' => 'months');
}
$weekseconds = seconds_in('weeks');
$weekseconds = pieform_render_expiry_seconds_in('weeks');
if ($seconds % $weekseconds == 0 && $seconds > 0) {
return array('number' => (int) ($seconds / $weekseconds), 'units' => 'weeks');
}
$dayseconds = seconds_in('days');
$dayseconds = pieform_render_expiry_seconds_in('days');
if ($seconds % $dayseconds == 0) {
return array('number' => (int) ($seconds / $dayseconds), 'units' => 'days');
}
......@@ -180,40 +140,18 @@ function pieform_get_value_expiry($element, Pieform $form) {
if ($unit == 'noenddate') {
return null;
}
$allunits = get_expiry_units();
$allunits = pieform_render_expire_get_expiry_units();
$number = $global[$name . '_number'];
if (!in_array($unit,$allunits) || $number < 0) {
return null;
}
return $number * seconds_in($unit);
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
var seconds = null;
//if ($('{$name}_units').value == 'noenddate') {
// seconds = null;
//}
//else {
// if ($('{$name}_number').value > 0) {
// var mult = $('{$name}_number').value * 60 * 60 * 24;
// if ($('{$name}_units').value == 'days') {
// seconds = mult;
// } else if ($('{$name}_units').value == 'weeks') {
// seconds = mult * 7;
// } else if ($('{$name}_units').value == 'months') {
// seconds = mult * 30;
// } else if ($('{$name}_units').value == 'years') {
// seconds = mult * 365;
// }
// }
// else {
// seconds = 0;
// }
//}
//data['{$name}'] = seconds;
data['{$name}_number'] = $('{$name}_number').value;
data['{$name}_units'] = $('{$name}_units').value;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment