]> git.mxchange.org Git - friendica.git/blob - src/Model/Group.php
Bugfix: Calls to a renamed function had been changed
[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                                                 AND `group_member`.`uid` = ?)
101                                         ) AS `count`
102                                 FROM `group`
103                                 WHERE `group`.`uid` = ?;",
104                         local_user(),
105                         local_user(),
106                         local_user()
107                 );
108
109                 return dba::inArray($stmt);
110         }
111
112         /**
113          * @brief Get the group id for a user/name couple
114          *
115          * Returns false if no group has been found.
116          *
117          * @param int $uid
118          * @param string $name
119          * @return int|boolean
120          */
121         public static function getIdByName($uid, $name)
122         {
123                 if (!$uid || !strlen($name)) {
124                         return false;
125                 }
126
127                 $group = dba::select('group', ['id'], ['uid' => $uid, 'name' => $name], ['limit' => 1]);
128                 if (DBM::is_result($group)) {
129                         return $group['id'];
130                 }
131
132                 return false;
133         }
134
135         /**
136          * @brief Mark a group as deleted
137          *
138          * @param type $gid
139          * @return boolean
140          */
141         public static function remove($gid) {
142                 if (! $gid) {
143                         return false;
144                 }
145
146                 // remove group from default posting lists
147                 $user = dba::select('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $uid], ['limit' => 1]);
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' => $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 type $uid
184          * @param type $name
185          * @return type
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                         $return = prune_deadguys($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 = t('Default privacy group for new contacts');
330                 }
331
332                 $o = replace_macros(get_markup_template('group_selection.tpl'), array(
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' => 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 = array();
372                 if ($cid) {
373                         $member_of = self::getByContactIdForUserId(local_user(), $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' => 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' => t('add'),
402                         '$title' => t('Groups'),
403                         '$groups' => $display_groups,
404                         'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
405                         'grouppage' => 'group/',
406                         '$edittext' => t('Edit group'),
407                         '$ungrouped' => $every === 'contacts' ? t('Contacts not in any group') : '',
408                         '$createtext' => t('Create a new group'),
409                         '$creategroup' => t('Group Name: '),
410                         '$editgroupstext' => t('Edit groups'),
411                         '$form_security_token' => get_form_security_token('group_edit'),
412                 ]);
413
414
415                 return $o;
416         }
417 }