4 function group_add($uid,$name) {
7 if(x($uid) && x($name)) {
8 $r = group_byname($uid,$name); // check for dups
11 // This could be a problem.
12 // Let's assume we've just created a group which we once deleted
13 // all the old members are gone, but the group remains so we don't break any security
14 // access lists. What we're doing here is reviving the dead group, but old content which
15 // was restricted to this group may now be seen by the new group members.
17 $z = q("SELECT * FROM `group` WHERE `id` = %d LIMIT 1",
20 if(count($z) && $z[0]['deleted']) {
21 $r = q("UPDATE `group` SET `deleted` = 0 WHERE `uid` = %d AND `name` = '%s'",
25 notice( t('A deleted group with this name was revived. Existing item permissions <strong>may</strong> apply to this group and any future members. If this is not what you intended, please create another group with a different name.') . EOL);
29 $r = q("INSERT INTO `group` ( `uid`, `name` )
40 function group_rmv($uid,$name) {
42 if(x($uid) && x($name)) {
43 $r = q("SELECT id FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
48 $group_id = $r[0]['id'];
52 // remove group from default posting lists
53 $r = q("SELECT def_gid, allow_gid, deny_gid FROM user WHERE uid = %d LIMIT 1",
60 if($user_info['def_gid'] == $group_id) {
61 $user_info['def_gid'] = 0;
64 if(strpos($user_info['allow_gid'], '<' . $group_id . '>') !== false) {
65 $user_info['allow_gid'] = str_replace('<' . $group_id . '>', '', $user_info['allow_gid']);
68 if(strpos($user_info['deny_gid'], '<' . $group_id . '>') !== false) {
69 $user_info['deny_gid'] = str_replace('<' . $group_id . '>', '', $user_info['deny_gid']);
74 q("UPDATE user SET def_gid = %d, allow_gid = '%s', deny_gid = '%s' WHERE uid = %d",
75 intval($user_info['def_gid']),
76 dbesc($user_info['allow_gid']),
77 dbesc($user_info['deny_gid']),
84 $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d ",
90 $r = q("UPDATE `group` SET `deleted` = 1 WHERE `uid` = %d AND `name` = '%s'",
102 function group_byname($uid,$name) {
103 if((! $uid) || (! strlen($name)))
105 $r = q("SELECT * FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
114 function group_rmv_member($uid,$name,$member) {
115 $gid = group_byname($uid,$name);
118 if(! ( $uid && $gid && $member))
120 $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d AND `contact-id` = %d",
131 function group_add_member($uid,$name,$member,$gid = 0) {
133 $gid = group_byname($uid,$name);
134 if((! $gid) || (! $uid) || (! $member))
137 $r = q("SELECT * FROM `group_member` WHERE `uid` = %d AND `gid` = %d AND `contact-id` = %d LIMIT 1",
143 return true; // You might question this, but
144 // we indicate success because the group member was in fact created
145 // -- It was just created at another time
147 $r = q("INSERT INTO `group_member` (`uid`, `gid`, `contact-id`)
148 VALUES( %d, %d, %d ) ",
156 function group_get_members($gid) {
159 $r = q("SELECT `group_member`.`contact-id`, `contact`.* FROM `group_member`
160 INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
161 WHERE `gid` = %d AND `group_member`.`uid` = %d AND
162 NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
163 ORDER BY `contact`.`name` ASC ",
173 function group_public_members($gid) {
176 $r = q("SELECT `contact`.`id` AS `contact-id` FROM `group_member`
177 INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
178 WHERE `gid` = %d AND `group_member`.`uid` = %d
179 AND `contact`.`network` = '%s' AND `contact`.`notify` != '' ",
181 intval(local_user()),
182 dbesc(NETWORK_OSTATUS)
191 function mini_group_select($uid,$gid = 0, $label = "") {
196 $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
199 $grps[] = array('name' => '', 'id' => '0', 'selected' => '');
202 $grps[] = array('name' => $rr['name'], 'id' => $rr['id'], 'selected' => (($gid == $rr['id']) ? 'true' : ''));
206 logger('groups: ' . print_r($grps,true));
209 $label = t('Default privacy group for new contacts');
211 $o = replace_macros(get_markup_template('group_selection.tpl'), array(
220 * @brief Create group sidebar widget
222 * @param string $every
223 * @param string $each
224 * @param string $editmode
225 * 'standard' => include link 'Edit groups'
226 * 'extended' => include link 'Create new group'
227 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
228 * @param int $group_id
232 function group_side($every="contacts",$each="group",$editmode = "standard", $group_id = 0, $cid = 0) {
242 'text' => t('Everybody'),
244 'selected' => (($group_id == 0) ? 'group-selected' : ''),
250 $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
251 intval($_SESSION['uid'])
253 $member_of = array();
255 $member_of = groups_containing(local_user(),$cid);
260 $selected = (($group_id == $rr['id']) ? ' group-selected' : '');
262 if ($editmode == "full") {
264 'href' => "group/".$rr['id'],
265 'title' => t('edit'),
274 'text' => $rr['name'],
275 'selected' => $selected,
276 'href' => $each."/".$rr['id'],
277 'edit' => $groupedit,
278 'ismember' => in_array($rr['id'],$member_of),
284 $tpl = get_markup_template("group_side.tpl");
285 $o = replace_macros($tpl, array(
286 '$title' => t('Groups'),
287 'newgroup' => (($editmode == "extended") || ($editmode == "full") ? 1 : ''),
288 '$editgroupstext' => t('Edit groups'),
289 'grouppage' => "group/",
290 '$edittext' => t('Edit group'),
291 '$createtext' => t('Create a new group'),
292 '$creategroup' => t('Group Name: '),
293 '$form_security_token' => get_form_security_token("group_edit"),
294 '$ungrouped' => (($every === 'contacts') ? t('Contacts not in any group') : ''),
295 '$groups' => $groups,
303 function expand_groups($a,$check_dead = false, $use_gcontact = false) {
304 if(! (is_array($a) && count($a)))
306 $groups = implode(',', $a);
307 $groups = dbesc($groups);
310 $r = q("SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
311 INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
312 INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
313 WHERE `gid` IN ($groups)");
315 $r = q("SELECT `contact-id` FROM `group_member` WHERE `gid` IN ( $groups )");
321 $ret[] = $rr['contact-id'];
322 if($check_dead AND !$use_gcontact) {
323 require_once('include/acl_selectors.php');
324 $ret = prune_deadguys($ret);
330 function member_of($c) {
332 $r = q("SELECT `group`.`name`, `group`.`id` FROM `group` INNER JOIN `group_member` ON `group_member`.`gid` = `group`.`id` WHERE `group_member`.`contact-id` = %d AND `group`.`deleted` = 0 ORDER BY `group`.`name` ASC ",
340 function groups_containing($uid,$c) {
342 $r = q("SELECT `gid` FROM `group_member` WHERE `uid` = %d AND `group_member`.`contact-id` = %d ",
356 * @brief count unread group items
358 * Count unread items of each groups
362 * 'name' => group name
363 * 'count' => counted unseen group items
366 function groups_count_unseen() {
368 $r = q("SELECT `group`.`id`, `group`.`name`,
369 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
370 WHERE `uid` = %d AND `unseen` AND
371 `contact-id` IN (SELECT `contact-id` FROM `group_member`
372 WHERE `group_member`.`gid` = `group`.`id` AND `group_member`.`uid` = %d)) AS `count`
373 FROM `group` WHERE `group`.`uid` = %d;",
374 intval(local_user()),
375 intval(local_user()),
383 * @brief Returns the default group for a given user and network
385 * @param int $uid User id
386 * @param string $network network name
388 * @return int group id
390 function get_default_group($uid, $network = "") {
394 if ($network == NETWORK_OSTATUS)
395 $default_group = get_pconfig($uid, "ostatus", "default_group");
397 if ($default_group != 0)
398 return $default_group;
400 $g = q("SELECT `def_gid` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
401 if($g && intval($g[0]["def_gid"]))
402 $default_group = $g[0]["def_gid"];
404 return $default_group;