]> git.mxchange.org Git - friendica.git/blob - src/Module/Group.php
a583c585a1aba0356c8f7f4724fe27c0249fd9a3
[friendica.git] / src / Module / Group.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Session;
27 use Friendica\Core\System;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model;
31
32 require_once 'boot.php';
33
34 class Group extends BaseModule
35 {
36         protected function post(array $request = [])
37         {
38                 if (DI::mode()->isAjax()) {
39                         $this->ajaxPost();
40                 }
41
42                 if (!Session::getLocalUser()) {
43                         DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
44                         DI::baseUrl()->redirect();
45                 }
46
47                 // @TODO: Replace with parameter from router
48                 if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) {
49                         BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
50
51                         $name = trim($request['groupname']);
52                         $r = Model\Group::create(Session::getLocalUser(), $name);
53                         if ($r) {
54                                 $r = Model\Group::getIdByName(Session::getLocalUser(), $name);
55                                 if ($r) {
56                                         DI::baseUrl()->redirect('group/' . $r);
57                                 }
58                         } else {
59                                 DI::sysmsg()->addNotice(DI::l10n()->t('Could not create group.'));
60                         }
61                         DI::baseUrl()->redirect('group');
62                 }
63
64                 // @TODO: Replace with parameter from router
65                 if ((DI::args()->getArgc() == 2) && intval(DI::args()->getArgv()[1])) {
66                         BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
67
68                         $group = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => Session::getLocalUser()]);
69                         if (!DBA::isResult($group)) {
70                                 DI::sysmsg()->addNotice(DI::l10n()->t('Group not found.'));
71                                 DI::baseUrl()->redirect('contact');
72                         }
73                         $groupname = trim($_POST['groupname']);
74                         if (strlen($groupname) && ($groupname != $group['name'])) {
75                                 if (!Model\Group::update($group['id'], $groupname)) {
76                                         DI::sysmsg()->addNotice(DI::l10n()->t('Group name was not changed.'));
77                                 }
78                         }
79                 }
80         }
81
82         public function ajaxPost()
83         {
84                 try {
85                         if (!Session::getLocalUser()) {
86                                 throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
87                         }
88
89                         if (isset($this->parameters['command'])) {
90                                 $group_id = $this->parameters['group'];
91                                 $contact_id = $this->parameters['contact'];
92
93                                 if (!Model\Group::exists($group_id, Session::getLocalUser())) {
94                                         throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
95                                 }
96
97                                 // @TODO Backward compatibility with user contacts, remove by version 2022.03
98                                 $cdata = Model\Contact::getPublicAndUserContactID($contact_id, Session::getLocalUser());
99                                 if (empty($cdata['public'])) {
100                                         throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
101                                 }
102
103                                 if (empty($cdata['user'])) {
104                                         throw new \Exception(DI::l10n()->t('Invalid contact.'), 404);
105                                 }
106
107                                 $contact = Model\Contact::getById($cdata['user'], ['deleted']);
108                                 if (!DBA::isResult($contact)) {
109                                         throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
110                                 }
111
112                                 if ($contact['deleted']) {
113                                         throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
114                                 }
115
116                                 switch($this->parameters['command']) {
117                                         case 'add':
118                                                 if (!Model\Group::addMember($group_id, $cdata['user'])) {
119                                                         throw new \Exception(DI::l10n()->t('Unable to add the contact to the group.'), 500);
120                                                 }
121
122                                                 $message = DI::l10n()->t('Contact successfully added to group.');
123                                                 break;
124                                         case 'remove':
125                                                 if (!Model\Group::removeMember($group_id, $cdata['user'])) {
126                                                         throw new \Exception(DI::l10n()->t('Unable to remove the contact from the group.'), 500);
127                                                 }
128
129                                                 $message = DI::l10n()->t('Contact successfully removed from group.');
130                                                 break;
131                                 }
132                         } else {
133                                 throw new \Exception(DI::l10n()->t('Bad request.'), 400);
134                         }
135
136                         DI::sysmsg()->addInfo($message);
137                         System::jsonExit(['status' => 'OK', 'message' => $message]);
138                 } catch (\Exception $e) {
139                         DI::sysmsg()->addNotice($e->getMessage());
140                         System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
141                 }
142         }
143
144         protected function content(array $request = []): string
145         {
146                 $change = false;
147
148                 if (!Session::getLocalUser()) {
149                         throw new \Friendica\Network\HTTPException\ForbiddenException();
150                 }
151
152                 $a = DI::app();
153
154                 DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[1] : 'everyone'));
155
156                 // With no group number provided we jump to the unassigned contacts as a starting point
157                 // @TODO: Replace with parameter from router
158                 if (DI::args()->getArgc() == 1) {
159                         DI::baseUrl()->redirect('group/none');
160                 }
161
162                 // Switch to text mode interface if we have more than 'n' contacts or group members
163                 $switchtotext = DI::pConfig()->get(Session::getLocalUser(), 'system', 'groupedit_image_limit');
164                 if (is_null($switchtotext)) {
165                         $switchtotext = DI::config()->get('system', 'groupedit_image_limit', 200);
166                 }
167
168                 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
169
170
171                 $context = [
172                         '$submit' => DI::l10n()->t('Save Group'),
173                         '$submit_filter' => DI::l10n()->t('Filter'),
174                 ];
175
176                 // @TODO: Replace with parameter from router
177                 if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) {
178                         return Renderer::replaceMacros($tpl, $context + [
179                                 '$title' => DI::l10n()->t('Create a group of contacts/friends.'),
180                                 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), '', ''],
181                                 '$gid' => 'new',
182                                 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
183                         ]);
184                 }
185
186                 $nogroup = false;
187
188                 // @TODO: Replace with parameter from router
189                 if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'none') ||
190                         (DI::args()->getArgc() == 1) && (DI::args()->getArgv()[0] === 'nogroup')) {
191                         $id = -1;
192                         $nogroup = true;
193                         $group = [
194                                 'id' => $id,
195                                 'name' => DI::l10n()->t('Contacts not in any group'),
196                         ];
197
198                         $members = [];
199                         $preselected = [];
200
201                         $context = $context + [
202                                 '$title' => $group['name'],
203                                 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
204                                 '$gid' => $id,
205                                 '$editable' => 0,
206                         ];
207                 }
208
209                 // @TODO: Replace with parameter from router
210                 if ((DI::args()->getArgc() == 3) && (DI::args()->getArgv()[1] === 'drop')) {
211                         BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
212
213                         // @TODO: Replace with parameter from router
214                         if (intval(DI::args()->getArgv()[2])) {
215                                 if (!Model\Group::exists(DI::args()->getArgv()[2], Session::getLocalUser())) {
216                                         DI::sysmsg()->addNotice(DI::l10n()->t('Group not found.'));
217                                         DI::baseUrl()->redirect('contact');
218                                 }
219
220                                 if (!Model\Group::remove(DI::args()->getArgv()[2])) {
221                                         DI::sysmsg()->addNotice(DI::l10n()->t('Unable to remove group.'));
222                                 }
223                         }
224                         DI::baseUrl()->redirect('group');
225                 }
226
227                 // @TODO: Replace with parameter from router
228                 if ((DI::args()->getArgc() > 2) && intval(DI::args()->getArgv()[1]) && intval(DI::args()->getArgv()[2])) {
229                         BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
230
231                         if (DBA::exists('contact', ['id' => DI::args()->getArgv()[2], 'uid' => Session::getLocalUser(), 'self' => false, 'pending' => false, 'blocked' => false])) {
232                                 $change = intval(DI::args()->getArgv()[2]);
233                         }
234                 }
235
236                 // @TODO: Replace with parameter from router
237                 if ((DI::args()->getArgc() > 1) && intval(DI::args()->getArgv()[1])) {
238                         $group = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => Session::getLocalUser(), 'deleted' => false]);
239                         if (!DBA::isResult($group)) {
240                                 DI::sysmsg()->addNotice(DI::l10n()->t('Group not found.'));
241                                 DI::baseUrl()->redirect('contact');
242                         }
243
244                         $members = Model\Contact\Group::getById($group['id']);
245                         $preselected = [];
246
247                         if (count($members)) {
248                                 foreach ($members as $member) {
249                                         $preselected[] = $member['id'];
250                                 }
251                         }
252
253                         if ($change) {
254                                 if (in_array($change, $preselected)) {
255                                         Model\Group::removeMember($group['id'], $change);
256                                 } else {
257                                         Model\Group::addMember($group['id'], $change);
258                                 }
259
260                                 $members = Model\Contact\Group::getById($group['id']);
261                                 $preselected = [];
262                                 if (count($members)) {
263                                         foreach ($members as $member) {
264                                                 $preselected[] = $member['id'];
265                                         }
266                                 }
267                         }
268
269                         $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
270                         $drop_txt = Renderer::replaceMacros($drop_tpl, [
271                                 '$id' => $group['id'],
272                                 '$delete' => DI::l10n()->t('Delete Group'),
273                                 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
274                         ]);
275
276                         $context = $context + [
277                                 '$title' => $group['name'],
278                                 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
279                                 '$gid' => $group['id'],
280                                 '$drop' => $drop_txt,
281                                 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
282                                 '$edit_name' => DI::l10n()->t('Edit Group Name'),
283                                 '$editable' => 1,
284                         ];
285                 }
286
287                 if (!isset($group)) {
288                         throw new \Friendica\Network\HTTPException\BadRequestException();
289                 }
290
291                 $groupeditor = [
292                         'label_members' => DI::l10n()->t('Members'),
293                         'members' => [],
294                         'label_contacts' => DI::l10n()->t('All Contacts'),
295                         'group_is_empty' => DI::l10n()->t('Group is empty'),
296                         'contacts' => [],
297                 ];
298
299                 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
300
301                 // Format the data of the group members
302                 foreach ($members as $member) {
303                         if ($member['url']) {
304                                 $entry = Contact::getContactTemplateVars($member);
305                                 $entry['label'] = 'members';
306                                 $entry['photo_menu'] = '';
307                                 $entry['change_member'] = [
308                                         'title'     => DI::l10n()->t("Remove contact from group"),
309                                         'gid'       => $group['id'],
310                                         'cid'       => $member['id'],
311                                         'sec_token' => $sec_token
312                                 ];
313
314                                 $groupeditor['members'][] = $entry;
315                         } else {
316                                 Model\Group::removeMember($group['id'], $member['id']);
317                         }
318                 }
319
320                 if ($nogroup) {
321                         $contacts = Model\Contact\Group::listUngrouped(Session::getLocalUser());
322                 } else {
323                         $contacts_stmt = DBA::select('contact', [],
324                                 ['rel' => [Model\Contact::FOLLOWER, Model\Contact::FRIEND, Model\Contact::SHARING],
325                                 'uid' => Session::getLocalUser(), 'pending' => false, 'blocked' => false, 'failed' => false, 'self' => false],
326                                 ['order' => ['name']]
327                         );
328                         $contacts = DBA::toArray($contacts_stmt);
329                         $context['$desc'] = DI::l10n()->t('Click on a contact to add or remove.');
330                 }
331
332                 if (DBA::isResult($contacts)) {
333                         // Format the data of the contacts who aren't in the contact group
334                         foreach ($contacts as $member) {
335                                 if (!in_array($member['id'], $preselected)) {
336                                         $entry = Contact::getContactTemplateVars($member);
337                                         $entry['label'] = 'contacts';
338                                         if (!$nogroup)
339                                                 $entry['photo_menu'] = [];
340
341                                         if (!$nogroup) {
342                                                 $entry['change_member'] = [
343                                                         'title'     => DI::l10n()->t("Add contact to group"),
344                                                         'gid'       => $group['id'],
345                                                         'cid'       => $member['id'],
346                                                         'sec_token' => $sec_token
347                                                 ];
348                                         }
349
350                                         $groupeditor['contacts'][] = $entry;
351                                 }
352                         }
353                 }
354
355                 $context['$groupeditor'] = $groupeditor;
356
357                 // If there are to many contacts we could provide an alternative view mode
358                 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
359                 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
360
361                 if ($change) {
362                         $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
363                         echo Renderer::replaceMacros($tpl, $context);
364                         System::exit();
365                 }
366
367                 return Renderer::replaceMacros($tpl, $context);
368         }
369 }