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 public function 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 $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);
101 if ($contact['deleted']) {
102 throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
105 switch($this->parameters['command']) {
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);
111 $message = DI::l10n()->t('Contact successfully added to group.');
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);
118 $message = DI::l10n()->t('Contact successfully removed from group.');
122 throw new \Exception(DI::l10n()->t('Bad request.'), 400);
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()]);
133 public function content(): string
138 throw new \Friendica\Network\HTTPException\ForbiddenException();
143 DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[1] : 'everyone'));
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');
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);
157 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
161 '$submit' => DI::l10n()->t('Save Group'),
162 '$submit_filter' => DI::l10n()->t('Filter'),
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: '), '', ''],
171 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
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')) {
184 'name' => DI::l10n()->t('Contacts not in any group'),
190 $context = $context + [
191 '$title' => $group['name'],
192 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
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');
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');
209 if (!Model\Group::remove(DI::args()->getArgv()[2])) {
210 notice(DI::l10n()->t('Unable to remove group.'));
213 DI::baseUrl()->redirect('group');
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');
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]);
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');
233 $members = Model\Contact\Group::getById($group['id']);
236 if (count($members)) {
237 foreach ($members as $member) {
238 $preselected[] = $member['id'];
243 if (in_array($change, $preselected)) {
244 Model\Group::removeMember($group['id'], $change);
246 Model\Group::addMember($group['id'], $change);
249 $members = Model\Contact\Group::getById($group['id']);
251 if (count($members)) {
252 foreach ($members as $member) {
253 $preselected[] = $member['id'];
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"),
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'),
276 if (!isset($group)) {
277 throw new \Friendica\Network\HTTPException\BadRequestException();
281 'label_members' => DI::l10n()->t('Members'),
283 'label_contacts' => DI::l10n()->t('All Contacts'),
284 'group_is_empty' => DI::l10n()->t('Group is empty'),
288 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
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
303 $groupeditor['members'][] = $entry;
305 Model\Group::removeMember($group['id'], $member['id']);
310 $contacts = Model\Contact\Group::listUngrouped(local_user());
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']]
317 $contacts = DBA::toArray($contacts_stmt);
318 $context['$desc'] = DI::l10n()->t('Click on a contact to add or remove.');
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';
328 $entry['photo_menu'] = [];
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
339 $groupeditor['contacts'][] = $entry;
344 $context['$groupeditor'] = $groupeditor;
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);
351 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
352 echo Renderer::replaceMacros($tpl, $context);
356 return Renderer::replaceMacros($tpl, $context);