From e76c454b7f25d984e7bb460902db8a83ba7317ca Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Tue, 27 May 2014 18:11:30 +0200 Subject: [PATCH] Added support for label group show order and i18n names --- .../install/files/acp/templates/labelAdd.tpl | 2 +- .../files/acp/templates/labelGroupAdd.tpl | 16 ++++-- .../files/acp/templates/labelGroupList.tpl | 4 +- .../install/files/acp/templates/labelList.tpl | 2 +- .../lib/acp/form/LabelGroupAddForm.class.php | 52 ++++++++++++++++--- .../lib/acp/form/LabelGroupEditForm.class.php | 18 ++++++- .../lib/acp/page/LabelGroupListPage.class.php | 4 +- .../lib/data/label/group/LabelGroup.class.php | 27 +++++++++- .../data/label/group/LabelGroupList.class.php | 5 ++ .../cache/builder/LabelCacheBuilder.class.php | 1 - .../lib/system/label/LabelHandler.class.php | 24 +++++++++ .../AbstractLabelObjectHandler.class.php | 36 ++++++++++--- .../object/ILabelObjectHandler.class.php | 4 +- wcfsetup/install/lang/de.xml | 3 +- wcfsetup/install/lang/en.xml | 3 +- wcfsetup/setup/db/install.sql | 3 +- 16 files changed, 174 insertions(+), 30 deletions(-) diff --git a/wcfsetup/install/files/acp/templates/labelAdd.tpl b/wcfsetup/install/files/acp/templates/labelAdd.tpl index 3a49b3f053..25b2b36299 100644 --- a/wcfsetup/install/files/acp/templates/labelAdd.tpl +++ b/wcfsetup/install/files/acp/templates/labelAdd.tpl @@ -49,7 +49,7 @@ {if $errorField == 'groupID'} diff --git a/wcfsetup/install/files/acp/templates/labelGroupAdd.tpl b/wcfsetup/install/files/acp/templates/labelGroupAdd.tpl index a1d9bc42e8..19ce26e6cd 100644 --- a/wcfsetup/install/files/acp/templates/labelGroupAdd.tpl +++ b/wcfsetup/install/files/acp/templates/labelGroupAdd.tpl @@ -18,6 +18,8 @@ {include file='aclPermissionJavaScript' containerID='groupPermissions' objectID=$groupID} {/if} +{assign var=labelForceSelection value=$forceSelection} +

{lang}wcf.acp.label.group.{$action}{/lang}

@@ -54,7 +56,7 @@
- + {if $errorField == 'groupName'} {if $errorType == 'empty'} @@ -64,12 +66,20 @@ {/if} {/if} + {include file='multipleLanguageInputJavascript' elementIdentifier='groupName' forceSelection=false} +
+ + +
+
+
+
-
-
+
+
diff --git a/wcfsetup/install/files/acp/templates/labelGroupList.tpl b/wcfsetup/install/files/acp/templates/labelGroupList.tpl index 556ab58ab4..710e5e1129 100644 --- a/wcfsetup/install/files/acp/templates/labelGroupList.tpl +++ b/wcfsetup/install/files/acp/templates/labelGroupList.tpl @@ -47,6 +47,7 @@ {lang}wcf.global.objectID{/lang} {lang}wcf.acp.label.group.groupName{/lang} + {lang}wcf.acp.label.group.showOrder{/lang} {event name='columnHeads'} @@ -62,7 +63,8 @@ {event name='rowButtons'} {@$group->groupID} - {$group->groupName} + {$group} + {@$group->showOrder} {event name='columns'} diff --git a/wcfsetup/install/files/acp/templates/labelList.tpl b/wcfsetup/install/files/acp/templates/labelList.tpl index 494e1bc63b..573d5b47b0 100644 --- a/wcfsetup/install/files/acp/templates/labelList.tpl +++ b/wcfsetup/install/files/acp/templates/labelList.tpl @@ -64,7 +64,7 @@ {@$label->labelID} {$label} - {$label->groupName} + {lang}{$label->groupName}{/lang} {event name='columns'} diff --git a/wcfsetup/install/files/lib/acp/form/LabelGroupAddForm.class.php b/wcfsetup/install/files/lib/acp/form/LabelGroupAddForm.class.php index 11573d0567..e08d1b2719 100644 --- a/wcfsetup/install/files/lib/acp/form/LabelGroupAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/LabelGroupAddForm.class.php @@ -5,8 +5,10 @@ use wcf\data\object\type\ObjectTypeCache; use wcf\form\AbstractForm; use wcf\system\acl\ACLHandler; use wcf\system\exception\UserInputException; +use wcf\system\language\I18nHandler; use wcf\system\WCF; use wcf\util\StringUtil; +use wcf\data\label\group\LabelGroupEditor; /** * Shows the label group add form. @@ -65,6 +67,12 @@ class LabelGroupAddForm extends AbstractForm { */ public $objectTypeID = 0; + /** + * show order + * @var integer + */ + public $showOrder = 0; + /** * @see \wcf\page\AbstractPage::readParameters() */ @@ -72,6 +80,8 @@ class LabelGroupAddForm extends AbstractForm { parent::readParameters(); $this->objectTypeID = ACLHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.label'); + + I18nHandler::getInstance()->register('groupName'); } /** @@ -80,9 +90,13 @@ class LabelGroupAddForm extends AbstractForm { public function readFormParameters() { parent::readFormParameters(); + I18nHandler::getInstance()->readValues(); + + if (I18nHandler::getInstance()->isPlainValue('groupName')) $this->groupName = I18nHandler::getInstance()->getValue('groupName'); + if (isset($_POST['forceSelection'])) $this->forceSelection = true; - if (isset($_POST['groupName'])) $this->groupName = StringUtil::trim($_POST['groupName']); if (isset($_POST['objectTypes']) && is_array($_POST['objectTypes'])) $this->objectTypes = $_POST['objectTypes']; + if (isset($_POST['showOrder'])) $this->showOrder = intval($_POST['showOrder']); } /** @@ -112,9 +126,19 @@ class LabelGroupAddForm extends AbstractForm { public function validate() { parent::validate(); - // validate class name - if (empty($this->groupName)) { - throw new UserInputException('groupName'); + // validate group name + try { + if (!I18nHandler::getInstance()->validateValue('groupName')) { + if (I18nHandler::getInstance()->isPlainValue('groupName')) { + throw new UserInputException('groupName'); + } + else { + throw new UserInputException('groupName', 'multilingual'); + } + } + } + catch (UserInputException $e) { + $this->errorType[$e->getField()] = $e->getType(); } // validate object type relations @@ -134,10 +158,21 @@ class LabelGroupAddForm extends AbstractForm { // save label $this->objectAction = new LabelGroupAction(array(), 'create', array('data' => array_merge($this->additionalFields, array( 'forceSelection' => ($this->forceSelection ? 1 : 0), - 'groupName' => $this->groupName + 'groupName' => $this->groupName, + 'showOrder' => $this->showOrder )))); $returnValues = $this->objectAction->executeAction(); + if (!I18nHandler::getInstance()->isPlainValue('groupName')) { + I18nHandler::getInstance()->save('groupName', 'wcf.acp.label.group'.$returnValues['returnValues']->groupID, 'wcf.acp.label', 1); + + // update group name + $groupEditor = new LabelGroupEditor($returnValues['returnValues']); + $groupEditor->update(array( + 'groupName' => 'wcf.acp.label.group'.$returnValues['returnValues']->groupID + )); + } + // save acl ACLHandler::getInstance()->save($returnValues['returnValues']->groupID, $this->objectTypeID); ACLHandler::getInstance()->disableAssignVariables(); @@ -155,12 +190,15 @@ class LabelGroupAddForm extends AbstractForm { $this->forceSelection = false; $this->groupName = ''; $this->objectTypes = array(); + $this->showOrder = 0; $this->setObjectTypeRelations(); // show success WCF::getTPL()->assign(array( 'success' => true )); + + I18nHandler::getInstance()->reset(); } /** @@ -170,13 +208,15 @@ class LabelGroupAddForm extends AbstractForm { parent::assignVariables(); ACLHandler::getInstance()->assignVariables($this->objectTypeID); + I18nHandler::getInstance()->assignVariables(); WCF::getTPL()->assign(array( 'action' => 'add', 'forceSelection' => $this->forceSelection, 'groupName' => $this->groupName, 'labelObjectTypeContainers' => $this->labelObjectTypeContainers, - 'objectTypeID' => $this->objectTypeID + 'objectTypeID' => $this->objectTypeID, + 'showOrder' => $this->showOrder )); } diff --git a/wcfsetup/install/files/lib/acp/form/LabelGroupEditForm.class.php b/wcfsetup/install/files/lib/acp/form/LabelGroupEditForm.class.php index 5c89a0cd4c..e99522f9d3 100644 --- a/wcfsetup/install/files/lib/acp/form/LabelGroupEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/LabelGroupEditForm.class.php @@ -5,6 +5,7 @@ use wcf\data\label\group\LabelGroupAction; use wcf\form\AbstractForm; use wcf\system\acl\ACLHandler; use wcf\system\exception\IllegalLinkException; +use wcf\system\language\I18nHandler; use wcf\system\WCF; /** @@ -59,10 +60,20 @@ class LabelGroupEditForm extends LabelGroupAddForm { public function save() { AbstractForm::save(); + $this->groupName = 'wcf.acp.label.group'.$this->group->groupID; + if (I18nHandler::getInstance()->isPlainValue('groupName')) { + I18nHandler::getInstance()->remove($this->groupName); + $this->groupName = I18nHandler::getInstance()->getValue('groupName'); + } + else { + I18nHandler::getInstance()->save('groupName', $this->groupName, 'wcf.acp.label', 1); + } + // update label $this->objectAction = new LabelGroupAction(array($this->groupID), 'update', array('data' => array_merge($this->additionalFields, array( 'forceSelection' => ($this->forceSelection ? 1 : 0), - 'groupName' => $this->groupName + 'groupName' => $this->groupName, + 'showOrder' => $this->showOrder )))); $this->objectAction->executeAction(); @@ -92,8 +103,11 @@ class LabelGroupEditForm extends LabelGroupAddForm { parent::readData(); if (empty($_POST)) { + I18nHandler::getInstance()->setOptions('groupName', 1, $this->group->groupName, 'wcf.acp.label.group\d+'); + $this->forceSelection = ($this->group->forceSelection ? true : false); $this->groupName = $this->group->groupName; + $this->showOrder = $this->group->showOrder; } } @@ -103,6 +117,8 @@ class LabelGroupEditForm extends LabelGroupAddForm { public function assignVariables() { parent::assignVariables(); + I18nHandler::getInstance()->assignVariables(!empty($_POST)); + WCF::getTPL()->assign(array( 'action' => 'edit', 'groupID' => $this->groupID, diff --git a/wcfsetup/install/files/lib/acp/page/LabelGroupListPage.class.php b/wcfsetup/install/files/lib/acp/page/LabelGroupListPage.class.php index 5921586515..5f885e3baf 100644 --- a/wcfsetup/install/files/lib/acp/page/LabelGroupListPage.class.php +++ b/wcfsetup/install/files/lib/acp/page/LabelGroupListPage.class.php @@ -21,12 +21,12 @@ class LabelGroupListPage extends SortablePage { /** * @see \wcf\page\SortablePage::$defaultSortField */ - public $defaultSortField = 'groupName'; + public $defaultSortField = 'showOrder'; /** * @see \wcf\page\SortablePage::$validSortFields */ - public $validSortFields = array('groupID', 'groupName'); + public $validSortFields = array('groupID', 'groupName', 'showOrder'); /** * @see \wcf\page\AbstractPage::$neededPermissions diff --git a/wcfsetup/install/files/lib/data/label/group/LabelGroup.class.php b/wcfsetup/install/files/lib/data/label/group/LabelGroup.class.php index 1195de32d9..f85272cf04 100644 --- a/wcfsetup/install/files/lib/data/label/group/LabelGroup.class.php +++ b/wcfsetup/install/files/lib/data/label/group/LabelGroup.class.php @@ -2,6 +2,7 @@ namespace wcf\data\label\group; use wcf\data\DatabaseObject; use wcf\system\request\IRouteController; +use wcf\system\WCF; /** * Represents a label group. @@ -28,6 +29,30 @@ class LabelGroup extends DatabaseObject implements IRouteController { * @see \wcf\data\ITitledObject::getTitle() */ public function getTitle() { - return $this->groupName; + return WCF::getLanguage()->get($this->groupName); + } + + /** + * Returns label group title. + * + * @return string + */ + public function __toString() { + return $this->getTitle(); + } + + /** + * Callback for uasort() to sort label groups by show order and (if equal) group id. + * + * @param \wcf\data\DatabaseObject $groupA + * @param \wcf\data\DatabaseObject $groupB + * @return integer + */ + public static function sortLabelGroups(DatabaseObject $groupA, DatabaseObject $groupB) { + if ($groupA->showOrder == $groupB->showOrder) { + return ($groupA->groupID > $groupB->groupID) ? 1 : -1; + } + + return ($groupA->showOrder > $groupB->showOrder) ? 1 : -1; } } diff --git a/wcfsetup/install/files/lib/data/label/group/LabelGroupList.class.php b/wcfsetup/install/files/lib/data/label/group/LabelGroupList.class.php index 84eaa4a8c5..5e848acbc4 100644 --- a/wcfsetup/install/files/lib/data/label/group/LabelGroupList.class.php +++ b/wcfsetup/install/files/lib/data/label/group/LabelGroupList.class.php @@ -17,4 +17,9 @@ class LabelGroupList extends DatabaseObjectList { * @see \wcf\data\DatabaseObjectList::$className */ public $className = 'wcf\data\label\group\LabelGroup'; + + /** + * @see \wcf\data\DatabaseObjectList::$sqlOrderBy + */ + public $sqlOrderBy = 'label_group.showOrder ASC, label_group.groupID'; } diff --git a/wcfsetup/install/files/lib/system/cache/builder/LabelCacheBuilder.class.php b/wcfsetup/install/files/lib/system/cache/builder/LabelCacheBuilder.class.php index 39957cb314..1a0172ac17 100644 --- a/wcfsetup/install/files/lib/system/cache/builder/LabelCacheBuilder.class.php +++ b/wcfsetup/install/files/lib/system/cache/builder/LabelCacheBuilder.class.php @@ -27,7 +27,6 @@ class LabelCacheBuilder extends AbstractCacheBuilder { // get label groups $groupList = new LabelGroupList(); - $groupList->sqlOrderBy = 'groupName'; $groupList->readObjects(); $groups = $groupList->getObjects(); foreach ($groups as &$group) { diff --git a/wcfsetup/install/files/lib/system/label/LabelHandler.class.php b/wcfsetup/install/files/lib/system/label/LabelHandler.class.php index 8b6b3f319e..f382e0d0a5 100644 --- a/wcfsetup/install/files/lib/system/label/LabelHandler.class.php +++ b/wcfsetup/install/files/lib/system/label/LabelHandler.class.php @@ -248,6 +248,22 @@ class LabelHandler extends SingletonFactory { } } + // order label ids by label group + $labelGroups =& $this->labelGroups; + foreach ($data as &$labels) { + usort($labels, function($a, $b) use($labelGroups) { + $groupA = $labelGroups['groups'][$a->groupID]; + $groupB = $labelGroups['groups'][$b->groupID]; + + if ($groupA->showOrder == $groupB->showOrder) { + return ($groupA->groupID > $groupB->groupID) ? 1 : -1; + } + + return ($groupA->showOrder > $groupB->showOrder) ? 1 : -1; + }); + } + unset($labels); + return $data; } @@ -298,6 +314,14 @@ class LabelHandler extends SingletonFactory { $data[$groupID] = $this->labelGroups['groups'][$groupID]; } + uasort($data, function($a, $b) { + if ($a->showOrder == $b->showOrder) { + return ($a->groupID > $b->groupID) ? 1 : -1; + } + + return ($a->showOrder > $b->showOrder) ? 1 : -1; + }); + return $data; } diff --git a/wcfsetup/install/files/lib/system/label/object/AbstractLabelObjectHandler.class.php b/wcfsetup/install/files/lib/system/label/object/AbstractLabelObjectHandler.class.php index 9247aa7f7d..013aef3a9a 100644 --- a/wcfsetup/install/files/lib/system/label/object/AbstractLabelObjectHandler.class.php +++ b/wcfsetup/install/files/lib/system/label/object/AbstractLabelObjectHandler.class.php @@ -64,13 +64,15 @@ abstract class AbstractLabelObjectHandler extends SingletonFactory implements IL $data[$groupID] = $this->labelGroups[$groupID]; } + uasort($data, array('\wcf\data\label\group\LabelGroup', 'sortLabelGroups')); + return $data; } /** * @see \wcf\system\label\manager\ILabelObjectHandler::validateLabelIDs() */ - public function validateLabelIDs(array $labelIDs, $optionName = '') { + public function validateLabelIDs(array $labelIDs, $optionName = '', $legacyReturnValue = true) { $optionID = 0; if (!empty($optionName)) { $optionID = LabelHandler::getInstance()->getOptionID($optionName); @@ -79,21 +81,31 @@ abstract class AbstractLabelObjectHandler extends SingletonFactory implements IL } } + $validationErrors = array(); $satisfiedGroups = array(); foreach ($labelIDs as $groupID => $labelID) { // only one label per group is allowed if (is_array($labelID)) { - return false; + $validationErrors[$groupID] = 'invalid'; + continue; + } + + // label group id is unknown + if (!isset($this->labelGroups[$groupID])) { + $validationErrors[0] = 'invalid'; + continue; } - // label group id is unknown or label id is invalid for this group - if (!isset($this->labelGroups[$groupID]) || !$this->labelGroups[$groupID]->isValid($labelID)) { - return false; + // label id is invalid for this group + if (!$this->labelGroups[$groupID]->isValid($labelID)) { + $validationErrors[$groupID] = 'invalid'; + continue; } // check permission if ($optionID && !$this->labelGroups[$groupID]->getPermission($optionID)) { - return false; + $validationErrors[0] = 'invalid'; + continue; } $satisfiedGroups[] = $groupID; @@ -107,11 +119,19 @@ abstract class AbstractLabelObjectHandler extends SingletonFactory implements IL continue; } - return false; + $validationErrors[$labelGroup->groupID] = 'missing'; + continue; } } - return true; + if ($legacyReturnValue) { + // WCF 2.0: boolean true/false + return (empty($validationErrors)); + } + else { + // WCF 2.1+: detailed error report (empty array if validation passed) + return $validationErrors; + } } /** diff --git a/wcfsetup/install/files/lib/system/label/object/ILabelObjectHandler.class.php b/wcfsetup/install/files/lib/system/label/object/ILabelObjectHandler.class.php index 3e322c0e00..ebb90f8ec6 100644 --- a/wcfsetup/install/files/lib/system/label/object/ILabelObjectHandler.class.php +++ b/wcfsetup/install/files/lib/system/label/object/ILabelObjectHandler.class.php @@ -33,9 +33,9 @@ interface ILabelObjectHandler { * * @param array $labelIDs * @param array $optionName - * @return boolean + * @return mixed */ - public function validateLabelIDs(array $labelIDs, $optionName = ''); + public function validateLabelIDs(array $labelIDs, $optionName = '', $legacyReturnValue = true); /** * Assigns labels to an object. diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 7af5c9d266..ac381a80b5 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -435,8 +435,8 @@ - + @@ -1929,6 +1929,7 @@ Fehler sind beispielsweise: + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 754e533aa7..621d5ead1f 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -432,8 +432,8 @@ Examples for medium ID detection: - + @@ -1872,6 +1872,7 @@ Errors are: + diff --git a/wcfsetup/setup/db/install.sql b/wcfsetup/setup/db/install.sql index 654ef966fe..a3c303891d 100644 --- a/wcfsetup/setup/db/install.sql +++ b/wcfsetup/setup/db/install.sql @@ -365,7 +365,8 @@ DROP TABLE IF EXISTS wcf1_label_group; CREATE TABLE wcf1_label_group ( groupID INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, groupName VARCHAR(80) NOT NULL, - forceSelection TINYINT(1) NOT NULL DEFAULT 0 + forceSelection TINYINT(1) NOT NULL DEFAULT 0, + showOrder INT(10) NOT NULL DEFAULT 0 ); DROP TABLE IF EXISTS wcf1_label_group_to_object; -- 2.20.1