3 * @copyright Copyright (C) 2020, Friendica
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;
30 use Friendica\Util\Strings;
32 require_once 'boot.php';
34 class Group extends BaseModule
36 public static function post(array $parameters = [])
40 if (DI::mode()->isAjax()) {
45 notice(DI::l10n()->t('Permission denied.'));
46 DI::baseUrl()->redirect();
49 // @TODO: Replace with parameter from router
50 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
51 BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
53 $name = Strings::escapeTags(trim($_POST['groupname']));
54 $r = Model\Group::create(local_user(), $name);
56 info(DI::l10n()->t('Group created.'));
57 $r = Model\Group::getIdByName(local_user(), $name);
59 DI::baseUrl()->redirect('group/' . $r);
62 notice(DI::l10n()->t('Could not create group.'));
64 DI::baseUrl()->redirect('group');
67 // @TODO: Replace with parameter from router
68 if (($a->argc == 2) && intval($a->argv[1])) {
69 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
71 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user()]);
72 if (!DBA::isResult($group)) {
73 notice(DI::l10n()->t('Group not found.'));
74 DI::baseUrl()->redirect('contact');
76 $groupname = Strings::escapeTags(trim($_POST['groupname']));
77 if (strlen($groupname) && ($groupname != $group['name'])) {
78 if (Model\Group::update($group['id'], $groupname)) {
79 info(DI::l10n()->t('Group name changed.'));
85 public static function ajaxPost()
91 throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
94 // POST /group/123/add/123
95 // POST /group/123/remove/123
96 // @TODO: Replace with parameter from router
98 list($group_id, $command, $contact_id) = array_slice($a->argv, 1);
100 if (!Model\Group::exists($group_id, local_user())) {
101 throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
104 $contact = DBA::selectFirst('contact', ['deleted'], ['id' => $contact_id, 'uid' => local_user()]);
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);
115 if (!Model\Group::addMember($group_id, $contact_id)) {
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, $contact_id)) {
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.');
129 throw new \Exception(DI::l10n()->t('Unknown group command.'), 400);
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 public static function content(array $parameters = [])
148 throw new \Friendica\Network\HTTPException\ForbiddenException();
153 DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[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
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 (($a->argc == 2) && ($a->argv[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 (($a->argc == 2) && ($a->argv[1] === 'none') ||
189 ($a->argc == 1) && ($a->argv[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 (($a->argc == 3) && ($a->argv[1] === 'drop')) {
210 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
212 // @TODO: Replace with parameter from router
213 if (intval($a->argv[2])) {
214 if (!Model\Group::exists($a->argv[2], local_user())) {
215 notice(DI::l10n()->t('Group not found.'));
216 DI::baseUrl()->redirect('contact');
219 if (Model\Group::remove($a->argv[2])) {
220 info(DI::l10n()->t('Group removed.'));
222 notice(DI::l10n()->t('Unable to remove group.'));
225 DI::baseUrl()->redirect('group');
228 // @TODO: Replace with parameter from router
229 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
230 BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
232 if (DBA::exists('contact', ['id' => $a->argv[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) {
233 $change = intval($a->argv[2]);
237 // @TODO: Replace with parameter from router
238 if (($a->argc > 1) && intval($a->argv[1])) {
239 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user(), 'deleted' => false]);
240 if (!DBA::isResult($group)) {
241 notice(DI::l10n()->t('Group not found.'));
242 DI::baseUrl()->redirect('contact');
245 $members = Model\Contact::getByGroupId($group['id']);
248 if (count($members)) {
249 foreach ($members as $member) {
250 $preselected[] = $member['id'];
255 if (in_array($change, $preselected)) {
256 Model\Group::removeMember($group['id'], $change);
258 Model\Group::addMember($group['id'], $change);
261 $members = Model\Contact::getByGroupId($group['id']);
263 if (count($members)) {
264 foreach ($members as $member) {
265 $preselected[] = $member['id'];
270 $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
271 $drop_txt = Renderer::replaceMacros($drop_tpl, [
272 '$id' => $group['id'],
273 '$delete' => DI::l10n()->t('Delete Group'),
274 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
277 $context = $context + [
278 '$title' => $group['name'],
279 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
280 '$gid' => $group['id'],
281 '$drop' => $drop_txt,
282 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
283 '$edit_name' => DI::l10n()->t('Edit Group Name'),
288 if (!isset($group)) {
289 throw new \Friendica\Network\HTTPException\BadRequestException();
293 'label_members' => DI::l10n()->t('Members'),
295 'label_contacts' => DI::l10n()->t('All Contacts'),
296 'group_is_empty' => DI::l10n()->t('Group is empty'),
300 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
302 // Format the data of the group members
303 foreach ($members as $member) {
304 if ($member['url']) {
305 $entry = Contact::getContactTemplateVars($member);
306 $entry['label'] = 'members';
307 $entry['photo_menu'] = '';
308 $entry['change_member'] = [
309 'title' => DI::l10n()->t("Remove contact from group"),
310 'gid' => $group['id'],
311 'cid' => $member['id'],
312 'sec_token' => $sec_token
315 $groupeditor['members'][] = $entry;
317 Model\Group::removeMember($group['id'], $member['id']);
322 $contacts = Model\Contact::getUngroupedList(local_user());
324 $contacts_stmt = DBA::select('contact', [],
325 ['uid' => local_user(), 'pending' => false, 'blocked' => 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);