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