]> git.mxchange.org Git - friendica.git/blob - src/Model/Group.php
1746a3962ec94547ed487136eae9ce2897cce28f
[friendica.git] / src / Model / Group.php
1 <?php
2
3 /**
4  * @file src/Model/Group.php
5  */
6
7 namespace Friendica\Model;
8
9 use Friendica\BaseObject;
10 use Friendica\Database\DBM;
11 use dba;
12
13 require_once 'boot.php';
14 require_once 'include/dba.php';
15 require_once 'include/text.php';
16
17 /**
18  * @brief functions for interacting with the group database table
19  */
20 class Group extends BaseObject
21 {
22         /**
23          * @brief Create a new contact group
24          *
25          * Note: If we found a deleted group with the same name, we restore it
26          *
27          * @param int $uid
28          * @param string $name
29          * @return boolean
30          */
31         public static function create($uid, $name)
32         {
33                 $return = false;
34                 if (x($uid) && x($name)) {
35                         $gid = self::getIdByName($uid, $name); // check for dupes
36                         if ($gid !== false) {
37                                 // This could be a problem.
38                                 // Let's assume we've just created a group which we once deleted
39                                 // all the old members are gone, but the group remains so we don't break any security
40                                 // access lists. What we're doing here is reviving the dead group, but old content which
41                                 // was restricted to this group may now be seen by the new group members.
42                                 $group = dba::selectFirst('group', ['deleted'], ['id' => $gid]);
43                                 if (DBM::is_result($group) && $group['deleted']) {
44                                         dba::update('group', ['deleted' => 0], ['gid' => $gid]);
45                                         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);
46                                 }
47                                 return true;
48                         }
49
50                         $return = dba::insert('group', ['uid' => $uid, 'name' => $name]);
51                         if ($return) {
52                                 $return = dba::lastInsertId();
53                         }
54                 }
55                 return $return;
56         }
57
58         /**
59          * @brief Get a list of group ids a contact belongs to
60          *
61          * @param int $cid
62          * @return array
63          */
64         public static function getIdsByContactId($cid)
65         {
66                 $condition = ['contact-id' => $cid];
67                 $stmt = dba::select('group_member', ['gid'], $condition);
68
69                 $return = [];
70
71                 while ($group = dba::fetch($stmt)) {
72                         $return[] = $group['gid'];
73                 }
74
75                 return $return;
76         }
77
78         /**
79          * @brief count unread group items
80          *
81          * Count unread items of each groups of the local user
82          *
83          * @return array
84          *      'id' => group id
85          *      'name' => group name
86          *      'count' => counted unseen group items
87          */
88         public static function countUnseen()
89         {
90                 $stmt = dba::p("SELECT `group`.`id`, `group`.`name`,
91                                 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
92                                         WHERE `uid` = ?
93                                         AND `unseen`
94                                         AND `contact-id` IN
95                                                 (SELECT `contact-id`
96                                                 FROM `group_member`
97                                                 WHERE `group_member`.`gid` = `group`.`id`)
98                                         ) AS `count`
99                                 FROM `group`
100                                 WHERE `group`.`uid` = ?;",
101                         local_user(),
102                         local_user()
103                 );
104
105                 return dba::inArray($stmt);
106         }
107
108         /**
109          * @brief Get the group id for a user/name couple
110          *
111          * Returns false if no group has been found.
112          *
113          * @param int $uid
114          * @param string $name
115          * @return int|boolean
116          */
117         public static function getIdByName($uid, $name)
118         {
119                 if (!$uid || !strlen($name)) {
120                         return false;
121                 }
122
123                 $group = dba::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
124                 if (DBM::is_result($group)) {
125                         return $group['id'];
126                 }
127
128                 return false;
129         }
130
131         /**
132          * @brief Mark a group as deleted
133          *
134          * @param int $gid
135          * @return boolean
136          */
137         public static function remove($gid) {
138                 if (! $gid) {
139                         return false;
140                 }
141
142                 $group = dba::selectFirst('group', ['uid'], ['gid' => $gid]);
143                 if (!DBM::is_result($group)) {
144                         return false;
145                 }
146
147                 // remove group from default posting lists
148                 $user = dba::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
149                 if (DBM::is_result($user)) {
150                         $change = false;
151
152                         if ($user['def_gid'] == $gid) {
153                                 $user['def_gid'] = 0;
154                                 $change = true;
155                         }
156                         if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
157                                 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
158                                 $change = true;
159                         }
160                         if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
161                                 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
162                                 $change = true;
163                         }
164
165                         if ($change) {
166                                 dba::update('user', $user, ['uid' => $group['uid']]);
167                         }
168                 }
169
170                 // remove all members
171                 dba::delete('group_member', ['gid' => $gid]);
172
173                 // remove group
174                 $return = dba::update('group', ['deleted' => 1], ['id' => $gid]);
175
176                 return $return;
177         }
178
179         /**
180          * @brief Mark a group as deleted based on its name
181          *
182          * @deprecated Use Group::remove instead
183          *
184          * @param int $uid
185          * @param string $name
186          * @return bool
187          */
188         public static function removeByName($uid, $name) {
189                 $return = false;
190                 if (x($uid) && x($name)) {
191                         $gid = self::getIdByName($uid, $name);
192
193                         $return = self::remove($gid);
194                 }
195
196                 return $return;
197         }
198
199         /**
200          * @brief Adds a contact to a group
201          *
202          * @param int $gid
203          * @param int $cid
204          * @return boolean
205          */
206         public static function addMember($gid, $cid)
207         {
208                 if (!$gid || !$cid) {
209                         return false;
210                 }
211
212                 $row_exists = dba::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
213                 if ($row_exists) {
214                         // Row already existing, nothing to do
215                         $return = true;
216                 } else {
217                         $return = dba::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
218                 }
219
220                 return $return;
221         }
222
223         /**
224          * @brief Removes a contact from a group
225          *
226          * @param int $gid
227          * @param int $cid
228          * @return boolean
229          */
230         public static function removeMember($gid, $cid)
231         {
232                 if (!$gid || !$cid) {
233                         return false;
234                 }
235
236                 $return = dba::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
237
238                 return $return;
239         }
240
241         /**
242          * @brief Removes a contact from a group based on its name
243          *
244          * @deprecated Use Group::removeMember instead
245          *
246          * @param int $uid
247          * @param string $name
248          * @param int $cid
249          * @return boolean
250          */
251         public static function removeMemberByName($uid, $name, $cid)
252         {
253                 $gid = self::getIdByName($uid, $name);
254
255                 $return = self::removeMember($gid, $cid);
256
257                 return $return;
258         }
259
260         /**
261          * @brief Returns the combined list of contact ids from a group id list
262          *
263          * @param array $group_ids
264          * @param boolean $check_dead
265          * @param boolean $use_gcontact
266          * @return array
267          */
268         public static function expand($group_ids, $check_dead = false, $use_gcontact = false)
269         {
270                 if (!is_array($group_ids) || !count($group_ids)) {
271                         return [];
272                 }
273
274                 $condition = '`gid` IN (' . substr(str_repeat("?, ", count($group_ids)), 0, -2) . ')';
275                 if ($use_gcontact) {
276                         $sql = 'SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
277                                         INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
278                                         INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
279                                 WHERE ' . $condition;
280                         $param_arr = array_merge([$sql], $group_ids);
281                         $stmt = call_user_func_array('dba::p', $param_arr);
282                 } else {
283                         $condition_array = array_merge([$condition], $group_ids);
284                         $stmt = dba::select('group_member', ['contact-id'], $condition_array);
285                 }
286
287                 $return = [];
288                 while($group_member = dba::fetch($stmt)) {
289                         $return[] = $group_member['contact-id'];
290                 }
291
292                 if ($check_dead && !$use_gcontact) {
293                         require_once 'include/acl_selectors.php';
294                         $return = prune_deadguys($return);
295                 }
296                 return $return;
297         }
298
299         /**
300          * @brief Returns a templated group selection list
301          *
302          * @param int $uid
303          * @param int $gid An optional pre-selected group
304          * @param string $label An optional label of the list
305          * @return string
306          */
307         public static function displayGroupSelection($uid, $gid = 0, $label = '')
308         {
309                 $o = '';
310
311                 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
312
313                 $display_groups = [
314                         [
315                                 'name' => '',
316                                 'id' => '0',
317                                 'selected' => ''
318                         ]
319                 ];
320                 while ($group = dba::fetch($stmt)) {
321                         $display_groups[] = [
322                                 'name' => $group['name'],
323                                 'id' => $group['id'],
324                                 'selected' => $gid == $group['id'] ? 'true' : ''
325                         ];
326                 }
327                 logger('groups: ' . print_r($display_groups, true));
328
329                 if ($label == '') {
330                         $label = t('Default privacy group for new contacts');
331                 }
332
333                 $o = replace_macros(get_markup_template('group_selection.tpl'), array(
334                         '$label' => $label,
335                         '$groups' => $display_groups
336                 ));
337                 return $o;
338         }
339
340         /**
341          * @brief Create group sidebar widget
342          *
343          * @param string $every
344          * @param string $each
345          * @param string $editmode
346          *      'standard' => include link 'Edit groups'
347          *      'extended' => include link 'Create new group'
348          *      'full' => include link 'Create new group' and provide for each group a link to edit this group
349          * @param int $group_id
350          * @param int $cid
351          * @return string
352          */
353         public static function sidebarWidget($every = 'contacts', $each = 'group', $editmode = 'standard', $group_id = 0, $cid = 0)
354         {
355                 $o = '';
356
357                 if (!local_user()) {
358                         return '';
359                 }
360
361                 $display_groups = [
362                         [
363                                 'text' => t('Everybody'),
364                                 'id' => 0,
365                                 'selected' => (($group_id == 0) ? 'group-selected' : ''),
366                                 'href' => $every,
367                         ]
368                 ];
369
370                 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
371
372                 $member_of = array();
373                 if ($cid) {
374                         $member_of = self::getIdsByContactId($cid);
375                 }
376
377                 while ($group = dba::fetch($stmt)) {
378                         $selected = (($group_id == $group['id']) ? ' group-selected' : '');
379
380                         if ($editmode == 'full') {
381                                 $groupedit = [
382                                         'href' => 'group/' . $group['id'],
383                                         'title' => t('edit'),
384                                 ];
385                         } else {
386                                 $groupedit = null;
387                         }
388
389                         $display_groups[] = [
390                                 'id'   => $group['id'],
391                                 'cid'  => $cid,
392                                 'text' => $group['name'],
393                                 'href' => $each . '/' . $group['id'],
394                                 'edit' => $groupedit,
395                                 'selected' => $selected,
396                                 'ismember' => in_array($group['id'], $member_of),
397                         ];
398                 }
399
400                 $tpl = get_markup_template('group_side.tpl');
401                 $o = replace_macros($tpl, [
402                         '$add' => t('add'),
403                         '$title' => t('Groups'),
404                         '$groups' => $display_groups,
405                         'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
406                         'grouppage' => 'group/',
407                         '$edittext' => t('Edit group'),
408                         '$ungrouped' => $every === 'contacts' ? t('Contacts not in any group') : '',
409                         '$createtext' => t('Create a new group'),
410                         '$creategroup' => t('Group Name: '),
411                         '$editgroupstext' => t('Edit groups'),
412                         '$form_security_token' => get_form_security_token('group_edit'),
413                 ]);
414
415
416                 return $o;
417         }
418 }