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