4 * @brief The group module (create and rename contact groups, add and
5 * remove contacts to the contact groups
10 function group_init(App $a) {
12 require_once 'include/group.php';
13 $a->page['aside'] = group_side('contacts', 'group', 'extended', (($a->argc > 1) ? intval($a->argv[1]) : 0));
17 function group_post(App $a) {
20 notice(t('Permission denied.') . EOL);
24 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
25 check_form_security_token_redirectOnErr('/group/new', 'group_edit');
27 $name = notags(trim($_POST['groupname']));
28 $r = group_add(local_user(), $name);
30 info(t('Group created.') . EOL);
31 $r = group_byname(local_user(), $name);
33 goaway(App::get_baseurl() . '/group/' . $r);
36 notice(t('Could not create group.') . EOL);
38 goaway(App::get_baseurl() . '/group');
42 if (($a->argc == 2) && (intval($a->argv[1]))) {
43 check_form_security_token_redirectOnErr('/group', 'group_edit');
45 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
49 if (! dbm::is_result($r)) {
50 notice(t('Group not found.') . EOL);
51 goaway(App::get_baseurl() . '/contacts');
55 $groupname = notags(trim($_POST['groupname']));
56 if ((strlen($groupname)) && ($groupname != $group['name'])) {
57 $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
64 info(t('Group name changed.') . EOL);
68 $a->page['aside'] = group_side();
73 function group_content(App $a) {
77 notice(t('Permission denied') . EOL);
81 // Switch to text mode interface if we have more than 'n' contacts or group members
83 $switchtotext = get_pconfig(local_user(), 'system', 'groupedit_image_limit');
84 if ($switchtotext === false) {
85 $switchtotext = get_config('system', 'groupedit_image_limit');
87 if ($switchtotext === false) {
91 $tpl = get_markup_template('group_edit.tpl');
94 '$submit' => t('Save Group'),
97 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
98 return replace_macros($tpl, $context + array(
99 '$title' => t('Create a group of contacts/friends.'),
100 '$gname' => array('groupname', t('Group Name: '), '', ''),
102 '$form_security_token' => get_form_security_token("group_edit"),
108 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
109 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
111 if (intval($a->argv[2])) {
112 $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
119 if (dbm::is_result($r)) {
120 $result = group_rmv(local_user(), $r[0]['name']);
124 info(t('Group removed.') . EOL);
126 notice(t('Unable to remove group.') . EOL);
129 goaway(App::get_baseurl() . '/group');
133 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
134 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
136 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
140 if (dbm::is_result($r)) {
141 $change = intval($a->argv[2]);
145 if (($a->argc > 1) && (intval($a->argv[1]))) {
146 require_once 'include/acl_selectors.php';
147 require_once 'mod/contacts.php';
149 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
154 if (! dbm::is_result($r)) {
155 notice(t('Group not found.') . EOL);
156 goaway(App::get_baseurl() . '/contacts');
160 $members = group_get_members($group['id']);
161 $preselected = array();
165 if (count($members)) {
166 foreach ($members as $member) {
167 $preselected[] = $member['id'];
172 if (in_array($change, $preselected)) {
173 group_rmv_member(local_user(), $group['name'], $change);
175 group_add_member(local_user(), $group['name'], $change);
178 $members = group_get_members($group['id']);
179 $preselected = array();
180 if (count($members)) {
181 foreach ($members as $member) {
182 $preselected[] = $member['id'];
187 $drop_tpl = get_markup_template('group_drop.tpl');
188 $drop_txt = replace_macros($drop_tpl, array(
189 '$id' => $group['id'],
190 '$delete' => t('Delete Group'),
191 '$form_security_token' => get_form_security_token("group_drop"),
195 $context = $context + array(
196 '$title' => t('Group Editor'),
197 '$gname' => array('groupname', t('Group Name: '), $group['name'], ''),
198 '$gid' => $group['id'],
199 '$drop' => $drop_txt,
200 '$form_security_token' => get_form_security_token('group_edit'),
201 '$edit_name' => t('Edit Group Name')
206 if (! isset($group)) {
210 $groupeditor = array(
211 'label_members' => t('Members'),
212 'members' => array(),
213 'label_contacts' => t('All Contacts'),
214 'group_is_empty' => t('Group is empty'),
215 'contacts' => array(),
218 $sec_token = addslashes(get_form_security_token('group_member_change'));
220 // Format the data of the group members
221 foreach ($members as $member) {
222 if ($member['url']) {
223 $entry = _contact_detail_for_template($member);
224 $entry['label'] = 'members';
225 $entry['photo_menu'] = '';
226 $entry['change_member'] = array(
227 'title' => t("Remove Contact"),
228 'gid' => $group['id'],
229 'cid' => $member['id'],
230 'sec_token' => $sec_token
233 $groupeditor['members'][] = $entry;
235 group_rmv_member(local_user(), $group['name'], $member['id']);
239 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
243 if (dbm::is_result($r)) {
244 // Format the data of the contacts who aren't in the contact group
245 foreach ($r as $member) {
246 if (! in_array($member['id'], $preselected)) {
247 $entry = _contact_detail_for_template($member);
248 $entry['label'] = 'contacts';
249 $entry['photo_menu'] = '';
250 $entry['change_member'] = array(
251 'title' => t("Add Contact"),
252 'gid' => $group['id'],
253 'cid' => $member['id'],
254 'sec_token' => $sec_token
257 $groupeditor['contacts'][] = $entry;
262 $context['$groupeditor'] = $groupeditor;
263 $context['$desc'] = t('Click on a contact to add or remove.');
265 // If there are to many contacts we could provide an alternative view mode
266 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
267 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
270 $tpl = get_markup_template('groupeditor.tpl');
271 echo replace_macros($tpl, $context);
275 return replace_macros($tpl, $context);