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\DBM;
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) ? intval($a->argv[1]) : 0));
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 (! DBM::is_result($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
89 $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
90 if (is_null($switchtotext)) {
91 $switchtotext = Config::get('system', 'groupedit_image_limit', 400);
94 $tpl = get_markup_template('group_edit.tpl');
97 '$submit' => L10n::t('Save Group'),
100 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
101 return replace_macros($tpl, $context + [
102 '$title' => L10n::t('Create a group of contacts/friends.'),
103 '$gname' => ['groupname', L10n::t('Group Name: '), '', ''],
105 '$form_security_token' => get_form_security_token("group_edit"),
111 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
112 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
114 if (intval($a->argv[2])) {
115 $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
122 if (DBM::is_result($r)) {
123 $result = Group::removeByName(local_user(), $r[0]['name']);
127 info(L10n::t('Group removed.') . EOL);
129 notice(L10n::t('Unable to remove group.') . EOL);
132 goaway(System::baseUrl() . '/group');
136 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
137 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
139 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
143 if (DBM::is_result($r)) {
144 $change = intval($a->argv[2]);
148 if (($a->argc > 1) && (intval($a->argv[1]))) {
149 require_once 'mod/contacts.php';
151 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
156 if (! DBM::is_result($r)) {
157 notice(L10n::t('Group not found.') . EOL);
158 goaway(System::baseUrl() . '/contacts');
162 $members = Contact::getByGroupId($group['id']);
167 if (count($members)) {
168 foreach ($members as $member) {
169 $preselected[] = $member['id'];
174 if (in_array($change, $preselected)) {
175 Group::removeMember($group['id'], $change);
177 Group::addMember($group['id'], $change);
180 $members = Contact::getByGroupId($group['id']);
182 if (count($members)) {
183 foreach ($members as $member) {
184 $preselected[] = $member['id'];
189 $drop_tpl = get_markup_template('group_drop.tpl');
190 $drop_txt = replace_macros($drop_tpl, [
191 '$id' => $group['id'],
192 '$delete' => L10n::t('Delete Group'),
193 '$form_security_token' => get_form_security_token("group_drop"),
197 $context = $context + [
198 '$title' => L10n::t('Group Editor'),
199 '$gname' => ['groupname', L10n::t('Group Name: '), $group['name'], ''],
200 '$gid' => $group['id'],
201 '$drop' => $drop_txt,
202 '$form_security_token' => get_form_security_token('group_edit'),
203 '$edit_name' => L10n::t('Edit Group Name')
208 if (! isset($group)) {
213 'label_members' => L10n::t('Members'),
215 'label_contacts' => L10n::t('All Contacts'),
216 'group_is_empty' => L10n::t('Group is empty'),
220 $sec_token = addslashes(get_form_security_token('group_member_change'));
222 // Format the data of the group members
223 foreach ($members as $member) {
224 if ($member['url']) {
225 $entry = _contact_detail_for_template($member);
226 $entry['label'] = 'members';
227 $entry['photo_menu'] = '';
228 $entry['change_member'] = [
229 'title' => L10n::t("Remove contact from group"),
230 'gid' => $group['id'],
231 'cid' => $member['id'],
232 'sec_token' => $sec_token
235 $groupeditor['members'][] = $entry;
237 Group::removeMember($group['id'], $member['id']);
241 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
245 if (DBM::is_result($r)) {
246 // Format the data of the contacts who aren't in the contact group
247 foreach ($r as $member) {
248 if (! in_array($member['id'], $preselected)) {
249 $entry = _contact_detail_for_template($member);
250 $entry['label'] = 'contacts';
251 $entry['photo_menu'] = '';
252 $entry['change_member'] = [
253 'title' => L10n::t("Add contact to group"),
254 'gid' => $group['id'],
255 'cid' => $member['id'],
256 'sec_token' => $sec_token
259 $groupeditor['contacts'][] = $entry;
264 $context['$groupeditor'] = $groupeditor;
265 $context['$desc'] = L10n::t('Click on a contact to add or remove.');
267 // If there are to many contacts we could provide an alternative view mode
268 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
269 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
272 $tpl = get_markup_template('groupeditor.tpl');
273 echo replace_macros($tpl, $context);
277 return replace_macros($tpl, $context);