2 namespace wcf\system\bulk\processing\user;
3 use wcf\data\user\group\UserGroup;
4 use wcf\data\user\UserEditor;
5 use wcf\data\user\UserList;
6 use wcf\data\DatabaseObject;
7 use wcf\data\DatabaseObjectList;
8 use wcf\system\exception\UserInputException;
9 use wcf\system\user\storage\UserStorageHandler;
11 use wcf\util\ArrayUtil;
14 * Abstract implementation of a user bulk processing action related to selecting
17 * @author Matthias Schmidt
18 * @copyright 2001-2018 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @package WoltLabSuite\Core\System\Bulk\Processing\User
23 abstract class AbstractUserGroupsUserBulkProcessingAction extends AbstractUserBulkProcessingAction {
25 * list of available user groups
28 public $availableUserGroups = [];
31 * name of the inputs used to store the selected user group ids
34 public $inputName = '';
37 * ids of selected user groups
40 public $userGroupIDs = [];
45 public function __construct(DatabaseObject $object) {
46 parent::__construct($object);
48 $this->availableUserGroups = UserGroup::getAccessibleGroups([], [UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::USERS]);
50 uasort($this->availableUserGroups, function(UserGroup $groupA, UserGroup $groupB) {
51 return strcmp($groupA->getName(), $groupB->getName());
58 public function executeAction(DatabaseObjectList $objectList) {
59 if (!($objectList instanceof UserList)) {
60 throw new \InvalidArgumentException("Object list is no instance of '".UserList::class."', instance of '".get_class($objectList)."' given.");
63 $users = $this->getAccessibleUsers($objectList);
66 WCF::getDB()->beginTransaction();
67 foreach ($users as $user) {
68 $user = new UserEditor($user);
69 $this->executeUserAction($user);
71 WCF::getDB()->commitTransaction();
73 UserStorageHandler::getInstance()->reset(array_keys($users), 'groupIDs');
78 * Execute the action for the given user.
80 * @param UserEditor $user
82 abstract protected function executeUserAction(UserEditor $user);
87 public function getHTML() {
88 return WCF::getTPL()->fetch('userGroupListUserBulkProcessing', 'wcf', [
89 'availableUserGroups' => $this->availableUserGroups,
90 'inputName' => $this->inputName,
91 'selectedUserGroupIDs' => $this->userGroupIDs
98 public function isAvailable() {
99 return !empty($this->availableUserGroups);
105 public function readFormParameters() {
106 if (isset($_POST[$this->inputName])) $this->userGroupIDs = ArrayUtil::toIntegerArray($_POST[$this->inputName]);
112 public function reset() {
113 $this->userGroupIDs = [];
119 public function validate() {
120 if (empty($this->userGroupIDs)) {
121 throw new UserInputException($this->inputName);
124 foreach ($this->userGroupIDs as $groupID) {
125 if (!isset($this->availableUserGroups[$groupID])) {
126 throw new UserInputException($this->inputName, 'noValidSelection');