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