]> git.mxchange.org Git - friendica.git/blob - src/Model/Group.php
Update function calls
[friendica.git] / src / Model / Group.php
1 <?php
2 /**
3  * @file src/Model/Group.php
4  */
5 namespace Friendica\Model;
6
7 use Friendica\BaseModule;
8 use Friendica\BaseObject;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Logger;
11 use Friendica\Database\DBA;
12 use Friendica\Util\Security;
13
14 require_once 'boot.php';
15 require_once 'include/dba.php';
16 require_once 'include/text.php';
17
18 /**
19  * @brief functions for interacting with the group database table
20  */
21 class Group extends BaseObject
22 {
23         /**
24          * @brief Create a new contact group
25          *
26          * Note: If we found a deleted group with the same name, we restore it
27          *
28          * @param int $uid
29          * @param string $name
30          * @return boolean
31          */
32         public static function create($uid, $name)
33         {
34                 $return = false;
35                 if (x($uid) && x($name)) {
36                         $gid = self::getIdByName($uid, $name); // check for dupes
37                         if ($gid !== false) {
38                                 // This could be a problem.
39                                 // Let's assume we've just created a group which we once deleted
40                                 // all the old members are gone, but the group remains so we don't break any security
41                                 // access lists. What we're doing here is reviving the dead group, but old content which
42                                 // was restricted to this group may now be seen by the new group members.
43                                 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
44                                 if (DBA::isResult($group) && $group['deleted']) {
45                                         DBA::update('group', ['deleted' => 0], ['id' => $gid]);
46                                         notice(L10n::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);
47                                 }
48                                 return true;
49                         }
50
51                         $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
52                         if ($return) {
53                                 $return = DBA::lastInsertId();
54                         }
55                 }
56                 return $return;
57         }
58
59         /**
60          * Update group information.
61          *
62          * @param  int    $id   Group ID
63          * @param  string $name Group name
64          *
65          * @return bool Was the update successful?
66          */
67         public static function update($id, $name)
68         {
69                 return DBA::update('group', ['name' => $name], ['id' => $id]);
70         }
71
72         /**
73          * @brief Get a list of group ids a contact belongs to
74          *
75          * @param int $cid
76          * @return array
77          */
78         public static function getIdsByContactId($cid)
79         {
80                 $condition = ['contact-id' => $cid];
81                 $stmt = DBA::select('group_member', ['gid'], $condition);
82
83                 $return = [];
84
85                 while ($group = DBA::fetch($stmt)) {
86                         $return[] = $group['gid'];
87                 }
88
89                 return $return;
90         }
91
92         /**
93          * @brief count unread group items
94          *
95          * Count unread items of each groups of the local user
96          *
97          * @return array
98          *      'id' => group id
99          *      'name' => group name
100          *      'count' => counted unseen group items
101          */
102         public static function countUnseen()
103         {
104                 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
105                                 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
106                                         WHERE `uid` = ?
107                                         AND `unseen`
108                                         AND `contact-id` IN
109                                                 (SELECT `contact-id`
110                                                 FROM `group_member`
111                                                 WHERE `group_member`.`gid` = `group`.`id`)
112                                         ) AS `count`
113                                 FROM `group`
114                                 WHERE `group`.`uid` = ?;",
115                         local_user(),
116                         local_user()
117                 );
118
119                 return DBA::toArray($stmt);
120         }
121
122         /**
123          * @brief Get the group id for a user/name couple
124          *
125          * Returns false if no group has been found.
126          *
127          * @param int $uid
128          * @param string $name
129          * @return int|boolean
130          */
131         public static function getIdByName($uid, $name)
132         {
133                 if (!$uid || !strlen($name)) {
134                         return false;
135                 }
136
137                 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
138                 if (DBA::isResult($group)) {
139                         return $group['id'];
140                 }
141
142                 return false;
143         }
144
145         /**
146          * @brief Mark a group as deleted
147          *
148          * @param int $gid
149          * @return boolean
150          */
151         public static function remove($gid) {
152                 if (! $gid) {
153                         return false;
154                 }
155
156                 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
157                 if (!DBA::isResult($group)) {
158                         return false;
159                 }
160
161                 // remove group from default posting lists
162                 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
163                 if (DBA::isResult($user)) {
164                         $change = false;
165
166                         if ($user['def_gid'] == $gid) {
167                                 $user['def_gid'] = 0;
168                                 $change = true;
169                         }
170                         if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
171                                 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
172                                 $change = true;
173                         }
174                         if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
175                                 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
176                                 $change = true;
177                         }
178
179                         if ($change) {
180                                 DBA::update('user', $user, ['uid' => $group['uid']]);
181                         }
182                 }
183
184                 // remove all members
185                 DBA::delete('group_member', ['gid' => $gid]);
186
187                 // remove group
188                 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
189
190                 return $return;
191         }
192
193         /**
194          * @brief Mark a group as deleted based on its name
195          *
196          * @deprecated Use Group::remove instead
197          *
198          * @param int $uid
199          * @param string $name
200          * @return bool
201          */
202         public static function removeByName($uid, $name) {
203                 $return = false;
204                 if (x($uid) && x($name)) {
205                         $gid = self::getIdByName($uid, $name);
206
207                         $return = self::remove($gid);
208                 }
209
210                 return $return;
211         }
212
213         /**
214          * @brief Adds a contact to a group
215          *
216          * @param int $gid
217          * @param int $cid
218          * @return boolean
219          */
220         public static function addMember($gid, $cid)
221         {
222                 if (!$gid || !$cid) {
223                         return false;
224                 }
225
226                 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
227                 if ($row_exists) {
228                         // Row already existing, nothing to do
229                         $return = true;
230                 } else {
231                         $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
232                 }
233
234                 return $return;
235         }
236
237         /**
238          * @brief Removes a contact from a group
239          *
240          * @param int $gid
241          * @param int $cid
242          * @return boolean
243          */
244         public static function removeMember($gid, $cid)
245         {
246                 if (!$gid || !$cid) {
247                         return false;
248                 }
249
250                 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
251
252                 return $return;
253         }
254
255         /**
256          * @brief Removes a contact from a group based on its name
257          *
258          * @deprecated Use Group::removeMember instead
259          *
260          * @param int $uid
261          * @param string $name
262          * @param int $cid
263          * @return boolean
264          */
265         public static function removeMemberByName($uid, $name, $cid)
266         {
267                 $gid = self::getIdByName($uid, $name);
268
269                 $return = self::removeMember($gid, $cid);
270
271                 return $return;
272         }
273
274         /**
275          * @brief Returns the combined list of contact ids from a group id list
276          *
277          * @param array $group_ids
278          * @param boolean $check_dead
279          * @return array
280          */
281         public static function expand($group_ids, $check_dead = false)
282         {
283                 if (!is_array($group_ids) || !count($group_ids)) {
284                         return [];
285                 }
286
287                 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
288
289                 $return = [];
290                 while($group_member = DBA::fetch($stmt)) {
291                         $return[] = $group_member['contact-id'];
292                 }
293
294                 if ($check_dead) {
295                         Contact::pruneUnavailable($return);
296                 }
297
298                 return $return;
299         }
300
301         /**
302          * @brief Returns a templated group selection list
303          *
304          * @param int $uid
305          * @param int $gid An optional pre-selected group
306          * @param string $label An optional label of the list
307          * @return string
308          */
309         public static function displayGroupSelection($uid, $gid = 0, $label = '')
310         {
311                 $o = '';
312
313                 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
314
315                 $display_groups = [
316                         [
317                                 'name' => '',
318                                 'id' => '0',
319                                 'selected' => ''
320                         ]
321                 ];
322                 while ($group = DBA::fetch($stmt)) {
323                         $display_groups[] = [
324                                 'name' => $group['name'],
325                                 'id' => $group['id'],
326                                 'selected' => $gid == $group['id'] ? 'true' : ''
327                         ];
328                 }
329                 Logger::log('groups: ' . print_r($display_groups, true));
330
331                 if ($label == '') {
332                         $label = L10n::t('Default privacy group for new contacts');
333                 }
334
335                 $o = replace_macros(get_markup_template('group_selection.tpl'), [
336                         '$label' => $label,
337                         '$groups' => $display_groups
338                 ]);
339                 return $o;
340         }
341
342         /**
343          * @brief Create group sidebar widget
344          *
345          * @param string $every
346          * @param string $each
347          * @param string $editmode
348          *      'standard' => include link 'Edit groups'
349          *      'extended' => include link 'Create new group'
350          *      'full' => include link 'Create new group' and provide for each group a link to edit this group
351          * @param int $group_id
352          * @param int $cid
353          * @return string
354          */
355         public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
356         {
357                 $o = '';
358
359                 if (!local_user()) {
360                         return '';
361                 }
362
363                 $display_groups = [
364                         [
365                                 'text' => L10n::t('Everybody'),
366                                 'id' => 0,
367                                 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
368                                 'href' => $every,
369                         ]
370                 ];
371
372                 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
373
374                 $member_of = [];
375                 if ($cid) {
376                         $member_of = self::getIdsByContactId($cid);
377                 }
378
379                 while ($group = DBA::fetch($stmt)) {
380                         $selected = (($group_id == $group['id']) ? ' group-selected' : '');
381
382                         if ($editmode == 'full') {
383                                 $groupedit = [
384                                         'href' => 'group/' . $group['id'],
385                                         'title' => L10n::t('edit'),
386                                 ];
387                         } else {
388                                 $groupedit = null;
389                         }
390
391                         $display_groups[] = [
392                                 'id'   => $group['id'],
393                                 'cid'  => $cid,
394                                 'text' => $group['name'],
395                                 'href' => $each . '/' . $group['id'],
396                                 'edit' => $groupedit,
397                                 'selected' => $selected,
398                                 'ismember' => in_array($group['id'], $member_of),
399                         ];
400                 }
401
402                 $tpl = get_markup_template('group_side.tpl');
403                 $o = replace_macros($tpl, [
404                         '$add' => L10n::t('add'),
405                         '$title' => L10n::t('Groups'),
406                         '$groups' => $display_groups,
407                         'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
408                         'grouppage' => 'group/',
409                         '$edittext' => L10n::t('Edit group'),
410                         '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
411                         '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
412                         '$createtext' => L10n::t('Create a new group'),
413                         '$creategroup' => L10n::t('Group Name: '),
414                         '$editgroupstext' => L10n::t('Edit groups'),
415                         '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
416                 ]);
417
418
419                 return $o;
420         }
421 }