3 * @file src/Model/Group.php
6 namespace Friendica\Model;
8 use Friendica\BaseModule;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Logger;
11 use Friendica\Core\Protocol;
12 use Friendica\Core\Renderer;
13 use Friendica\Database\DBA;
16 * @brief 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 * @brief 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(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 * @brief 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'];
124 * @brief count unread group items
126 * Count unread items of each groups of the local user
130 * 'name' => group name
131 * 'count' => counted unseen group items
134 public static function countUnseen()
136 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
137 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
143 WHERE `group_member`.`gid` = `group`.`id`)
146 WHERE `group`.`uid` = ?;",
151 return DBA::toArray($stmt);
155 * @brief Get the group id for a user/name couple
157 * Returns false if no group has been found.
160 * @param string $name
161 * @return int|boolean
164 public static function getIdByName($uid, $name)
166 if (!$uid || !strlen($name)) {
170 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
171 if (DBA::isResult($group)) {
179 * @brief Mark a group as deleted
185 public static function remove($gid)
191 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
192 if (!DBA::isResult($group)) {
196 // remove group from default posting lists
197 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
198 if (DBA::isResult($user)) {
201 if ($user['def_gid'] == $gid) {
202 $user['def_gid'] = 0;
205 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
206 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
209 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
210 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
215 DBA::update('user', $user, ['uid' => $group['uid']]);
219 // remove all members
220 DBA::delete('group_member', ['gid' => $gid]);
223 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
229 * @brief Mark a group as deleted based on its name
232 * @param string $name
235 * @deprecated Use Group::remove instead
238 public static function removeByName($uid, $name)
241 if (!empty($uid) && !empty($name)) {
242 $gid = self::getIdByName($uid, $name);
244 $return = self::remove($gid);
251 * @brief Adds a contact to a group
258 public static function addMember($gid, $cid)
260 if (!$gid || !$cid) {
264 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
266 // Row already existing, nothing to do
269 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
276 * @brief Removes a contact from a group
283 public static function removeMember($gid, $cid)
285 if (!$gid || !$cid) {
289 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
295 * @brief Removes a contact from a group based on its name
298 * @param string $name
302 * @deprecated Use Group::removeMember instead
305 public static function removeMemberByName($uid, $name, $cid)
307 $gid = self::getIdByName($uid, $name);
309 $return = self::removeMember($gid, $cid);
315 * @brief Returns the combined list of contact ids from a group id list
318 * @param array $group_ids
319 * @param boolean $check_dead
323 public static function expand($uid, array $group_ids, $check_dead = false)
325 if (!is_array($group_ids) || !count($group_ids)) {
331 $networks = Protocol::SUPPORT_PRIVATE;
333 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $uid]);
334 if (DBA::isResult($mailacct)) {
335 $pubmail = $mailacct['pubmail'];
339 $networks = array_diff($networks, [Protocol::MAIL]);
342 $key = array_search(self::FOLLOWERS, $group_ids);
343 if ($key !== false) {
344 $followers = Contact::selectToArray(['id'], [
346 'rel' => [Contact::FOLLOWER, Contact::FRIEND],
347 'network' => $networks,
348 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
354 foreach ($followers as $follower) {
355 $return[] = $follower['id'];
358 unset($group_ids[$key]);
361 $key = array_search(self::MUTUALS, $group_ids);
362 if ($key !== false) {
363 $mutuals = Contact::selectToArray(['id'], [
365 'rel' => [Contact::FRIEND],
366 'network' => $networks,
367 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
373 foreach ($mutuals as $mutual) {
374 $return[] = $mutual['id'];
377 unset($group_ids[$key]);
380 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
381 while ($group_member = DBA::fetch($stmt)) {
382 $return[] = $group_member['contact-id'];
387 $return = Contact::pruneUnavailable($return);
394 * @brief Returns a templated group selection list
397 * @param int $gid An optional pre-selected group
398 * @param string $label An optional label of the list
402 public static function displayGroupSelection($uid, $gid = 0, $label = '')
412 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
413 while ($group = DBA::fetch($stmt)) {
414 $display_groups[] = [
415 'name' => $group['name'],
416 'id' => $group['id'],
417 'selected' => $gid == $group['id'] ? 'true' : ''
422 Logger::info('Got groups', $display_groups);
425 $label = L10n::t('Default privacy group for new contacts');
428 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
430 '$groups' => $display_groups
436 * @brief Create group sidebar widget
438 * @param string $every
439 * @param string $each
440 * @param string $editmode
441 * 'standard' => include link 'Edit groups'
442 * 'extended' => include link 'Create new group'
443 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
444 * @param string $group_id
449 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
457 'text' => L10n::t('Everybody'),
459 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
466 $member_of = self::getIdsByContactId($cid);
469 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
470 while ($group = DBA::fetch($stmt)) {
471 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
473 if ($editmode == 'full') {
475 'href' => 'group/' . $group['id'],
476 'title' => L10n::t('edit'),
482 $display_groups[] = [
483 'id' => $group['id'],
485 'text' => $group['name'],
486 'href' => $each . '/' . $group['id'],
487 'edit' => $groupedit,
488 'selected' => $selected,
489 'ismember' => in_array($group['id'], $member_of),
494 // Don't show the groups on the network page when there is only one
495 if ((count($display_groups) <= 2) && ($each == 'network')) {
499 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
500 $o = Renderer::replaceMacros($tpl, [
501 '$add' => L10n::t('add'),
502 '$title' => L10n::t('Groups'),
503 '$groups' => $display_groups,
504 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
505 'grouppage' => 'group/',
506 '$edittext' => L10n::t('Edit group'),
507 '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
508 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
509 '$createtext' => L10n::t('Create a new group'),
510 '$creategroup' => L10n::t('Group Name: '),
511 '$editgroupstext' => L10n::t('Edit groups'),
512 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),