3 * @copyright Copyright (C) 2010-2021, 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 require_once 'boot.php';
33 class Group extends BaseModule
35 protected function post(array $request = [], array $post = [])
37 if (DI::mode()->isAjax()) {
42 notice(DI::l10n()->t('Permission denied.'));
43 DI::baseUrl()->redirect();
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');
50 $name = trim($_POST['groupname']);
51 $r = Model\Group::create(local_user(), $name);
53 $r = Model\Group::getIdByName(local_user(), $name);
55 DI::baseUrl()->redirect('group/' . $r);
58 notice(DI::l10n()->t('Could not create group.'));
60 DI::baseUrl()->redirect('group');
63 // @TODO: Replace with parameter from router
64 if ((DI::args()->getArgc() == 2) && intval(DI::args()->getArgv()[1])) {
65 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
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');
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.'));
81 public function ajaxPost()
85 throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
88 if (isset($this->parameters['command'])) {
89 $group_id = $this->parameters['group'];
90 $contact_id = $this->parameters['contact'];
92 if (!Model\Group::exists($group_id, local_user())) {
93 throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
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);
102 if (empty($cdata['user'])) {
103 throw new \Exception(DI::l10n()->t('Invalid contact.'), 404);
106 $contact = Model\Contact::getById($cdata['user'], ['deleted']);
107 if (!DBA::isResult($contact)) {
108 throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
111 if ($contact['deleted']) {
112 throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
115 switch($this->parameters['command']) {
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);
121 $message = DI::l10n()->t('Contact successfully added to group.');
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);
128 $message = DI::l10n()->t('Contact successfully removed from group.');
132 throw new \Exception(DI::l10n()->t('Bad request.'), 400);
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()]);
143 protected function content(array $request = []): string
148 throw new \Friendica\Network\HTTPException\ForbiddenException();
153 DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[1] : 'everyone'));
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');
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);
167 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
171 '$submit' => DI::l10n()->t('Save Group'),
172 '$submit_filter' => DI::l10n()->t('Filter'),
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: '), '', ''],
181 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
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')) {
194 'name' => DI::l10n()->t('Contacts not in any group'),
200 $context = $context + [
201 '$title' => $group['name'],
202 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
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');
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');
219 if (!Model\Group::remove(DI::args()->getArgv()[2])) {
220 notice(DI::l10n()->t('Unable to remove group.'));
223 DI::baseUrl()->redirect('group');
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');
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]);
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');
243 $members = Model\Contact\Group::getById($group['id']);
246 if (count($members)) {
247 foreach ($members as $member) {
248 $preselected[] = $member['id'];
253 if (in_array($change, $preselected)) {
254 Model\Group::removeMember($group['id'], $change);
256 Model\Group::addMember($group['id'], $change);
259 $members = Model\Contact\Group::getById($group['id']);
261 if (count($members)) {
262 foreach ($members as $member) {
263 $preselected[] = $member['id'];
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"),
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'),
286 if (!isset($group)) {
287 throw new \Friendica\Network\HTTPException\BadRequestException();
291 'label_members' => DI::l10n()->t('Members'),
293 'label_contacts' => DI::l10n()->t('All Contacts'),
294 'group_is_empty' => DI::l10n()->t('Group is empty'),
298 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
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
313 $groupeditor['members'][] = $entry;
315 Model\Group::removeMember($group['id'], $member['id']);
320 $contacts = Model\Contact\Group::listUngrouped(local_user());
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']]
327 $contacts = DBA::toArray($contacts_stmt);
328 $context['$desc'] = DI::l10n()->t('Click on a contact to add or remove.');
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';
338 $entry['photo_menu'] = [];
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
349 $groupeditor['contacts'][] = $entry;
354 $context['$groupeditor'] = $groupeditor;
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);
361 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
362 echo Renderer::replaceMacros($tpl, $context);
366 return Renderer::replaceMacros($tpl, $context);