Commit 1f2db621 authored by Nigel McNie's avatar Nigel McNie Committed by Nigel McNie
Browse files

Upgraded to pieforms 0.2.0

parent 51b2408a
This diff is collapsed.
......@@ -29,11 +29,12 @@
*
* The element must have the 'value' field set.
*
* @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
* @todo rename to inputbutton
*/
function pieform_render_button($element, Pieform $form) {
function pieform_element_button(Pieform $form, $element) {
if (!isset($element['value'])) {
throw new PieformException('Button elements must have a value');
}
......
......@@ -30,23 +30,32 @@
* General documentation about the calendar is available at
* http://www.dynarch.com/demos/jscalendar/doc/html/reference.html
*
* @param array $element The element to render
* @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_calendar($element, Pieform $form) {
function pieform_element_calendar(Pieform $form, $element) {
$id = $form->get_name() . '_' . $element['name'];
$value = $form->get_value($element);
if ($value) {
$value = Pieform::hsc(strftime($element['caloptions']['ifFormat'], $value));
}
// Build the HTML
$result = '<input type="text"'
. $form->element_attributes($element)
. ' value="' . ( $form->get_value($element) ? Pieform::hsc(strftime($element['caloptions']['ifFormat'],$form->get_value($element))) : '' ) . '">';
. ' value="' . $value . '">';
if (isset($element['imagefile'])) {
$result .= '<a href="" id="'. $id . '_btn" onclick="return false;" class="pieform-calendar-toggle">'
$result .= '<a href="" id="'. $id . '_btn" onclick="return false;" class="pieform-calendar-toggle"'
. ' tabindex="' . $element['tabindex'] . '">'
. '<img src="' . $element['imagefile'] . '" alt=""></a>';
}
else {
$result .= '<input type="button" id="' . $id . '_btn" onclick="return false;" class="pieform-calendar-toggle" value="...">';
$result .= '<input type="button" id="' . $id . '_btn" onclick="return false;" class="pieform-calendar-toggle"'
. ' value="..." tabindex="' . $element['tabindex'] . '">';
}
// Build the configuring javascript
$options = array_merge($element['caloptions'], array('inputField' => $id, 'button' => $id . '_btn'));
$encodedoptions = json_encode($options);
......@@ -55,10 +64,17 @@ function pieform_render_calendar($element, Pieform $form) {
$encodedoptions = preg_replace('/("' . $function . '"):"([a-zA-Z0-9$]+)"/', '\1:\2', $encodedoptions);
}
$result .= '<script type="text/javascript">Calendar.setup(' . $encodedoptions . ');</script>';
return $result;
}
function pieform_render_calendar_set_attributes($element) {
/**
* Sets default attributes of the calendar element.
*
* @param array $element The element to configure
* @return array The configured element
*/
function pieform_element_calendar_set_attributes($element) {
$element['jsroot'] = isset($element['jsroot']) ? $element['jsroot'] : '';
$element['language'] = isset($element['language']) ? $element['language'] : 'en';
$element['theme'] = isset($element['theme']) ? $element['theme'] : 'calendar-win2k-2';
......@@ -67,8 +83,13 @@ function pieform_render_calendar_set_attributes($element) {
return $element;
}
/** Returns code to go in <head> for all instances of calendar */
function pieform_get_headdata_calendar($element) {
/**
* Returns code to go in <head> for the given calendar instance
*
* @param array $element The element to get <head> code for
* @return array An array of HTML elements to go in the <head>
*/
function pieform_element_calendar_get_headdata($element) {
if (isset($element['themefile'])) {
$themefile = $element['themefile'];
}
......@@ -90,10 +111,16 @@ function pieform_get_headdata_calendar($element) {
return $result;
}
function pieform_get_value_calendar($element, Pieform $form) {
/**
* Retrieves the value of the calendar as a unix timestamp
*
* @param Pieform $form The form the element is attached to
* @param array $element The element to get the value for
* @return int The unix timestamp represented by the calendar
*/
function pieform_element_calendar_get_value(Pieform $form, $element) {
$name = $element['name'];
$global = ($form->get_method() == 'get') ? $_GET : $_POST;
$global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
if (isset($element['value'])) {
return $element['value'];
......@@ -107,7 +134,7 @@ function pieform_get_value_calendar($element, Pieform $form) {
$value = strtotime($global[$name]);
if ($value === false) {
$form->set_error($name, 'TODO');
$form->set_error($name, 'TODO (error for invalid calendar value)');
return null;
}
return $value;
......@@ -120,6 +147,4 @@ function pieform_get_value_calendar($element, Pieform $form) {
return null;
}
// TODO: (possibly, also might need the javascript version for ajax forms)
?>
......@@ -29,11 +29,11 @@
* normal submit buttons, only their name is changed (for use by the Pieform
* class internally).
*
* @param array $element The element to render
* @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_cancel($element, Pieform $form) {
function pieform_element_cancel(Pieform $form, $element) {
if (!isset($element['value'])) {
throw new PieformException('Cancel elements must have a value');
}
......@@ -46,14 +46,9 @@ function pieform_render_cancel($element, Pieform $form) {
. ' value="' . Pieform::hsc($element['value']) . '">';
}
// @todo how to support cancel buttons for ajax post? Possibly do a full post regardless...
// or allow the user to specify a javascript function to run... it could do document.location=
// @todo also, cancel buttons don't need to be sent around via js... maybe make this return empty string
function pieform_get_value_js_cancel($element, Pieform $form) {
//$formname = $form->get_name();
//$name = $element['name'];
//return " data['{$name}_cancel'] = document.forms['$formname'].elements['{$name}_cancel'].value;\n";
return '';
function pieform_element_cancel_set_attributes($element) {
$element['cancelelement'] = true;
return $element;
}
?>
......@@ -27,21 +27,20 @@
/**
* Provides a basic checkbox input.
*
* @param array $element The element to render
* @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_checkbox($element, Pieform $form) {
function pieform_element_checkbox(Pieform $form, $element) {
$checked = false;
// @todo use of 'value' and 'checked' here is ambiguous, need to write
// test cases and pick just one of them
if (!empty($element['value'])) {
$checked = true;
}
if ($form->get_value($element)) {
$global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
if (isset($global[$element['name']])) {
$checked = true;
}
else if (!$form->is_submitted() && !empty($element['checked'])) {
else if (!$form->is_submitted() && !empty($element['defaultvalue'])) {
$checked = true;
}
......@@ -51,16 +50,4 @@ function pieform_render_checkbox($element, Pieform $form) {
. '>';
}
function pieform_get_value_js_checkbox($element, Pieform $form) {
$formname = $form->get_name();
$name = $element['name'];
return <<<EOF
if (document.forms['$formname'].elements['$name'].checked) {
data['$name'] = 'on';
}
EOF;
}
?>
......@@ -27,38 +27,45 @@
/**
* Provides a date picker, in the form of three dropdowns.
*
* @param array $element The element to render
* @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_date($element, Pieform $form) {
function pieform_element_date(Pieform $form, $element) {
$result = '';
$name = $element['name'];
$name = $element['name'];
$element['minyear'] = (isset($element['minyear'])) ? intval($element['minyear']) : 1950;
$element['maxyear'] = (isset($element['maxyear'])) ? intval($element['maxyear']) : 2050;
if (!array_key_exists('defaultvalue', $element)) {
if (!isset($element['defaultvalue'])) {
$element['defaultvalue'] = time();
}
$required = (!empty($element['rules']['required']));
// Year
$value = pieform_render_select_get_value('year', $element['minyear'], $element['maxyear'], $element, $form);
$year = '<select name="' . $name . '_year" id="' . $name . '_year"' . (isset($element['optional']) && !isset($element['defaultvalue']) ? ' disabled="disabled"' : '') . ">\n";
$value = pieform_element_date_get_timeperiod_value('year', $element['minyear'], $element['maxyear'], $element, $form);
$year = '<select name="' . $name . '_year" id="' . $name . '_year"'
. (!$required && !isset($element['defaultvalue']) ? ' disabled="disabled"' : '')
. ' tabindex="' . $element['tabindex'] . "\">\n";
for ($i = $element['minyear']; $i <= $element['maxyear']; $i++) {
$year .= "\t<option value=\"$i\"" . (($value == $i) ? ' selected="selected"' : '') . ">$i</option>\n";
}
$year .= "</select>\n";
// Month
$value = pieform_render_select_get_value('month', 1, 12, $element, $form);
$month = '<select name="' . $name . '_month" id="' . $name . '_month"' . (isset($element['optional']) && !isset($element['defaultvalue']) ? ' disabled="disabled"' : '') . ">\n";
$value = pieform_element_date_get_timeperiod_value('month', 1, 12, $element, $form);
$month = '<select name="' . $name . '_month" id="' . $name . '_month"'
. (!$required && !isset($element['defaultvalue']) ? ' disabled="disabled"' : '')
. ' tabindex="' . $element['tabindex'] . "\">\n";
for ($i = 1; $i <= 12; $i++) {
$month .= "\t<option value=\"$i\"" . (($value == $i) ? ' selected="selected"' : '') . '>' . date('M', strtotime("2000-$i-01")) . "</option>\n";
}
$month .= "</select>\n";
// Day
$value = pieform_render_select_get_value('day', 1, 31, $element, $form);
$day = '<select name="' . $name . '_day" id="' . $name . '_day"' . (isset($element['optional']) && !isset($element['defaultvalue']) ? ' disabled="disabled"' : '') . ">\n";
$value = pieform_element_date_get_timeperiod_value('day', 1, 31, $element, $form);
$day = '<select name="' . $name . '_day" id="' . $name . '_day"'
. (!$required && !isset($element['defaultvalue']) ? ' disabled="disabled"' : '')
. ' tabindex="' . $element['tabindex'] . "\">\n";
for ($i = 1; $i <= 31; $i++) {
$day .= "\t<option value=\"$i\"" . (($value == $i) ? ' selected="selected"' : '') . ">$i</option>\n";
}
......@@ -67,7 +74,7 @@ function pieform_render_date($element, Pieform $form) {
$result = $year . $month . $day;
// Optional control
if (isset($element['optional'])) {
if (!$required) {
$optional = <<<EOF
<script type="text/javascript">
function {$name}_toggle(x) {
......@@ -87,8 +94,11 @@ EOF;
// @todo this needs cleaning up, namely:
// - get_string is a mahara-ism
// - 'optional' => true should be 'required' => false shouldn't it?
$optional .= ' ' . get_string('or') . ' <input type="checkbox" ' . ( isset($element['defaultvalue']) ? '' : 'checked ') . 'name="' . $name . '_optional" id="' . $name . '_optional" onchange="' . $name . '_toggle(this)">';
$optional .= ' <label for="' . $name . '_optional">' . get_string('notspecified');
$optional .= ' ' . $form->i18n('element', 'date', 'or', $element) . ' <input type="checkbox" '
. (isset($element['defaultvalue']) ? '' : 'checked="checked" ')
. 'name="' . $name . '_optional" id="' . $name . '_optional" onchange="' . $name . '_toggle(this)" '
. 'tabindex="' . $element['tabindex'] . '">';
$optional .= ' <label for="' . $name . '_optional">' . $form->i18n('element', 'date', 'notspecified', $element);
$result .= $optional;
}
......@@ -96,10 +106,16 @@ EOF;
return $result;
}
/** gets the value explicitly from the request */
function pieform_get_value_date($element, Pieform $form) {
/**
* Gets the value of the date element from the request and converts it into a
* unix timestamp.
*
* @param Pieform $form The form the element is attached to
* @param array $element The element to get the value for
*/
function pieform_element_date_get_value(Pieform $form, $element) {
$name = $element['name'];
$global = ($form->get_method() == 'get') ? $_GET : $_POST;
$global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
if ( isset($global[$name . '_day']) && isset($global[$name . '_month']) && isset($global[$name . '_year']) ) {
$time = mktime(0, 0, 0, $global[$name . '_month'], $global[$name . '_day'], $global[$name . '_year']);
if (false === $time) {
......@@ -111,19 +127,17 @@ function pieform_get_value_date($element, Pieform $form) {
return null;
}
function pieform_get_value_js_date($element, Pieform $form) {
$formname = $form->get_name();
$name = $element['name'];
return <<<EOF
data['{$name}_year'] = document.forms['$formname'].elements['{$name}_year'].value;
data['{$name}_month'] = document.forms['$formname'].elements['{$name}_month'].value;
data['{$name}_day'] = document.forms['$formname'].elements['{$name}_day'].value;
EOF;
function pieform_element_date_i18n() {
return array(
'en.utf8' => array(
'or' => 'or',
'notspecified' => 'Not specified'
)
);
}
/** helper: used when rendering the element, to get the value for it */
function pieform_render_select_get_value($timeperiod, $min, $max, $element, Pieform $form) {
function pieform_element_date_get_timeperiod_value($timeperiod, $min, $max, $element, Pieform $form) {
static $lookup = array(
'year' => 0,
'month' => 1,
......@@ -139,7 +153,7 @@ function pieform_render_select_get_value($timeperiod, $min, $max, $element, Pief
return $value;
}
$global = ($form->get_method() == 'get') ? $_GET : $_POST;
$global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
if (isset($global[$element['name'] . '_' . $timeperiod])) {
$value = $global[$element['name'] . '_' . $timeperiod];
if ($value < $min || $value > $max) {
......
......@@ -28,33 +28,34 @@
* 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) {
$formname = $form->get_name();
$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'])
else if (isset($global[$element['name']])
&& isset($global[$element['name'] . '_units'])) {
$values = array('number' => $global[$element['name'] . '_number'],
$values = array('number' => $global[$element['name']],
'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 +65,21 @@ function pieform_render_expiry($element, Pieform $form) {
// @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 .= ' type="text" size="4" name="' . $name . '_number"';
$numberinput .= ' id="' . $name . '_number" value="' . $values['number'] . '" tabindex="' . $element['tabindex'] . "\">\n";
$numberinput .= ($values['units'] == 'noenddate' && empty($element['rules']['required'])) ? ' disabled="disabled"' : '';
$numberinput .= ' type="text" size="4" name="' . $name . '"';
$numberinput .= ' id="' . $formname . '_' . $name . '" value="' . $values['number'] . '" tabindex="' . $element['tabindex'] . '"';
$numberinput .= (isset($element['error']) ? ' class="error"' : '') . ">\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";
$uselect .= 'name="' . $name . '_units" id="' . $formname . '_' . $name . '_units"' . ' tabindex="' . $element['tabindex'] . "\">\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";
......@@ -79,11 +87,11 @@ function pieform_render_expiry($element, Pieform $form) {
$script = <<<EOJS
<script type="text/javascript" language="javascript">
function {$name}_change() {
if ($('{$name}_units').value == 'noenddate') {
$('{$name}_number').disabled = true;
if ($('{$formname}_{$name}_units').value == 'noenddate') {
$('{$formname}_{$name}').disabled = true;
}
else {
$('{$name}_number').disabled = false;
$('{$formname}_{$name}').disabled = false;
}
}
</script>
......@@ -92,71 +100,78 @@ EOJS;
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];
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;
}
?>
......@@ -28,21 +28,49 @@
* Renders a fieldset. Fieldsets contain other elements, and do not count as a
* "true" element, in that they do not have a value and cannot be validated.
*
* @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_fieldset($element, Pieform $form) {
$result = "\n<fieldset>\n";
function pieform_element_fieldset(Pieform $form, $element) {
$result = "\n<fieldset";
if (!empty($element['collapsable'])) {
$classes = array('collapsable');
// Work out whether any of the children have errors on them
$error = false;
foreach ($element['elements'] as $subelement) {
if (isset($subelement['error'])) {
$error = true;
break;
}
}
if (!empty($element['collapsed']) && !$error) {
$classes[] = 'collapsed';
}
$result .= ' class="' . implode(' ', $classes) . '"';
}
$result .= ">\n";
if (isset($element['legend'])) {
$result .= '<legend>' . Pieform::hsc($element['legend']) . "</legend>\n";
$result .= '<legend';
if (!empty($element['collapsable'])) {
$id = substr(md5(microtime()), 0, 4);
$result .= ' id="' . $id . '">';
$result .= '<script type="text/javascript">';
$result .= "var a = A({'href':'', 'tabindex':{$form->get_property('tabindex')}}, " . json_encode($element['legend']) . "); ";