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