4 * @brief The group module (create and rename contact groups, add and
5 * remove contacts to the contact groups
9 function group_init(App $a) {
11 require_once 'include/group.php';
12 $a->page['aside'] = group_side('contacts', 'group', 'extended', (($a->argc > 1) ? intval($a->argv[1]) : 0));
16 function group_post(App $a) {
19 notice(t('Permission denied.') . EOL);
23 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
24 check_form_security_token_redirectOnErr('/group/new', 'group_edit');
26 $name = notags(trim($_POST['groupname']));
27 $r = group_add(local_user(), $name);
29 info(t('Group created.') . EOL);
30 $r = group_byname(local_user(), $name);
32 goaway(App::get_baseurl() . '/group/' . $r);
35 notice(t('Could not create group.') . EOL);
37 goaway(App::get_baseurl() . '/group');
41 if (($a->argc == 2) && (intval($a->argv[1]))) {
42 check_form_security_token_redirectOnErr('/group', 'group_edit');
44 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
48 if (! dbm::is_result($r)) {
49 notice(t('Group not found.') . EOL);
50 goaway(App::get_baseurl() . '/contacts');
54 $groupname = notags(trim($_POST['groupname']));
55 if ((strlen($groupname)) && ($groupname != $group['name'])) {
56 $r = q("UPDATE `group` SET `name` = '%s' WHERE `uid` = %d AND `id` = %d",
63 info(t('Group name changed.') . EOL);
67 $a->page['aside'] = group_side();
72 function group_content(App $a) {
76 notice(t('Permission denied') . EOL);
80 // Switch to text mode interface if we have more than 'n' contacts or group members
82 $switchtotext = get_pconfig(local_user(), 'system', 'groupedit_image_limit');
83 if ($switchtotext === false) {
84 $switchtotext = get_config('system', 'groupedit_image_limit');
86 if ($switchtotext === false) {
90 $tpl = get_markup_template('group_edit.tpl');
93 '$submit' => t('Save Group'),
96 if (($a->argc == 2) && ($a->argv[1] === 'new')) {
97 return replace_macros($tpl, $context + array(
98 '$title' => t('Create a group of contacts/friends.'),
99 '$gname' => array('groupname', t('Group Name: '), '', ''),
101 '$form_security_token' => get_form_security_token("group_edit"),
107 if (($a->argc == 3) && ($a->argv[1] === 'drop')) {
108 check_form_security_token_redirectOnErr('/group', 'group_drop', 't');
110 if (intval($a->argv[2])) {
111 $r = q("SELECT `name` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
118 if (dbm::is_result($r)) {
119 $result = group_rmv(local_user(), $r[0]['name']);
123 info(t('Group removed.') . EOL);
125 notice(t('Unable to remove group.') . EOL);
128 goaway(App::get_baseurl() . '/group');
132 if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
133 check_form_security_token_ForbiddenOnErr('group_member_change', 't');
135 $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1",
139 if (dbm::is_result($r)) {
140 $change = intval($a->argv[2]);
144 if (($a->argc > 1) && (intval($a->argv[1]))) {
145 require_once 'include/acl_selectors.php';
146 require_once 'mod/contacts.php';
148 $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1",
153 if (! dbm::is_result($r)) {
154 notice(t('Group not found.') . EOL);
155 goaway(App::get_baseurl() . '/contacts');
159 $members = group_get_members($group['id']);
160 $preselected = array();
164 if (count($members)) {
165 foreach ($members as $member) {
166 $preselected[] = $member['id'];
171 if (in_array($change, $preselected)) {
172 group_rmv_member(local_user(), $group['name'], $change);
174 group_add_member(local_user(), $group['name'], $change);
177 $members = group_get_members($group['id']);
178 $preselected = array();
179 if (count($members)) {
180 foreach ($members as $member) {
181 $preselected[] = $member['id'];
186 $drop_tpl = get_markup_template('group_drop.tpl');
187 $drop_txt = replace_macros($drop_tpl, array(
188 '$id' => $group['id'],
189 '$delete' => t('Delete Group'),
190 '$form_security_token' => get_form_security_token("group_drop"),
194 $context = $context + array(
195 '$title' => t('Group Editor'),
196 '$gname' => array('groupname', t('Group Name: '), $group['name'], ''),
197 '$gid' => $group['id'],
198 '$drop' => $drop_txt,
199 '$form_security_token' => get_form_security_token('group_edit'),
200 '$edit_name' => t('Edit Group Name')
205 if (! isset($group)) {
209 $groupeditor = array(
210 'label_members' => t('Members'),
211 'members' => array(),
212 'label_contacts' => t('All Contacts'),
213 'group_is_empty' => t('Group is empty'),
214 'contacts' => array(),
217 $sec_token = addslashes(get_form_security_token('group_member_change'));
219 // Format the data of the group members
220 foreach ($members as $member) {
221 if ($member['url']) {
222 $entry = _contact_detail_for_template($member);
223 $entry['label'] = 'members';
224 $entry['photo_menu'] = '';
225 $entry['change_member'] = array(
226 'title' => t("Remove Contact"),
227 'gid' => $group['id'],
228 'cid' => $member['id'],
229 'sec_token' => $sec_token
232 $groupeditor['members'][] = $entry;
234 group_rmv_member(local_user(), $group['name'], $member['id']);
238 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `self` ORDER BY `name` ASC",
242 if (dbm::is_result($r)) {
243 // Format the data of the contacts who aren't in the contact group
244 foreach ($r as $member) {
245 if (! in_array($member['id'], $preselected)) {
246 $entry = _contact_detail_for_template($member);
247 $entry['label'] = 'contacts';
248 $entry['photo_menu'] = '';
249 $entry['change_member'] = array(
250 'title' => t("Add Contact"),
251 'gid' => $group['id'],
252 'cid' => $member['id'],
253 'sec_token' => $sec_token
256 $groupeditor['contacts'][] = $entry;
261 $context['$groupeditor'] = $groupeditor;
262 $context['$desc'] = t('Click on a contact to add or remove.');
264 // If there are to many contacts we could provide an alternative view mode
265 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
266 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
269 $tpl = get_markup_template('groupeditor.tpl');
270 echo replace_macros($tpl, $context);
274 return replace_macros($tpl, $context);