4 * @brief The group module (create and rename contact groups, add and
5 * remove contacts to the contact groups
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
16 use Friendica\Util\Security;
18 function group_init(App $a) {
20 $a->page['aside'] = Model\Group::sidebarWidget('contacts', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
24 function group_post(App $a) {
27 notice(L10n::t('Permission denied.') . EOL);
31 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
32 Security::check_form_security_token_redirectOnErr('/group/new', 'group_edit');
34 $name = notags(trim($_POST['groupname']));
35 $r = Model\Group::create(local_user(), $name);
37 info(L10n::t('Group created.') . EOL);
38 $r = Model\Group::getIdByName(local_user(), $name);
40 goaway(System::baseUrl() . '/group/' . $r);
43 notice(L10n::t('Could not create group.') . EOL);
45 goaway(System::baseUrl() . '/group');
49 if (($a->argc == 2) && intval($a->argv[1])) {
50 Security::check_form_security_token_redirectOnErr('/group', 'group_edit');
52 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
56 if (!DBA::isResult($r)) {
57 notice(L10n::t('Group not found.') . EOL);
58 goaway(System::baseUrl() . '/contact');
62 $groupname = notags(trim($_POST['groupname']));
63 if (strlen($groupname) && ($groupname != $group['name'])) {
64 $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
65 DBA::escape($groupname),
71 info(L10n::t('Group name changed.') . EOL);
75 $a->page['aside'] = Model\Group::sidebarWidget();
80 function group_content(App $a) {
84 notice(L10n::t('Permission denied') . EOL);
88 // With no group number provided we jump to the unassigned contacts as a starting point
93 // Switch to text mode interface if we have more than 'n' contacts or group members
94 $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
95 if (is_null($switchtotext)) {
96 $switchtotext = Config::get('system', 'groupedit_image_limit', 400);
99 $tpl = get_markup_template('group_edit.tpl');
102 '$submit' => L10n::t('Save Group'),
103 '$submit_filter' => L10n::t('Filter'),
106 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
107 return replace_macros($tpl, $context + [
108 '$title' => L10n::t('Create a group of contacts/friends.'),
109 '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
111 '$form_security_token' => Security::get_form_security_token("group_edit"),
119 if (($a->argc == 2) && ($a->argv[1] === 'none')) {
124 'name' => L10n::t('Contacts not in any group'),
131 $context = $context + [
132 '$title' => $group['name'],
133 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
140 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
141 Security::check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
143 if (intval($a->argv[2])) {
144 $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
151 if (DBA::isResult($r)) {
152 $result = Model\Group::removeByName(local_user(), $r[0]['name']);
156 info(L10n::t('Group removed.') . EOL);
158 notice(L10n::t('Unable to remove group.') . EOL);
161 goaway(System::baseUrl() . '/group');
165 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
166 Security::check_form_security_token_ForbiddenOnErr('group_member_change', 't');
168 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
172 if (DBA::isResult($r)) {
173 $change = intval($a->argv[2]);
177 if (($a->argc > 1) && intval($a->argv[1])) {
178 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
183 if (!DBA::isResult($r)) {
184 notice(L10n::t('Group not found.') . EOL);
185 goaway(System::baseUrl() . '/contact');
189 $members = Model\Contact::getByGroupId($group['id']);
194 if (count($members)) {
195 foreach ($members as $member) {
196 $preselected[] = $member['id'];
201 if (in_array($change, $preselected)) {
202 Model\Group::removeMember($group['id'], $change);
204 Model\Group::addMember($group['id'], $change);
207 $members = Model\Contact::getByGroupId($group['id']);
209 if (count($members)) {
210 foreach ($members as $member) {
211 $preselected[] = $member['id'];
216 $drop_tpl = get_markup_template('group_drop.tpl');
217 $drop_txt = replace_macros($drop_tpl, [
218 '$id' => $group['id'],
219 '$delete' => L10n::t('Delete Group'),
220 '$form_security_token' => Security::get_form_security_token("group_drop"),
224 $context = $context + [
225 '$title' => $group['name'],
226 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
227 '$gid' => $group['id'],
228 '$drop' => $drop_txt,
229 '$form_security_token' => Security::get_form_security_token('group_edit'),
230 '$edit_name' => L10n::t('Edit Group Name'),
236 if (!isset($group)) {
241 'label_members' => L10n::t('Members'),
243 'label_contacts' => L10n::t('All Contacts'),
244 'group_is_empty' => L10n::t('Group is empty'),
248 $sec_token = addslashes(Security::get_form_security_token('group_member_change'));
250 // Format the data of the group members
251 foreach ($members as $member) {
252 if ($member['url']) {
253 $entry = Module\Contact::getContactTemplateVars($member);
254 $entry['label'] = 'members';
255 $entry['photo_menu'] = '';
256 $entry['change_member'] = [
257 'title' => L10n::t("Remove contact from group"),
258 'gid' => $group['id'],
259 'cid' => $member['id'],
260 'sec_token' => $sec_token
263 $groupeditor['members'][] = $entry;
265 Model\Group::removeMember($group['id'], $member['id']);
270 $r = Model\Contact::getUngroupedList(local_user());
272 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
275 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
278 if (DBA::isResult($r)) {
279 // Format the data of the contacts who aren't in the contact group
280 foreach ($r as $member) {
281 if (!in_array($member['id'], $preselected)) {
282 $entry = Module\Contact::getContactTemplateVars($member);
283 $entry['label'] = 'contacts';
285 $entry['photo_menu'] = [];
288 $entry['change_member'] = [
289 'title' => L10n::t("Add contact to group"),
290 'gid' => $group['id'],
291 'cid' => $member['id'],
292 'sec_token' => $sec_token
296 $groupeditor['contacts'][] = $entry;
301 $context['$groupeditor'] = $groupeditor;
303 // If there are to many contacts we could provide an alternative view mode
304 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
305 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
308 $tpl = get_markup_template('groupeditor.tpl');
309 echo replace_macros($tpl, $context);
313 return replace_macros($tpl, $context);