number.inc 24 KB

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