From 0885640cef19c995e561a0900d1ae262b47ff864 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 7 Dec 2011 17:21:23 +0100 Subject: [PATCH] Implements an fileSize option-type for easier handling of file sizes. It transparently converts inputs like 42 kB or 1337 MiB to Integers to save in the database and converts the back to strings in the ACP. --- .../option/FileSizeOptionType.class.php | 57 +++++++++++++++++++ .../FileSizeUserGroupOptionType.class.php | 23 ++++++++ 2 files changed, 80 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/option/FileSizeOptionType.class.php create mode 100644 wcfsetup/install/files/lib/system/option/user/group/FileSizeUserGroupOptionType.class.php diff --git a/wcfsetup/install/files/lib/system/option/FileSizeOptionType.class.php b/wcfsetup/install/files/lib/system/option/FileSizeOptionType.class.php new file mode 100644 index 0000000000..0d20ef10c7 --- /dev/null +++ b/wcfsetup/install/files/lib/system/option/FileSizeOptionType.class.php @@ -0,0 +1,57 @@ + + * @package com.woltlab.wcf + * @subpackage system.option + * @category Community Framework + */ +class FileSizeOptionType extends IntegerOptionType { + /** + * @see wcf\system\option\IOptionType::getData() + */ + public function getData(Option $option, $newValue) { + $number = intval($newValue); + if (preg_match('/[kmgt]i?b$/i', $newValue, $multiplier)) { + switch (StringUtil::toLowerCase($multiplier[0])) { + case 'tb': + $number *= 1000; + case 'gb': + $number *= 1000; + case 'mb': + $number *= 1000; + case 'kb': + $number *= 1000; + break; + case 'tib': + $number *= 1024; + case 'gib': + $number *= 1024; + case 'mib': + $number *= 1024; + case 'kib': + $number *= 1024; + break; + } + } + + return $number; + } + + /** + * @see wcf\system\option\IOptionType::getFormElement() + */ + public function getFormElement(Option $option, $value) { + // TODO: Maybe show more digits after the comma? + $value = FileUtil::formatFileSize($value); + return parent::getFormElement($option, $value); + } +} diff --git a/wcfsetup/install/files/lib/system/option/user/group/FileSizeUserGroupOptionType.class.php b/wcfsetup/install/files/lib/system/option/user/group/FileSizeUserGroupOptionType.class.php new file mode 100644 index 0000000000..6af706ce24 --- /dev/null +++ b/wcfsetup/install/files/lib/system/option/user/group/FileSizeUserGroupOptionType.class.php @@ -0,0 +1,23 @@ + + * @package com.woltlab.wcf + * @subpackage system.option.user.group + * @category Community Framework + */ +class FileSizeUserGroupOptionType extends FileSizeOptionType implements IUserGroupOptionType { + /** + * @see wcf\system\option\user.group\IUserGroupOptionType::merge() + */ + public function merge(array $values) { + return max($values); + } +} -- 2.20.1