Merge remote-tracking branch 'origin/6.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / option / FloatOptionType.class.php
CommitLineData
11ade432 1<?php
a9229942 2
11ade432 3namespace wcf\system\option;
a9229942 4
11ade432 5use wcf\data\option\Option;
246ad0e4 6use wcf\system\database\util\PreparedStatementConditionBuilder;
11ade432 7use wcf\system\WCF;
246ad0e4 8use wcf\util\StringUtil;
11ade432
AE
9
10/**
a17de04e 11 * Option type implementation for float values.
a9229942
TD
12 *
13 * @author Tobias Friebel
14 * @copyright 2001-2011 Tobias Friebel
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11ade432 16 */
a9229942
TD
17class FloatOptionType extends TextOptionType
18{
19 /**
20 * @inheritDoc
21 */
22 protected $inputClass = 'short textRight';
23
24 /**
25 * @inheritDoc
26 */
27 public function getFormElement(Option $option, $value)
28 {
29 $value = \str_replace('.', WCF::getLanguage()->get('wcf.global.decimalPoint'), $value);
30
31 return parent::getFormElement($option, $value);
32 }
33
34 /**
35 * @inheritDoc
36 */
37 public function getData(Option $option, $newValue)
38 {
bf7c893c 39 return $this->toFloat($newValue);
a9229942
TD
40 }
41
42 /**
43 * @inheritDoc
44 */
45 public function compare($value1, $value2)
46 {
47 if ($value1 == $value2) {
48 return 0;
49 }
50
51 return ($value1 > $value2) ? 1 : -1;
52 }
bf7c893c 53
246ad0e4
AE
54 /**
55 * @inheritDoc
56 */
57 public function getCondition(PreparedStatementConditionBuilder &$conditions, Option $option, $value)
58 {
59 if (!isset($_POST['searchOptions'][$option->optionName])) {
60 return false;
61 }
62
a945e985 63 $value = StringUtil::trim($value ?: '0');
246ad0e4
AE
64 $value = $this->toFloat($value);
65
66 $conditions->add("option_value.userOption" . $option->optionID . " LIKE ?", [$value]);
2746ec19 67
246ad0e4
AE
68 return true;
69 }
70
bf7c893c
AE
71 /**
72 * Converts a localized string value into a float value.
73 */
2746ec19 74 protected function toFloat($value): float
bf7c893c
AE
75 {
76 $value = \str_replace(' ', '', $value);
77 $value = \str_replace(WCF::getLanguage()->get('wcf.global.thousandsSeparator'), '', $value);
78 $value = \str_replace(WCF::getLanguage()->get('wcf.global.decimalPoint'), '.', $value);
79
80 return \floatval($value);
81 }
11ade432 82}