]> git.mxchange.org Git - friendica.git/blob - src/Module/Group.php
Use rawContent for Special Options to avoid a protected options() method
[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\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model;
30
31 require_once 'boot.php';
32
33 class Group extends BaseModule
34 {
35         protected function post(array $request = [])
36         {
37                 if (DI::mode()->isAjax()) {
38                         $this->ajaxPost();
39                 }
40
41                 if (!local_user()) {
42                         notice(DI::l10n()->t('Permission denied.'));
43                         DI::baseUrl()->redirect();
44                 }
45
46                 // @TODO: Replace with parameter from router
47                 if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) {
48                         BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
49
50                         $name = trim($request['groupname']);
51                         $r = Model\Group::create(local_user(), $name);
52                         if ($r) {
53                                 $r = Model\Group::getIdByName(local_user(), $name);
54                                 if ($r) {
55                                         DI::baseUrl()->redirect('group/' . $r);
56                                 }
57                         } else {
58                                 notice(DI::l10n()->t('Could not create group.'));
59                         }
60                         DI::baseUrl()->redirect('group');
61                 }
62
63                 // @TODO: Replace with parameter from router
64                 if ((DI::args()->getArgc() == 2) && intval(DI::args()->getArgv()[1])) {
65                         BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
66
67                         $group = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => local_user()]);
68                         if (!DBA::isResult($group)) {
69                                 notice(DI::l10n()->t('Group not found.'));
70                                 DI::baseUrl()->redirect('contact');
71                         }
72                         $groupname = trim($_POST['groupname']);
73                         if (strlen($groupname) && ($groupname != $group['name'])) {
74                                 if (!Model\Group::update($group['id'], $groupname)) {
75                                         notice(DI::l10n()->t('Group name was not changed.'));
76                                 }
77                         }
78                 }
79         }
80
81         public function ajaxPost()
82         {
83                 try {
84                         if (!local_user()) {
85                                 throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
86                         }
87
88                         if (isset($this->parameters['command'])) {
89                                 $group_id = $this->parameters['group'];
90                                 $contact_id = $this->parameters['contact'];
91
92                                 if (!Model\Group::exists($group_id, local_user())) {
93                                         throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
94                                 }
95
96                                 // @TODO Backward compatibility with user contacts, remove by version 2022.03
97                                 $cdata = Model\Contact::getPublicAndUserContactID($contact_id, local_user());
98                                 if (empty($cdata['public'])) {
99                                         throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
100                                 }
101
102                                 if (empty($cdata['user'])) {
103                                         throw new \Exception(DI::l10n()->t('Invalid contact.'), 404);
104                                 }
105
106                                 $contact = Model\Contact::getById($cdata['user'], ['deleted']);
107                                 if (!DBA::isResult($contact)) {
108                                         throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
109                                 }
110
111                                 if ($contact['deleted']) {
112                                         throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
113                                 }
114
115                                 switch($this->parameters['command']) {
116                                         case 'add':
117                                                 if (!Model\Group::addMember($group_id, $cdata['user'])) {
118                                                         throw new \Exception(DI::l10n()->t('Unable to add the contact to the group.'), 500);
119                                                 }
120
121                                                 $message = DI::l10n()->t('Contact successfully added to group.');
122                                                 break;
123                                         case 'remove':
124                                                 if (!Model\Group::removeMember($group_id, $cdata['user'])) {
125                                                         throw new \Exception(DI::l10n()->t('Unable to remove the contact from the group.'), 500);
126                                                 }
127
128                                                 $message = DI::l10n()->t('Contact successfully removed from group.');
129                                                 break;
130                                 }
131                         } else {
132                                 throw new \Exception(DI::l10n()->t('Bad request.'), 400);
133                         }
134
135                         info($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         protected function content(array $request = []): string
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', ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[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 (DI::args()->getArgc() == 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 ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[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 ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'none') ||
189                         (DI::args()->getArgc() == 1) && (DI::args()->getArgv()[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 ((DI::args()->getArgc() == 3) && (DI::args()->getArgv()[1] === 'drop')) {
210                         BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
211
212                         // @TODO: Replace with parameter from router
213                         if (intval(DI::args()->getArgv()[2])) {
214                                 if (!Model\Group::exists(DI::args()->getArgv()[2], local_user())) {
215                                         notice(DI::l10n()->t('Group not found.'));
216                                         DI::baseUrl()->redirect('contact');
217                                 }
218
219                                 if (!Model\Group::remove(DI::args()->getArgv()[2])) {
220                                         notice(DI::l10n()->t('Unable to remove group.'));
221                                 }
222                         }
223                         DI::baseUrl()->redirect('group');
224                 }
225
226                 // @TODO: Replace with parameter from router
227                 if ((DI::args()->getArgc() > 2) && intval(DI::args()->getArgv()[1]) && intval(DI::args()->getArgv()[2])) {
228                         BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
229
230                         if (DBA::exists('contact', ['id' => DI::args()->getArgv()[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) {
231                                 $change = intval(DI::args()->getArgv()[2]);
232                         }
233                 }
234
235                 // @TODO: Replace with parameter from router
236                 if ((DI::args()->getArgc() > 1) && intval(DI::args()->getArgv()[1])) {
237                         $group = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => local_user(), 'deleted' => false]);
238                         if (!DBA::isResult($group)) {
239                                 notice(DI::l10n()->t('Group not found.'));
240                                 DI::baseUrl()->redirect('contact');
241                         }
242
243                         $members = Model\Contact\Group::getById($group['id']);
244                         $preselected = [];
245
246                         if (count($members)) {
247                                 foreach ($members as $member) {
248                                         $preselected[] = $member['id'];
249                                 }
250                         }
251
252                         if ($change) {
253                                 if (in_array($change, $preselected)) {
254                                         Model\Group::removeMember($group['id'], $change);
255                                 } else {
256                                         Model\Group::addMember($group['id'], $change);
257                                 }
258
259                                 $members = Model\Contact\Group::getById($group['id']);
260                                 $preselected = [];
261                                 if (count($members)) {
262                                         foreach ($members as $member) {
263                                                 $preselected[] = $member['id'];
264                                         }
265                                 }
266                         }
267
268                         $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
269                         $drop_txt = Renderer::replaceMacros($drop_tpl, [
270                                 '$id' => $group['id'],
271                                 '$delete' => DI::l10n()->t('Delete Group'),
272                                 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
273                         ]);
274
275                         $context = $context + [
276                                 '$title' => $group['name'],
277                                 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
278                                 '$gid' => $group['id'],
279                                 '$drop' => $drop_txt,
280                                 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
281                                 '$edit_name' => DI::l10n()->t('Edit Group Name'),
282                                 '$editable' => 1,
283                         ];
284                 }
285
286                 if (!isset($group)) {
287                         throw new \Friendica\Network\HTTPException\BadRequestException();
288                 }
289
290                 $groupeditor = [
291                         'label_members' => DI::l10n()->t('Members'),
292                         'members' => [],
293                         'label_contacts' => DI::l10n()->t('All Contacts'),
294                         'group_is_empty' => DI::l10n()->t('Group is empty'),
295                         'contacts' => [],
296                 ];
297
298                 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
299
300                 // Format the data of the group members
301                 foreach ($members as $member) {
302                         if ($member['url']) {
303                                 $entry = Contact::getContactTemplateVars($member);
304                                 $entry['label'] = 'members';
305                                 $entry['photo_menu'] = '';
306                                 $entry['change_member'] = [
307                                         'title'     => DI::l10n()->t("Remove contact from group"),
308                                         'gid'       => $group['id'],
309                                         'cid'       => $member['id'],
310                                         'sec_token' => $sec_token
311                                 ];
312
313                                 $groupeditor['members'][] = $entry;
314                         } else {
315                                 Model\Group::removeMember($group['id'], $member['id']);
316                         }
317                 }
318
319                 if ($nogroup) {
320                         $contacts = Model\Contact\Group::listUngrouped(local_user());
321                 } else {
322                         $contacts_stmt = DBA::select('contact', [],
323                                 ['rel' => [Model\Contact::FOLLOWER, Model\Contact::FRIEND, Model\Contact::SHARING],
324                                 'uid' => local_user(), 'pending' => false, 'blocked' => false, 'failed' => false, 'self' => false],
325                                 ['order' => ['name']]
326                         );
327                         $contacts = DBA::toArray($contacts_stmt);
328                         $context['$desc'] = DI::l10n()->t('Click on a contact to add or remove.');
329                 }
330
331                 if (DBA::isResult($contacts)) {
332                         // Format the data of the contacts who aren't in the contact group
333                         foreach ($contacts as $member) {
334                                 if (!in_array($member['id'], $preselected)) {
335                                         $entry = Contact::getContactTemplateVars($member);
336                                         $entry['label'] = 'contacts';
337                                         if (!$nogroup)
338                                                 $entry['photo_menu'] = [];
339
340                                         if (!$nogroup) {
341                                                 $entry['change_member'] = [
342                                                         'title'     => DI::l10n()->t("Add contact to group"),
343                                                         'gid'       => $group['id'],
344                                                         'cid'       => $member['id'],
345                                                         'sec_token' => $sec_token
346                                                 ];
347                                         }
348
349                                         $groupeditor['contacts'][] = $entry;
350                                 }
351                         }
352                 }
353
354                 $context['$groupeditor'] = $groupeditor;
355
356                 // If there are to many contacts we could provide an alternative view mode
357                 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
358                 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
359
360                 if ($change) {
361                         $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
362                         echo Renderer::replaceMacros($tpl, $context);
363                         exit();
364                 }
365
366                 return Renderer::replaceMacros($tpl, $context);
367         }
368 }