]> git.mxchange.org Git - friendica.git/blob - include/group.php
065c2241febec2bb286f5ba2086ad1c57a60049f
[friendica.git] / include / group.php
1 <?php
2
3
4 function group_add($uid,$name) {
5
6         $ret = false;
7         if(x($uid) && x($name)) {
8                 $r = group_byname($uid,$name); // check for dups
9                 if($r !== false) 
10                         return true;
11                 $r = q("INSERT INTO `group` ( `uid`, `name` )
12                         VALUES( %d, '%s' ) ",
13                         intval($uid),
14                         dbesc($name)
15                 );
16                 $ret = $r;
17         }       
18         return $ret;
19 }
20
21
22 function group_rmv($uid,$name) {
23         $ret = false;
24         if(x($uid) && x($name)) {
25                 $r = q("SELECT * FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
26                         intval($uid),
27                         dbesc($name)
28                 );
29                 if(count($r))
30                         $group_id = $r[0]['id'];
31                 if(! $group_id)
32                         return false;
33
34                 // remove all members
35                 $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d ",
36                         intval($uid),
37                         intval($group_id)
38                 );
39
40                 // remove group
41                 $r = q("UPDATE `group` SET `deleted` = 1 WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
42                         intval($uid),
43                         dbesc($name)
44                 );
45
46                 $ret = $r;
47
48         }
49
50         return $ret;
51 }
52
53 function group_byname($uid,$name) {
54         if((! $uid) || (! strlen($name)))
55                 return false;
56         $r = q("SELECT * FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
57                 intval($uid),
58                 dbesc($name)
59         );
60         if(count($r))
61                 return $r[0]['id'];
62         return false;
63 }
64
65 function group_rmv_member($uid,$name,$member) {
66         $gid = group_byname($uid,$name);
67         if(! $gid)
68                 return false;
69         if(! ( $uid && $gid && $member))
70                 return false;
71         $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d AND `contact-id` = %d LIMIT 1 ",
72                 intval($uid),
73                 intval($gid),
74                 intval($member)
75         );
76         return $r;
77         
78
79 }
80
81
82 function group_add_member($uid,$name,$member) {
83         $gid = group_byname($uid,$name);
84         if((! $gid) || (! $uid) || (! $member))
85                 return false;
86
87         $r = q("SELECT * FROM `group_member` WHERE `uid` = %d AND `id` = %d AND `contact-id` = %d LIMIT 1",     
88                 intval($uid),
89                 intval($gid),
90                 intval($member)
91         );
92         if(count($r))
93                 return true;    // You might question this, but 
94                                 // we indicate success because the group was in fact created
95                                 // -- It was just created at another time
96         if(! count($r))
97                 $r = q("INSERT INTO `group_member` (`uid`, `gid`, `contact-id`)
98                         VALUES( %d, %d, %d ) ",
99                         intval($uid),
100                         intval($gid),
101                         intval($member)
102         );
103         return $r;
104 }
105
106 function group_get_members($gid) {
107         $ret = array();
108         if(intval($gid)) {
109                 $r = q("SELECT `group_member`.`contact-id`, `contact`.* FROM `group_member` 
110                         LEFT JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id` 
111                         WHERE `gid` = %d AND `group_member`.`uid` = %d ORDER BY `contact`.`name` ASC ",
112                         intval($gid),
113                         intval(local_user())
114                 );
115                 if(count($r))
116                         $ret = $r;
117         }
118         return $ret;
119 }
120
121 function group_public_members($gid) {
122         $ret = 0;
123         if(intval($gid)) {
124                 $r = q("SELECT `contact`.`id` AS `contact-id` FROM `group_member` 
125                         LEFT JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id` 
126                         WHERE `gid` = %d AND `group_member`.`uid` = %d AND `contact`.`network` != 'dfrn' ",
127                         intval($gid),
128                         intval(local_user())
129                 );              
130                 if(count($r))
131                         $ret = count($r);
132         }
133         return $ret;
134 }
135
136
137
138 function group_side($every="contacts",$each="group",$edit = false) {
139
140         $o = '';
141
142         if(! local_user())
143                 return '';
144
145         $createtext = t('Create a new group');
146         $linktext= t('Everybody');
147
148 $o .= <<< EOT
149
150 <div id="group-sidebar">
151 <h3>Groups</h3>
152
153 <div id="sidebar-new-group">
154 <a href="group/new">$createtext</a>
155 </div>
156
157 <div id="sidebar-group-list">
158         <ul id="sidebar-group-ul">
159         <li class="sidebar-group-li" ><a href="$every" >$linktext</a></li>
160
161 EOT;
162
163         $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d",
164                 intval($_SESSION['uid'])
165         );
166         if(count($r)) {
167                 foreach($r as $rr)
168                         $o .= ' <li class="sidebar-group-li">' . (($edit) ? "<a href=\"group/{$rr['id']}\" title=\"" . t('Edit') . "\" ><img src=\"images/spencil.gif\" alt=\"" . t('Edit') . "\"></a> " : "") . "<a href=\"$each/{$rr['id']}\">{$rr['name']}</a></li>\r\n";
169         }
170         $o .= " </ul>\r\n       </div>\r\n</div>";      
171
172         return $o;
173 }
174
175 function expand_groups($a) {
176         if(! (is_array($a) && count($a)))
177                 return array();
178         $groups = implode(',', $a);
179         $groups = dbesc($groups);
180         $r = q("SELECT `contact-id` FROM `group_member` WHERE `gid` IN ( $groups )");
181         $ret = array();
182         if(count($r))
183                 foreach($r as $rr)
184                         $ret[] = $rr['contact-id'];
185         return $ret;
186 }