3 * @file src/Module/Group.php
6 namespace Friendica\Module;
8 use Friendica\BaseModule;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Renderer;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
16 use Friendica\Util\Strings;
18 require_once 'boot.php';
20 class Group extends BaseModule
22 public static function post(array $parameters = [])
26 if (DI::mode()->isAjax()) {
31 notice(L10n::t('Permission denied.'));
32 DI::baseUrl()->redirect();
35 // @TODO: Replace with parameter from router
36 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
37 BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
39 $name = Strings::escapeTags(trim($_POST['groupname']));
40 $r = Model\Group::create(local_user(), $name);
42 info(L10n::t('Group created.'));
43 $r = Model\Group::getIdByName(local_user(), $name);
45 DI::baseUrl()->redirect('group/' . $r);
48 notice(L10n::t('Could not create group.'));
50 DI::baseUrl()->redirect('group');
53 // @TODO: Replace with parameter from router
54 if (($a->argc == 2) && intval($a->argv[1])) {
55 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
57 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user()]);
58 if (!DBA::isResult($group)) {
59 notice(L10n::t('Group not found.'));
60 DI::baseUrl()->redirect('contact');
62 $groupname = Strings::escapeTags(trim($_POST['groupname']));
63 if (strlen($groupname) && ($groupname != $group['name'])) {
64 if (Model\Group::update($group['id'], $groupname)) {
65 info(L10n::t('Group name changed.'));
71 public static function ajaxPost()
77 throw new \Exception(L10n::t('Permission denied.'), 403);
80 // POST /group/123/add/123
81 // POST /group/123/remove/123
82 // @TODO: Replace with parameter from router
84 list($group_id, $command, $contact_id) = array_slice($a->argv, 1);
86 if (!Model\Group::exists($group_id, local_user())) {
87 throw new \Exception(L10n::t('Unknown group.'), 404);
90 $contact = DBA::selectFirst('contact', ['deleted'], ['id' => $contact_id, 'uid' => local_user()]);
91 if (!DBA::isResult($contact)) {
92 throw new \Exception(L10n::t('Contact not found.'), 404);
95 if ($contact['deleted']) {
96 throw new \Exception(L10n::t('Contact is deleted.'), 410);
101 if (!Model\Group::addMember($group_id, $contact_id)) {
102 throw new \Exception(L10n::t('Unable to add the contact to the group.'), 500);
105 $message = L10n::t('Contact successfully added to group.');
108 if (!Model\Group::removeMember($group_id, $contact_id)) {
109 throw new \Exception(L10n::t('Unable to remove the contact from the group.'), 500);
112 $message = L10n::t('Contact successfully removed from group.');
115 throw new \Exception(L10n::t('Unknown group command.'), 400);
118 throw new \Exception(L10n::t('Bad request.'), 400);
122 System::jsonExit(['status' => 'OK', 'message' => $message]);
123 } catch (\Exception $e) {
124 notice($e->getMessage());
125 System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
129 public static function content(array $parameters = [])
134 throw new \Friendica\Network\HTTPException\ForbiddenException();
139 DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
141 // With no group number provided we jump to the unassigned contacts as a starting point
142 // @TODO: Replace with parameter from router
144 DI::baseUrl()->redirect('group/none');
147 // Switch to text mode interface if we have more than 'n' contacts or group members
148 $switchtotext = DI::pConfig()->get(local_user(), 'system', 'groupedit_image_limit');
149 if (is_null($switchtotext)) {
150 $switchtotext = Config::get('system', 'groupedit_image_limit', 200);
153 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
157 '$submit' => L10n::t('Save Group'),
158 '$submit_filter' => L10n::t('Filter'),
161 // @TODO: Replace with parameter from router
162 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
163 return Renderer::replaceMacros($tpl, $context + [
164 '$title' => L10n::t('Create a group of contacts/friends.'),
165 '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
167 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
173 // @TODO: Replace with parameter from router
174 if (($a->argc == 2) && ($a->argv[1] === 'none') ||
175 ($a->argc == 1) && ($a->argv[0] === 'nogroup')) {
180 'name' => L10n::t('Contacts not in any group'),
186 $context = $context + [
187 '$title' => $group['name'],
188 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
194 // @TODO: Replace with parameter from router
195 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
196 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
198 // @TODO: Replace with parameter from router
199 if (intval($a->argv[2])) {
200 if (!Model\Group::exists($a->argv[2], local_user())) {
201 notice(L10n::t('Group not found.'));
202 DI::baseUrl()->redirect('contact');
205 if (Model\Group::remove($a->argv[2])) {
206 info(L10n::t('Group removed.'));
208 notice(L10n::t('Unable to remove group.'));
211 DI::baseUrl()->redirect('group');
214 // @TODO: Replace with parameter from router
215 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
216 BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
218 if (DBA::exists('contact', ['id' => $a->argv[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) {
219 $change = intval($a->argv[2]);
223 // @TODO: Replace with parameter from router
224 if (($a->argc > 1) && intval($a->argv[1])) {
225 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user(), 'deleted' => false]);
226 if (!DBA::isResult($group)) {
227 notice(L10n::t('Group not found.'));
228 DI::baseUrl()->redirect('contact');
231 $members = Model\Contact::getByGroupId($group['id']);
234 if (count($members)) {
235 foreach ($members as $member) {
236 $preselected[] = $member['id'];
241 if (in_array($change, $preselected)) {
242 Model\Group::removeMember($group['id'], $change);
244 Model\Group::addMember($group['id'], $change);
247 $members = Model\Contact::getByGroupId($group['id']);
249 if (count($members)) {
250 foreach ($members as $member) {
251 $preselected[] = $member['id'];
256 $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
257 $drop_txt = Renderer::replaceMacros($drop_tpl, [
258 '$id' => $group['id'],
259 '$delete' => L10n::t('Delete Group'),
260 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
263 $context = $context + [
264 '$title' => $group['name'],
265 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
266 '$gid' => $group['id'],
267 '$drop' => $drop_txt,
268 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
269 '$edit_name' => L10n::t('Edit Group Name'),
274 if (!isset($group)) {
275 throw new \Friendica\Network\HTTPException\BadRequestException();
279 'label_members' => L10n::t('Members'),
281 'label_contacts' => L10n::t('All Contacts'),
282 'group_is_empty' => L10n::t('Group is empty'),
286 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
288 // Format the data of the group members
289 foreach ($members as $member) {
290 if ($member['url']) {
291 $entry = Contact::getContactTemplateVars($member);
292 $entry['label'] = 'members';
293 $entry['photo_menu'] = '';
294 $entry['change_member'] = [
295 'title' => L10n::t("Remove contact from group"),
296 'gid' => $group['id'],
297 'cid' => $member['id'],
298 'sec_token' => $sec_token
301 $groupeditor['members'][] = $entry;
303 Model\Group::removeMember($group['id'], $member['id']);
308 $contacts = Model\Contact::getUngroupedList(local_user());
310 $contacts_stmt = DBA::select('contact', [],
311 ['uid' => local_user(), 'pending' => false, 'blocked' => false, 'self' => false],
312 ['order' => ['name']]
314 $contacts = DBA::toArray($contacts_stmt);
315 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
318 if (DBA::isResult($contacts)) {
319 // Format the data of the contacts who aren't in the contact group
320 foreach ($contacts as $member) {
321 if (!in_array($member['id'], $preselected)) {
322 $entry = Contact::getContactTemplateVars($member);
323 $entry['label'] = 'contacts';
325 $entry['photo_menu'] = [];
328 $entry['change_member'] = [
329 'title' => L10n::t("Add contact to group"),
330 'gid' => $group['id'],
331 'cid' => $member['id'],
332 'sec_token' => $sec_token
336 $groupeditor['contacts'][] = $entry;
341 $context['$groupeditor'] = $groupeditor;
343 // If there are to many contacts we could provide an alternative view mode
344 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
345 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
348 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
349 echo Renderer::replaceMacros($tpl, $context);
353 return Renderer::replaceMacros($tpl, $context);