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