]> git.mxchange.org Git - friendica.git/blob - include/group.php
default ajax settings after conversion
[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
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. 
16
17                         $z = q("SELECT * FROM `group` WHERE `id` = %d LIMIT 1",
18                                 intval($r)
19                         );
20                         if(count($z) && $z[0]['deleted']) {
21                                 $r = q("UPDATE `group` SET `deleted` = 0 WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
22                                         intval($uid),
23                                         dbesc($name)
24                                 );
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); 
26                         }
27                         return true;
28                 }
29                 $r = q("INSERT INTO `group` ( `uid`, `name` )
30                         VALUES( %d, '%s' ) ",
31                         intval($uid),
32                         dbesc($name)
33                 );
34                 $ret = $r;
35         }       
36         return $ret;
37 }
38
39
40 function group_rmv($uid,$name) {
41         $ret = false;
42         if(x($uid) && x($name)) {
43                 $r = q("SELECT * FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
44                         intval($uid),
45                         dbesc($name)
46                 );
47                 if(count($r))
48                         $group_id = $r[0]['id'];
49                 if(! $group_id)
50                         return false;
51
52                 // remove all members
53                 $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d ",
54                         intval($uid),
55                         intval($group_id)
56                 );
57
58                 // remove group
59                 $r = q("UPDATE `group` SET `deleted` = 1 WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
60                         intval($uid),
61                         dbesc($name)
62                 );
63
64                 $ret = $r;
65
66         }
67
68         return $ret;
69 }
70
71 function group_byname($uid,$name) {
72         if((! $uid) || (! strlen($name)))
73                 return false;
74         $r = q("SELECT * FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
75                 intval($uid),
76                 dbesc($name)
77         );
78         if(count($r))
79                 return $r[0]['id'];
80         return false;
81 }
82
83 function group_rmv_member($uid,$name,$member) {
84         $gid = group_byname($uid,$name);
85         if(! $gid)
86                 return false;
87         if(! ( $uid && $gid && $member))
88                 return false;
89         $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d AND `contact-id` = %d LIMIT 1 ",
90                 intval($uid),
91                 intval($gid),
92                 intval($member)
93         );
94         return $r;
95         
96
97 }
98
99
100 function group_add_member($uid,$name,$member) {
101         $gid = group_byname($uid,$name);
102         if((! $gid) || (! $uid) || (! $member))
103                 return false;
104
105         $r = q("SELECT * FROM `group_member` WHERE `uid` = %d AND `id` = %d AND `contact-id` = %d LIMIT 1",     
106                 intval($uid),
107                 intval($gid),
108                 intval($member)
109         );
110         if(count($r))
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
114         if(! count($r))
115                 $r = q("INSERT INTO `group_member` (`uid`, `gid`, `contact-id`)
116                         VALUES( %d, %d, %d ) ",
117                         intval($uid),
118                         intval($gid),
119                         intval($member)
120         );
121         return $r;
122 }
123
124 function group_get_members($gid) {
125         $ret = array();
126         if(intval($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 ",
130                         intval($gid),
131                         intval(local_user())
132                 );
133                 if(count($r))
134                         $ret = $r;
135         }
136         return $ret;
137 }
138
139 function group_public_members($gid) {
140         $ret = 0;
141         if(intval($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` != '' ",
146                         intval($gid),
147                         intval(local_user()),
148                         dbesc(NETWORK_OSTATUS)
149                 );              
150                 if(count($r))
151                         $ret = count($r);
152         }
153         return $ret;
154 }
155
156
157
158 function group_side($every="contacts",$each="group",$edit = false, $group_id = 0, $cid = 0) {
159
160         $o = '';
161
162         if(! local_user())
163                 return '';
164
165         $groups = array();
166         
167         $groups[] = array(
168                 'text'  => t('Everybody'),
169                 'id' => 0,
170                 'selected' => (($group_id == 0) ? 'group-selected' : ''),
171                 'href'  => $every,
172         );
173
174
175
176         $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
177                 intval($_SESSION['uid'])
178         );
179         $member_of = array();
180         if($cid) {
181                 $member_of = groups_containing(local_user(),$cid);
182         } 
183
184         if(count($r)) {
185                 foreach($r as $rr) {
186                         $selected = (($group_id == $rr['id']) ? ' group-selected' : '');
187                         
188                         if ($edit) {
189                                 $groupedit = array(
190                                         'href' => "group/".$rr['id'],
191                                         'title' => t('edit'),
192                                 );
193                         } else {
194                                 $groupedit = null;
195                         }
196                         
197                         $groups[] = array(
198                                 'id'            => $rr['id'],
199                                 'cid'           => $cid,
200                                 'text'          => $rr['name'],
201                                 'selected'      => $selected,
202                                 'href'          => $each."/".$rr['id'],
203                                 'edit'          => $groupedit,
204                                 'ismember'      => in_array($rr['id'],$member_of),
205                         );
206                 }
207         }
208         
209         
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,
216                 '$add'                  => t('add'),
217         ));
218                 
219         
220         return $o;
221 }
222
223 function expand_groups($a) {
224         if(! (is_array($a) && count($a)))
225                 return array();
226         $groups = implode(',', $a);
227         $groups = dbesc($groups);
228         $r = q("SELECT `contact-id` FROM `group_member` WHERE `gid` IN ( $groups )");
229         $ret = array();
230         if(count($r))
231                 foreach($r as $rr)
232                         $ret[] = $rr['contact-id'];
233         return $ret;
234 }
235
236
237 function member_of($c) {
238
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 ",
240                 intval($c)
241         );
242
243         return $r;
244
245 }
246
247 function groups_containing($uid,$c) {
248
249         $r = q("SELECT `gid` FROM `group_member` WHERE `uid` = %d AND `group_member`.`contact-id` = %d ",
250                 intval($uid),
251                 intval($c)
252         );
253
254         $ret = array();
255         if(count($r)) {
256                 foreach($r as $rr)
257                         $ret[] = $rr['gid'];
258         }
259
260         return $ret;
261 }