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