]> git.mxchange.org Git - friendica.git/blob - include/group.php
Merge remote-tracking branch 'refs/remotes/friendica/develop' into develop
[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'",
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 id FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
44                         intval($uid),
45                         dbesc($name)
46                 );
47                 if (dbm::is_result($r))
48                         $group_id = $r[0]['id'];
49                 if(! $group_id)
50                         return false;
51
52                 // remove group from default posting lists
53                 $r = q("SELECT def_gid, allow_gid, deny_gid FROM user WHERE uid = %d LIMIT 1",
54                        intval($uid)
55                 );
56                 if ($r) {
57                         $user_info = $r[0];
58                         $change = false;
59
60                         if($user_info['def_gid'] == $group_id) {
61                                 $user_info['def_gid'] = 0;
62                                 $change = true;
63                         }
64                         if(strpos($user_info['allow_gid'], '<' . $group_id . '>') !== false) {
65                                 $user_info['allow_gid'] = str_replace('<' . $group_id . '>', '', $user_info['allow_gid']);
66                                 $change = true;
67                         }
68                         if(strpos($user_info['deny_gid'], '<' . $group_id . '>') !== false) {
69                                 $user_info['deny_gid'] = str_replace('<' . $group_id . '>', '', $user_info['deny_gid']);
70                                 $change = true;
71                         }
72
73                         if($change) {
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']),
78                                   intval($uid)
79                                 );
80                         }
81                 }
82
83                 // remove all members
84                 $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d ",
85                         intval($uid),
86                         intval($group_id)
87                 );
88
89                 // remove group
90                 $r = q("UPDATE `group` SET `deleted` = 1 WHERE `uid` = %d AND `name` = '%s'",
91                         intval($uid),
92                         dbesc($name)
93                 );
94
95                 $ret = $r;
96
97         }
98
99         return $ret;
100 }
101
102 function group_byname($uid,$name) {
103         if((! $uid) || (! strlen($name)))
104                 return false;
105         $r = q("SELECT * FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
106                 intval($uid),
107                 dbesc($name)
108         );
109         if (dbm::is_result($r))
110                 return $r[0]['id'];
111         return false;
112 }
113
114 function group_rmv_member($uid,$name,$member) {
115         $gid = group_byname($uid,$name);
116         if(! $gid)
117                 return false;
118         if(! ( $uid && $gid && $member))
119                 return false;
120         $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d AND `contact-id` = %d",
121                 intval($uid),
122                 intval($gid),
123                 intval($member)
124         );
125         return $r;
126
127
128 }
129
130
131 function group_add_member($uid,$name,$member,$gid = 0) {
132         if(! $gid)
133                 $gid = group_byname($uid,$name);
134         if((! $gid) || (! $uid) || (! $member))
135                 return false;
136
137         $r = q("SELECT * FROM `group_member` WHERE `uid` = %d AND `gid` = %d AND `contact-id` = %d LIMIT 1",
138                 intval($uid),
139                 intval($gid),
140                 intval($member)
141         );
142         if (dbm::is_result($r))
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
146         if (! dbm::is_result($r)) {
147                 $r = q("INSERT INTO `group_member` (`uid`, `gid`, `contact-id`)
148                         VALUES( %d, %d, %d ) ",
149                         intval($uid),
150                         intval($gid),
151                         intval($member)
152                 );
153         }
154         return $r;
155 }
156
157 function group_get_members($gid) {
158         $ret = array();
159         if(intval($gid)) {
160                 $r = q("SELECT `group_member`.`contact-id`, `contact`.* FROM `group_member`
161                         INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
162                         WHERE `gid` = %d AND `group_member`.`uid` = %d AND
163                                 NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
164                                 ORDER BY `contact`.`name` ASC ",
165                         intval($gid),
166                         intval(local_user())
167                 );
168                 if (dbm::is_result($r))
169                         $ret = $r;
170         }
171         return $ret;
172 }
173
174 function group_public_members($gid) {
175         $ret = 0;
176         if(intval($gid)) {
177                 $r = q("SELECT `contact`.`id` AS `contact-id` FROM `group_member`
178                         INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
179                         WHERE `gid` = %d AND `group_member`.`uid` = %d
180                         AND  `contact`.`network` = '%s' AND `contact`.`notify` != '' ",
181                         intval($gid),
182                         intval(local_user()),
183                         dbesc(NETWORK_OSTATUS)
184                 );
185                 if (dbm::is_result($r))
186                         $ret = count($r);
187         }
188         return $ret;
189 }
190
191
192 function mini_group_select($uid,$gid = 0, $label = "") {
193
194         $grps = array();
195         $o = '';
196
197         $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
198                 intval($uid)
199         );
200         $grps[] = array('name' => '', 'id' => '0', 'selected' => '');
201         if (dbm::is_result($r)) {
202                 foreach ($r as $rr) {
203                         $grps[] = array('name' => $rr['name'], 'id' => $rr['id'], 'selected' => (($gid == $rr['id']) ? 'true' : ''));
204                 }
205
206         }
207         logger('groups: ' . print_r($grps,true));
208
209         if ($label == "")
210                 $label = t('Default privacy group for new contacts');
211
212         $o = replace_macros(get_markup_template('group_selection.tpl'), array(
213                 '$label' => $label,
214                 '$groups' => $grps
215         ));
216         return $o;
217 }
218
219
220 /**
221  * @brief Create group sidebar widget
222  *
223  * @param string $every
224  * @param string $each
225  * @param string $editmode
226  *      'standard' => include link 'Edit groups'
227  *      'extended' => include link 'Create new group'
228  *      'full' => include link 'Create new group' and provide for each group a link to edit this group
229  * @param int $group_id
230  * @param int $cid
231  * @return string
232  */
233 function group_side($every="contacts",$each="group",$editmode = "standard", $group_id = 0, $cid = 0) {
234
235         $o = '';
236
237         if (! local_user())
238                 return '';
239
240         $groups = array();
241
242         $groups[] = array(
243                 'text'  => t('Everybody'),
244                 'id' => 0,
245                 'selected' => (($group_id == 0) ? 'group-selected' : ''),
246                 'href'  => $every,
247         );
248
249
250
251         $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
252                 intval($_SESSION['uid'])
253         );
254         $member_of = array();
255         if($cid) {
256                 $member_of = groups_containing(local_user(),$cid);
257         }
258
259         if (dbm::is_result($r)) {
260                 foreach ($r as $rr) {
261                         $selected = (($group_id == $rr['id']) ? ' group-selected' : '');
262
263                         if ($editmode == "full") {
264                                 $groupedit = array(
265                                         'href' => "group/".$rr['id'],
266                                         'title' => t('edit'),
267                                 );
268                         } else {
269                                 $groupedit = null;
270                         }
271
272                         $groups[] = array(
273                                 'id'            => $rr['id'],
274                                 'cid'           => $cid,
275                                 'text'          => $rr['name'],
276                                 'selected'      => $selected,
277                                 'href'          => $each."/".$rr['id'],
278                                 'edit'          => $groupedit,
279                                 'ismember'      => in_array($rr['id'],$member_of),
280                         );
281                 }
282         }
283
284
285         $tpl = get_markup_template("group_side.tpl");
286         $o = replace_macros($tpl, array(
287                 '$title'        => t('Groups'),
288                 'newgroup'      => (($editmode == "extended") || ($editmode == "full") ? 1 : ''),
289                 '$editgroupstext' => t('Edit groups'),
290                 'grouppage'     => "group/",
291                 '$edittext'     => t('Edit group'),
292                 '$createtext'   => t('Create a new group'),
293                 '$creategroup'  => t('Group Name: '),
294                 '$form_security_token' => get_form_security_token("group_edit"),
295                 '$ungrouped'    => (($every === 'contacts') ? t('Contacts not in any group') : ''),
296                 '$groups'       => $groups,
297                 '$add'          => t('add'),
298         ));
299
300
301         return $o;
302 }
303
304 function expand_groups($a,$check_dead = false, $use_gcontact = false) {
305         if(! (is_array($a) && count($a)))
306                 return array();
307         $groups = implode(',', $a);
308         $groups = dbesc($groups);
309
310         if ($use_gcontact)
311                 $r = q("SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
312                                 INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
313                                 INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
314                         WHERE `gid` IN ($groups)");
315         else
316                 $r = q("SELECT `contact-id` FROM `group_member` WHERE `gid` IN ( $groups )");
317
318
319         $ret = array();
320         if (dbm::is_result($r))
321                 foreach($r as $rr)
322                         $ret[] = $rr['contact-id'];
323         if($check_dead AND !$use_gcontact) {
324                 require_once('include/acl_selectors.php');
325                 $ret = prune_deadguys($ret);
326         }
327         return $ret;
328 }
329
330
331 function member_of($c) {
332
333         $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 ",
334                 intval($c)
335         );
336
337         return $r;
338
339 }
340
341 function groups_containing($uid,$c) {
342
343         $r = q("SELECT `gid` FROM `group_member` WHERE `uid` = %d AND `group_member`.`contact-id` = %d ",
344                 intval($uid),
345                 intval($c)
346         );
347
348         $ret = array();
349         if (dbm::is_result($r)) {
350                 foreach($r as $rr)
351                         $ret[] = $rr['gid'];
352         }
353
354         return $ret;
355 }
356 /**
357  * @brief count unread group items
358  *
359  * Count unread items of each groups
360  *
361  * @return array
362  *      'id' => group id
363  *      'name' => group name
364  *      'count' => counted unseen group items
365  *
366  */
367 function groups_count_unseen() {
368
369         $r = q("SELECT `group`.`id`, `group`.`name`,
370                         (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
371                                 WHERE `uid` = %d AND `unseen` AND
372                                         `contact-id` IN (SELECT `contact-id` FROM `group_member`
373                                                                 WHERE `group_member`.`gid` = `group`.`id` AND `group_member`.`uid` = %d)) AS `count`
374                         FROM `group` WHERE `group`.`uid` = %d;",
375                 intval(local_user()),
376                 intval(local_user()),
377                 intval(local_user())
378         );
379
380         return $r;
381 }
382
383 /**
384  * @brief Returns the default group for a given user and network
385  *
386  * @param int $uid User id
387  * @param string $network network name
388  *
389  * @return int group id
390  */
391 function get_default_group($uid, $network = "") {
392
393         $default_group = 0;
394
395         if ($network == NETWORK_OSTATUS)
396                 $default_group = get_pconfig($uid, "ostatus", "default_group");
397
398         if ($default_group != 0)
399                 return $default_group;
400
401         $g = q("SELECT `def_gid` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
402         if($g && intval($g[0]["def_gid"]))
403                 $default_group = $g[0]["def_gid"];
404
405         return $default_group;
406 }