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