3 * @file src/Model/Group.php
6 namespace Friendica\Model;
8 use Friendica\BaseModule;
9 use Friendica\Core\Logger;
10 use Friendica\Core\Protocol;
11 use Friendica\Core\Renderer;
12 use Friendica\Database\DBA;
16 * functions for interacting with the group database table
20 const FOLLOWERS = '~';
23 public static function getByUserId($uid, $includesDeleted = false)
25 $conditions = ['uid' => $uid];
27 if (!$includesDeleted) {
28 $conditions['deleted'] = false;
31 return DBA::selectToArray('group', [], $conditions);
35 * @param int $group_id
39 public static function exists($group_id, $uid = null)
41 $condition = ['id' => $group_id, 'deleted' => false];
49 return DBA::exists('group', $condition);
53 * Create a new contact group
55 * Note: If we found a deleted group with the same name, we restore it
62 public static function create($uid, $name)
65 if (!empty($uid) && !empty($name)) {
66 $gid = self::getIdByName($uid, $name); // check for dupes
68 // This could be a problem.
69 // Let's assume we've just created a group which we once deleted
70 // all the old members are gone, but the group remains so we don't break any security
71 // access lists. What we're doing here is reviving the dead group, but old content which
72 // was restricted to this group may now be seen by the new group members.
73 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
74 if (DBA::isResult($group) && $group['deleted']) {
75 DBA::update('group', ['deleted' => 0], ['id' => $gid]);
76 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.') . EOL);
81 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
83 $return = DBA::lastInsertId();
90 * Update group information.
92 * @param int $id Group ID
93 * @param string $name Group name
95 * @return bool Was the update successful?
98 public static function update($id, $name)
100 return DBA::update('group', ['name' => $name], ['id' => $id]);
104 * Get a list of group ids a contact belongs to
110 public static function getIdsByContactId($cid)
114 $stmt = DBA::select('group_member', ['gid'], ['contact-id' => $cid]);
115 while ($group = DBA::fetch($stmt)) {
116 $return[] = $group['gid'];
121 $contact = Contact::getById($cid, ['rel']);
122 if ($contact['rel'] == Contact::FOLLOWER || $contact['rel'] == Contact::FRIEND) {
123 $return[] = self::FOLLOWERS;
126 if ($contact['rel'] == Contact::FRIEND) {
127 $return[] = self::MUTUALS;
134 * count unread group items
136 * Count unread items of each groups of the local user
140 * 'name' => group name
141 * 'count' => counted unseen group items
144 public static function countUnseen()
146 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
147 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
153 WHERE `group_member`.`gid` = `group`.`id`)
156 WHERE `group`.`uid` = ?;",
161 return DBA::toArray($stmt);
165 * Get the group id for a user/name couple
167 * Returns false if no group has been found.
170 * @param string $name
171 * @return int|boolean
174 public static function getIdByName($uid, $name)
176 if (!$uid || !strlen($name)) {
180 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
181 if (DBA::isResult($group)) {
189 * Mark a group as deleted
195 public static function remove($gid)
201 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
202 if (!DBA::isResult($group)) {
206 // remove group from default posting lists
207 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
208 if (DBA::isResult($user)) {
211 if ($user['def_gid'] == $gid) {
212 $user['def_gid'] = 0;
215 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
216 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
219 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
220 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
225 DBA::update('user', $user, ['uid' => $group['uid']]);
229 // remove all members
230 DBA::delete('group_member', ['gid' => $gid]);
233 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
239 * Mark a group as deleted based on its name
242 * @param string $name
245 * @deprecated Use Group::remove instead
248 public static function removeByName($uid, $name)
251 if (!empty($uid) && !empty($name)) {
252 $gid = self::getIdByName($uid, $name);
254 $return = self::remove($gid);
261 * Adds a contact to a group
268 public static function addMember($gid, $cid)
270 if (!$gid || !$cid) {
274 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
276 // Row already existing, nothing to do
279 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
286 * Removes a contact from a group
293 public static function removeMember($gid, $cid)
295 if (!$gid || !$cid) {
299 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
305 * Removes a contact from a group based on its name
308 * @param string $name
312 * @deprecated Use Group::removeMember instead
315 public static function removeMemberByName($uid, $name, $cid)
317 $gid = self::getIdByName($uid, $name);
319 $return = self::removeMember($gid, $cid);
325 * Returns the combined list of contact ids from a group id list
328 * @param array $group_ids
329 * @param boolean $check_dead
333 public static function expand($uid, array $group_ids, $check_dead = false)
335 if (!is_array($group_ids) || !count($group_ids)) {
341 $networks = Protocol::SUPPORT_PRIVATE;
343 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $uid]);
344 if (DBA::isResult($mailacct)) {
345 $pubmail = $mailacct['pubmail'];
349 $networks = array_diff($networks, [Protocol::MAIL]);
352 $key = array_search(self::FOLLOWERS, $group_ids);
353 if ($key !== false) {
354 $followers = Contact::selectToArray(['id'], [
356 'rel' => [Contact::FOLLOWER, Contact::FRIEND],
357 'network' => $networks,
358 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
364 foreach ($followers as $follower) {
365 $return[] = $follower['id'];
368 unset($group_ids[$key]);
371 $key = array_search(self::MUTUALS, $group_ids);
372 if ($key !== false) {
373 $mutuals = Contact::selectToArray(['id'], [
375 'rel' => [Contact::FRIEND],
376 'network' => $networks,
377 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
383 foreach ($mutuals as $mutual) {
384 $return[] = $mutual['id'];
387 unset($group_ids[$key]);
390 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
391 while ($group_member = DBA::fetch($stmt)) {
392 $return[] = $group_member['contact-id'];
397 $return = Contact::pruneUnavailable($return);
404 * Returns a templated group selection list
407 * @param int $gid An optional pre-selected group
408 * @param string $label An optional label of the list
412 public static function displayGroupSelection($uid, $gid = 0, $label = '')
422 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
423 while ($group = DBA::fetch($stmt)) {
424 $display_groups[] = [
425 'name' => $group['name'],
426 'id' => $group['id'],
427 'selected' => $gid == $group['id'] ? 'true' : ''
432 Logger::info('Got groups', $display_groups);
435 $label = DI::l10n()->t('Default privacy group for new contacts');
438 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
440 '$groups' => $display_groups
446 * Create group sidebar widget
448 * @param string $every
449 * @param string $each
450 * @param string $editmode
451 * 'standard' => include link 'Edit groups'
452 * 'extended' => include link 'Create new group'
453 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
454 * @param string $group_id
459 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
467 'text' => DI::l10n()->t('Everybody'),
469 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
476 $member_of = self::getIdsByContactId($cid);
479 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
480 while ($group = DBA::fetch($stmt)) {
481 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
483 if ($editmode == 'full') {
485 'href' => 'group/' . $group['id'],
486 'title' => DI::l10n()->t('edit'),
492 $display_groups[] = [
493 'id' => $group['id'],
495 'text' => $group['name'],
496 'href' => $each . '/' . $group['id'],
497 'edit' => $groupedit,
498 'selected' => $selected,
499 'ismember' => in_array($group['id'], $member_of),
504 // Don't show the groups on the network page when there is only one
505 if ((count($display_groups) <= 2) && ($each == 'network')) {
509 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
510 $o = Renderer::replaceMacros($tpl, [
511 '$add' => DI::l10n()->t('add'),
512 '$title' => DI::l10n()->t('Groups'),
513 '$groups' => $display_groups,
514 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
515 'grouppage' => 'group/',
516 '$edittext' => DI::l10n()->t('Edit group'),
517 '$ungrouped' => $every === 'contact' ? DI::l10n()->t('Contacts not in any group') : '',
518 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
519 '$createtext' => DI::l10n()->t('Create a new group'),
520 '$creategroup' => DI::l10n()->t('Group Name: '),
521 '$editgroupstext' => DI::l10n()->t('Edit groups'),
522 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),