/src/wp-content/plugins/custom-content-type-manager/validators/number.php

https://bitbucket.org/murtuza88/carolina-home-stone · PHP · 129 lines · 89 code · 12 blank · 28 comment · 19 complexity · 3f8b6be211b2156734148c038b113697 MD5 · raw file

  1. <?php
  2. /**
  3. * Numeric validation can be configured to allow a range of numbers,
  4. * @package CCTM_Validator
  5. *
  6. */
  7. class CCTM_Rule_number extends CCTM_Validator {
  8. public $options = array(
  9. 'min' => '',
  10. 'max' => '',
  11. 'allow_negative' => '',
  12. 'allow_decimals' => '',
  13. 'decimal_places' => '',
  14. 'decimal_separator' => '.',
  15. );
  16. /**
  17. * @return string a description of what the validation rule is and does.
  18. */
  19. public function get_description() {
  20. return __('Input must be a number, i.e. something that you could use in arithmetic.', CCTM_TXTDOMAIN);
  21. }
  22. /**
  23. * @return string the human-readable name of the validation rules.
  24. */
  25. public function get_name() {
  26. return __('Number', CCTM_TXTDOMAIN);
  27. }
  28. /**
  29. * Implement this if your validation rule requires some options: this should
  30. * return some form elements that will dynamically be shown on the page via
  31. * an AJAX request if this validator is selected.
  32. * Do not include the entire form, just the inputs you need!
  33. */
  34. public function get_options_html() {
  35. $min = '';
  36. if (is_numeric($this->min)) {
  37. $min = $this->min;
  38. }
  39. $max = '';
  40. if (is_numeric($this->max)) {
  41. $max = $this->max;
  42. }
  43. $negative_is_checked = '';
  44. if ($this->allow_negative) {
  45. $negative_is_checked = ' checked="checked"';
  46. }
  47. $decimals_is_checked = '';
  48. if ($this->allow_decimals) {
  49. $decimals_is_checked = ' checked="checked"';
  50. }
  51. $decimal_places = '';
  52. if ($this->decimal_places != '') {
  53. $decimal_places = (int) $this->decimal_places;
  54. }
  55. $comma_selected = '';
  56. $period_selected = '';
  57. if ('.' == $this->decimal_separator) {
  58. $period_selected = ' selected="selected"';
  59. }
  60. if (',' == $this->decimal_separator) {
  61. $comma_selected = ' selected="selected"';
  62. }
  63. $options = '<label class="cctm_label" for="'.$this->get_field_id('min').'">'.__('Min Value', CCTM_TXTDOMAIN).'</label>
  64. <input type="text" name="'.$this->get_field_name('min').'" id="'.$this->get_field_id('min').'" value="'.$min.'">
  65. <label class="cctm_label" for="'.$this->get_field_id('max').'">'.__('Max Value', CCTM_TXTDOMAIN).'</label>
  66. <input type="text" name="'.$this->get_field_name('max').'" id="'.$this->get_field_id('max').'" value="'.$max.'"><br/>
  67. <input type="checkbox" name="'.$this->get_field_name('allow_negative').'" id="'.$this->get_field_id('allow_negative').'" value="1" class="cctm_checkbox" '.$this->is_checked('allow_negative').' '.$negative_is_checked.'>
  68. <label class="cctm_checkbox_label" for="'.$this->get_field_id('allow_negative').'">'.__('Allow Negative Numbers', CCTM_TXTDOMAIN).'</label><br/>
  69. <input type="checkbox" name="'.$this->get_field_name('allow_decimals').'" id="'.$this->get_field_id('allow_decimals').'" value="1" class="cctm_checkbox" '.$this->is_checked('allow_decimals').' '.$decimals_is_checked.'>
  70. <label class="cctm_checkbox_label" for="'.$this->get_field_id('allow_decimals').'">'.__('Allow Decimals', CCTM_TXTDOMAIN).'</label><br/>
  71. <label class="cctm_label" for="'.$this->get_field_id('decimal_places').'">'.__('Maximum Decimal Places', CCTM_TXTDOMAIN).'</label>
  72. <input type="text" name="'.$this->get_field_name('decimal_places').'" id="'.$this->get_field_id('decimal_places').'" value="'.$decimal_places.'"><br/>
  73. <label class="cctm_label" for="'.$this->get_field_id('decimal_separator').'">'.__('Decimal Separator', CCTM_TXTDOMAIN).'</label>
  74. <select name="'.$this->get_field_name('decimal_separator').'" id="'.$this->get_field_id('decimal_separator').'">
  75. <option value="."'.$period_selected.'>'.__('Period', CCTM_TXTDOMAIN).' (.)</option>
  76. <option value=","'.$comma_selected.'>'.__('Comma', CCTM_TXTDOMAIN).' (,)</option>
  77. </select>
  78. ';
  79. return $options;
  80. }
  81. /**
  82. * Run the rule: check the user input. Return the (filtered) value that should
  83. * be used to repopulate the form.
  84. *
  85. * @param string $input (as it is stored in the database)
  86. * @return string
  87. */
  88. public function validate($input) {
  89. // Gotta be a number before we'll even talk to you
  90. if (!is_numeric($input)) {
  91. $this->error_msg = sprintf(__('The %s field must be numeric.', CCTM_TXTDOMAIN), $this->get_subject());
  92. }
  93. elseif (!$this->allow_negative && $input < 0) {
  94. $this->error_msg = sprintf(__('The %s field may not be negative.', CCTM_TXTDOMAIN), $this->get_subject());
  95. }
  96. elseif ($this->min && $this->max && ($input < $this->min || $input > $this->max)) {
  97. $this->error_msg = sprintf(__('The %s field must be between %s and %s.', CCTM_TXTDOMAIN), $this->get_subject(), $this->min, $this->max);
  98. }
  99. elseif ($this->min && $input < $this->min) {
  100. $this->error_msg = sprintf(__('The %s field must be greater than %s.', CCTM_TXTDOMAIN), $this->get_subject(), $this->min);
  101. }
  102. elseif ($this->max && $input > $this->max) {
  103. $this->error_msg = sprintf(__('The %s field must be less than %s.', CCTM_TXTDOMAIN), $this->get_subject(), $this->max);
  104. }
  105. // Whole integers only
  106. elseif (!$this->allow_decimals && strpos($input, $this->decimal_separator) !== false) {
  107. $this->error_msg = sprintf(__('The %s field must be a whole number (no decimals allowed).', CCTM_TXTDOMAIN), $this->get_subject());
  108. }
  109. // We do some strrev trickery to count # of decimal places
  110. elseif ($this->allow_decimals && strrpos(strrev($input), $this->decimal_separator) > $this->decimal_places) {
  111. $this->error_msg = sprintf(__('The %s field cannot contain more than %s decimal places.', CCTM_TXTDOMAIN), $this->get_subject(), $this->decimal_places);
  112. }
  113. return $input;
  114. }
  115. }
  116. /*EOF*/