From d79ecf83a066f93f47b6d46badbf42578eb8873b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Sun, 30 Jul 2017 13:55:31 +0200 Subject: [PATCH] Add userTrophyCondition See #2315 --- com.woltlab.wcf/objectType.xml | 26 ++ .../data/user/trophy/UserTrophyList.class.php | 8 +- .../condition/UserTrophyCondition.class.php | 248 ++++++++++++++++++ ...rTrophyExcludedTrophiesCondition.class.php | 3 +- ...xcludedTrophyCategoriesCondition.class.php | 2 +- wcfsetup/install/lang/de.xml | 3 + wcfsetup/install/lang/en.xml | 3 + 7 files changed, 286 insertions(+), 7 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/condition/UserTrophyCondition.class.php diff --git a/com.woltlab.wcf/objectType.xml b/com.woltlab.wcf/objectType.xml index e8d28999e4..f9a7b2f946 100644 --- a/com.woltlab.wcf/objectType.xml +++ b/com.woltlab.wcf/objectType.xml @@ -589,6 +589,12 @@ wcf\system\condition\UserOptionsCondition userOptions + + com.woltlab.wcf.userTrophyCondition + com.woltlab.wcf.condition.userGroupAssignment + wcf\system\condition\UserTrophyCondition + general + @@ -662,6 +668,12 @@ wcf\system\condition\UserOptionsCondition userOptions + + com.woltlab.wcf.userTrophyCondition + com.woltlab.wcf.condition.trophy + wcf\system\condition\UserTrophyCondition + general + @@ -782,6 +794,13 @@ com.woltlab.wcf.user userOptions + + com.woltlab.wcf.userTrophyCondition + com.woltlab.wcf.condition.notice + wcf\system\condition\UserTrophyCondition + com.woltlab.wcf.user + general + @@ -934,6 +953,13 @@ com.woltlab.wcf.user userOptions + + com.woltlab.wcf.userTrophyCondition + com.woltlab.wcf.condition.ad + wcf\system\condition\UserTrophyCondition + com.woltlab.wcf.user + general + diff --git a/wcfsetup/install/files/lib/data/user/trophy/UserTrophyList.class.php b/wcfsetup/install/files/lib/data/user/trophy/UserTrophyList.class.php index 94f9a65b71..074ad845eb 100644 --- a/wcfsetup/install/files/lib/data/user/trophy/UserTrophyList.class.php +++ b/wcfsetup/install/files/lib/data/user/trophy/UserTrophyList.class.php @@ -35,12 +35,12 @@ class UserTrophyList extends DatabaseObjectList { if (!$includeDisabled) { if (!empty($trophyList->sqlJoins)) $trophyList->sqlJoins .= ' '; if (!empty($trophyList->sqlConditionJoins)) $trophyList->sqlConditionJoins .= ' '; - $trophyList->sqlJoins .= 'LEFT JOIN wcf'. WCF_N . '_trophy trophy ON user_trophy.trophyID = trophy.trophyID'; - $trophyList->sqlConditionJoins .= 'LEFT JOIN wcf'. WCF_N . '_trophy trophy ON user_trophy.trophyID = trophy.trophyID'; + $trophyList->sqlJoins .= 'LEFT JOIN wcf'.WCF_N.'_trophy trophy ON user_trophy.trophyID = trophy.trophyID'; + $trophyList->sqlConditionJoins .= 'LEFT JOIN wcf'.WCF_N.'_trophy trophy ON user_trophy.trophyID = trophy.trophyID'; // trophy category join - $trophyList->sqlJoins .= ' LEFT JOIN wcf'. WCF_N . '_category category ON trophy.categoryID = category.categoryID'; - $trophyList->sqlConditionJoins .= ' LEFT JOIN wcf'. WCF_N . '_category category ON trophy.categoryID = category.categoryID'; + $trophyList->sqlJoins .= ' LEFT JOIN wcf'.WCF_N.'_category category ON trophy.categoryID = category.categoryID'; + $trophyList->sqlConditionJoins .= ' LEFT JOIN wcf'.WCF_N.'_category category ON trophy.categoryID = category.categoryID'; $trophyList->getConditionBuilder()->add('trophy.isDisabled = ?', [0]); $trophyList->getConditionBuilder()->add('category.isDisabled = ?', [0]); diff --git a/wcfsetup/install/files/lib/system/condition/UserTrophyCondition.class.php b/wcfsetup/install/files/lib/system/condition/UserTrophyCondition.class.php new file mode 100644 index 0000000000..af28b4af0e --- /dev/null +++ b/wcfsetup/install/files/lib/system/condition/UserTrophyCondition.class.php @@ -0,0 +1,248 @@ + + * @package WoltLabSuite\Core\System\Condition + */ +class UserTrophyCondition extends AbstractMultipleFieldsCondition implements IContentCondition, IObjectListCondition, IUserCondition { + use TObjectListUserCondition; + + /** + * @inheritDoc + */ + protected $descriptions = [ + 'userTrophy' => 'wcf.user.condition.userTrophy.description', + 'notUserTrophy' => 'wcf.user.condition.notUserTrophy.description' + ]; + + /** + * @inheritDoc + */ + protected $labels = [ + 'userTrophy' => 'wcf.user.condition.userTrophy', + 'notUserTrophy' => 'wcf.user.condition.notUserTrophy' + ]; + + /** + * ids of the selected trophies the user has earned + * @var integer[] + */ + protected $userTrophy = []; + + /** + * ids of the selected trophies the user has not earned + * @var integer[] + */ + protected $notUserTrophy = []; + + /** + * selectable trophies + * @var Trophy[] + */ + protected $trophies; + + /** + * @inheritDoc + */ + public function addObjectListCondition(DatabaseObjectList $objectList, array $conditionData) { + if (!($objectList instanceof UserList)) { + throw new \InvalidArgumentException("Object list is no instance of '".UserList::class."', instance of '".get_class($objectList)."' given."); + } + + if (isset($conditionData['userTrophy'])) { + $objectList->getConditionBuilder()->add('user_table.userID IN (SELECT userID FROM wcf'.WCF_N.'_user_trophy WHERE trophyID IN (?) GROUP BY userID HAVING COUNT(userID) = ?)', [$conditionData['trophyIDs'], count($conditionData['trophyIDs'])]); + } + if (isset($conditionData['notUserTrophy'])) { + $objectList->getConditionBuilder()->add('user_table.userID NOT IN (SELECT userID FROM wcf'.WCF_N.'_user_trophy WHERE trophyID IN (?))', [$conditionData['notTrophyIDs']]); + } + } + + /** + * @inheritDoc + */ + public function checkUser(Condition $condition, User $user) { + $trophies = UserTrophyList::getUserTrophies([$user->getObjectID()], false)[$user->getObjectID()]; + $trophyIDs = array_keys($trophies); + + if (!empty($condition->conditionData['userTrophy']) && count(array_diff($condition->conditionData['userTrophy'], $trophyIDs))) { + return false; + } + + if (!empty($condition->conditionData['notUserTrophy']) && count(array_intersect($condition->conditionData['notUserTrophy'], $trophyIDs))) { + return false; + } + + return true; + } + + /** + * @inheritDoc + */ + public function getData() { + $data = []; + + if (!empty($this->userTrophy)) { + $data['userTrophy'] = $this->userTrophy; + } + if (!empty($this->notUserTrophy)) { + $data['notUserTrophy'] = $this->notUserTrophy; + } + + if (!empty($data)) { + return $data; + } + + return null; + } + + /** + * @inheritDoc + */ + public function getHTML() { + if (!count($this->getTrophies())) { + return ''; + } + + return <<getErrorClass('userTrophy')}> +
{$this->getLabel('userTrophy')}
+
+ {$this->getOptionElements('userTrophy')} + {$this->getDescriptionElement('userTrophy')} + {$this->getErrorMessageElement('userTrophy')} +
+ +getErrorClass('notUserTrophy')}> +
{$this->getLabel('notUserTrophy')}
+
+ {$this->getOptionElements('notUserTrophy')} + {$this->getDescriptionElement('notUserTrophy')} + {$this->getErrorMessageElement('notUserTrophy')} +
+ +HTML; + } + + /** + * Returns the option elements for the user group selection. + * + * @param string $identifier + * @return string + */ + protected function getOptionElements($identifier) { + $trophies = $this->getTrophies(); + + $returnValue = ""; + foreach ($trophies as $trophy) { + /** @noinspection PhpVariableVariableInspection */ + $returnValue .= ""; + } + + return $returnValue; + } + + /** + * Returns the selectable user groups. + * + * @return Trophy[] + */ + protected function getTrophies() { + if ($this->trophies == null) { + $trophyList = new TrophyList(); + $trophyList->readObjects(); + $this->trophies = $trophyList->getObjects(); + + uasort($this->trophies, function(Trophy $a, Trophy $b) { + return strcmp($a->getTitle(), $b->getTitle()); + }); + } + + return $this->trophies; + } + + /** + * @inheritDoc + */ + public function readFormParameters() { + if (isset($_POST['userTrophy'])) $this->userTrophy = ArrayUtil::toIntegerArray($_POST['userTrophy']); + if (isset($_POST['notUserTrophy'])) $this->notUserTrophy = ArrayUtil::toIntegerArray($_POST['notUserTrophy']); + } + + /** + * @inheritDoc + */ + public function reset() { + $this->userTrophy = []; + $this->notUserTrophy = []; + } + + /** + * @inheritDoc + */ + public function setData(Condition $condition) { + if ($condition->userTrophy !== null) { + $this->userTrophy = $condition->userTrophy; + } + if ($condition->notUserTrophy !== null) { + $this->notUserTrophy = $condition->notUserTrophy; + } + } + + /** + * Sets the selectable user groups. + * + * @param Trophy[] $trophies + */ + public function setUserGroups(array $trophies) { + $this->trophies = $trophies; + } + + /** + * @inheritDoc + */ + public function validate() { + $trophies = $this->getTrophies(); + foreach ($this->userTrophy as $trophyID) { + if (!isset($trophies[$trophyID])) { + $this->errorMessages['userTrophy'] = 'wcf.global.form.error.noValidSelection'; + + throw new UserInputException('userTrophy', 'noValidSelection'); + } + } + foreach ($this->notUserTrophy as $trophyID) { + if (!isset($trophies[$trophyID])) { + $this->errorMessages['notUserTrophy'] = 'wcf.global.form.error.noValidSelection'; + + throw new UserInputException('notUserTrophy', 'noValidSelection'); + } + } + + if (count(array_intersect($this->notUserTrophy, $this->userTrophy))) { + $this->errorMessages['notUserTrophy'] = 'wcf.user.condition.notUserTrophy.error.userTrophyIntersection'; + + throw new UserInputException('notUserTrophy', 'userTrophyIntersection'); + } + } + + /** + * @inheritDoc + */ + public function showContent(Condition $condition) { + return $this->checkUser($condition, WCF::getUser()); + } +} diff --git a/wcfsetup/install/files/lib/system/condition/user/trophy/UserTrophyExcludedTrophiesCondition.class.php b/wcfsetup/install/files/lib/system/condition/user/trophy/UserTrophyExcludedTrophiesCondition.class.php index c86fba3eb2..00f159d0b0 100644 --- a/wcfsetup/install/files/lib/system/condition/user/trophy/UserTrophyExcludedTrophiesCondition.class.php +++ b/wcfsetup/install/files/lib/system/condition/user/trophy/UserTrophyExcludedTrophiesCondition.class.php @@ -1,9 +1,8 @@ email} + + + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 862eb736d4..3661a948a4 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -3829,6 +3829,9 @@ Open the link below to access the user profile: + + + -- 2.20.1