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