4 * @brief The group module (create and rename contact groups, add and
5 * remove contacts to the contact groups
9 use Friendica\Core\System;
11 function group_init(App $a) {
13 require_once 'include/group.php';
14 $a->page['aside'] = group_side('contacts', 'group', 'extended', (($a->argc > 1) ? intval($a->argv[1]) : 0));
18 function group_post(App $a) {
21 notice(t('Permission denied.') . EOL);
25 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
26 check_form_security_token_redirectOnErr('/group/new', 'group_edit');
28 $name = notags(trim($_POST['groupname']));
29 $r = group_add(local_user(), $name);
31 info(t('Group created.') . EOL);
32 $r = group_byname(local_user(), $name);
34 goaway(System::baseUrl() . '/group/' . $r);
37 notice(t('Could not create group.') . EOL);
39 goaway(System::baseUrl() . '/group');
43 if (($a->argc == 2) && (intval($a->argv[1]))) {
44 check_form_security_token_redirectOnErr('/group', 'group_edit');
46 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
50 if (! dbm::is_result($r)) {
51 notice(t('Group not found.') . EOL);
52 goaway(System::baseUrl() . '/contacts');
56 $groupname = notags(trim($_POST['groupname']));
57 if ((strlen($groupname)) && ($groupname != $group['name'])) {
58 $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
65 info(t('Group name changed.') . EOL);
69 $a->page['aside'] = group_side();
74 function group_content(App $a) {
78 notice(t('Permission denied') . EOL);
82 // Switch to text mode interface if we have more than 'n' contacts or group members
84 $switchtotext = get_pconfig(local_user(), 'system', 'groupedit_image_limit');
85 if ($switchtotext === false) {
86 $switchtotext = get_config('system', 'groupedit_image_limit');
88 if ($switchtotext === false) {
92 $tpl = get_markup_template('group_edit.tpl');
95 '$submit' => t('Save Group'),
98 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
99 return replace_macros($tpl, $context + array(
100 '$title' => t('Create a group of contacts/friends.'),
101 '$gname' => array('groupname', t('Group Name: '), '', ''),
103 '$form_security_token' => get_form_security_token("group_edit"),
109 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
110 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
112 if (intval($a->argv[2])) {
113 $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
120 if (dbm::is_result($r)) {
121 $result = group_rmv(local_user(), $r[0]['name']);
125 info(t('Group removed.') . EOL);
127 notice(t('Unable to remove group.') . EOL);
130 goaway(System::baseUrl() . '/group');
134 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
135 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
137 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
141 if (dbm::is_result($r)) {
142 $change = intval($a->argv[2]);
146 if (($a->argc > 1) && (intval($a->argv[1]))) {
147 require_once 'include/acl_selectors.php';
148 require_once 'mod/contacts.php';
150 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
155 if (! dbm::is_result($r)) {
156 notice(t('Group not found.') . EOL);
157 goaway(System::baseUrl() . '/contacts');
161 $members = group_get_members($group['id']);
162 $preselected = array();
166 if (count($members)) {
167 foreach ($members as $member) {
168 $preselected[] = $member['id'];
173 if (in_array($change, $preselected)) {
174 group_rmv_member(local_user(), $group['name'], $change);
176 group_add_member(local_user(), $group['name'], $change);
179 $members = group_get_members($group['id']);
180 $preselected = array();
181 if (count($members)) {
182 foreach ($members as $member) {
183 $preselected[] = $member['id'];
188 $drop_tpl = get_markup_template('group_drop.tpl');
189 $drop_txt = replace_macros($drop_tpl, array(
190 '$id' => $group['id'],
191 '$delete' => t('Delete Group'),
192 '$form_security_token' => get_form_security_token("group_drop"),
196 $context = $context + array(
197 '$title' => t('Group Editor'),
198 '$gname' => array('groupname', t('Group Name: '), $group['name'], ''),
199 '$gid' => $group['id'],
200 '$drop' => $drop_txt,
201 '$form_security_token' => get_form_security_token('group_edit'),
202 '$edit_name' => t('Edit Group Name')
207 if (! isset($group)) {
211 $groupeditor = array(
212 'label_members' => t('Members'),
213 'members' => array(),
214 'label_contacts' => t('All Contacts'),
215 'group_is_empty' => t('Group is empty'),
216 'contacts' => array(),
219 $sec_token = addslashes(get_form_security_token('group_member_change'));
221 // Format the data of the group members
222 foreach ($members as $member) {
223 if ($member['url']) {
224 $entry = _contact_detail_for_template($member);
225 $entry['label'] = 'members';
226 $entry['photo_menu'] = '';
227 $entry['change_member'] = array(
228 'title' => t("Remove Contact"),
229 'gid' => $group['id'],
230 'cid' => $member['id'],
231 'sec_token' => $sec_token
234 $groupeditor['members'][] = $entry;
236 group_rmv_member(local_user(), $group['name'], $member['id']);
240 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
244 if (dbm::is_result($r)) {
245 // Format the data of the contacts who aren't in the contact group
246 foreach ($r as $member) {
247 if (! in_array($member['id'], $preselected)) {
248 $entry = _contact_detail_for_template($member);
249 $entry['label'] = 'contacts';
250 $entry['photo_menu'] = '';
251 $entry['change_member'] = array(
252 'title' => t("Add Contact"),
253 'gid' => $group['id'],
254 'cid' => $member['id'],
255 'sec_token' => $sec_token
258 $groupeditor['contacts'][] = $entry;
263 $context['$groupeditor'] = $groupeditor;
264 $context['$desc'] = t('Click on a contact to add or remove.');
266 // If there are to many contacts we could provide an alternative view mode
267 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
268 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
271 $tpl = get_markup_template('groupeditor.tpl');
272 echo replace_macros($tpl, $context);
276 return replace_macros($tpl, $context);