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;
14 use Friendica\Model\Contact;
15 use Friendica\Model\Group;
17 function group_init(App $a) {
19 $a->page['aside'] = Group::sidebarWidget('contacts', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone'));
23 function group_post(App $a) {
26 notice(L10n::t('Permission denied.') . EOL);
30 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
31 check_form_security_token_redirectOnErr('/group/new', 'group_edit');
33 $name = notags(trim($_POST['groupname']));
34 $r = Group::create(local_user(), $name);
36 info(L10n::t('Group created.') . EOL);
37 $r = Group::getIdByName(local_user(), $name);
39 goaway(System::baseUrl() . '/group/' . $r);
42 notice(L10n::t('Could not create group.') . EOL);
44 goaway(System::baseUrl() . '/group');
48 if (($a->argc == 2) && (intval($a->argv[1]))) {
49 check_form_security_token_redirectOnErr('/group', 'group_edit');
51 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
55 if (! DBA::isResult($r)) {
56 notice(L10n::t('Group not found.') . EOL);
57 goaway(System::baseUrl() . '/contacts');
61 $groupname = notags(trim($_POST['groupname']));
62 if ((strlen($groupname)) && ($groupname != $group['name'])) {
63 $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
70 info(L10n::t('Group name changed.') . EOL);
74 $a->page['aside'] = Group::sidebarWidget();
79 function group_content(App $a) {
83 notice(L10n::t('Permission denied') . EOL);
87 // Switch to text mode interface if we have more than 'n' contacts or group members
90 goaway(System::baseUrl() . '/contacts');
93 $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
94 if (is_null($switchtotext)) {
95 $switchtotext = Config::get('system', 'groupedit_image_limit', 400);
98 $tpl = get_markup_template('group_edit.tpl');
101 '$submit' => L10n::t('Save Group'),
102 '$submit_filter' => L10n::t('Filter'),
105 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
106 return replace_macros($tpl, $context + [
107 '$title' => L10n::t('Create a group of contacts/friends.'),
108 '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
110 '$form_security_token' => get_form_security_token("group_edit"),
116 if (($a->argc == 2) && ($a->argv[1] === 'none')) {
117 require_once 'mod/contacts.php';
123 'name' => L10n::t('Contacts not in any group'),
130 $context = $context + [
131 '$title' => $group['name'],
132 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
139 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
140 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
142 if (intval($a->argv[2])) {
143 $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
150 if (DBA::isResult($r)) {
151 $result = Group::removeByName(local_user(), $r[0]['name']);
155 info(L10n::t('Group removed.') . EOL);
157 notice(L10n::t('Unable to remove group.') . EOL);
160 goaway(System::baseUrl() . '/group');
164 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
165 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
167 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
171 if (DBA::isResult($r)) {
172 $change = intval($a->argv[2]);
176 if (($a->argc > 1) && (intval($a->argv[1]))) {
177 require_once 'mod/contacts.php';
179 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
184 if (! DBA::isResult($r)) {
185 notice(L10n::t('Group not found.') . EOL);
186 goaway(System::baseUrl() . '/contacts');
190 $members = Contact::getByGroupId($group['id']);
195 if (count($members)) {
196 foreach ($members as $member) {
197 $preselected[] = $member['id'];
202 if (in_array($change, $preselected)) {
203 Group::removeMember($group['id'], $change);
205 Group::addMember($group['id'], $change);
208 $members = Contact::getByGroupId($group['id']);
210 if (count($members)) {
211 foreach ($members as $member) {
212 $preselected[] = $member['id'];
217 $drop_tpl = get_markup_template('group_drop.tpl');
218 $drop_txt = replace_macros($drop_tpl, [
219 '$id' => $group['id'],
220 '$delete' => L10n::t('Delete Group'),
221 '$form_security_token' => get_form_security_token("group_drop"),
225 $context = $context + [
226 '$title' => $group['name'],
227 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
228 '$gid' => $group['id'],
229 '$drop' => $drop_txt,
230 '$form_security_token' => get_form_security_token('group_edit'),
231 '$edit_name' => L10n::t('Edit Group Name'),
237 if (! isset($group)) {
242 'label_members' => L10n::t('Members'),
244 'label_contacts' => L10n::t('All Contacts'),
245 'group_is_empty' => L10n::t('Group is empty'),
249 $sec_token = addslashes(get_form_security_token('group_member_change'));
251 // Format the data of the group members
252 foreach ($members as $member) {
253 if ($member['url']) {
254 $entry = _contact_detail_for_template($member);
255 $entry['label'] = 'members';
256 $entry['photo_menu'] = '';
257 $entry['change_member'] = [
258 'title' => L10n::t("Remove contact from group"),
259 'gid' => $group['id'],
260 'cid' => $member['id'],
261 'sec_token' => $sec_token
264 $groupeditor['members'][] = $entry;
266 Group::removeMember($group['id'], $member['id']);
271 $r = Contact::getUngroupedList(local_user());
273 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
276 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
279 if (DBA::isResult($r)) {
280 // Format the data of the contacts who aren't in the contact group
281 foreach ($r as $member) {
282 if (! in_array($member['id'], $preselected)) {
283 $entry = _contact_detail_for_template($member);
284 $entry['label'] = 'contacts';
286 $entry['photo_menu'] = [];
289 $entry['change_member'] = [
290 'title' => L10n::t("Add contact to group"),
291 'gid' => $group['id'],
292 'cid' => $member['id'],
293 'sec_token' => $sec_token
297 $groupeditor['contacts'][] = $entry;
302 $context['$groupeditor'] = $groupeditor;
304 // If there are to many contacts we could provide an alternative view mode
305 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
306 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
309 $tpl = get_markup_template('groupeditor.tpl');
310 echo replace_macros($tpl, $context);
314 return replace_macros($tpl, $context);