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;
13 function group_init(App $a) {
15 require_once 'include/group.php';
16 $a->page['aside'] = group_side('contacts', 'group', 'extended', (($a->argc > 1) ? intval($a->argv[1]) : 0));
20 function group_post(App $a) {
23 notice(t('Permission denied.') . EOL);
27 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
28 check_form_security_token_redirectOnErr('/group/new', 'group_edit');
30 $name = notags(trim($_POST['groupname']));
31 $r = group_add(local_user(), $name);
33 info(t('Group created.') . EOL);
34 $r = group_byname(local_user(), $name);
36 goaway(System::baseUrl() . '/group/' . $r);
39 notice(t('Could not create group.') . EOL);
41 goaway(System::baseUrl() . '/group');
45 if (($a->argc == 2) && (intval($a->argv[1]))) {
46 check_form_security_token_redirectOnErr('/group', 'group_edit');
48 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
52 if (! dbm::is_result($r)) {
53 notice(t('Group not found.') . EOL);
54 goaway(System::baseUrl() . '/contacts');
58 $groupname = notags(trim($_POST['groupname']));
59 if ((strlen($groupname)) && ($groupname != $group['name'])) {
60 $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
67 info(t('Group name changed.') . EOL);
71 $a->page['aside'] = group_side();
76 function group_content(App $a) {
80 notice(t('Permission denied') . EOL);
84 // Switch to text mode interface if we have more than 'n' contacts or group members
86 $switchtotext = PConfig::get(local_user(), 'system', 'groupedit_image_limit');
87 if ($switchtotext === false) {
88 $switchtotext = Config::get('system', 'groupedit_image_limit');
90 if ($switchtotext === false) {
94 $tpl = get_markup_template('group_edit.tpl');
97 '$submit' => t('Save Group'),
100 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
101 return replace_macros($tpl, $context + array(
102 '$title' => t('Create a group of contacts/friends.'),
103 '$gname' => array('groupname', 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_rmv(local_user(), $r[0]['name']);
127 info(t('Group removed.') . EOL);
129 notice(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 'include/acl_selectors.php';
150 require_once 'mod/contacts.php';
152 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
157 if (! dbm::is_result($r)) {
158 notice(t('Group not found.') . EOL);
159 goaway(System::baseUrl() . '/contacts');
163 $members = group_get_members($group['id']);
164 $preselected = array();
168 if (count($members)) {
169 foreach ($members as $member) {
170 $preselected[] = $member['id'];
175 if (in_array($change, $preselected)) {
176 group_rmv_member(local_user(), $group['name'], $change);
178 group_add_member(local_user(), $group['name'], $change);
181 $members = group_get_members($group['id']);
182 $preselected = array();
183 if (count($members)) {
184 foreach ($members as $member) {
185 $preselected[] = $member['id'];
190 $drop_tpl = get_markup_template('group_drop.tpl');
191 $drop_txt = replace_macros($drop_tpl, array(
192 '$id' => $group['id'],
193 '$delete' => t('Delete Group'),
194 '$form_security_token' => get_form_security_token("group_drop"),
198 $context = $context + array(
199 '$title' => t('Group Editor'),
200 '$gname' => array('groupname', t('Group Name: '), $group['name'], ''),
201 '$gid' => $group['id'],
202 '$drop' => $drop_txt,
203 '$form_security_token' => get_form_security_token('group_edit'),
204 '$edit_name' => t('Edit Group Name')
209 if (! isset($group)) {
213 $groupeditor = array(
214 'label_members' => t('Members'),
215 'members' => array(),
216 'label_contacts' => t('All Contacts'),
217 'group_is_empty' => t('Group is empty'),
218 'contacts' => array(),
221 $sec_token = addslashes(get_form_security_token('group_member_change'));
223 // Format the data of the group members
224 foreach ($members as $member) {
225 if ($member['url']) {
226 $entry = _contact_detail_for_template($member);
227 $entry['label'] = 'members';
228 $entry['photo_menu'] = '';
229 $entry['change_member'] = array(
230 'title' => t("Remove Contact"),
231 'gid' => $group['id'],
232 'cid' => $member['id'],
233 'sec_token' => $sec_token
236 $groupeditor['members'][] = $entry;
238 group_rmv_member(local_user(), $group['name'], $member['id']);
242 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
246 if (dbm::is_result($r)) {
247 // Format the data of the contacts who aren't in the contact group
248 foreach ($r as $member) {
249 if (! in_array($member['id'], $preselected)) {
250 $entry = _contact_detail_for_template($member);
251 $entry['label'] = 'contacts';
252 $entry['photo_menu'] = '';
253 $entry['change_member'] = array(
254 'title' => t("Add Contact"),
255 'gid' => $group['id'],
256 'cid' => $member['id'],
257 'sec_token' => $sec_token
260 $groupeditor['contacts'][] = $entry;
265 $context['$groupeditor'] = $groupeditor;
266 $context['$desc'] = t('Click on a contact to add or remove.');
268 // If there are to many contacts we could provide an alternative view mode
269 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
270 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
273 $tpl = get_markup_template('groupeditor.tpl');
274 echo replace_macros($tpl, $context);
278 return replace_macros($tpl, $context);