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;
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 $r = Model\Group::getIdByName(local_user(), $name);
58 DI::baseUrl()->redirect('group/' . $r);
61 notice(DI::l10n()->t('Could not create group.'));
63 DI::baseUrl()->redirect('group');
66 // @TODO: Replace with parameter from router
67 if (($a->argc == 2) && intval($a->argv[1])) {
68 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
70 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user()]);
71 if (!DBA::isResult($group)) {
72 notice(DI::l10n()->t('Group not found.'));
73 DI::baseUrl()->redirect('contact');
75 $groupname = Strings::escapeTags(trim($_POST['groupname']));
76 if (strlen($groupname) && ($groupname != $group['name'])) {
77 if (!Model\Group::update($group['id'], $groupname)) {
78 notice(DI::l10n()->t('Group name was not changed.'));
84 public static function ajaxPost()
90 throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
93 // POST /group/123/add/123
94 // POST /group/123/remove/123
95 // @TODO: Replace with parameter from router
97 list($group_id, $command, $contact_id) = array_slice($a->argv, 1);
99 if (!Model\Group::exists($group_id, local_user())) {
100 throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
103 $contact = DBA::selectFirst('contact', ['deleted'], ['id' => $contact_id, 'uid' => local_user()]);
104 if (!DBA::isResult($contact)) {
105 throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
108 if ($contact['deleted']) {
109 throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
114 if (!Model\Group::addMember($group_id, $contact_id)) {
115 throw new \Exception(DI::l10n()->t('Unable to add the contact to the group.'), 500);
118 $message = DI::l10n()->t('Contact successfully added to group.');
121 if (!Model\Group::removeMember($group_id, $contact_id)) {
122 throw new \Exception(DI::l10n()->t('Unable to remove the contact from the group.'), 500);
125 $message = DI::l10n()->t('Contact successfully removed from group.');
128 throw new \Exception(DI::l10n()->t('Unknown group command.'), 400);
131 throw new \Exception(DI::l10n()->t('Bad request.'), 400);
135 System::jsonExit(['status' => 'OK', 'message' => $message]);
136 } catch (\Exception $e) {
137 notice($e->getMessage());
138 System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
142 public static function content(array $parameters = [])
147 throw new \Friendica\Network\HTTPException\ForbiddenException();
152 DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
154 // With no group number provided we jump to the unassigned contacts as a starting point
155 // @TODO: Replace with parameter from router
157 DI::baseUrl()->redirect('group/none');
160 // Switch to text mode interface if we have more than 'n' contacts or group members
161 $switchtotext = DI::pConfig()->get(local_user(), 'system', 'groupedit_image_limit');
162 if (is_null($switchtotext)) {
163 $switchtotext = DI::config()->get('system', 'groupedit_image_limit', 200);
166 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
170 '$submit' => DI::l10n()->t('Save Group'),
171 '$submit_filter' => DI::l10n()->t('Filter'),
174 // @TODO: Replace with parameter from router
175 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
176 return Renderer::replaceMacros($tpl, $context + [
177 '$title' => DI::l10n()->t('Create a group of contacts/friends.'),
178 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), '', ''],
180 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
186 // @TODO: Replace with parameter from router
187 if (($a->argc == 2) && ($a->argv[1] === 'none') ||
188 ($a->argc == 1) && ($a->argv[0] === 'nogroup')) {
193 'name' => DI::l10n()->t('Contacts not in any group'),
199 $context = $context + [
200 '$title' => $group['name'],
201 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
207 // @TODO: Replace with parameter from router
208 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
209 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
211 // @TODO: Replace with parameter from router
212 if (intval($a->argv[2])) {
213 if (!Model\Group::exists($a->argv[2], local_user())) {
214 notice(DI::l10n()->t('Group not found.'));
215 DI::baseUrl()->redirect('contact');
218 if (!Model\Group::remove($a->argv[2])) {
219 notice(DI::l10n()->t('Unable to remove group.'));
222 DI::baseUrl()->redirect('group');
225 // @TODO: Replace with parameter from router
226 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
227 BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
229 if (DBA::exists('contact', ['id' => $a->argv[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) {
230 $change = intval($a->argv[2]);
234 // @TODO: Replace with parameter from router
235 if (($a->argc > 1) && intval($a->argv[1])) {
236 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user(), 'deleted' => false]);
237 if (!DBA::isResult($group)) {
238 notice(DI::l10n()->t('Group not found.'));
239 DI::baseUrl()->redirect('contact');
242 $members = Model\Contact\Group::getById($group['id']);
245 if (count($members)) {
246 foreach ($members as $member) {
247 $preselected[] = $member['id'];
252 if (in_array($change, $preselected)) {
253 Model\Group::removeMember($group['id'], $change);
255 Model\Group::addMember($group['id'], $change);
258 $members = Model\Contact\Group::getById($group['id']);
260 if (count($members)) {
261 foreach ($members as $member) {
262 $preselected[] = $member['id'];
267 $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
268 $drop_txt = Renderer::replaceMacros($drop_tpl, [
269 '$id' => $group['id'],
270 '$delete' => DI::l10n()->t('Delete Group'),
271 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
274 $context = $context + [
275 '$title' => $group['name'],
276 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
277 '$gid' => $group['id'],
278 '$drop' => $drop_txt,
279 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
280 '$edit_name' => DI::l10n()->t('Edit Group Name'),
285 if (!isset($group)) {
286 throw new \Friendica\Network\HTTPException\BadRequestException();
290 'label_members' => DI::l10n()->t('Members'),
292 'label_contacts' => DI::l10n()->t('All Contacts'),
293 'group_is_empty' => DI::l10n()->t('Group is empty'),
297 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
299 // Format the data of the group members
300 foreach ($members as $member) {
301 if ($member['url']) {
302 $entry = Contact::getContactTemplateVars($member);
303 $entry['label'] = 'members';
304 $entry['photo_menu'] = '';
305 $entry['change_member'] = [
306 'title' => DI::l10n()->t("Remove contact from group"),
307 'gid' => $group['id'],
308 'cid' => $member['id'],
309 'sec_token' => $sec_token
312 $groupeditor['members'][] = $entry;
314 Model\Group::removeMember($group['id'], $member['id']);
319 $contacts = Model\Contact\Group::listUngrouped(local_user());
321 $contacts_stmt = DBA::select('contact', [],
322 ['rel' => [Model\Contact::FOLLOWER, Model\Contact::FRIEND, Model\Contact::SHARING],
323 'uid' => local_user(), 'pending' => false, 'blocked' => false, 'failed' => false, 'self' => false],
324 ['order' => ['name']]
326 $contacts = DBA::toArray($contacts_stmt);
327 $context['$desc'] = DI::l10n()->t('Click on a contact to add or remove.');
330 if (DBA::isResult($contacts)) {
331 // Format the data of the contacts who aren't in the contact group
332 foreach ($contacts as $member) {
333 if (!in_array($member['id'], $preselected)) {
334 $entry = Contact::getContactTemplateVars($member);
335 $entry['label'] = 'contacts';
337 $entry['photo_menu'] = [];
340 $entry['change_member'] = [
341 'title' => DI::l10n()->t("Add contact to group"),
342 'gid' => $group['id'],
343 'cid' => $member['id'],
344 'sec_token' => $sec_token
348 $groupeditor['contacts'][] = $entry;
353 $context['$groupeditor'] = $groupeditor;
355 // If there are to many contacts we could provide an alternative view mode
356 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
357 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
360 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
361 echo Renderer::replaceMacros($tpl, $context);
365 return Renderer::replaceMacros($tpl, $context);