3 * @file src/Module/Group.php
6 namespace Friendica\Module;
8 use Friendica\BaseModule;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
14 use Friendica\Util\Strings;
16 require_once 'boot.php';
18 class Group extends BaseModule
20 public static function post(array $parameters = [])
24 if (DI::mode()->isAjax()) {
29 notice(DI::l10n()->t('Permission denied.'));
30 DI::baseUrl()->redirect();
33 // @TODO: Replace with parameter from router
34 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
35 BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
37 $name = Strings::escapeTags(trim($_POST['groupname']));
38 $r = Model\Group::create(local_user(), $name);
40 info(DI::l10n()->t('Group created.'));
41 $r = Model\Group::getIdByName(local_user(), $name);
43 DI::baseUrl()->redirect('group/' . $r);
46 notice(DI::l10n()->t('Could not create group.'));
48 DI::baseUrl()->redirect('group');
51 // @TODO: Replace with parameter from router
52 if (($a->argc == 2) && intval($a->argv[1])) {
53 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
55 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user()]);
56 if (!DBA::isResult($group)) {
57 notice(DI::l10n()->t('Group not found.'));
58 DI::baseUrl()->redirect('contact');
60 $groupname = Strings::escapeTags(trim($_POST['groupname']));
61 if (strlen($groupname) && ($groupname != $group['name'])) {
62 if (Model\Group::update($group['id'], $groupname)) {
63 info(DI::l10n()->t('Group name changed.'));
69 public static function ajaxPost()
75 throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
78 // POST /group/123/add/123
79 // POST /group/123/remove/123
80 // @TODO: Replace with parameter from router
82 list($group_id, $command, $contact_id) = array_slice($a->argv, 1);
84 if (!Model\Group::exists($group_id, local_user())) {
85 throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
88 $contact = DBA::selectFirst('contact', ['deleted'], ['id' => $contact_id, 'uid' => local_user()]);
89 if (!DBA::isResult($contact)) {
90 throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
93 if ($contact['deleted']) {
94 throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
99 if (!Model\Group::addMember($group_id, $contact_id)) {
100 throw new \Exception(DI::l10n()->t('Unable to add the contact to the group.'), 500);
103 $message = DI::l10n()->t('Contact successfully added to group.');
106 if (!Model\Group::removeMember($group_id, $contact_id)) {
107 throw new \Exception(DI::l10n()->t('Unable to remove the contact from the group.'), 500);
110 $message = DI::l10n()->t('Contact successfully removed from group.');
113 throw new \Exception(DI::l10n()->t('Unknown group command.'), 400);
116 throw new \Exception(DI::l10n()->t('Bad request.'), 400);
120 System::jsonExit(['status' => 'OK', 'message' => $message]);
121 } catch (\Exception $e) {
122 notice($e->getMessage());
123 System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
127 public static function content(array $parameters = [])
132 throw new \Friendica\Network\HTTPException\ForbiddenException();
137 DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
139 // With no group number provided we jump to the unassigned contacts as a starting point
140 // @TODO: Replace with parameter from router
142 DI::baseUrl()->redirect('group/none');
145 // Switch to text mode interface if we have more than 'n' contacts or group members
146 $switchtotext = DI::pConfig()->get(local_user(), 'system', 'groupedit_image_limit');
147 if (is_null($switchtotext)) {
148 $switchtotext = DI::config()->get('system', 'groupedit_image_limit', 200);
151 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
155 '$submit' => DI::l10n()->t('Save Group'),
156 '$submit_filter' => DI::l10n()->t('Filter'),
159 // @TODO: Replace with parameter from router
160 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
161 return Renderer::replaceMacros($tpl, $context + [
162 '$title' => DI::l10n()->t('Create a group of contacts/friends.'),
163 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), '', ''],
165 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
171 // @TODO: Replace with parameter from router
172 if (($a->argc == 2) && ($a->argv[1] === 'none') ||
173 ($a->argc == 1) && ($a->argv[0] === 'nogroup')) {
178 'name' => DI::l10n()->t('Contacts not in any group'),
184 $context = $context + [
185 '$title' => $group['name'],
186 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
192 // @TODO: Replace with parameter from router
193 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
194 BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
196 // @TODO: Replace with parameter from router
197 if (intval($a->argv[2])) {
198 if (!Model\Group::exists($a->argv[2], local_user())) {
199 notice(DI::l10n()->t('Group not found.'));
200 DI::baseUrl()->redirect('contact');
203 if (Model\Group::remove($a->argv[2])) {
204 info(DI::l10n()->t('Group removed.'));
206 notice(DI::l10n()->t('Unable to remove group.'));
209 DI::baseUrl()->redirect('group');
212 // @TODO: Replace with parameter from router
213 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
214 BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
216 if (DBA::exists('contact', ['id' => $a->argv[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) {
217 $change = intval($a->argv[2]);
221 // @TODO: Replace with parameter from router
222 if (($a->argc > 1) && intval($a->argv[1])) {
223 $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user(), 'deleted' => false]);
224 if (!DBA::isResult($group)) {
225 notice(DI::l10n()->t('Group not found.'));
226 DI::baseUrl()->redirect('contact');
229 $members = Model\Contact::getByGroupId($group['id']);
232 if (count($members)) {
233 foreach ($members as $member) {
234 $preselected[] = $member['id'];
239 if (in_array($change, $preselected)) {
240 Model\Group::removeMember($group['id'], $change);
242 Model\Group::addMember($group['id'], $change);
245 $members = Model\Contact::getByGroupId($group['id']);
247 if (count($members)) {
248 foreach ($members as $member) {
249 $preselected[] = $member['id'];
254 $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
255 $drop_txt = Renderer::replaceMacros($drop_tpl, [
256 '$id' => $group['id'],
257 '$delete' => DI::l10n()->t('Delete Group'),
258 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
261 $context = $context + [
262 '$title' => $group['name'],
263 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
264 '$gid' => $group['id'],
265 '$drop' => $drop_txt,
266 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
267 '$edit_name' => DI::l10n()->t('Edit Group Name'),
272 if (!isset($group)) {
273 throw new \Friendica\Network\HTTPException\BadRequestException();
277 'label_members' => DI::l10n()->t('Members'),
279 'label_contacts' => DI::l10n()->t('All Contacts'),
280 'group_is_empty' => DI::l10n()->t('Group is empty'),
284 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
286 // Format the data of the group members
287 foreach ($members as $member) {
288 if ($member['url']) {
289 $entry = Contact::getContactTemplateVars($member);
290 $entry['label'] = 'members';
291 $entry['photo_menu'] = '';
292 $entry['change_member'] = [
293 'title' => DI::l10n()->t("Remove contact from group"),
294 'gid' => $group['id'],
295 'cid' => $member['id'],
296 'sec_token' => $sec_token
299 $groupeditor['members'][] = $entry;
301 Model\Group::removeMember($group['id'], $member['id']);
306 $contacts = Model\Contact::getUngroupedList(local_user());
308 $contacts_stmt = DBA::select('contact', [],
309 ['uid' => local_user(), 'pending' => false, 'blocked' => false, 'self' => false],
310 ['order' => ['name']]
312 $contacts = DBA::toArray($contacts_stmt);
313 $context['$desc'] = DI::l10n()->t('Click on a contact to add or remove.');
316 if (DBA::isResult($contacts)) {
317 // Format the data of the contacts who aren't in the contact group
318 foreach ($contacts as $member) {
319 if (!in_array($member['id'], $preselected)) {
320 $entry = Contact::getContactTemplateVars($member);
321 $entry['label'] = 'contacts';
323 $entry['photo_menu'] = [];
326 $entry['change_member'] = [
327 'title' => DI::l10n()->t("Add contact to group"),
328 'gid' => $group['id'],
329 'cid' => $member['id'],
330 'sec_token' => $sec_token
334 $groupeditor['contacts'][] = $entry;
339 $context['$groupeditor'] = $groupeditor;
341 // If there are to many contacts we could provide an alternative view mode
342 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
343 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
346 $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
347 echo Renderer::replaceMacros($tpl, $context);
351 return Renderer::replaceMacros($tpl, $context);