From b564bf81ff2611a5a1cf65d01f6054d7630ff2f3 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Tue, 27 May 2014 20:09:39 +0200 Subject: [PATCH] Added ability to compare two option values --- .../option/AbstractOptionType.class.php | 7 ++++ .../system/option/BooleanOptionType.class.php | 11 ++++++ .../system/option/DateOptionType.class.php | 11 ++++++ .../option/FileSizeOptionType.class.php | 11 ++++++ .../system/option/FloatOptionType.class.php | 11 ++++++ .../lib/system/option/IOptionType.class.php | 14 ++++++++ .../system/option/IntegerOptionType.class.php | 11 ++++++ .../option/TextareaOptionType.class.php | 23 ++++++++++++ .../BBCodeSelectUserGroupOptionType.class.php | 36 +++++++++++++++++++ ...finiteIntegerUserGroupOptionType.class.php | 18 ++++++++++ ...nverseIntegerUserGroupOptionType.class.php | 18 ++++++++++ ...nverseIntegerUserGroupOptionType.class.php | 11 ++++++ .../UserGroupsUserGroupOptionType.class.php | 23 ++++++++++++ 13 files changed, 205 insertions(+) diff --git a/wcfsetup/install/files/lib/system/option/AbstractOptionType.class.php b/wcfsetup/install/files/lib/system/option/AbstractOptionType.class.php index a8356ab368..2725a8b963 100644 --- a/wcfsetup/install/files/lib/system/option/AbstractOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/AbstractOptionType.class.php @@ -44,4 +44,11 @@ abstract class AbstractOptionType implements IOptionType { public function supportI18n() { return $this->supportI18n; } + + /** + * @see \wcf\system\option\IOptionType::compare() + */ + public function compare() { + return 0; + } } diff --git a/wcfsetup/install/files/lib/system/option/BooleanOptionType.class.php b/wcfsetup/install/files/lib/system/option/BooleanOptionType.class.php index cab4b4ae4e..7c8fb32b3a 100644 --- a/wcfsetup/install/files/lib/system/option/BooleanOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/BooleanOptionType.class.php @@ -90,4 +90,15 @@ class BooleanOptionType extends AbstractOptionType implements ISearchableUserOpt public function getConditionData(Option $option, $newValue) { return $newValue; } + + /** + * @see \wcf\system\option\IOptionType::compare() + */ + public function compare($value1, $value2) { + if ($value1 == $value2) { + return 0; + } + + return ($value1 === true) ? 1 : -1; + } } diff --git a/wcfsetup/install/files/lib/system/option/DateOptionType.class.php b/wcfsetup/install/files/lib/system/option/DateOptionType.class.php index 46fc3bb972..1af53100a7 100644 --- a/wcfsetup/install/files/lib/system/option/DateOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/DateOptionType.class.php @@ -38,4 +38,15 @@ class DateOptionType extends TextOptionType { throw new UserInputException($option->optionName, 'validationFailed'); } } + + /** + * @see \wcf\system\option\IOptionType::compare() + */ + public function compare($value1, $value2) { + if ($value1 == $value2) { + return 0; + } + + return (strtotime($value1) > strtotime($value2)) ? 1 : -1; + } } diff --git a/wcfsetup/install/files/lib/system/option/FileSizeOptionType.class.php b/wcfsetup/install/files/lib/system/option/FileSizeOptionType.class.php index 034f94c721..cf85e6fc4f 100644 --- a/wcfsetup/install/files/lib/system/option/FileSizeOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/FileSizeOptionType.class.php @@ -63,4 +63,15 @@ class FileSizeOptionType extends TextOptionType { $value = FileUtil::formatFileSize($value); return parent::getFormElement($option, $value); } + + /** + * @see \wcf\system\option\IOptionType::compare() + */ + public function compare($value1, $value2) { + if ($value1 == $value2) { + return 0; + } + + return ($value1 > $value2) ? 1 : -1; + } } diff --git a/wcfsetup/install/files/lib/system/option/FloatOptionType.class.php b/wcfsetup/install/files/lib/system/option/FloatOptionType.class.php index abfc82fa47..c9153f18b4 100644 --- a/wcfsetup/install/files/lib/system/option/FloatOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/FloatOptionType.class.php @@ -37,4 +37,15 @@ class FloatOptionType extends TextOptionType { $newValue = str_replace(WCF::getLanguage()->get('wcf.global.decimalPoint'), '.', $newValue); return floatval($newValue); } + + /** + * @see \wcf\system\option\IOptionType::compare() + */ + public function compare($value1, $value2) { + if ($value1 == $value2) { + return 0; + } + + return ($value1 > $value2) ? 1 : -1; + } } diff --git a/wcfsetup/install/files/lib/system/option/IOptionType.class.php b/wcfsetup/install/files/lib/system/option/IOptionType.class.php index 23ae61c631..a7dbfb9473 100644 --- a/wcfsetup/install/files/lib/system/option/IOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/IOptionType.class.php @@ -55,4 +55,18 @@ interface IOptionType { * @return boolean */ public function supportI18n(); + + /** + * Compares two values and returns a PHP-like comparison result. + * + * $value1 < $value2 => -1 + * $value1 == $value2 => 0 + * $value1 > $value2 => 1 + * + * + * @param mixed $value1 + * @param mixed $value2 + * @return integer + */ + public function compare($value1, $value2); } diff --git a/wcfsetup/install/files/lib/system/option/IntegerOptionType.class.php b/wcfsetup/install/files/lib/system/option/IntegerOptionType.class.php index 51cbd76468..e74b297d24 100644 --- a/wcfsetup/install/files/lib/system/option/IntegerOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/IntegerOptionType.class.php @@ -51,4 +51,15 @@ class IntegerOptionType extends TextOptionType { throw new UserInputException($option->optionName, 'tooHigh'); } } + + /** + * @see \wcf\system\option\IOptionType::compare() + */ + public function compare($value1, $value2) { + if ($value1 == $value2) { + return 0; + } + + return ($value1 > $value2) ? 1 : -1; + } } diff --git a/wcfsetup/install/files/lib/system/option/TextareaOptionType.class.php b/wcfsetup/install/files/lib/system/option/TextareaOptionType.class.php index 26cf6231ab..73b55bde42 100644 --- a/wcfsetup/install/files/lib/system/option/TextareaOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/TextareaOptionType.class.php @@ -54,4 +54,27 @@ class TextareaOptionType extends TextOptionType { return $newValue; } + + /** + * @see \wcf\system\option\IOptionType::compare() + */ + public function compare($value1, $value2) { + $value1 = explode("\n", StringUtil::unifyNewlines($value1)); + $value2 = explode("\n", StringUtil::unifyNewlines($value2)); + + // check if value1 contains more elements than value2 + $diff = array_diff($value1, $value2); + if (!empty($diff)) { + return 1; + } + + // check if value1 contains less elements than value2 + $diff = array_diff($value2, $value1); + if (!empty($diff)) { + return -1; + } + + // both lists are equal + return 0; + } } diff --git a/wcfsetup/install/files/lib/system/option/user/group/BBCodeSelectUserGroupOptionType.class.php b/wcfsetup/install/files/lib/system/option/user/group/BBCodeSelectUserGroupOptionType.class.php index 89e010e629..1b4b6f9c7f 100644 --- a/wcfsetup/install/files/lib/system/option/user/group/BBCodeSelectUserGroupOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/user/group/BBCodeSelectUserGroupOptionType.class.php @@ -121,4 +121,40 @@ class BBCodeSelectUserGroupOptionType extends AbstractOptionType implements IUse } } } + + /** + * @see \wcf\system\option\IOptionType::compare() + */ + public function compare($value1, $value2) { + $value1 = explode(',', $value1); + $value2 = explode(',', $value2); + + // handle special 'all' value + if (in_array('all', $value1)) { + if (in_array('all', $value2)) { + return 0; + } + else { + return 1; + } + } + else if (in_array('all', $value2)) { + return -1; + } + + // check if value1 contains more BBCodes than value2 + $diff = array_diff($value1, $value2); + if (!empty($diff)) { + return 1; + } + + // check if value1 contains less BBCodes than value2 + $diff = array_diff($value2, $value1); + if (!empty($diff)) { + return -1; + } + + // both lists of BBCodes are equal + return 0; + } } diff --git a/wcfsetup/install/files/lib/system/option/user/group/InfiniteIntegerUserGroupOptionType.class.php b/wcfsetup/install/files/lib/system/option/user/group/InfiniteIntegerUserGroupOptionType.class.php index a9e455cf29..9205d74835 100644 --- a/wcfsetup/install/files/lib/system/option/user/group/InfiniteIntegerUserGroupOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/user/group/InfiniteIntegerUserGroupOptionType.class.php @@ -30,4 +30,22 @@ class InfiniteIntegerUserGroupOptionType extends IntegerUserGroupOptionType { return parent::merge($defaultValue, $groupValue); } } + + /** + * @see \wcf\system\option\IOptionType::compare() + */ + public function compare($value1, $value2) { + if ($value1 == $value2) { + return 0; + } + + if ($value1 == -1) { + return 1; + } + else if ($value2 == -1) { + return -1; + } + + return parent::compare($value1, $value2); + } } diff --git a/wcfsetup/install/files/lib/system/option/user/group/InfiniteInverseIntegerUserGroupOptionType.class.php b/wcfsetup/install/files/lib/system/option/user/group/InfiniteInverseIntegerUserGroupOptionType.class.php index 8f7742dc41..a6e615b84c 100644 --- a/wcfsetup/install/files/lib/system/option/user/group/InfiniteInverseIntegerUserGroupOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/user/group/InfiniteInverseIntegerUserGroupOptionType.class.php @@ -29,4 +29,22 @@ class InfiniteInverseIntegerUserGroupOptionType extends InverseIntegerUserGroupO return min($defaultValue, $groupValue); } + + /** + * @see \wcf\system\option\IOptionType::compare() + */ + public function compare($value1, $value2) { + if ($value1 == $value2) { + return 0; + } + + if ($value1 == -1) { + return 1; + } + else if ($value2 == -1) { + return -1; + } + + return ($value1 < $value2) ? 1 : -1; + } } diff --git a/wcfsetup/install/files/lib/system/option/user/group/InverseIntegerUserGroupOptionType.class.php b/wcfsetup/install/files/lib/system/option/user/group/InverseIntegerUserGroupOptionType.class.php index 42e89e82ba..2ac21bd667 100644 --- a/wcfsetup/install/files/lib/system/option/user/group/InverseIntegerUserGroupOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/user/group/InverseIntegerUserGroupOptionType.class.php @@ -25,4 +25,15 @@ class InverseIntegerUserGroupOptionType extends IntegerOptionType implements IUs return $groupValue; } + + /** + * @see \wcf\system\option\IOptionType::compare() + */ + public function compare($value1, $value2) { + if ($value1 == $value2) { + return 0; + } + + return ($value1 < $value2) ? 1 : -1; + } } diff --git a/wcfsetup/install/files/lib/system/option/user/group/UserGroupsUserGroupOptionType.class.php b/wcfsetup/install/files/lib/system/option/user/group/UserGroupsUserGroupOptionType.class.php index a9da44301b..2c0843c803 100644 --- a/wcfsetup/install/files/lib/system/option/user/group/UserGroupsUserGroupOptionType.class.php +++ b/wcfsetup/install/files/lib/system/option/user/group/UserGroupsUserGroupOptionType.class.php @@ -75,4 +75,27 @@ class UserGroupsUserGroupOptionType extends AbstractOptionType implements IUserG return implode(',', array_unique(array_merge($defaultValue, $groupValue))); } + + /** + * @see \wcf\system\option\IOptionType::compare() + */ + public function compare($value1, $value2) { + $value1 = explode(',', $value1); + $value2 = explode(',', $value2); + + // check if value1 contains more elements than value2 + $diff = array_diff($value1, $value2); + if (!empty($diff)) { + return 1; + } + + // check if value1 contains less elements than value2 + $diff = array_diff($value2, $value1); + if (!empty($diff)) { + return -1; + } + + // both lists are equal + return 0; + } } -- 2.20.1