e9b14cc96a4b18d61690bd21648e7c9787e6e9b1
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 namespace wcf\system\bulk\processing\user;
4
5 use wcf\data\DatabaseObject;
6 use wcf\data\DatabaseObjectList;
7 use wcf\data\user\group\UserGroup;
8 use wcf\data\user\UserEditor;
9 use wcf\data\user\UserList;
10 use wcf\system\exception\UserInputException;
11 use wcf\system\user\storage\UserStorageHandler;
12 use wcf\system\WCF;
13 use wcf\util\ArrayUtil;
14
15 /**
16 * Abstract implementation of a user bulk processing action related to selecting
17 * user groups.
18 *
19 * @author Matthias Schmidt
20 * @copyright 2001-2019 WoltLab GmbH
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 * @package WoltLabSuite\Core\System\Bulk\Processing\User
23 * @since 3.0
24 */
25 abstract class AbstractUserGroupsUserBulkProcessingAction extends AbstractUserBulkProcessingAction
26 {
27 /**
28 * list of available user groups
29 * @var UserGroup[]
30 */
31 public $availableUserGroups = [];
32
33 /**
34 * name of the inputs used to store the selected user group ids
35 * @var string
36 */
37 public $inputName = '';
38
39 /**
40 * ids of selected user groups
41 * @var int[]
42 */
43 public $userGroupIDs = [];
44
45 /**
46 * @inheritDoc
47 */
48 public function __construct(DatabaseObject $object)
49 {
50 parent::__construct($object);
51
52 $this->availableUserGroups = UserGroup::getSortedAccessibleGroups(
53 [],
54 [UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::OWNER, UserGroup::USERS]
55 );
56 }
57
58 /**
59 * @inheritDoc
60 */
61 public function executeAction(DatabaseObjectList $objectList)
62 {
63 if (!($objectList instanceof UserList)) {
64 throw new \InvalidArgumentException("Object list is no instance of '" . UserList::class . "', instance of '" . \get_class($objectList) . "' given.");
65 }
66
67 $users = $this->getAccessibleUsers($objectList);
68
69 if (!empty($users)) {
70 WCF::getDB()->beginTransaction();
71 foreach ($users as $user) {
72 $user = new UserEditor($user);
73 $this->executeUserAction($user);
74 }
75 WCF::getDB()->commitTransaction();
76
77 UserStorageHandler::getInstance()->reset(\array_keys($users), 'groupIDs');
78 }
79 }
80
81 /**
82 * Execute the action for the given user.
83 *
84 * @param UserEditor $user
85 */
86 abstract protected function executeUserAction(UserEditor $user);
87
88 /**
89 * @inheritDoc
90 */
91 public function getHTML()
92 {
93 return WCF::getTPL()->fetch('userGroupListUserBulkProcessing', 'wcf', [
94 'availableUserGroups' => $this->availableUserGroups,
95 'inputName' => $this->inputName,
96 'selectedUserGroupIDs' => $this->userGroupIDs,
97 ]);
98 }
99
100 /**
101 * @inheritDoc
102 */
103 public function isAvailable()
104 {
105 return !empty($this->availableUserGroups);
106 }
107
108 /**
109 * @inheritDoc
110 */
111 public function readFormParameters()
112 {
113 if (isset($_POST[$this->inputName])) {
114 $this->userGroupIDs = ArrayUtil::toIntegerArray($_POST[$this->inputName]);
115 }
116 }
117
118 /**
119 * @inheritDoc
120 */
121 public function reset()
122 {
123 $this->userGroupIDs = [];
124 }
125
126 /**
127 * @inheritDoc
128 */
129 public function validate()
130 {
131 if (empty($this->userGroupIDs)) {
132 throw new UserInputException($this->inputName);
133 }
134
135 foreach ($this->userGroupIDs as $groupID) {
136 if (!isset($this->availableUserGroups[$groupID])) {
137 throw new UserInputException($this->inputName, 'noValidSelection');
138 }
139 }
140 }
141 }