Skip to content

Commit

Permalink
Upgrading webform module from 7.x-3.19 to 7.x-3.21.
Browse files Browse the repository at this point in the history
  • Loading branch information
superspring committed Dec 6, 2014
1 parent 05f319c commit 60bbe97
Show file tree
Hide file tree
Showing 20 changed files with 346 additions and 153 deletions.
12 changes: 9 additions & 3 deletions sites/all/modules/contrib/webform/components/date.inc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ function webform_expand_date($element) {
$timestring = webform_strtodate('c', $element['#default_value'], $timezone);
$element['#default_value'] = webform_date_array($timestring, 'date');
}
// Prevent an error in PHP 5.4 caused by core's treatment of the #value.
if (isset($element['#value'])) {
unset($element['#value']);
}

// Set defaults according to existing #default_value (set by Form API)
if (isset($element['#default_value']['month']) || isset($element['#default_value']['day']) || isset($element['#default_value']['year'])) {
Expand Down Expand Up @@ -200,6 +204,8 @@ function webform_expand_date($element) {
break;
}
unset($element[$type]['#value']);
$element[$type]['#title'] = $none;
$element[$type]['#title_display'] = 'invisible';
$element[$type]['#default_value'] = isset($default_values[$type]) ? $default_values[$type] : NULL;
$element[$type]['#options'] = array('' => $none) + $element[$type]['#options'];
}
Expand Down Expand Up @@ -233,9 +239,9 @@ function webform_expand_date($element) {
function theme_webform_date($variables) {
$element = $variables['element'];

$element['year']['#attributes']['class'] = array('year');
$element['month']['#attributes']['class'] = array('month');
$element['day']['#attributes']['class'] = array('day');
$element['year']['#attributes']['class'][] = 'year';
$element['month']['#attributes']['class'][] = 'month';
$element['day']['#attributes']['class'][] = 'day';

// Add error classes to all items within the element.
if (form_get_error($element)) {
Expand Down
4 changes: 2 additions & 2 deletions sites/all/modules/contrib/webform/components/fieldset.inc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function _webform_render_fieldset($component, $value = NULL, $filter = TRUE) {
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
'#collapsible' => $component['extra']['collapsible'],
'#collapsed' => $component['extra']['collapsed'],
'#attributes' => array('class' => array('webform-component-fieldset'), 'id' => 'webform-component-' . $component['form_key']),
'#attributes' => array('class' => array('webform-component-fieldset')),
'#pre_render' => array('webform_fieldset_prerender', 'webform_element_title_display'),
'#translatable' => array('title', 'description'),
);
Expand All @@ -74,7 +74,7 @@ function _webform_render_fieldset($component, $value = NULL, $filter = TRUE) {
* Pre-render function to set a fieldset ID.
*/
function webform_fieldset_prerender($element) {
$element['#attributes']['id'] = 'webform-component-' . str_replace('_', '-', implode('--', array_slice($element['#parents'], 1)));
$element['#id'] = 'webform-component-' . str_replace('_', '-', implode('--', array_slice($element['#parents'], 1)));
return $element;
}

Expand Down
2 changes: 1 addition & 1 deletion sites/all/modules/contrib/webform/components/file.inc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function _webform_edit_file_check_directory($element) {
$scheme = $element['extra']['scheme']['#value'];
$directory = $element['extra']['directory']['#value'];

$destination_dir = file_stream_wrapper_uri_normalize($scheme . '://' . $directory . '/webform');
$destination_dir = file_stream_wrapper_uri_normalize($scheme . '://webform/' . $directory);

// Sanity check input to prevent use parent (../) directories.
if (preg_match('/\.\.[\/\\\]/', $destination_dir . '/')) {
Expand Down
1 change: 1 addition & 0 deletions sites/all/modules/contrib/webform/components/grid.inc
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ function theme_webform_grid($variables) {
// Render each radio button in the row.
$radios = form_process_radios($question_element);
foreach (element_children($radios) as $key) {
$radios[$key]['#title'] = $question_element['#title'] . ' - ' . $radios[$key]['#title'];
$radios[$key]['#title_display'] = 'invisible';
$row[] = array('data' => drupal_render($radios[$key]), 'class' => array('checkbox', 'webform-grid-option'));
}
Expand Down
3 changes: 0 additions & 3 deletions sites/all/modules/contrib/webform/components/markup.inc
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ function _webform_render_markup($component, $value = NULL, $filter = TRUE) {
'#translatable' => array('title', 'markup'),
);

// TODO: Remove when #markup becomes available in D7.
$element['#value'] = $element['#markup'];

return $element;
}

Expand Down
171 changes: 135 additions & 36 deletions sites/all/modules/contrib/webform/components/number.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function _webform_defaults_number() {
'type' => 'textfield',
'field_prefix' => '',
'field_suffix' => '',
'disabled' => 0,
'unique' => 0,
'title_display' => 0,
'description' => '',
Expand Down Expand Up @@ -99,6 +100,15 @@ function _webform_edit_number($component) {
'#weight' => 1.2,
'#parents' => array('extra', 'field_suffix'),
);
$form['display']['disabled'] = array(
'#type' => 'checkbox',
'#title' => t('Disabled'),
'#return_value' => 1,
'#description' => t('Make this field non-editable. Useful for setting an unchangeable default value.'),
'#weight' => 11,
'#default_value' => $component['extra']['disabled'],
'#parents' => array('extra', 'disabled'),
);
$form['display']['decimals'] = array(
'#type' => 'select',
'#title' => t('Decimal places'),
Expand Down Expand Up @@ -266,9 +276,17 @@ function _webform_render_number($component, $value = NULL, $filter = TRUE) {
'#max' => $component['extra']['max'],
'#step' => $component['extra']['step'] ? abs($component['extra']['step']) : '',
'#integer' => $component['extra']['integer'],
'#translatable' => array('title', 'description'),
'#point' => $component['extra']['point'],
'#separator' => $component['extra']['separator'],
'#decimals' => $component['extra']['decimals'],
'#translatable' => array('title', 'description', 'field_prefix', 'field_suffix'),
);

// Set the decimal count to zero for integers.
if ($element['#integer'] && $element['#decimals'] === '') {
$element['#decimals'] = 0;
}

// Flip the min and max properties to make min less than max if needed.
if ($element['#min'] !== '' && $element['#max'] !== '' && $element['#min'] > $element['#max']) {
$max = $element['#min'];
Expand Down Expand Up @@ -308,14 +326,32 @@ function _webform_render_number($component, $value = NULL, $filter = TRUE) {

// Set user-entered values.
if (isset($value[0])) {
$element['#default_value'] = $value[0];
// If the value has been standardized, convert it to the expected format
// for display to the user.
if (webform_number_format_match($value[0], '.', '')) {
$element['#default_value'] = _webform_number_format($component, $value[0]);
}
// Otherwise use the user-defined input.
else {
$element['#default_value'] = $value[0];
}
}

// Enforce uniqueness.
if ($component['extra']['unique']) {
$element['#element_validate'][] = 'webform_validate_unique';
}

// Set readonly if disabled.
if ($component['extra']['disabled']) {
if ($filter) {
$element['#attributes']['readonly'] = 'readonly';
}
else {
$element['#disabled'] = TRUE;
}
}

return $element;
}

Expand All @@ -342,8 +378,8 @@ function _webform_display_number($component, $value, $format = 'html') {
*/
function theme_webform_display_number($variables) {
$element = $variables['element'];
$prefix = $element['#format'] == 'html' ? filter_xss($element['#field_prefix']) : $element['#field_prefix'];
$suffix = $element['#format'] == 'html' ? filter_xss($element['#field_suffix']) : $element['#field_suffix'];
$prefix = $element['#format'] == 'html' ? '' : $element['#field_prefix'];
$suffix = $element['#format'] == 'html' ? '' : $element['#field_suffix'];
$value = $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
return $value !== '' ? ($prefix . $value . $suffix) : ' ';
}
Expand All @@ -365,39 +401,28 @@ function _webform_analysis_number($component, $sids = array(), $single = FALSE)

$population = array();
$submissions = 0;
$nonzero = 0;
$not_empty = 0;
$non_zero = 0;
$non_empty = 0;
$sum = 0;

$result = $query->execute();
foreach ($result as $data) {
$value = trim($data['data']);
if ($value == '') {
$number = 0.0;
}
else {
$number = $value * 1.0;
}

if ($value !== '') {
$not_empty++;
}

if ($number > 0) {
$nonzero++;
$sum += $number;
}
$number = (float)$value;
$non_empty += (integer)($value !== '');
$non_zero += (integer)($number != 0.0);
$sum += $number;
$population[] = $number;
$submissions++;
}
sort($population, SORT_NUMERIC);

// Average and population count.
if ($component['extra']['excludezero']) {
$average = $nonzero ? ($sum / $nonzero) : 0;
$average = $non_zero ? ($sum / $non_zero) : 0;
$average_title = t('Average !mu excluding zeros/blanks', array('!mu' => $advanced_stats ? '(μ)' : ''));
// Sample (sub-set of total population).
$population_count = $nonzero - 1;
$population_count = $non_zero - 1;
$sigma = 'sd';
$description = t('sample');
}
Expand All @@ -414,8 +439,8 @@ function _webform_analysis_number($component, $sids = array(), $single = FALSE)
$average = _webform_number_format($component, $average);
$sum = _webform_number_format($component, $sum);

$rows[0] = array(t('Zero/blank'), ($submissions - $nonzero));
$rows[1] = array(t('User entered value'), $not_empty);
$rows[0] = array(t('Zero/blank'), ($submissions - $non_zero));
$rows[1] = array(t('User entered value'), $non_empty);
$rows[2] = array(t('Sum') . ($advanced_stats ? ' (Σ)' : ''), $sum);
$rows[3] = array($average_title, $average);

Expand All @@ -438,6 +463,11 @@ function _webform_analysis_number($component, $sids = array(), $single = FALSE)
$stddev = sqrt($stddev);
}

// Skip the rest of the distribution rows if standard deviation is 0.
if (empty($stddev)) {
return $rows;
}

// Build normal distribution table rows.
$count = array();
$percent = array();
Expand Down Expand Up @@ -536,12 +566,20 @@ function _webform_csv_data_number($component, $export_options, $value) {
* None. Calls a form_set_error if the number is not valid.
*/
function _webform_validate_number($element, &$form_state) {
// Trim spaces for basic cleanup.
$value = trim($element['#value']);
form_set_value($element, $value, $form_state);

if ($value != '') {
// First check that the entered value matches the expected value.
if (!webform_number_format_match($value, $element['#point'], $element['#separator'])) {
form_error($element, t('%name field value must format numbers as "@example".', array('%name' => $element['#title'], '@example' => webform_number_format(12345.6789, $element['#decimals'], $element['#point'], $element['#separator']))));
return;
}

// Numeric test.
if (is_numeric($value)) {
$numeric_value = webform_number_standardize($value, $element['#point']);
if (is_numeric($numeric_value)) {
// Range test.
if ($element['#min'] != '' && $element['#max'] != '') {
// Flip minimum and maximum if needed.
Expand All @@ -553,25 +591,25 @@ function _webform_validate_number($element, &$form_state) {
$min = $element['#max'];
$max = $element['#min'];
}
if ($value > $max || $value < $min) {
form_error($element, t('%name field value of @value should be in the range @min to @max.', array('%name' => $element['#title'], '@value' => $value, '@min' => $min, '@max' => $max)));
if ($numeric_value > $max || $numeric_value < $min) {
form_error($element, t('%name field value of @value should be in the range @min to @max.', array('%name' => $element['#title'], '@value' => $value, '@min' => $element['#min'], '@max' => $element['#max'])));
}
}
elseif ($element['#max'] != '' && $value > $element['#max']) {
elseif ($element['#max'] != '' && $numeric_value > $element['#max']) {
form_error($element, t('%name field value must be less than @max.', array('%name' => $element['#title'], '@max' => $element['#max'])));
}
elseif ($element['#min'] != '' && $value < $element['#min']) {
elseif ($element['#min'] != '' && $numeric_value < $element['#min']) {
form_error($element, t('%name field value must be greater than @min.', array('%name' => $element['#title'], '@min' => $element['#min'])));
}

// Integer test.
if ($element['#integer'] && !is_int($value * 1)) {
if ($element['#integer'] && !is_int($numeric_value * 1)) {
form_error($element, t('%name field value of @value must be an integer.', array('%name' => $element['#title'], '@value' => $value)));
}

// Step test.
$starting_number = $element['#min'] ? $element['#min'] : 0;
if ($element['#step'] != 0 && fmod($element['#value'] - $starting_number, $element['#step']) != 0) {
if ($element['#step'] != 0 && webform_modulo($numeric_value - $starting_number, $element['#step']) != 0) {
$samples = array(
$starting_number,
$starting_number + ($element['#step'] * 1),
Expand All @@ -590,7 +628,20 @@ function _webform_validate_number($element, &$form_state) {
form_error($element, t('%name field value of @value must be numeric.', array('%name' => $element['#title'], '@value' => $value)));
}
}
}

/**
* Implements _webform_submit_component().
*/
function _webform_submit_number($component, $value) {
// Because _webform_validate_number() ensures the format matches when moving
// forward through a form, this should always pass before saving into the
// database. When moving backwards in a form, do not adjust the value, since
// it has not yet been validated.
if (webform_number_format_match($value, $component['extra']['point'], $component['extra']['separator'])) {
$value = webform_number_standardize($value, $component['extra']['point']);
}
return $value;
}

/**
Expand Down Expand Up @@ -696,15 +747,38 @@ function _webform_number_select_options($component) {
}

/**
* Apply number format.
* Apply number format based on a component and number value.
*/
function _webform_number_format($component, $value) {
return webform_number_format($value, $component['extra']['decimals'], $component['extra']['point'], $component['extra']['separator']);
}

/**
* Validates if a provided number string matches an expected format.
*
* This function allows the thousands separator to be optional, but decimal
* points must be in the right location.
*
* Based on http://stackoverflow.com/questions/5917082/regular-expression-to-match-numbers-with-or-without-commas-and-decimals-in-text.
*/
function webform_number_format_match($value, $point, $separator) {
return preg_match('/^(-? ?[1-9](?:\d{0,2})(?:' . ($separator ? (preg_quote($separator, '/') . '?') : '') . '\d{3})*(?:' . preg_quote($point, '/') . '\d*[0-9])?|0?' . preg_quote($point, '/') . '\d*[1-9]|0)$/', $value);
}

/**
* Format a number with thousands separator, decimal point, and decimal places.
*
* This function is a wrapper around PHP's native number_format(), but allows
* the decimal places parameter to be NULL or an empty string, resulting in a
* behavior of no change to the decimal places.
*/
function webform_number_format($value, $decimals = NULL, $point = '.', $separator = ',') {
if (!is_numeric($value)) {
return '';
}

// If no decimal places are specified, do a best guess length of decimals.
$decimals = $component['extra']['decimals'];
if ($decimals === '') {
if (is_null($decimals) || $decimals === '') {
// If it's an integer, no decimals needed.
if (is_int(($value . '') * 1)) {
$decimals = 0;
Expand All @@ -717,5 +791,30 @@ function _webform_number_format($component, $value) {
}
}

return number_format($value, $decimals, $component['extra']['point'], $component['extra']['separator']);
return number_format($value, $decimals, $point, $separator);
}

/**
* Given a number, convert it to string compatible with a PHP float.
*
* @param string $value
* The string value to be standardized into a numeric string.
* @param $point
* The point separator between the whole number and the decimals.
*/
function webform_number_standardize($value, $point) {
// For simplicity, strip everything that's not the decimal point.
$value = preg_replace('/[^\-0-9' . preg_quote($point, '/') . ']/', '', $value);
// Convert the decimal point to a period.
$value = str_replace($point, '.', $value);
return $value;
}

/**
* Custom modulo function that properly handles float division.
*
* See https://drupal.org/node/1601968.
*/
function webform_modulo($a, $b) {
return $a - $b * (($b < 0) ? ceil($a / $b) : floor($a / $b));
}
Loading

0 comments on commit 60bbe97

Please sign in to comment.