Implements an fileSize option-type for easier handling of file sizes.
authorTim Düsterhus <timwolla@arcor.de>
Wed, 7 Dec 2011 16:21:23 +0000 (17:21 +0100)
committerTim Düsterhus <timwolla@arcor.de>
Wed, 7 Dec 2011 16:21:23 +0000 (17:21 +0100)
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.

wcfsetup/install/files/lib/system/option/FileSizeOptionType.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/option/user/group/FileSizeUserGroupOptionType.class.php [new file with mode: 0644]

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 (file)
index 0000000..0d20ef1
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+namespace wcf\system\option;
+use wcf\data\option\Option;
+use wcf\util\FileUtil;
+use wcf\util\StringUtil;
+
+/**
+ * FileSizeOptionType is an implementation of IOptionType for file sizes.
+ *
+ * @author     Tim Düsterhus
+ * @copyright  2011 Tim Düsterhus
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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 (file)
index 0000000..6af706c
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+namespace wcf\system\option\user\group;
+use wcf\system\option\FileSizeOptionType;
+
+/**
+ * FileSizeUserGroupOptionType is an implementation of IUserGroupOptionType for file sizes.
+ * The merge of option values returns the highest value.
+ * 
+ * @author     Tim Düsterhus
+ * @copyright  2011 Tim Düsterhus
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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);
+       }
+}