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