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\PConfig;
12 use Friendica\Core\Renderer;
13 use Friendica\Core\System;
14 use Friendica\Database\DBA;
17 use Friendica\Util\Strings;
19 require_once 'boot.php';
21 class Group extends BaseModule
23 public static function post(array $parameters = [])
27 if (DI::mode()->isAjax()) {
32 notice(L10n::t('Permission denied.'));
33 DI::baseUrl()->redirect();
36 // @TODO: Replace with parameter from router
37 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
38 BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
40 $name = Strings::escapeTags(trim($_POST['groupname']));
41 $r = Model\Group::create(local_user(), $name);
43 info(L10n::t('Group created.'));
44 $r = Model\Group::getIdByName(local_user(), $name);
46 DI::baseUrl()->redirect('group/' . $r);
49 notice(L10n::t('Could not create group.'));
51 DI::baseUrl()->redirect('group');
54 // @TODO: Replace with parameter from router
55 if (($a->argc == 2) && intval($a->argv[1])) {
56 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
58 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user()]);
59 if (!DBA::isResult($group)) {
60 notice(L10n::t('Group not found.'));
61 DI::baseUrl()->redirect('contact');
63 $groupname = Strings::escapeTags(trim($_POST['groupname']));
64 if (strlen($groupname) && ($groupname != $group['name'])) {
65 if (Model\Group::update($group['id'], $groupname)) {
66 info(L10n::t('Group name changed.'));
72 public static function ajaxPost()
78 throw new \Exception(L10n::t('Permission denied.'), 403);
81 // POST /group/123/add/123
82 // POST /group/123/remove/123
83 // @TODO: Replace with parameter from router
85 list($group_id, $command, $contact_id) = array_slice($a->argv, 1);
87 if (!Model\Group::exists($group_id, local_user())) {
88 throw new \Exception(L10n::t('Unknown group.'), 404);
91 $contact = DBA::selectFirst('contact', ['deleted'], ['id' => $contact_id, 'uid' => local_user()]);
92 if (!DBA::isResult($contact)) {
93 throw new \Exception(L10n::t('Contact not found.'), 404);
96 if ($contact['deleted']) {
97 throw new \Exception(L10n::t('Contact is deleted.'), 410);
102 if (!Model\Group::addMember($group_id, $contact_id)) {
103 throw new \Exception(L10n::t('Unable to add the contact to the group.'), 500);
106 $message = L10n::t('Contact successfully added to group.');
109 if (!Model\Group::removeMember($group_id, $contact_id)) {
110 throw new \Exception(L10n::t('Unable to remove the contact from the group.'), 500);
113 $message = L10n::t('Contact successfully removed from group.');
116 throw new \Exception(L10n::t('Unknown group command.'), 400);
119 throw new \Exception(L10n::t('Bad request.'), 400);
123 System::jsonExit(['status' => 'OK', 'message' => $message]);
124 } catch (\Exception $e) {
125 notice($e->getMessage());
126 System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
130 public static function content(array $parameters = [])
135 throw new \Friendica\Network\HTTPException\ForbiddenException();
140 $a->page['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
142 // With no group number provided we jump to the unassigned contacts as a starting point
143 // @TODO: Replace with parameter from router
145 DI::baseUrl()->redirect('group/none');
148 // Switch to text mode interface if we have more than 'n' contacts or group members
149 $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
150 if (is_null($switchtotext)) {
151 $switchtotext = Config::get('system', 'groupedit_image_limit', 200);
154 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
158 '$submit' => L10n::t('Save Group'),
159 '$submit_filter' => L10n::t('Filter'),
162 // @TODO: Replace with parameter from router
163 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
164 return Renderer::replaceMacros($tpl, $context + [
165 '$title' => L10n::t('Create a group of contacts/friends.'),
166 '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
168 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
174 // @TODO: Replace with parameter from router
175 if (($a->argc == 2) && ($a->argv[1] === 'none') ||
176 ($a->argc == 1) && ($a->argv[0] === 'nogroup')) {
181 'name' => L10n::t('Contacts not in any group'),
187 $context = $context + [
188 '$title' => $group['name'],
189 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
195 // @TODO: Replace with parameter from router
196 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
197 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
199 // @TODO: Replace with parameter from router
200 if (intval($a->argv[2])) {
201 if (!Model\Group::exists($a->argv[2], local_user())) {
202 notice(L10n::t('Group not found.'));
203 DI::baseUrl()->redirect('contact');
206 if (Model\Group::remove($a->argv[2])) {
207 info(L10n::t('Group removed.'));
209 notice(L10n::t('Unable to remove group.'));
212 DI::baseUrl()->redirect('group');
215 // @TODO: Replace with parameter from router
216 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
217 BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
219 if (DBA::exists('contact', ['id' => $a->argv[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) {
220 $change = intval($a->argv[2]);
224 // @TODO: Replace with parameter from router
225 if (($a->argc > 1) && intval($a->argv[1])) {
226 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user(), 'deleted' => false]);
227 if (!DBA::isResult($group)) {
228 notice(L10n::t('Group not found.'));
229 DI::baseUrl()->redirect('contact');
232 $members = Model\Contact::getByGroupId($group['id']);
235 if (count($members)) {
236 foreach ($members as $member) {
237 $preselected[] = $member['id'];
242 if (in_array($change, $preselected)) {
243 Model\Group::removeMember($group['id'], $change);
245 Model\Group::addMember($group['id'], $change);
248 $members = Model\Contact::getByGroupId($group['id']);
250 if (count($members)) {
251 foreach ($members as $member) {
252 $preselected[] = $member['id'];
257 $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
258 $drop_txt = Renderer::replaceMacros($drop_tpl, [
259 '$id' => $group['id'],
260 '$delete' => L10n::t('Delete Group'),
261 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
264 $context = $context + [
265 '$title' => $group['name'],
266 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
267 '$gid' => $group['id'],
268 '$drop' => $drop_txt,
269 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
270 '$edit_name' => L10n::t('Edit Group Name'),
275 if (!isset($group)) {
276 throw new \Friendica\Network\HTTPException\BadRequestException();
280 'label_members' => L10n::t('Members'),
282 'label_contacts' => L10n::t('All Contacts'),
283 'group_is_empty' => L10n::t('Group is empty'),
287 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
289 // Format the data of the group members
290 foreach ($members as $member) {
291 if ($member['url']) {
292 $entry = Contact::getContactTemplateVars($member);
293 $entry['label'] = 'members';
294 $entry['photo_menu'] = '';
295 $entry['change_member'] = [
296 'title' => L10n::t("Remove contact from group"),
297 'gid' => $group['id'],
298 'cid' => $member['id'],
299 'sec_token' => $sec_token
302 $groupeditor['members'][] = $entry;
304 Model\Group::removeMember($group['id'], $member['id']);
309 $contacts = Model\Contact::getUngroupedList(local_user());
311 $contacts_stmt = DBA::select('contact', [],
312 ['uid' => local_user(), 'pending' => false, 'blocked' => false, 'self' => false],
313 ['order' => ['name']]
315 $contacts = DBA::toArray($contacts_stmt);
316 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
319 if (DBA::isResult($contacts)) {
320 // Format the data of the contacts who aren't in the contact group
321 foreach ($contacts as $member) {
322 if (!in_array($member['id'], $preselected)) {
323 $entry = Contact::getContactTemplateVars($member);
324 $entry['label'] = 'contacts';
326 $entry['photo_menu'] = [];
329 $entry['change_member'] = [
330 'title' => L10n::t("Add contact to group"),
331 'gid' => $group['id'],
332 'cid' => $member['id'],
333 'sec_token' => $sec_token
337 $groupeditor['contacts'][] = $entry;
342 $context['$groupeditor'] = $groupeditor;
344 // If there are to many contacts we could provide an alternative view mode
345 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
346 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
349 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
350 echo Renderer::replaceMacros($tpl, $context);
354 return Renderer::replaceMacros($tpl, $context);