3 * @copyright Copyright (C) 2010-2023, 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\System;
27 use Friendica\Database\DBA;
31 class Group extends BaseModule
33 protected function post(array $request = [])
35 if (DI::mode()->isAjax()) {
39 if (!DI::userSession()->getLocalUserId()) {
40 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
41 DI::baseUrl()->redirect();
44 // @TODO: Replace with parameter from router
45 if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) {
46 BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
48 $name = trim($request['groupname']);
49 $r = Model\Group::create(DI::userSession()->getLocalUserId(), $name);
51 $r = Model\Group::getIdByName(DI::userSession()->getLocalUserId(), $name);
53 DI::baseUrl()->redirect('group/' . $r);
56 DI::sysmsg()->addNotice(DI::l10n()->t('Could not create group.'));
58 DI::baseUrl()->redirect('group');
61 // @TODO: Replace with parameter from router
62 if ((DI::args()->getArgc() == 2) && intval(DI::args()->getArgv()[1])) {
63 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
65 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => DI::userSession()->getLocalUserId()]);
66 if (!DBA::isResult($group)) {
67 DI::sysmsg()->addNotice(DI::l10n()->t('Group not found.'));
68 DI::baseUrl()->redirect('contact');
70 $groupname = trim($_POST['groupname']);
71 if (strlen($groupname) && ($groupname != $group['name'])) {
72 if (!Model\Group::update($group['id'], $groupname)) {
73 DI::sysmsg()->addNotice(DI::l10n()->t('Group name was not changed.'));
79 public function ajaxPost()
82 if (!DI::userSession()->getLocalUserId()) {
83 throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
86 if (isset($this->parameters['command'])) {
87 $group_id = $this->parameters['group'];
88 $contact_id = $this->parameters['contact'];
90 if (!Model\Group::exists($group_id, DI::userSession()->getLocalUserId())) {
91 throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
94 // @TODO Backward compatibility with user contacts, remove by version 2022.03
95 $cdata = Model\Contact::getPublicAndUserContactID($contact_id, DI::userSession()->getLocalUserId());
96 if (empty($cdata['public'])) {
97 throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
100 if (empty($cdata['user'])) {
101 throw new \Exception(DI::l10n()->t('Invalid contact.'), 404);
104 $contact = Model\Contact::getById($cdata['user'], ['deleted']);
105 if (!DBA::isResult($contact)) {
106 throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
109 if ($contact['deleted']) {
110 throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
113 switch($this->parameters['command']) {
115 if (!Model\Group::addMember($group_id, $cdata['user'])) {
116 throw new \Exception(DI::l10n()->t('Unable to add the contact to the group.'), 500);
119 $message = DI::l10n()->t('Contact successfully added to group.');
122 if (!Model\Group::removeMember($group_id, $cdata['user'])) {
123 throw new \Exception(DI::l10n()->t('Unable to remove the contact from the group.'), 500);
126 $message = DI::l10n()->t('Contact successfully removed from group.');
130 throw new \Exception(DI::l10n()->t('Bad request.'), 400);
133 DI::sysmsg()->addInfo($message);
134 System::jsonExit(['status' => 'OK', 'message' => $message]);
135 } catch (\Exception $e) {
136 DI::sysmsg()->addNotice($e->getMessage());
137 System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
141 protected function content(array $request = []): string
145 if (!DI::userSession()->getLocalUserId()) {
146 throw new \Friendica\Network\HTTPException\ForbiddenException();
151 DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[1] : 'everyone'));
153 // With no group number provided we jump to the unassigned contacts as a starting point
154 // @TODO: Replace with parameter from router
155 if (DI::args()->getArgc() == 1) {
156 DI::baseUrl()->redirect('group/none');
159 // Switch to text mode interface if we have more than 'n' contacts or group members
160 $switchtotext = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'groupedit_image_limit');
161 if (is_null($switchtotext)) {
162 $switchtotext = DI::config()->get('system', 'groupedit_image_limit', 200);
165 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
169 '$submit' => DI::l10n()->t('Save Group'),
170 '$submit_filter' => DI::l10n()->t('Filter'),
173 // @TODO: Replace with parameter from router
174 if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) {
175 return Renderer::replaceMacros($tpl, $context + [
176 '$title' => DI::l10n()->t('Create a group of contacts/friends.'),
177 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), '', ''],
179 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
185 // @TODO: Replace with parameter from router
186 if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'none') ||
187 (DI::args()->getArgc() == 1) && (DI::args()->getArgv()[0] === 'nogroup')) {
192 'name' => DI::l10n()->t('Contacts not in any group'),
198 $context = $context + [
199 '$title' => $group['name'],
200 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
206 // @TODO: Replace with parameter from router
207 if ((DI::args()->getArgc() == 3) && (DI::args()->getArgv()[1] === 'drop')) {
208 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
210 // @TODO: Replace with parameter from router
211 if (intval(DI::args()->getArgv()[2])) {
212 if (!Model\Group::exists(DI::args()->getArgv()[2], DI::userSession()->getLocalUserId())) {
213 DI::sysmsg()->addNotice(DI::l10n()->t('Group not found.'));
214 DI::baseUrl()->redirect('contact');
217 if (!Model\Group::remove(DI::args()->getArgv()[2])) {
218 DI::sysmsg()->addNotice(DI::l10n()->t('Unable to remove group.'));
221 DI::baseUrl()->redirect('group');
224 // @TODO: Replace with parameter from router
225 if ((DI::args()->getArgc() > 2) && intval(DI::args()->getArgv()[1]) && intval(DI::args()->getArgv()[2])) {
226 BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
228 if (DBA::exists('contact', ['id' => DI::args()->getArgv()[2], 'uid' => DI::userSession()->getLocalUserId(), 'self' => false, 'pending' => false, 'blocked' => false])) {
229 $change = intval(DI::args()->getArgv()[2]);
233 // @TODO: Replace with parameter from router
234 if ((DI::args()->getArgc() > 1) && intval(DI::args()->getArgv()[1])) {
235 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => DI::userSession()->getLocalUserId(), 'deleted' => false]);
236 if (!DBA::isResult($group)) {
237 DI::sysmsg()->addNotice(DI::l10n()->t('Group not found.'));
238 DI::baseUrl()->redirect('contact');
241 $members = Model\Contact\Group::getById($group['id']);
244 if (count($members)) {
245 foreach ($members as $member) {
246 $preselected[] = $member['id'];
251 if (in_array($change, $preselected)) {
252 Model\Group::removeMember($group['id'], $change);
254 Model\Group::addMember($group['id'], $change);
257 $members = Model\Contact\Group::getById($group['id']);
259 if (count($members)) {
260 foreach ($members as $member) {
261 $preselected[] = $member['id'];
266 $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
267 $drop_txt = Renderer::replaceMacros($drop_tpl, [
268 '$id' => $group['id'],
269 '$delete' => DI::l10n()->t('Delete Group'),
270 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
273 $context = $context + [
274 '$title' => $group['name'],
275 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
276 '$gid' => $group['id'],
277 '$drop' => $drop_txt,
278 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
279 '$edit_name' => DI::l10n()->t('Edit Group Name'),
284 if (!isset($group)) {
285 throw new \Friendica\Network\HTTPException\BadRequestException();
289 'label_members' => DI::l10n()->t('Members'),
291 'label_contacts' => DI::l10n()->t('All Contacts'),
292 'group_is_empty' => DI::l10n()->t('Group is empty'),
296 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
298 // Format the data of the group members
299 foreach ($members as $member) {
300 if ($member['url']) {
301 $entry = Contact::getContactTemplateVars($member);
302 $entry['label'] = 'members';
303 $entry['photo_menu'] = '';
304 $entry['change_member'] = [
305 'title' => DI::l10n()->t("Remove contact from group"),
306 'gid' => $group['id'],
307 'cid' => $member['id'],
308 'sec_token' => $sec_token
311 $groupeditor['members'][] = $entry;
313 Model\Group::removeMember($group['id'], $member['id']);
318 $contacts = Model\Contact\Group::listUngrouped(DI::userSession()->getLocalUserId());
320 $contacts_stmt = DBA::select('contact', [],
321 ['rel' => [Model\Contact::FOLLOWER, Model\Contact::FRIEND, Model\Contact::SHARING],
322 'uid' => DI::userSession()->getLocalUserId(), 'pending' => false, 'blocked' => false, 'failed' => false, 'self' => false],
323 ['order' => ['name']]
325 $contacts = DBA::toArray($contacts_stmt);
326 $context['$desc'] = DI::l10n()->t('Click on a contact to add or remove.');
329 if (DBA::isResult($contacts)) {
330 // Format the data of the contacts who aren't in the contact group
331 foreach ($contacts as $member) {
332 if (!in_array($member['id'], $preselected)) {
333 $entry = Contact::getContactTemplateVars($member);
334 $entry['label'] = 'contacts';
336 $entry['photo_menu'] = [];
339 $entry['change_member'] = [
340 'title' => DI::l10n()->t("Add contact to group"),
341 'gid' => $group['id'],
342 'cid' => $member['id'],
343 'sec_token' => $sec_token
347 $groupeditor['contacts'][] = $entry;
352 $context['$groupeditor'] = $groupeditor;
354 // If there are to many contacts we could provide an alternative view mode
355 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
356 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
359 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
360 echo Renderer::replaceMacros($tpl, $context);
364 return Renderer::replaceMacros($tpl, $context);