]> git.mxchange.org Git - friendica.git/blob - include/group.php
improved browser language detect, set user language on login
[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 
127                         AND `contact`.`network` != 'dfrn' AND `contact`.`network` != 'mail' AND `contact`.`network` != 'face' ",
128                         intval($gid),
129                         intval(local_user())
130                 );              
131                 if(count($r))
132                         $ret = count($r);
133         }
134         return $ret;
135 }
136
137
138
139 function group_side($every="contacts",$each="group",$edit = false) {
140
141         $o = '';
142
143         if(! local_user())
144                 return '';
145
146         $createtext = t('Create a new group');
147         $linktext= t('Everybody');
148
149 $o .= <<< EOT
150
151 <div id="group-sidebar">
152 <h3>Groups</h3>
153
154 <div id="sidebar-new-group">
155 <a href="group/new">$createtext</a>
156 </div>
157
158 <div id="sidebar-group-list">
159         <ul id="sidebar-group-ul">
160         <li class="sidebar-group-li" ><a href="$every" >$linktext</a></li>
161
162 EOT;
163
164         $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d",
165                 intval($_SESSION['uid'])
166         );
167         if(count($r)) {
168                 foreach($r as $rr)
169                         $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";
170         }
171         $o .= " </ul>\r\n       </div>\r\n</div>";      
172
173         return $o;
174 }
175
176 function expand_groups($a) {
177         if(! (is_array($a) && count($a)))
178                 return array();
179         $groups = implode(',', $a);
180         $groups = dbesc($groups);
181         $r = q("SELECT `contact-id` FROM `group_member` WHERE `gid` IN ( $groups )");
182         $ret = array();
183         if(count($r))
184                 foreach($r as $rr)
185                         $ret[] = $rr['contact-id'];
186         return $ret;
187 }