3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model;
24 use Friendica\BaseModule;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Renderer;
28 use Friendica\Database\DBA;
32 * functions for interacting with the group database table
36 const FOLLOWERS = '~';
39 public static function getByUserId($uid, $includesDeleted = false)
41 $conditions = ['uid' => $uid];
43 if (!$includesDeleted) {
44 $conditions['deleted'] = false;
47 return DBA::selectToArray('group', [], $conditions);
51 * @param int $group_id
55 public static function exists($group_id, $uid = null)
57 $condition = ['id' => $group_id, 'deleted' => false];
65 return DBA::exists('group', $condition);
69 * Create a new contact group
71 * Note: If we found a deleted group with the same name, we restore it
78 public static function create($uid, $name)
81 if (!empty($uid) && !empty($name)) {
82 $gid = self::getIdByName($uid, $name); // check for dupes
84 // This could be a problem.
85 // Let's assume we've just created a group which we once deleted
86 // all the old members are gone, but the group remains so we don't break any security
87 // access lists. What we're doing here is reviving the dead group, but old content which
88 // was restricted to this group may now be seen by the new group members.
89 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
90 if (DBA::isResult($group) && $group['deleted']) {
91 DBA::update('group', ['deleted' => 0], ['id' => $gid]);
92 notice(DI::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.'));
97 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
99 $return = DBA::lastInsertId();
106 * Update group information.
108 * @param int $id Group ID
109 * @param string $name Group name
111 * @return bool Was the update successful?
114 public static function update($id, $name)
116 return DBA::update('group', ['name' => $name], ['id' => $id]);
120 * Get a list of group ids a contact belongs to
126 public static function getIdsByContactId($cid)
130 $stmt = DBA::select('group_member', ['gid'], ['contact-id' => $cid]);
131 while ($group = DBA::fetch($stmt)) {
132 $return[] = $group['gid'];
137 $contact = Contact::getById($cid, ['rel']);
138 if ($contact['rel'] == Contact::FOLLOWER || $contact['rel'] == Contact::FRIEND) {
139 $return[] = self::FOLLOWERS;
142 if ($contact['rel'] == Contact::FRIEND) {
143 $return[] = self::MUTUALS;
150 * count unread group items
152 * Count unread items of each groups of the local user
156 * 'name' => group name
157 * 'count' => counted unseen group items
160 public static function countUnseen()
162 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
163 (SELECT COUNT(*) FROM `post-user`
169 WHERE `group_member`.`gid` = `group`.`id`)
172 WHERE `group`.`uid` = ?;",
177 return DBA::toArray($stmt);
181 * Get the group id for a user/name couple
183 * Returns false if no group has been found.
186 * @param string $name
187 * @return int|boolean
190 public static function getIdByName($uid, $name)
192 if (!$uid || !strlen($name)) {
196 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
197 if (DBA::isResult($group)) {
205 * Mark a group as deleted
211 public static function remove($gid)
217 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
218 if (!DBA::isResult($group)) {
222 // remove group from default posting lists
223 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
224 if (DBA::isResult($user)) {
227 if ($user['def_gid'] == $gid) {
228 $user['def_gid'] = 0;
231 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
232 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
235 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
236 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
241 DBA::update('user', $user, ['uid' => $group['uid']]);
245 // remove all members
246 DBA::delete('group_member', ['gid' => $gid]);
249 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
255 * Adds a contact to a group
262 public static function addMember($gid, $cid)
264 if (!$gid || !$cid) {
268 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
270 // Row already existing, nothing to do
273 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
280 * Removes a contact from a group
287 public static function removeMember($gid, $cid)
289 if (!$gid || !$cid) {
293 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
299 * Returns the combined list of contact ids from a group id list
302 * @param array $group_ids
303 * @param boolean $check_dead
307 public static function expand($uid, array $group_ids, $check_dead = false)
309 if (!is_array($group_ids) || !count($group_ids)) {
315 $networks = Protocol::SUPPORT_PRIVATE;
317 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $uid]);
318 if (DBA::isResult($mailacct)) {
319 $pubmail = $mailacct['pubmail'];
323 $networks = array_diff($networks, [Protocol::MAIL]);
326 $key = array_search(self::FOLLOWERS, $group_ids);
327 if ($key !== false) {
328 $followers = Contact::selectToArray(['id'], [
330 'rel' => [Contact::FOLLOWER, Contact::FRIEND],
331 'network' => $networks,
332 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
338 foreach ($followers as $follower) {
339 $return[] = $follower['id'];
342 unset($group_ids[$key]);
345 $key = array_search(self::MUTUALS, $group_ids);
346 if ($key !== false) {
347 $mutuals = Contact::selectToArray(['id'], [
349 'rel' => [Contact::FRIEND],
350 'network' => $networks,
351 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
357 foreach ($mutuals as $mutual) {
358 $return[] = $mutual['id'];
361 unset($group_ids[$key]);
364 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
365 while ($group_member = DBA::fetch($stmt)) {
366 $return[] = $group_member['contact-id'];
371 $return = Contact::pruneUnavailable($return);
378 * Returns a templated group selection list
381 * @param int $gid An optional pre-selected group
382 * @param string $label An optional label of the list
386 public static function displayGroupSelection($uid, $gid = 0, $label = '')
396 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
397 while ($group = DBA::fetch($stmt)) {
398 $display_groups[] = [
399 'name' => $group['name'],
400 'id' => $group['id'],
401 'selected' => $gid == $group['id'] ? 'true' : ''
406 Logger::info('Got groups', $display_groups);
409 $label = DI::l10n()->t('Default privacy group for new contacts');
412 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
414 '$groups' => $display_groups
420 * Create group sidebar widget
422 * @param string $every
423 * @param string $each
424 * @param string $editmode
425 * 'standard' => include link 'Edit groups'
426 * 'extended' => include link 'Create new group'
427 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
428 * @param string $group_id
433 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
441 'text' => DI::l10n()->t('Everybody'),
443 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
450 $member_of = self::getIdsByContactId($cid);
453 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
454 while ($group = DBA::fetch($stmt)) {
455 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
457 if ($editmode == 'full') {
459 'href' => 'group/' . $group['id'],
460 'title' => DI::l10n()->t('edit'),
466 if ($each == 'group') {
467 $count = DBA::count('group_member', ['gid' => $group['id']]);
468 $group_name = sprintf('%s (%d)', $group['name'], $count);
470 $group_name = $group['name'];
473 $display_groups[] = [
474 'id' => $group['id'],
476 'text' => $group_name,
477 'href' => $each . '/' . $group['id'],
478 'edit' => $groupedit,
479 'selected' => $selected,
480 'ismember' => in_array($group['id'], $member_of),
485 // Don't show the groups on the network page when there is only one
486 if ((count($display_groups) <= 2) && ($each == 'network')) {
490 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
491 $o = Renderer::replaceMacros($tpl, [
492 '$add' => DI::l10n()->t('add'),
493 '$title' => DI::l10n()->t('Groups'),
494 '$groups' => $display_groups,
495 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
496 'grouppage' => 'group/',
497 '$edittext' => DI::l10n()->t('Edit group'),
498 '$ungrouped' => $every === 'contact' ? DI::l10n()->t('Contacts not in any group') : '',
499 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
500 '$createtext' => DI::l10n()->t('Create a new group'),
501 '$creategroup' => DI::l10n()->t('Group Name: '),
502 '$editgroupstext' => DI::l10n()->t('Edit groups'),
503 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),