]> git.mxchange.org Git - friendica.git/blob - src/Module/Moderation/Users/Active.php
The query condition for active users are unified
[friendica.git] / src / Module / Moderation / Users / Active.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Moderation\Users;
23
24 use Friendica\Content\Pager;
25 use Friendica\Core\Renderer;
26 use Friendica\Model\User;
27 use Friendica\Module\Moderation\BaseUsers;
28
29 class Active extends BaseUsers
30 {
31         protected function post(array $request = [])
32         {
33                 $this->checkModerationAccess();
34
35                 self::checkFormSecurityTokenRedirectOnError($this->baseUrl, 'moderation_users_active');
36
37                 $users = $request['user'] ?? [];
38
39                 if (!empty($request['page_users_block'])) {
40                         foreach ($users as $uid) {
41                                 User::block($uid);
42                         }
43                         $this->systemMessages->addInfo($this->tt('%s user blocked', '%s users blocked', count($users)));
44                 }
45
46                 if (!empty($request['page_users_delete'])) {
47                         foreach ($users as $uid) {
48                                 if ($this->session->getLocalUserId() != $uid) {
49                                         User::remove($uid);
50                                 } else {
51                                         $this->systemMessages->addNotice($this->t('You can\'t remove yourself'));
52                                 }
53                         }
54
55                         $this->systemMessages->addInfo($this->tt('%s user deleted', '%s users deleted', count($users)));
56                 }
57
58                 $this->baseUrl->redirect($this->args->getQueryString());
59         }
60
61         protected function content(array $request = []): string
62         {
63                 parent::content();
64
65                 $action = $this->parameters['action'] ?? '';
66                 $uid    = $this->parameters['uid']    ?? 0;
67
68                 if ($uid) {
69                         $user = User::getById($uid, ['username', 'blocked']);
70                         if (!$user) {
71                                 $this->systemMessages->addNotice($this->t('User not found'));
72                                 $this->baseUrl->redirect('moderation/users');
73                         }
74                 }
75
76                 switch ($action) {
77                         case 'delete':
78                                 if ($this->session->getLocalUserId() != $uid) {
79                                         self::checkFormSecurityTokenRedirectOnError('moderation/users/active', 'moderation_users_active', 't');
80                                         // delete user
81                                         User::remove($uid);
82
83                                         $this->systemMessages->addNotice($this->t('User "%s" deleted', $user['username']));
84                                 } else {
85                                         $this->systemMessages->addNotice($this->t('You can\'t remove yourself'));
86                                 }
87
88                                 $this->baseUrl->redirect('moderation/users/active');
89                                 break;
90                         case 'block':
91                                 self::checkFormSecurityTokenRedirectOnError('moderation/users/active', 'moderation_users_active', 't');
92                                 User::block($uid);
93                                 $this->systemMessages->addNotice($this->t('User "%s" blocked', $user['username']));
94                                 $this->baseUrl->redirect('moderation/users/active');
95                                 break;
96                 }
97                 $pager = new Pager($this->l10n, $this->args->getQueryString(), 100);
98
99                 $valid_orders = [
100                         'name',
101                         'email',
102                         'register_date',
103                         'last-activity',
104                         'last-item',
105                         'page-flags',
106                 ];
107
108                 $order           = 'name';
109                 $order_direction = '+';
110                 if (!empty($request['o'])) {
111                         $new_order = $request['o'];
112                         if ($new_order[0] === '-') {
113                                 $order_direction = '-';
114                                 $new_order       = substr($new_order, 1);
115                         }
116
117                         if (in_array($new_order, $valid_orders)) {
118                                 $order = $new_order;
119                         }
120                 }
121
122                 $users = User::getList($pager->getStart(), $pager->getItemsPerPage(), 'active', $order, ($order_direction == '-'));
123
124                 $users = array_map($this->setupUserCallback(), $users);
125
126                 $th_users = array_map(null, [$this->t('Name'), $this->t('Email'), $this->t('Register date'), $this->t('Last login'), $this->t('Last public item'), $this->t('Type')], $valid_orders);
127
128                 $count = $this->database->count('user', ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `uid` != ?", 0]);
129
130                 $t = Renderer::getMarkupTemplate('moderation/users/active.tpl');
131                 return self::getTabsHTML('active') . Renderer::replaceMacros($t, [
132                         // strings //
133                         '$title'          => $this->t('Moderation'),
134                         '$page'           => $this->t('Active Accounts'),
135                         '$select_all'     => $this->t('select all'),
136                         '$delete'         => $this->t('Delete'),
137                         '$block'          => $this->t('Block'),
138                         '$blocked'        => $this->t('User blocked'),
139                         '$siteadmin'      => $this->t('Site admin'),
140                         '$accountexpired' => $this->t('Account expired'),
141                         '$h_newuser'      => $this->t('Create a new user'),
142
143                         '$th_users'              => $th_users,
144                         '$order_users'           => $order,
145                         '$order_direction_users' => $order_direction,
146
147                         '$confirm_delete_multi' => $this->t('Selected users will be deleted!\n\nEverything these users had posted on this site will be permanently deleted!\n\nAre you sure?'),
148                         '$confirm_delete'       => $this->t('The user {0} will be deleted!\n\nEverything this user has posted on this site will be permanently deleted!\n\nAre you sure?'),
149
150                         '$form_security_token' => self::getFormSecurityToken('moderation_users_active'),
151
152                         // values //
153                         '$baseurl'      => $this->baseUrl,
154                         '$query_string' => $this->args->getQueryString(),
155
156                         '$users' => $users,
157                         '$count' => $count,
158                         '$pager' => $pager->renderFull($count),
159                 ]);
160         }
161 }