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' LIMIT 1",
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 * FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
48 $group_id = $r[0]['id'];
53 $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d ",
59 $r = q("UPDATE `group` SET `deleted` = 1 WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
71 function group_byname($uid,$name) {
72 if((! $uid) || (! strlen($name)))
74 $r = q("SELECT * FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
83 function group_rmv_member($uid,$name,$member) {
84 $gid = group_byname($uid,$name);
87 if(! ( $uid && $gid && $member))
89 $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d AND `contact-id` = %d LIMIT 1 ",
100 function group_add_member($uid,$name,$member) {
101 $gid = group_byname($uid,$name);
102 if((! $gid) || (! $uid) || (! $member))
105 $r = q("SELECT * FROM `group_member` WHERE `uid` = %d AND `id` = %d AND `contact-id` = %d LIMIT 1",
111 return true; // You might question this, but
112 // we indicate success because the group member was in fact created
113 // -- It was just created at another time
115 $r = q("INSERT INTO `group_member` (`uid`, `gid`, `contact-id`)
116 VALUES( %d, %d, %d ) ",
124 function group_get_members($gid) {
127 $r = q("SELECT `group_member`.`contact-id`, `contact`.* FROM `group_member`
128 LEFT JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
129 WHERE `gid` = %d AND `group_member`.`uid` = %d ORDER BY `contact`.`name` ASC ",
139 function group_public_members($gid) {
142 $r = q("SELECT `contact`.`id` AS `contact-id` FROM `group_member`
143 LEFT JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
144 WHERE `gid` = %d AND `group_member`.`uid` = %d
145 AND `contact`.`network` = '%s' AND `contact`.`notify` != '' ",
147 intval(local_user()),
148 dbesc(NETWORK_OSTATUS)
158 function group_side($every="contacts",$each="group",$edit = false, $group_id = 0, $cid = 0) {
168 'text' => t('Everybody'),
170 'selected' => (($group_id == 0) ? 'group-selected' : ''),
176 $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
177 intval($_SESSION['uid'])
179 $member_of = array();
181 $member_of = groups_containing(local_user(),$cid);
186 $selected = (($group_id == $rr['id']) ? ' group-selected' : '');
190 'href' => "group/".$rr['id'],
191 'title' => t('edit'),
200 'text' => $rr['name'],
201 'selected' => $selected,
202 'href' => $each."/".$rr['id'],
203 'edit' => $groupedit,
204 'ismember' => in_array($rr['id'],$member_of),
210 $tpl = get_markup_template("group_side.tpl");
211 $o = replace_macros($tpl, array(
212 '$title' => t('Groups'),
213 '$edittext' => t('Edit group'),
214 '$createtext' => t('Create a new group'),
215 '$groups' => $groups,
223 function expand_groups($a) {
224 if(! (is_array($a) && count($a)))
226 $groups = implode(',', $a);
227 $groups = dbesc($groups);
228 $r = q("SELECT `contact-id` FROM `group_member` WHERE `gid` IN ( $groups )");
232 $ret[] = $rr['contact-id'];
237 function member_of($c) {
239 $r = q("SELECT `group`.`name`, `group`.`id` FROM `group` LEFT JOIN `group_member` ON `group_member`.`gid` = `group`.`id` WHERE `group_member`.`contact-id` = %d AND `group`.`deleted` = 0 ORDER BY `group`.`name` ASC ",
247 function groups_containing($uid,$c) {
249 $r = q("SELECT `gid` FROM `group_member` WHERE `uid` = %d AND `group_member`.`contact-id` = %d ",