ddfdd9c8974f502887fd71bbcac0a13570bb1c6c
[GitHub/WoltLab/WCF.git] /
1 <?php
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;
11 use wcf\system\WCF;
12 use wcf\util\ArrayUtil;
13
14 /**
15 * Abstract implementation of a user bulk processing action related to selecting
16 * user groups.
17 *
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
22 * @since 3.0
23 */
24 abstract class AbstractUserGroupsUserBulkProcessingAction extends AbstractUserBulkProcessingAction {
25 /**
26 * list of available user groups
27 * @var UserGroup[]
28 */
29 public $availableUserGroups = [];
30
31 /**
32 * name of the inputs used to store the selected user group ids
33 * @var string
34 */
35 public $inputName = '';
36
37 /**
38 * ids of selected user groups
39 * @var integer[]
40 */
41 public $userGroupIDs = [];
42
43 /**
44 * @inheritDoc
45 */
46 public function __construct(DatabaseObject $object) {
47 parent::__construct($object);
48
49 $this->availableUserGroups = UserGroup::getAccessibleGroups([], [UserGroup::GUESTS, UserGroup::EVERYONE, UserGroup::USERS]);
50
51 uasort($this->availableUserGroups, function(UserGroup $groupA, UserGroup $groupB) {
52 return strcmp($groupA->getName(), $groupB->getName());
53 });
54 }
55
56 /**
57 * @inheritDoc
58 */
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.");
62 }
63
64 $users = $this->getAccessibleUsers($objectList);
65
66 if (!empty($users)) {
67 WCF::getDB()->beginTransaction();
68 foreach ($users as $user) {
69 $user = new UserEditor($user);
70 $this->executeUserAction($user);
71 }
72 WCF::getDB()->commitTransaction();
73
74 UserStorageHandler::getInstance()->reset(array_keys($users), 'groupIDs');
75 }
76 }
77
78 /**
79 * Execute the action for the given user.
80 *
81 * @param UserEditor $user
82 */
83 abstract protected function executeUserAction(UserEditor $user);
84
85 /**
86 * @inheritDoc
87 */
88 public function getHTML() {
89 return WCF::getTPL()->fetch('userGroupListUserBulkProcessing', 'wcf', [
90 'availableUserGroups' => $this->availableUserGroups,
91 'inputName' => $this->inputName,
92 'selectedUserGroupIDs' => $this->userGroupIDs
93 ]);
94 }
95
96 /**
97 * @inheritDoc
98 */
99 public function isAvailable() {
100 return !empty($this->availableUserGroups);
101 }
102
103 /**
104 * @inheritDoc
105 */
106 public function readFormParameters() {
107 if (isset($_POST[$this->inputName])) $this->userGroupIDs = ArrayUtil::toIntegerArray($_POST[$this->inputName]);
108 }
109
110 /**
111 * @inheritDoc
112 */
113 public function reset() {
114 $this->userGroupIDs = [];
115 }
116
117 /**
118 * @inheritDoc
119 */
120 public function validate() {
121 if (empty($this->userGroupIDs)) {
122 throw new UserInputException($this->inputName);
123 }
124
125 foreach ($this->userGroupIDs as $groupID) {
126 if (!isset($this->availableUserGroups[$groupID])) {
127 throw new UserInputException($this->inputName, 'noValidSelection');
128 }
129 }
130 }
131 }