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-2016 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)) return;
61 $users = $this->getAccessibleUsers($objectList);
64 WCF::getDB()->beginTransaction();
65 foreach ($users as $user) {
66 $user = new UserEditor($user);
67 $this->executeUserAction($user);
69 WCF::getDB()->commitTransaction();
71 UserStorageHandler::getInstance()->reset(array_keys($users), 'groupIDs');
76 * Execute the action for the given user.
78 * @param \wcf\data\user\UserEditor $user
80 abstract protected function executeUserAction(UserEditor $user);
85 public function getHTML() {
86 return WCF::getTPL()->fetch('userGroupListUserBulkProcessing', 'wcf', [
87 'availableUserGroups' => $this->availableUserGroups,
88 'inputName' => $this->inputName,
89 'selectedUserGroupIDs' => $this->userGroupIDs
96 public function isAvailable() {
97 return !empty($this->availableUserGroups);
103 public function readFormParameters() {
104 if (isset($_POST[$this->inputName])) $this->userGroupIDs = ArrayUtil::toIntegerArray($_POST[$this->inputName]);
110 public function reset() {
111 $this->userGroupIDs = [];
117 public function validate() {
118 if (empty($this->userGroupIDs)) {
119 throw new UserInputException($this->inputName);
122 foreach ($this->userGroupIDs as $groupID) {
123 if (!isset($this->availableUserGroups[$groupID])) {
124 throw new UserInputException($this->inputName, 'noValidSelection');