[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/webform/components/ -> number.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Webform module number component.
   6   */
   7  
   8  /**
   9   * Implements _webform_defaults_component().
  10   */
  11  function _webform_defaults_number() {
  12    return array(
  13      'name' => '',
  14      'form_key' => NULL,
  15      'pid' => 0,
  16      'weight' => 0,
  17      'value' => '',
  18      'mandatory' => 0,
  19      'extra' => array(
  20        'type' => 'textfield',
  21        'field_prefix' => '',
  22        'field_suffix' => '',
  23        'unique' => 0,
  24        'title_display' => 0,
  25        'description' => '',
  26        'attributes' => array(),
  27        'private' => FALSE,
  28        'min' => '',
  29        'max' => '',
  30        'step' => '',
  31        'decimals' => '',
  32        'point' => '.',
  33        'separator' => ',',
  34        'integer' => 0,
  35        'excludezero' => 0,
  36      ),
  37    );
  38  }
  39  
  40  /**
  41   * Implements _webform_theme_component().
  42   */
  43  function _webform_theme_number() {
  44    return array(
  45      'webform_number' => array(
  46        'variables' => array('element' => NULL),
  47        'file' => 'components/number.inc',
  48      ),
  49      'webform_display_number' => array(
  50        'variables' => 'element',
  51        'file' => 'components/number.inc',
  52      ),
  53    );
  54  }
  55  
  56  /**
  57   * Implements _webform_edit_component().
  58   */
  59  function _webform_edit_number($component) {
  60    $form = array();
  61    $form['value'] = array(
  62      '#type' => 'textfield',
  63      '#title' => t('Default value'),
  64      '#default_value' => $component['value'],
  65      '#description' => t('The default value of the field.') . theme('webform_token_help'),
  66      '#size' => 60,
  67      '#maxlength' => 1024,
  68      '#weight' => 0,
  69    );
  70    $form['display']['type'] = array(
  71      '#type' => 'radios',
  72      '#title' => t('Element type'),
  73      '#options' => array(
  74        'textfield' => t('Text field'),
  75        'select' => t('Select list'),
  76      ),
  77      '#default_value' => $component['extra']['type'],
  78      '#description' => t('A minimum and maximum value are required if displaying as a select.'),
  79      '#weight' => -1,
  80      '#parents' => array('extra', 'type'),
  81    );
  82    $form['display']['field_prefix'] = array(
  83      '#type' => 'textfield',
  84      '#title' => t('Prefix text placed to the left of the field'),
  85      '#default_value' => $component['extra']['field_prefix'],
  86      '#description' => t('Examples: $, #, -.') . ' ' . t('This option only affects text fields.'),
  87      '#size' => 20,
  88      '#maxlength' => 127,
  89      '#weight' => 1.1,
  90      '#parents' => array('extra', 'field_prefix'),
  91    );
  92    $form['display']['field_suffix'] = array(
  93      '#type' => 'textfield',
  94      '#title' => t('Postfix text placed to the right of the field'),
  95      '#default_value' => $component['extra']['field_suffix'],
  96      '#description' => t('Examples: lb, kg, %.') . ' ' . t('This option only affects text fields.'),
  97      '#size' => 20,
  98      '#maxlength' => 127,
  99      '#weight' => 1.2,
 100      '#parents' => array('extra', 'field_suffix'),
 101    );
 102    $form['display']['decimals'] = array(
 103      '#type' => 'select',
 104      '#title' => t('Decimal places'),
 105      '#options' => array('' => t('Automatic')) + drupal_map_assoc(range(0, 10)),
 106      '#description' => t('Automatic will display up to @count decimals places if needed. A value of "2" is common to format currency amounts.', array('@count' => '4')),
 107      '#default_value' => $component['extra']['decimals'],
 108      '#weight' => 2,
 109      '#parents' => array('extra', 'decimals'),
 110      '#element_validate' => array('_webform_edit_number_validate'),
 111    );
 112    $form['display']['separator'] = array(
 113      '#type' => 'select',
 114      '#title' => t('Thousands separator'),
 115      '#options' => array(
 116        ',' => t('Comma (,)'),
 117        '.' => t('Period (.)'),
 118        ' ' => t('Space ( )'),
 119        '' => t('None'),
 120      ),
 121      '#default_value' => $component['extra']['separator'],
 122      '#weight' => 3,
 123      '#parents' => array('extra', 'separator'),
 124      '#element_validate' => array('_webform_edit_number_validate'),
 125    );
 126    $form['display']['point'] = array(
 127      '#type' => 'select',
 128      '#title' => t('Decimal point'),
 129      '#options' => array(
 130        ',' => t('Comma (,)'),
 131        '.' => t('Period (.)'),
 132      ),
 133      '#default_value' => $component['extra']['point'],
 134      '#weight' => 4,
 135      '#parents' => array('extra', 'point'),
 136      '#element_validate' => array('_webform_edit_number_validate'),
 137    );
 138    $form['validation']['unique'] = array(
 139      '#type' => 'checkbox',
 140      '#title' => t('Unique'),
 141      '#return_value' => 1,
 142      '#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'),
 143      '#weight' => 1,
 144      '#default_value' => $component['extra']['unique'],
 145      '#parents' => array('extra', 'unique'),
 146    );
 147    $form['validation']['integer'] = array(
 148      '#type' => 'checkbox',
 149      '#title' => t('Integer'),
 150      '#return_value' => 1,
 151      '#description' => t('Permit only integer values as input. e.g. 12.34 would be invalid.'),
 152      '#weight' => 1.5,
 153      '#default_value' => $component['extra']['integer'],
 154      '#parents' => array('extra', 'integer'),
 155    );
 156    $form['validation']['min'] = array(
 157      '#type' => 'textfield',
 158      '#title' => t('Minimum'),
 159      '#default_value' => $component['extra']['min'],
 160      '#description' => t('Minimum numeric value. e.g. 0 would ensure positive numbers.'),
 161      '#size' => 5,
 162      '#maxlength' => 10,
 163      '#weight' => 2,
 164      '#parents' => array('extra', 'min'),
 165      '#element_validate' => array('_webform_edit_number_validate'),
 166    );
 167    $form['validation']['max'] = array(
 168      '#type' => 'textfield',
 169      '#title' => t('Maximum'),
 170      '#default_value' => $component['extra']['max'],
 171      '#description' => t('Maximum numeric value. This may also determine the display width of your field.'),
 172      '#size' => 5,
 173      '#maxlength' => 10,
 174      '#weight' => 2,
 175      '#parents' => array('extra', 'max'),
 176      '#element_validate' => array('_webform_edit_number_validate'),
 177    );
 178    $form['validation']['step'] = array(
 179      '#type' => 'textfield',
 180      '#title' => t('Step'),
 181      '#default_value' => $component['extra']['step'],
 182      '#description' => t('Limit options to a specific increment. e.g. a step of "5" would allow values 5, 10, 15, etc.'),
 183      '#size' => 5,
 184      '#maxlength' => 10,
 185      '#weight' => 3,
 186      '#parents' => array('extra', 'step'),
 187      '#element_validate' => array('_webform_edit_number_validate'),
 188    );
 189    // Analysis settings.
 190    $form['analysis'] = array(
 191      '#type' => 'fieldset',
 192      '#title' => t('Analysis'),
 193      '#collapsible' => TRUE,
 194      '#collapsed' => FALSE,
 195      '#weight' => 10,
 196    );
 197    $form['analysis']['excludezero'] = array(
 198      '#type' => 'checkbox',
 199      '#title' => t('Exclude zero'),
 200      '#return_value' => 1,
 201      '#description' => t('Exclude entries of zero (or blank) when counting submissions to calculate average and standard deviation.'),
 202      '#weight' => 1.5,
 203      '#default_value' => $component['extra']['excludezero'],
 204      '#parents' => array('extra', 'excludezero'),
 205    );
 206    return $form;
 207  }
 208  
 209  /**
 210   * Theme function to render a number component.
 211   */
 212  function theme_webform_number($element) {
 213    // This IF statement is mostly in place to allow our tests to set type="text"
 214    // because SimpleTest does not support type="number".
 215    if (!isset($element['#attributes']['type'])) {
 216      $element['#attributes']['type'] = 'number';
 217    }
 218  
 219    // Step property *must* be a full number with 0 prefix if a decimal.
 220    if (!is_int($element['#step'] * 1)) {
 221      $decimals = strlen($element['#step']) - strrpos($element['#step'], '.') - 1;
 222      $element['#step'] = sprintf('%1.' . $decimals . 'F', $element['#step']);
 223    }
 224  
 225    // Convert properties to attributes on the element if set.
 226    foreach (array('id', 'name', 'value', 'size', 'min', 'max', 'step') as $property) {
 227      if (isset($element['#' . $property]) && $element['#' . $property] !== '') {
 228        $element['#attributes'][$property] = $element['#' . $property];
 229      }
 230    }
 231    _form_set_class($element, array('form-text', 'form-number'));
 232  
 233    $output = '';
 234    if (isset($element['#field_prefix'])) {
 235      $output .= '<span class="field-prefix">'. $element['#field_prefix'] .'</span> ';
 236    }
 237  
 238    $output .= '<input' . drupal_attributes($element['#attributes']) . ' />';
 239  
 240    if (isset($element['#field_suffix'])) {
 241      $output .= ' <span class="field-suffix">'. $element['#field_suffix'] .'</span>';
 242    }
 243  
 244    return theme('form_element', $element, $output);
 245  }
 246  
 247  /**
 248   * Implements _webform_render_component().
 249   */
 250  function _webform_render_number($component, $value = NULL, $filter = TRUE) {
 251    $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
 252  
 253    $element = array(
 254      '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
 255      '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
 256      '#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'],
 257      '#required' => $component['mandatory'],
 258      '#weight' => $component['weight'],
 259      '#field_prefix' => empty($component['extra']['field_prefix']) ? NULL : ($filter ? _webform_filter_xss($component['extra']['field_prefix']) : $component['extra']['field_prefix']),
 260      '#field_suffix' => empty($component['extra']['field_suffix']) ? NULL : ($filter ? _webform_filter_xss($component['extra']['field_suffix']) : $component['extra']['field_suffix']),
 261      '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
 262      '#attributes' => $component['extra']['attributes'],
 263      '#element_validate' => array('_webform_validate_number'),
 264      '#theme_wrappers' => array('webform_element_wrapper'),
 265      '#pre_render' => array('webform_element_title_display'),
 266      '#post_render' => array('webform_element_wrapper'),
 267      '#min' => $component['extra']['min'],
 268      '#max' => $component['extra']['max'],
 269      '#step' => abs($component['extra']['step']),
 270      '#integer' => $component['extra']['integer'],
 271      '#translatable' => array('title', 'description', 'field_prefix', 'field_suffix'),
 272    );
 273  
 274    // Flip the min and max properties to make min less than max if needed.
 275    if ($element['#min'] !== '' && $element['#max'] !== '' && $element['#min'] > $element['#max']) {
 276      $max = $element['#min'];
 277      $element['#min'] = $element['#max'];
 278      $element['#max'] = $max;
 279    }
 280  
 281    // Ensure #step starts with a zero if a decimal.
 282    if (!is_int($element['#step'] * 1)) {
 283      $decimals = strlen($element['#step']) - strrpos($element['#step'], '.') - 1;
 284      $element['#step'] = sprintf('%1.' . $decimals . 'F', $element['#step']);
 285    }
 286  
 287    if ($component['extra']['type'] == 'textfield') {
 288      // Render as textfield.
 289      $element['#type'] = 'webform_number';
 290  
 291      // Set the size property based on #max, to ensure consistent behavior for
 292      // browsers that do not support type = number.
 293      if ($element['#max']) {
 294        $element['#size'] = strlen($element['#max']) + 1;
 295      }
 296    }
 297    else {
 298      // Render as select.
 299      $element['#type'] = 'select';
 300  
 301      // Create user-specified options list as an array.
 302      $element['#options'] = _webform_number_select_options($component);
 303  
 304      // Add default options if using a select list with no default. This matches
 305      // Drupal 7's handling of select lists.
 306      if ($component['extra']['type'] == 'select' && $element['#default_value'] === '') {
 307        $element['#options'] = array('' => ($component['mandatory'] ? t('- Select -') : t('- None -'))) + $element['#options'];
 308      }
 309    }
 310  
 311    // Set user-entered values.
 312    if (isset($value[0])) {
 313      $element['#default_value'] = $value[0];
 314    }
 315  
 316    // Enforce uniqueness.
 317    if ($component['extra']['unique']) {
 318      $element['#element_validate'][] = 'webform_validate_unique';
 319    }
 320  
 321    return $element;
 322  }
 323  
 324  /**
 325   * Implements _webform_display_component().
 326   */
 327  function _webform_display_number($component, $value, $format = 'html') {
 328    $empty = !isset($value[0]) || $value[0] === '';
 329    return array(
 330      '#title' => $component['name'],
 331      '#weight' => $component['weight'],
 332      '#theme' => 'webform_display_number',
 333      '#theme_wrappers' => $format == 'html' ? array('webform_element', 'webform_element_wrapper') : array('webform_element_text'),
 334      '#post_render' => array('webform_element_wrapper'),
 335      '#field_prefix' => $empty ? '' : $component['extra']['field_prefix'],
 336      '#field_suffix' => $empty ? '' : $component['extra']['field_suffix'],
 337      '#format' => $format,
 338      '#value' => $empty ? '' : _webform_number_format($component, $value[0]),
 339      '#translatable' => array('title', 'field_prefix', 'field_suffix'),
 340    );
 341  }
 342  
 343  /**
 344   * Format the output of data for this component.
 345   */
 346  function theme_webform_display_number($element) {
 347    $prefix = $element['#format'] == 'html' ? filter_xss($element['#field_prefix']) : $element['#field_prefix'];
 348    $suffix = $element['#format'] == 'html' ? filter_xss($element['#field_suffix']) : $element['#field_suffix'];
 349    $value = $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
 350    return $value !== '' ? ($prefix . $value . $suffix) : ' ';
 351  }
 352  
 353  /**
 354   * Implements _webform_analysis_component().
 355   */
 356  function _webform_analysis_number($component, $sids = array(), $single = FALSE) {
 357    $advanced_stats = $single;
 358  
 359    $placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array();
 360    $sidfilter = count($sids) ? " AND sid in (" . implode(",", $placeholders) . ")" : "";
 361    $query = 'SELECT data ' .
 362      ' FROM {webform_submitted_data} ' .
 363      ' WHERE nid = %d ' .
 364      ' AND cid = %d ' . $sidfilter;
 365  
 366    $population = array();
 367    $submissions = 0;
 368    $nonzero = 0;
 369    $not_empty = 0;
 370    $sum = 0;
 371  
 372    $result = db_query($query, array_merge(array($component['nid'], $component['cid']), $sids));
 373    while ($data = db_fetch_array($result)) {
 374      $value = trim($data['data']);
 375      if ($value == '') {
 376        $number = 0.0;
 377      }
 378      else{
 379        $number = $value * 1.0;
 380      }
 381  
 382      if ($value !== '') {
 383        $not_empty++;
 384      }
 385  
 386      if ($number > 0) {
 387        $nonzero++;
 388        $sum += $number;
 389      }
 390      $population[] = $number;
 391      $submissions++;
 392    }
 393    sort($population, SORT_NUMERIC);
 394  
 395    // Average and population count.
 396    if ($component['extra']['excludezero']) {
 397      $average = $nonzero ? ($sum / $nonzero) : 0;
 398      $average_title = t('Average !mu excluding zeros/blanks', array('!mu' => $advanced_stats ? '(&mu;)' : ''));
 399      // Sample (sub-set of total population).
 400      $population_count = $nonzero - 1;
 401      $sigma = 'sd';
 402      $description = t('sample');
 403    }
 404    else {
 405      $average = $submissions ? ($sum / $submissions) : 0;
 406      $average_title = t('Average !mu including zeros/blanks', array('!mu' => $advanced_stats ? '(&mu;)' : ''));
 407      // Population.
 408      $population_count = $submissions;
 409      $sigma = '&sigma;';
 410      $description = t('population');
 411    }
 412  
 413    // Formatting.
 414    $average = _webform_number_format($component, $average);
 415    $sum = _webform_number_format($component, $sum);
 416  
 417    $rows[0] = array(t('Zero/blank'), ($submissions - $nonzero));
 418    $rows[1] = array(t('User entered value'), $not_empty);
 419    $rows[2] = array(t('Sum') . ($advanced_stats ? ' (&Sigma;)' : ''), $sum);
 420    $rows[3] = array($average_title, $average);
 421  
 422    if (!$advanced_stats && $sum != 0) {
 423      $rows[4] = array('', l(t('More stats »'), 'node/' . $component['nid'] . '/webform-results/analysis/' . $component['cid']));
 424    }
 425  
 426    // Normal distribution information.
 427    if ($advanced_stats && $population_count && $sum != 0) {
 428      // Standard deviation.
 429      $stddev = 0;
 430      foreach($population as $value) {
 431        // Obtain the total of squared variances.
 432        $stddev += pow(($value - $average), 2);
 433      }
 434      if ($population_count > 0) {
 435        $stddev = sqrt($stddev / $population_count);
 436      }
 437      else {
 438        $stddev = sqrt($stddev);
 439      }
 440  
 441      // Build normal distribution table rows.
 442      $count = array();
 443      $percent = array();
 444      $limit = array();
 445      $index = 0;
 446  
 447      $count[] = 0;
 448      $limit[] = $average - ($stddev * 4);
 449      foreach ($population as $value) {
 450        while ($value >= $limit[$index]) {
 451          $percent[] = number_format($count[$index] / $population_count * 100, 2, '.', '');
 452          $limit[] = $limit[$index] + $stddev;
 453          $index += 1;
 454          if ($limit[$index] == $average) {
 455            $limit[$index] = $limit[$index] + $stddev;
 456          }
 457          $count[$index] = 0;
 458        }
 459        $count[$index] += 1;
 460      }
 461      $percent[] = number_format($count[$index] / $population_count * 100, 2, '.', '');
 462  
 463      // Format normal distribution table output.
 464      $stddev = _webform_number_format($component, $stddev);
 465      $low = _webform_number_format($component, $population[0]);
 466      $high = _webform_number_format($component, end($population));
 467      foreach($limit as $key => $value) {
 468        $limit[$key] = _webform_number_format($component, $value);
 469      }
 470  
 471      // Column headings (override potential theme uppercase, e.g. Seven in D7).
 472      $header = array(
 473        t('Normal Distribution'),
 474        array('data' => '-4' . $sigma, 'style' => 'text-transform: lowercase;',),
 475        array('data' => '-3' . $sigma, 'style' => 'text-transform: lowercase;',),
 476        array('data' => '-2' . $sigma, 'style' => 'text-transform: lowercase;',),
 477        array('data' => '-1' . $sigma, 'style' => 'text-transform: lowercase;',),
 478        array('data' => '+1' . $sigma, 'style' => 'text-transform: lowercase;',),
 479        array('data' => '+2' . $sigma, 'style' => 'text-transform: lowercase;',),
 480        array('data' => '+3' . $sigma, 'style' => 'text-transform: lowercase;',),
 481        array('data' => '+4' . $sigma, 'style' => 'text-transform: lowercase;',),
 482      );
 483  
 484      // Insert row labels.
 485      array_unshift($limit, t('Boundary'));
 486      array_unshift($count, t('Count'));
 487      array_unshift($percent, t('% of !description', array('!description' => $description)));
 488  
 489      $output = theme('table', $header, array($limit, $count, $percent), array('class' => 'webform-grid'));
 490  
 491      $rows[4] = array(t('Range'), t('!low to !high', array('!low' => $low, '!high' => $high)));
 492      $rows[5] = array(t('Standard deviation (!sigma)', array('!sigma' => $sigma)), $stddev);
 493      $rows[6] = array(array('data' => $output, 'colspan' => 2));
 494    }
 495  
 496    return $rows;
 497  }
 498  
 499  /**
 500   * Implements _webform_table_component().
 501   */
 502  function _webform_table_number($component, $value) {
 503    return isset($value[0]) ? _webform_number_format($component, $value[0]) : '';
 504  }
 505  
 506  /**
 507   * Implements _webform_csv_headers_component().
 508   */
 509  function _webform_csv_headers_number($component, $export_options) {
 510    $header = array();
 511    $header[0] = '';
 512    $header[1] = '';
 513    $header[2] = $component['name'];
 514    return $header;
 515  }
 516  
 517  /**
 518   * Implements _webform_csv_data_component().
 519   */
 520  function _webform_csv_data_number($component, $export_options, $value) {
 521    if (isset($value[0]) && $component['extra']['decimals'] !== '') {
 522      $value[0] = number_format($value[0], $component['extra']['decimals'], '.', '');
 523    }
 524    return isset($value[0]) ? $value[0] : '';
 525  }
 526  
 527  /**
 528   * A Drupal Form API Validation function. Validates the entered values from
 529   * number components on the client-side form.
 530   *
 531   * @param $element
 532   *   The form element. May either be a select or a webform_number element.
 533   * @param $form_state
 534   *   The full form state for the webform.
 535   * @return
 536   *   None. Calls a form_set_error if the number is not valid.
 537   */
 538  function _webform_validate_number($element, &$form_state) {
 539    $value = trim($element['#value']);
 540    form_set_value($element, $value, $form_state);
 541  
 542    if ($value != '') {
 543      // Numeric test.
 544      if (is_numeric($value)) {
 545        // Range test.
 546        if ($element['#min'] != '' && $element['#max'] != '') {
 547          // Flip minimum and maximum if needed.
 548          if ($element['#max'] > $element['#min']) {
 549            $min = $element['#min'];
 550            $max = $element['#max'];
 551          }
 552          else {
 553            $min = $element['#max'];
 554            $max = $element['#min'];
 555          }
 556          if ($value > $max || $value < $min) {
 557            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)));
 558          }
 559        }
 560        elseif ($element['#max'] != '' && $value > $element['#max']) {
 561          form_error($element, t('%name field value must be less than @max.', array('%name' => $element['#title'], '@max' => $element['#max'])));
 562        }
 563        elseif ($element['#min'] != '' && $value < $element['#min']) {
 564          form_error($element, t('%name field value must be greater than @min.', array('%name' => $element['#title'], '@min' => $element['#min'])));
 565        }
 566  
 567        // Integer test.
 568        if ($element['#integer'] && !is_int($value * 1)) {
 569          form_error($element, t('%name field value of @value must be an integer.', array('%name' => $element['#title'], '@value' => $value)));
 570        }
 571  
 572        // Step test.
 573        $starting_number = $element['#min'] ? $element['#min'] : 0;
 574        if ($element['#step'] != 0 && fmod($element['#value'] - $starting_number, $element['#step']) != 0) {
 575          $samples = array(
 576            $starting_number,
 577            $starting_number + ($element['#step'] * 1),
 578            $starting_number + ($element['#step'] * 2),
 579            $starting_number + ($element['#step'] * 3),
 580          );
 581          if ($starting_number) {
 582            form_error($element, t('%name field value must be @start plus a multiple of @step. i.e. @samples, etc.', array('%name' => $element['#title'], '@start' => $element['#min'], '@step' => $element['#step'], '@samples' => implode(', ', $samples))));
 583          }
 584          else {
 585            form_error($element, t('%name field value must be a multiple of @step. i.e. @samples, etc.', array('%name' => $element['#title'], '@step' => $element['#step'], '@samples' => implode(', ', $samples))));
 586          }
 587        }
 588      }
 589      else {
 590        form_error($element, t('%name field value of @value must be numeric.', array('%name' => $element['#title'], '@value' => $value)));
 591      }
 592    }
 593  
 594  }
 595  
 596  /**
 597   * Validation of number edit form items.
 598   */
 599  function _webform_edit_number_validate($element, &$form_state) {
 600    // Shorten field names.
 601    $values = $form_state['values']['extra'];
 602  
 603    switch ($element['#name']) {
 604      case 'extra[min]':
 605        if ($values['min'] == '') {
 606          if ($values['type'] == 'select') {
 607            form_error($element, t('Minimum is required when using a select list element.'));
 608          }
 609        }
 610        else {
 611          if (!is_numeric($values['min'])) {
 612            form_error($element, t('Minimum must be numeric.'));
 613          }
 614          if ($values['integer'] && !is_int($values['min'] * 1)) {
 615            form_error($element, t('Minimum must have an integer value.'));
 616          }
 617        }
 618        break;
 619      case 'extra[max]':
 620        if ($values['max'] == '') {
 621          if ($values['type'] == 'select') {
 622            form_error($element, t('Maximum is required when using a select list element.'));
 623          }
 624        }
 625        else {
 626          if (!is_numeric($values['max'])) {
 627            form_error($element, t('Maximum must be numeric.'));
 628          }
 629          if ($values['integer'] && !is_int($values['max'] * 1)) {
 630            form_error($element, t('Maximum must have an integer value.'));
 631          }
 632        }
 633        break;
 634      case 'extra[step]':
 635        if ($values['step'] != '') {
 636          if (!is_numeric($values['step'])) {
 637            form_error($element, t('Step must be numeric.'));
 638          }
 639          else {
 640            if ($values['integer'] && !is_int($values['step'] * 1)) {
 641              form_error($element, t('Step must have an integer value.'));
 642            }
 643          }
 644        }
 645        break;
 646    }
 647    return TRUE;
 648  }
 649  
 650  /**
 651   * Generate select list options.
 652   */
 653  function _webform_number_select_options($component) {
 654    $options = array();
 655    $step = abs($component['extra']['step']);
 656  
 657    // Step is optional and defaults to 1.
 658    $step = empty($step) ? 1 : $step;
 659  
 660    // Generate list in correct direction.
 661    $min = $component['extra']['min'];
 662    $max = $component['extra']['max'];
 663    $flipped = FALSE;
 664    if ($max < $min) {
 665      $min = $component['extra']['max'];
 666      $max = $component['extra']['min'];
 667      $flipped = TRUE;
 668    }
 669  
 670    for ($f = $min; $f <= $max; $f += $step) {
 671      $options[$f . ''] = $f . '';
 672    }
 673  
 674    // TODO: HTML5 browsers apparently do not include the max value if it does
 675    // not line up with step. Restore this if needed in the future.
 676    // Add end limit if it's been skipped due to step.
 677    //if (end($options) != $max) {
 678    //  $options[$f] = $max;
 679    //}
 680  
 681    if ($flipped) {
 682      $options = array_reverse($options, TRUE);
 683    }
 684  
 685    // Apply requisite number formatting.
 686    foreach ($options as $key => $value) {
 687      $options[$key] = _webform_number_format($component, $value);
 688    }
 689  
 690    return $options;
 691  }
 692  
 693  /**
 694   * Apply number format.
 695   */
 696  function _webform_number_format($component, $value) {
 697    // If no decimal places are specified, do a best guess length of decimals.
 698    $decimals = $component['extra']['decimals'];
 699    if ($decimals === '') {
 700      // If it's an integer, no decimals needed.
 701      if (is_int(($value . '') * 1)) {
 702        $decimals = 0;
 703      }
 704      else {
 705        $decimals = strlen($value) - strrpos($value, '.') - 1;
 706      }
 707      if ($decimals > 4) {
 708        $decimals = 4;
 709      }
 710    }
 711  
 712    return number_format($value, $decimals, $component['extra']['point'], $component['extra']['separator']);
 713  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7