FloatNumber.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Float element
  5. *
  6. * Accepts values like 3.1415 and 3,1415 (its processed and converted to 3.1415)
  7. *
  8. * Class Float
  9. */
  10. class FloatNumber extends HTML_QuickForm_text
  11. {
  12. /**
  13. * @param string $elementName
  14. * @param string $elementLabel
  15. * @param array $attributes
  16. */
  17. public function __construct($elementName = null, $elementLabel = null, $attributes = null)
  18. {
  19. if (!isset($attributes['id'])) {
  20. $attributes['id'] = $elementName;
  21. }
  22. $attributes['type'] = 'float';
  23. $attributes['class'] = 'form-control';
  24. parent::__construct($elementName, $elementLabel, $attributes);
  25. $this->_appendName = true;
  26. $this->setType('float');
  27. }
  28. /**
  29. * @param string $value
  30. */
  31. public function setValue($value)
  32. {
  33. $value = api_float_val($value);
  34. $this->updateAttributes(
  35. array(
  36. 'value'=>$value
  37. )
  38. );
  39. }
  40. /**
  41. * @return float
  42. */
  43. public function getValue()
  44. {
  45. $value = $this->getAttribute('value');
  46. $value = api_float_val($value);
  47. return $value;
  48. }
  49. /**
  50. * @param mixed $value
  51. * @param array $submitValues
  52. * @param array $errors
  53. */
  54. public function getSubmitValue($value, &$submitValues, &$errors)
  55. {
  56. $value = api_float_val($value);
  57. $elementName = $this->getName();
  58. $submitValues[$elementName] = $value;
  59. return $value;
  60. }
  61. /**
  62. * We check the options and return only the values that _could_ have been
  63. * selected. We also return a scalar value if select is not "multiple"
  64. */
  65. public function exportValue(&$submitValues, $assoc = false)
  66. {
  67. $value = $this->_findValue($submitValues);
  68. $value = api_float_val($value);
  69. if (!$value) {
  70. $value = '';
  71. }
  72. return $this->_prepareValue($value, $assoc);
  73. }
  74. }