2 declare(strict_types=1);
3 namespace wcf\system\bulk\processing\user;
4 use wcf\data\user\group\UserGroup;
5 use wcf\data\user\UserEditor;
6 use wcf\data\user\UserList;
7 use wcf\data\DatabaseObject;
8 use wcf\data\DatabaseObjectList;
9 use wcf\system\exception\UserInputException;
10 use wcf\system\user\storage\UserStorageHandler;
12 use wcf\util\ArrayUtil;
15 * Abstract implementation of a user bulk processing action related to selecting
18 * @author Matthias Schmidt
19 * @copyright 2001-2018 WoltLab GmbH
20 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
21 * @package WoltLabSuite\Core\System\Bulk\Processing\User
24 abstract class AbstractUserGroupsUserBulkProcessingAction extends AbstractUserBulkProcessingAction {
26 * list of available user groups
29 public $availableUserGroups = [];
32 * name of the inputs used to store the selected user group ids
35 public $inputName = '';
38 * ids of selected user groups
41 public $userGroupIDs = [];
46 public function __construct(DatabaseObject $object) {
47 parent::__construct($object);
49 $this->availableUserGroups = UserGroup::getAccessibleGroups([], [UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::USERS]);
51 uasort($this->availableUserGroups, function(UserGroup $groupA, UserGroup $groupB) {
52 return strcmp($groupA->getName(), $groupB->getName());
59 public function executeAction(DatabaseObjectList $objectList) {
60 if (!($objectList instanceof UserList)) {
61 throw new \InvalidArgumentException("Object list is no instance of '".UserList::class."', instance of '".get_class($objectList)."' given.");
64 $users = $this->getAccessibleUsers($objectList);
67 WCF::getDB()->beginTransaction();
68 foreach ($users as $user) {
69 $user = new UserEditor($user);
70 $this->executeUserAction($user);
72 WCF::getDB()->commitTransaction();
74 UserStorageHandler::getInstance()->reset(array_keys($users), 'groupIDs');
79 * Execute the action for the given user.
81 * @param UserEditor $user
83 abstract protected function executeUserAction(UserEditor $user);
88 public function getHTML() {
89 return WCF::getTPL()->fetch('userGroupListUserBulkProcessing', 'wcf', [
90 'availableUserGroups' => $this->availableUserGroups,
91 'inputName' => $this->inputName,
92 'selectedUserGroupIDs' => $this->userGroupIDs
99 public function isAvailable() {
100 return !empty($this->availableUserGroups);
106 public function readFormParameters() {
107 if (isset($_POST[$this->inputName])) $this->userGroupIDs = ArrayUtil::toIntegerArray($_POST[$this->inputName]);
113 public function reset() {
114 $this->userGroupIDs = [];
120 public function validate() {
121 if (empty($this->userGroupIDs)) {
122 throw new UserInputException($this->inputName);
125 foreach ($this->userGroupIDs as $groupID) {
126 if (!isset($this->availableUserGroups[$groupID])) {
127 throw new UserInputException($this->inputName, 'noValidSelection');