]> git.mxchange.org Git - friendica.git/blob - src/Model/Group.php
Merge remote-tracking branch 'upstream/develop' into issue-4069
[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/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::select('group', ['deleted'], ['id' => $gid], ['limit' => 1]);
42                                 if (DBM::is_result($group) && $group['deleted']) {
43                                         dba::update('group', ['deleted' => 0], ['gid' => $gid]);
44                                         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);
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         private 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                         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::select('group', ['id'], ['uid' => $uid, 'name' => $name], ['limit' => 1]);
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 type $gid
135          * @return boolean
136          */
137         public static function remove($gid) {
138                 if (! $gid) {
139                         return false;
140                 }
141
142                 // remove group from default posting lists
143                 $user = dba::select('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $uid], ['limit' => 1]);
144                 if (DBM::is_result($user)) {
145                         $change = false;
146
147                         if ($user['def_gid'] == $gid) {
148                                 $user['def_gid'] = 0;
149                                 $change = true;
150                         }
151                         if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
152                                 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
153                                 $change = true;
154                         }
155                         if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
156                                 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
157                                 $change = true;
158                         }
159
160                         if ($change) {
161                                 dba::update('user', $user, ['uid' => $uid]);
162                         }
163                 }
164
165                 // remove all members
166                 dba::delete('group_member', ['gid' => $gid]);
167
168                 // remove group
169                 $return = dba::update('group', ['deleted' => 1], ['id' => $gid]);
170
171                 return $return;
172         }
173
174         /**
175          * @brief Mark a group as deleted based on its name
176          *
177          * @deprecated Use Group::remove instead
178          *
179          * @param type $uid
180          * @param type $name
181          * @return type
182          */
183         public static function removeByName($uid, $name) {
184                 $return = false;
185                 if (x($uid) && x($name)) {
186                         $gid = self::getIdByName($uid, $name);
187
188                         $return = self::remove($gid);
189                 }
190
191                 return $return;
192         }
193
194         /**
195          * @brief Adds a contact to a group
196          *
197          * @param int $gid
198          * @param int $cid
199          * @return boolean
200          */
201         public static function addMember($gid, $cid)
202         {
203                 if (!$gid || !$cid) {
204                         return false;
205                 }
206
207                 $row_exists = dba::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
208                 if ($row_exists) {
209                         // Row already existing, nothing to do
210                         $return = true;
211                 } else {
212                         $return = dba::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
213                 }
214
215                 return $return;
216         }
217
218         /**
219          * @brief Removes a contact from a group
220          *
221          * @param int $gid
222          * @param int $cid
223          * @return boolean
224          */
225         public static function removeMember($gid, $cid)
226         {
227                 if (!$gid || !$cid) {
228                         return false;
229                 }
230
231                 $return = dba::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
232
233                 return $return;
234         }
235
236         /**
237          * @brief Removes a contact from a group based on its name
238          *
239          * @deprecated Use Group::removeMember instead
240          *
241          * @param int $uid
242          * @param string $name
243          * @param int $cid
244          * @return boolean
245          */
246         public static function removeMemberByName($uid, $name, $cid)
247         {
248                 $gid = self::getIdByName($uid, $name);
249
250                 $return = self::removeMember($gid, $cid);
251
252                 return $return;
253         }
254
255         /**
256          * @brief Returns the combined list of contact ids from a group id list
257          *
258          * @param array $group_ids
259          * @param boolean $check_dead
260          * @param boolean $use_gcontact
261          * @return array
262          */
263         public static function expand($group_ids, $check_dead = false, $use_gcontact = false)
264         {
265                 if (!is_array($group_ids) || !count($group_ids)) {
266                         return [];
267                 }
268
269                 $condition = '`gid` IN (' . substr(str_repeat("?, ", count($group_ids)), 0, -2) . ')';
270                 if ($use_gcontact) {
271                         $sql = 'SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
272                                         INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
273                                         INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
274                                 WHERE ' . $condition;
275                         $param_arr = array_merge([$sql], $group_ids);
276                         $stmt = call_user_func_array('dba::p', $param_arr);
277                 } else {
278                         $condition_array = array_merge([$condition], $group_ids);
279                         $stmt = dba::select('group_member', ['contact-id'], $condition_array);
280                 }
281
282                 $return = [];
283                 while($group_member = dba::fetch($stmt)) {
284                         $return[] = $group_member['contact-id'];
285                 }
286
287                 if ($check_dead && !$use_gcontact) {
288                         require_once 'include/acl_selectors.php';
289                         $return = prune_deadguys($return);
290                 }
291                 return $return;
292         }
293
294         /**
295          * @brief Returns a templated group selection list
296          *
297          * @param int $uid
298          * @param int $gid An optional pre-selected group
299          * @param string $label An optional label of the list
300          * @return string
301          */
302         public static function displayGroupSelection($uid, $gid = 0, $label = '')
303         {
304                 $o = '';
305
306                 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
307
308                 $display_groups = [
309                         [
310                                 'name' => '',
311                                 'id' => '0',
312                                 'selected' => ''
313                         ]
314                 ];
315                 while ($group = dba::fetch($stmt)) {
316                         $display_groups[] = [
317                                 'name' => $group['name'],
318                                 'id' => $group['id'],
319                                 'selected' => $gid == $group['id'] ? 'true' : ''
320                         ];
321                 }
322                 logger('groups: ' . print_r($display_groups, true));
323
324                 if ($label == '') {
325                         $label = t('Default privacy group for new contacts');
326                 }
327
328                 $o = replace_macros(get_markup_template('group_selection.tpl'), array(
329                         '$label' => $label,
330                         '$groups' => $display_groups
331                 ));
332                 return $o;
333         }
334
335         /**
336          * @brief Create group sidebar widget
337          *
338          * @param string $every
339          * @param string $each
340          * @param string $editmode
341          *      'standard' => include link 'Edit groups'
342          *      'extended' => include link 'Create new group'
343          *      'full' => include link 'Create new group' and provide for each group a link to edit this group
344          * @param int $group_id
345          * @param int $cid
346          * @return string
347          */
348         public static function sidebarWidget($every = 'contacts', $each = 'group', $editmode = 'standard', $group_id = 0, $cid = 0)
349         {
350                 $o = '';
351
352                 if (!local_user()) {
353                         return '';
354                 }
355
356                 $display_groups = [
357                         [
358                                 'text' => t('Everybody'),
359                                 'id' => 0,
360                                 'selected' => (($group_id == 0) ? 'group-selected' : ''),
361                                 'href' => $every,
362                         ]
363                 ];
364
365                 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
366
367                 $member_of = array();
368                 if ($cid) {
369                         $member_of = self::getIdsByContactId($cid);
370                 }
371
372                 while ($group = dba::fetch($stmt)) {
373                         $selected = (($group_id == $group['id']) ? ' group-selected' : '');
374
375                         if ($editmode == 'full') {
376                                 $groupedit = [
377                                         'href' => 'group/' . $group['id'],
378                                         'title' => t('edit'),
379                                 ];
380                         } else {
381                                 $groupedit = null;
382                         }
383
384                         $display_groups[] = [
385                                 'id'   => $group['id'],
386                                 'cid'  => $cid,
387                                 'text' => $group['name'],
388                                 'href' => $each . '/' . $group['id'],
389                                 'edit' => $groupedit,
390                                 'selected' => $selected,
391                                 'ismember' => in_array($group['id'], $member_of),
392                         ];
393                 }
394
395                 $tpl = get_markup_template('group_side.tpl');
396                 $o = replace_macros($tpl, [
397                         '$add' => t('add'),
398                         '$title' => t('Groups'),
399                         '$groups' => $display_groups,
400                         'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
401                         'grouppage' => 'group/',
402                         '$edittext' => t('Edit group'),
403                         '$ungrouped' => $every === 'contacts' ? t('Contacts not in any group') : '',
404                         '$createtext' => t('Create a new group'),
405                         '$creategroup' => t('Group Name: '),
406                         '$editgroupstext' => t('Edit groups'),
407                         '$form_security_token' => get_form_security_token('group_edit'),
408                 ]);
409
410
411                 return $o;
412         }
413 }