3 * @file src/Module/Group.php
6 namespace Friendica\Module;
8 use Friendica\BaseModule;
9 use Friendica\Core\Config;
10 use Friendica\Core\Renderer;
11 use Friendica\Core\System;
12 use Friendica\Database\DBA;
15 use Friendica\Util\Strings;
17 require_once 'boot.php';
19 class Group extends BaseModule
21 public static function post(array $parameters = [])
25 if (DI::mode()->isAjax()) {
30 notice(DI::l10n()->t('Permission denied.'));
31 DI::baseUrl()->redirect();
34 // @TODO: Replace with parameter from router
35 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
36 BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
38 $name = Strings::escapeTags(trim($_POST['groupname']));
39 $r = Model\Group::create(local_user(), $name);
41 info(DI::l10n()->t('Group created.'));
42 $r = Model\Group::getIdByName(local_user(), $name);
44 DI::baseUrl()->redirect('group/' . $r);
47 notice(DI::l10n()->t('Could not create group.'));
49 DI::baseUrl()->redirect('group');
52 // @TODO: Replace with parameter from router
53 if (($a->argc == 2) && intval($a->argv[1])) {
54 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
56 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user()]);
57 if (!DBA::isResult($group)) {
58 notice(DI::l10n()->t('Group not found.'));
59 DI::baseUrl()->redirect('contact');
61 $groupname = Strings::escapeTags(trim($_POST['groupname']));
62 if (strlen($groupname) && ($groupname != $group['name'])) {
63 if (Model\Group::update($group['id'], $groupname)) {
64 info(DI::l10n()->t('Group name changed.'));
70 public static function ajaxPost()
76 throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
79 // POST /group/123/add/123
80 // POST /group/123/remove/123
81 // @TODO: Replace with parameter from router
83 list($group_id, $command, $contact_id) = array_slice($a->argv, 1);
85 if (!Model\Group::exists($group_id, local_user())) {
86 throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
89 $contact = DBA::selectFirst('contact', ['deleted'], ['id' => $contact_id, 'uid' => local_user()]);
90 if (!DBA::isResult($contact)) {
91 throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
94 if ($contact['deleted']) {
95 throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
100 if (!Model\Group::addMember($group_id, $contact_id)) {
101 throw new \Exception(DI::l10n()->t('Unable to add the contact to the group.'), 500);
104 $message = DI::l10n()->t('Contact successfully added to group.');
107 if (!Model\Group::removeMember($group_id, $contact_id)) {
108 throw new \Exception(DI::l10n()->t('Unable to remove the contact from the group.'), 500);
111 $message = DI::l10n()->t('Contact successfully removed from group.');
114 throw new \Exception(DI::l10n()->t('Unknown group command.'), 400);
117 throw new \Exception(DI::l10n()->t('Bad request.'), 400);
121 System::jsonExit(['status' => 'OK', 'message' => $message]);
122 } catch (\Exception $e) {
123 notice($e->getMessage());
124 System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
128 public static function content(array $parameters = [])
133 throw new \Friendica\Network\HTTPException\ForbiddenException();
138 DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
140 // With no group number provided we jump to the unassigned contacts as a starting point
141 // @TODO: Replace with parameter from router
143 DI::baseUrl()->redirect('group/none');
146 // Switch to text mode interface if we have more than 'n' contacts or group members
147 $switchtotext = DI::pConfig()->get(local_user(), 'system', 'groupedit_image_limit');
148 if (is_null($switchtotext)) {
149 $switchtotext = Config::get('system', 'groupedit_image_limit', 200);
152 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
156 '$submit' => DI::l10n()->t('Save Group'),
157 '$submit_filter' => DI::l10n()->t('Filter'),
160 // @TODO: Replace with parameter from router
161 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
162 return Renderer::replaceMacros($tpl, $context + [
163 '$title' => DI::l10n()->t('Create a group of contacts/friends.'),
164 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), '', ''],
166 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
172 // @TODO: Replace with parameter from router
173 if (($a->argc == 2) && ($a->argv[1] === 'none') ||
174 ($a->argc == 1) && ($a->argv[0] === 'nogroup')) {
179 'name' => DI::l10n()->t('Contacts not in any group'),
185 $context = $context + [
186 '$title' => $group['name'],
187 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
193 // @TODO: Replace with parameter from router
194 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
195 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
197 // @TODO: Replace with parameter from router
198 if (intval($a->argv[2])) {
199 if (!Model\Group::exists($a->argv[2], local_user())) {
200 notice(DI::l10n()->t('Group not found.'));
201 DI::baseUrl()->redirect('contact');
204 if (Model\Group::remove($a->argv[2])) {
205 info(DI::l10n()->t('Group removed.'));
207 notice(DI::l10n()->t('Unable to remove group.'));
210 DI::baseUrl()->redirect('group');
213 // @TODO: Replace with parameter from router
214 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
215 BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
217 if (DBA::exists('contact', ['id' => $a->argv[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) {
218 $change = intval($a->argv[2]);
222 // @TODO: Replace with parameter from router
223 if (($a->argc > 1) && intval($a->argv[1])) {
224 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user(), 'deleted' => false]);
225 if (!DBA::isResult($group)) {
226 notice(DI::l10n()->t('Group not found.'));
227 DI::baseUrl()->redirect('contact');
230 $members = Model\Contact::getByGroupId($group['id']);
233 if (count($members)) {
234 foreach ($members as $member) {
235 $preselected[] = $member['id'];
240 if (in_array($change, $preselected)) {
241 Model\Group::removeMember($group['id'], $change);
243 Model\Group::addMember($group['id'], $change);
246 $members = Model\Contact::getByGroupId($group['id']);
248 if (count($members)) {
249 foreach ($members as $member) {
250 $preselected[] = $member['id'];
255 $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
256 $drop_txt = Renderer::replaceMacros($drop_tpl, [
257 '$id' => $group['id'],
258 '$delete' => DI::l10n()->t('Delete Group'),
259 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
262 $context = $context + [
263 '$title' => $group['name'],
264 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
265 '$gid' => $group['id'],
266 '$drop' => $drop_txt,
267 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
268 '$edit_name' => DI::l10n()->t('Edit Group Name'),
273 if (!isset($group)) {
274 throw new \Friendica\Network\HTTPException\BadRequestException();
278 'label_members' => DI::l10n()->t('Members'),
280 'label_contacts' => DI::l10n()->t('All Contacts'),
281 'group_is_empty' => DI::l10n()->t('Group is empty'),
285 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
287 // Format the data of the group members
288 foreach ($members as $member) {
289 if ($member['url']) {
290 $entry = Contact::getContactTemplateVars($member);
291 $entry['label'] = 'members';
292 $entry['photo_menu'] = '';
293 $entry['change_member'] = [
294 'title' => DI::l10n()->t("Remove contact from group"),
295 'gid' => $group['id'],
296 'cid' => $member['id'],
297 'sec_token' => $sec_token
300 $groupeditor['members'][] = $entry;
302 Model\Group::removeMember($group['id'], $member['id']);
307 $contacts = Model\Contact::getUngroupedList(local_user());
309 $contacts_stmt = DBA::select('contact', [],
310 ['uid' => local_user(), 'pending' => false, 'blocked' => false, 'self' => false],
311 ['order' => ['name']]
313 $contacts = DBA::toArray($contacts_stmt);
314 $context['$desc'] = DI::l10n()->t('Click on a contact to add or remove.');
317 if (DBA::isResult($contacts)) {
318 // Format the data of the contacts who aren't in the contact group
319 foreach ($contacts as $member) {
320 if (!in_array($member['id'], $preselected)) {
321 $entry = Contact::getContactTemplateVars($member);
322 $entry['label'] = 'contacts';
324 $entry['photo_menu'] = [];
327 $entry['change_member'] = [
328 'title' => DI::l10n()->t("Add contact to group"),
329 'gid' => $group['id'],
330 'cid' => $member['id'],
331 'sec_token' => $sec_token
335 $groupeditor['contacts'][] = $entry;
340 $context['$groupeditor'] = $groupeditor;
342 // If there are to many contacts we could provide an alternative view mode
343 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
344 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
347 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
348 echo Renderer::replaceMacros($tpl, $context);
352 return Renderer::replaceMacros($tpl, $context);