3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Session;
27 use Friendica\Core\System;
28 use Friendica\Database\DBA;
32 require_once 'boot.php';
34 class Group extends BaseModule
36 protected function post(array $request = [])
38 if (DI::mode()->isAjax()) {
42 if (!Session::getLocalUser()) {
43 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
44 DI::baseUrl()->redirect();
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');
51 $name = trim($request['groupname']);
52 $r = Model\Group::create(Session::getLocalUser(), $name);
54 $r = Model\Group::getIdByName(Session::getLocalUser(), $name);
56 DI::baseUrl()->redirect('group/' . $r);
59 DI::sysmsg()->addNotice(DI::l10n()->t('Could not create group.'));
61 DI::baseUrl()->redirect('group');
64 // @TODO: Replace with parameter from router
65 if ((DI::args()->getArgc() == 2) && intval(DI::args()->getArgv()[1])) {
66 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
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');
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.'));
82 public function ajaxPost()
85 if (!Session::getLocalUser()) {
86 throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
89 if (isset($this->parameters['command'])) {
90 $group_id = $this->parameters['group'];
91 $contact_id = $this->parameters['contact'];
93 if (!Model\Group::exists($group_id, Session::getLocalUser())) {
94 throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
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);
103 if (empty($cdata['user'])) {
104 throw new \Exception(DI::l10n()->t('Invalid contact.'), 404);
107 $contact = Model\Contact::getById($cdata['user'], ['deleted']);
108 if (!DBA::isResult($contact)) {
109 throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
112 if ($contact['deleted']) {
113 throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
116 switch($this->parameters['command']) {
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);
122 $message = DI::l10n()->t('Contact successfully added to group.');
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);
129 $message = DI::l10n()->t('Contact successfully removed from group.');
133 throw new \Exception(DI::l10n()->t('Bad request.'), 400);
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()]);
144 protected function content(array $request = []): string
148 if (!Session::getLocalUser()) {
149 throw new \Friendica\Network\HTTPException\ForbiddenException();
154 DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[1] : 'everyone'));
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');
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);
168 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
172 '$submit' => DI::l10n()->t('Save Group'),
173 '$submit_filter' => DI::l10n()->t('Filter'),
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: '), '', ''],
182 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
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')) {
195 'name' => DI::l10n()->t('Contacts not in any group'),
201 $context = $context + [
202 '$title' => $group['name'],
203 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
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');
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');
220 if (!Model\Group::remove(DI::args()->getArgv()[2])) {
221 DI::sysmsg()->addNotice(DI::l10n()->t('Unable to remove group.'));
224 DI::baseUrl()->redirect('group');
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');
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]);
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');
244 $members = Model\Contact\Group::getById($group['id']);
247 if (count($members)) {
248 foreach ($members as $member) {
249 $preselected[] = $member['id'];
254 if (in_array($change, $preselected)) {
255 Model\Group::removeMember($group['id'], $change);
257 Model\Group::addMember($group['id'], $change);
260 $members = Model\Contact\Group::getById($group['id']);
262 if (count($members)) {
263 foreach ($members as $member) {
264 $preselected[] = $member['id'];
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"),
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'),
287 if (!isset($group)) {
288 throw new \Friendica\Network\HTTPException\BadRequestException();
292 'label_members' => DI::l10n()->t('Members'),
294 'label_contacts' => DI::l10n()->t('All Contacts'),
295 'group_is_empty' => DI::l10n()->t('Group is empty'),
299 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
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
314 $groupeditor['members'][] = $entry;
316 Model\Group::removeMember($group['id'], $member['id']);
321 $contacts = Model\Contact\Group::listUngrouped(Session::getLocalUser());
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']]
328 $contacts = DBA::toArray($contacts_stmt);
329 $context['$desc'] = DI::l10n()->t('Click on a contact to add or remove.');
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';
339 $entry['photo_menu'] = [];
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
350 $groupeditor['contacts'][] = $entry;
355 $context['$groupeditor'] = $groupeditor;
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);
362 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
363 echo Renderer::replaceMacros($tpl, $context);
367 return Renderer::replaceMacros($tpl, $context);