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\PConfig;
11 use Friendica\Core\System;
12 use Friendica\Database\DBM;
14 function group_init(App $a) {
16 require_once 'include/group.php';
17 $a->page['aside'] = group_side('contacts', 'group', 'extended', (($a->argc > 1) ? intval($a->argv[1]) : 0));
21 function group_post(App $a) {
24 notice(t('Permission denied.') . EOL);
28 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
29 check_form_security_token_redirectOnErr('/group/new', 'group_edit');
31 $name = notags(trim($_POST['groupname']));
32 $r = group_add(local_user(), $name);
34 info(t('Group created.') . EOL);
35 $r = group_byname(local_user(), $name);
37 goaway(System::baseUrl() . '/group/' . $r);
40 notice(t('Could not create group.') . EOL);
42 goaway(System::baseUrl() . '/group');
46 if (($a->argc == 2) && (intval($a->argv[1]))) {
47 check_form_security_token_redirectOnErr('/group', 'group_edit');
49 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
53 if (! DBM::is_result($r)) {
54 notice(t('Group not found.') . EOL);
55 goaway(System::baseUrl() . '/contacts');
59 $groupname = notags(trim($_POST['groupname']));
60 if ((strlen($groupname)) && ($groupname != $group['name'])) {
61 $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
68 info(t('Group name changed.') . EOL);
72 $a->page['aside'] = group_side();
77 function group_content(App $a) {
81 notice(t('Permission denied') . EOL);
85 // Switch to text mode interface if we have more than 'n' contacts or group members
87 $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
88 if ($switchtotext === false) {
89 $switchtotext = Config::get('system', 'groupedit_image_limit');
91 if ($switchtotext === false) {
95 $tpl = get_markup_template('group_edit.tpl');
98 '$submit' => t('Save Group'),
101 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
102 return replace_macros($tpl, $context + array(
103 '$title' => t('Create a group of contacts/friends.'),
104 '$gname' => array('groupname', t('Group Name: '), '', ''),
106 '$form_security_token' => get_form_security_token("group_edit"),
112 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
113 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
115 if (intval($a->argv[2])) {
116 $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
123 if (DBM::is_result($r)) {
124 $result = group_rmv(local_user(), $r[0]['name']);
128 info(t('Group removed.') . EOL);
130 notice(t('Unable to remove group.') . EOL);
133 goaway(System::baseUrl() . '/group');
137 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
138 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
140 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
144 if (DBM::is_result($r)) {
145 $change = intval($a->argv[2]);
149 if (($a->argc > 1) && (intval($a->argv[1]))) {
150 require_once 'include/acl_selectors.php';
151 require_once 'mod/contacts.php';
153 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
158 if (! DBM::is_result($r)) {
159 notice(t('Group not found.') . EOL);
160 goaway(System::baseUrl() . '/contacts');
164 $members = group_get_members($group['id']);
165 $preselected = array();
169 if (count($members)) {
170 foreach ($members as $member) {
171 $preselected[] = $member['id'];
176 if (in_array($change, $preselected)) {
177 group_rmv_member(local_user(), $group['name'], $change);
179 group_add_member(local_user(), $group['name'], $change);
182 $members = group_get_members($group['id']);
183 $preselected = array();
184 if (count($members)) {
185 foreach ($members as $member) {
186 $preselected[] = $member['id'];
191 $drop_tpl = get_markup_template('group_drop.tpl');
192 $drop_txt = replace_macros($drop_tpl, array(
193 '$id' => $group['id'],
194 '$delete' => t('Delete Group'),
195 '$form_security_token' => get_form_security_token("group_drop"),
199 $context = $context + array(
200 '$title' => t('Group Editor'),
201 '$gname' => array('groupname', t('Group Name: '), $group['name'], ''),
202 '$gid' => $group['id'],
203 '$drop' => $drop_txt,
204 '$form_security_token' => get_form_security_token('group_edit'),
205 '$edit_name' => t('Edit Group Name')
210 if (! isset($group)) {
214 $groupeditor = array(
215 'label_members' => t('Members'),
216 'members' => array(),
217 'label_contacts' => t('All Contacts'),
218 'group_is_empty' => t('Group is empty'),
219 'contacts' => array(),
222 $sec_token = addslashes(get_form_security_token('group_member_change'));
224 // Format the data of the group members
225 foreach ($members as $member) {
226 if ($member['url']) {
227 $entry = _contact_detail_for_template($member);
228 $entry['label'] = 'members';
229 $entry['photo_menu'] = '';
230 $entry['change_member'] = array(
231 'title' => t("Remove Contact"),
232 'gid' => $group['id'],
233 'cid' => $member['id'],
234 'sec_token' => $sec_token
237 $groupeditor['members'][] = $entry;
239 group_rmv_member(local_user(), $group['name'], $member['id']);
243 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
247 if (DBM::is_result($r)) {
248 // Format the data of the contacts who aren't in the contact group
249 foreach ($r as $member) {
250 if (! in_array($member['id'], $preselected)) {
251 $entry = _contact_detail_for_template($member);
252 $entry['label'] = 'contacts';
253 $entry['photo_menu'] = '';
254 $entry['change_member'] = array(
255 'title' => t("Add Contact"),
256 'gid' => $group['id'],
257 'cid' => $member['id'],
258 'sec_token' => $sec_token
261 $groupeditor['contacts'][] = $entry;
266 $context['$groupeditor'] = $groupeditor;
267 $context['$desc'] = t('Click on a contact to add or remove.');
269 // If there are to many contacts we could provide an alternative view mode
270 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
271 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
274 $tpl = get_markup_template('groupeditor.tpl');
275 echo replace_macros($tpl, $context);
279 return replace_macros($tpl, $context);