3 * @file src/Model/Group.php
5 namespace Friendica\Model;
7 use Friendica\BaseModule;
8 use Friendica\BaseObject;
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
18 class Group extends BaseObject
20 const FOLLOWERS = '~';
23 public static function getByUserId($uid, $includesDeleted = false)
25 $DB = self::getApp()->getDatabase();
27 $conditions = ['uid' => $uid];
29 if (!$includesDeleted) {
30 $conditions['deleted'] = false;
33 $groupsStmt = $DB->select('group', [], $conditions);
35 return $DB->toArray($groupsStmt);
39 * @param int $group_id
43 public static function exists($group_id, $uid = null)
45 $condition = ['id' => $group_id, 'deleted' => false];
53 return DBA::exists('group', $condition);
57 * @brief Create a new contact group
59 * Note: If we found a deleted group with the same name, we restore it
66 public static function create($uid, $name)
69 if (!empty($uid) && !empty($name)) {
70 $gid = self::getIdByName($uid, $name); // check for dupes
72 // This could be a problem.
73 // Let's assume we've just created a group which we once deleted
74 // all the old members are gone, but the group remains so we don't break any security
75 // access lists. What we're doing here is reviving the dead group, but old content which
76 // was restricted to this group may now be seen by the new group members.
77 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
78 if (DBA::isResult($group) && $group['deleted']) {
79 DBA::update('group', ['deleted' => 0], ['id' => $gid]);
80 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);
85 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
87 $return = DBA::lastInsertId();
94 * Update group information.
96 * @param int $id Group ID
97 * @param string $name Group name
99 * @return bool Was the update successful?
102 public static function update($id, $name)
104 return DBA::update('group', ['name' => $name], ['id' => $id]);
108 * @brief Get a list of group ids a contact belongs to
114 public static function getIdsByContactId($cid)
118 $stmt = DBA::select('group_member', ['gid'], ['contact-id' => $cid]);
119 while ($group = DBA::fetch($stmt)) {
120 $return[] = $group['gid'];
128 * @brief count unread group items
130 * Count unread items of each groups of the local user
134 * 'name' => group name
135 * 'count' => counted unseen group items
138 public static function countUnseen()
140 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
141 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
147 WHERE `group_member`.`gid` = `group`.`id`)
150 WHERE `group`.`uid` = ?;",
155 return DBA::toArray($stmt);
159 * @brief Get the group id for a user/name couple
161 * Returns false if no group has been found.
164 * @param string $name
165 * @return int|boolean
168 public static function getIdByName($uid, $name)
170 if (!$uid || !strlen($name)) {
174 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
175 if (DBA::isResult($group)) {
183 * @brief Mark a group as deleted
189 public static function remove($gid) {
194 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
195 if (!DBA::isResult($group)) {
199 // remove group from default posting lists
200 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
201 if (DBA::isResult($user)) {
204 if ($user['def_gid'] == $gid) {
205 $user['def_gid'] = 0;
208 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
209 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
212 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
213 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
218 DBA::update('user', $user, ['uid' => $group['uid']]);
222 // remove all members
223 DBA::delete('group_member', ['gid' => $gid]);
226 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
232 * @brief Mark a group as deleted based on its name
234 * @deprecated Use Group::remove instead
237 * @param string $name
241 public static function removeByName($uid, $name) {
243 if (!empty($uid) && !empty($name)) {
244 $gid = self::getIdByName($uid, $name);
246 $return = self::remove($gid);
253 * @brief Adds a contact to a group
260 public static function addMember($gid, $cid)
262 if (!$gid || !$cid) {
266 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
268 // Row already existing, nothing to do
271 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
278 * @brief Removes a contact from a group
285 public static function removeMember($gid, $cid)
287 if (!$gid || !$cid) {
291 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
297 * @brief Removes a contact from a group based on its name
299 * @deprecated Use Group::removeMember instead
302 * @param string $name
307 public static function removeMemberByName($uid, $name, $cid)
309 $gid = self::getIdByName($uid, $name);
311 $return = self::removeMember($gid, $cid);
317 * @brief Returns the combined list of contact ids from a group id list
320 * @param array $group_ids
321 * @param boolean $check_dead
325 public static function expand($uid, array $group_ids, $check_dead = false)
327 if (!is_array($group_ids) || !count($group_ids)) {
333 $key = array_search(self::FOLLOWERS, $group_ids);
334 if ($key !== false) {
335 $followers = Contact::selectToArray(['id'], [
337 'rel' => [Contact::FOLLOWER, Contact::FRIEND],
338 'protocol' => Protocol::SUPPORT_PRIVATE,
341 foreach ($followers as $follower) {
342 $return[] = $follower['id'];
345 unset($group_ids[$key]);
348 $key = array_search(self::MUTUALS, $group_ids);
349 if ($key !== false) {
350 $mutuals = Contact::selectToArray(['id'], [
352 'rel' => [Contact::FRIEND],
353 'protocol' => Protocol::SUPPORT_PRIVATE,
356 foreach ($mutuals as $mutual) {
357 $return[] = $mutual['id'];
360 unset($group_ids[$key]);
363 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
364 while($group_member = DBA::fetch($stmt)) {
365 $return[] = $group_member['contact-id'];
370 Contact::pruneUnavailable($return);
377 * @brief Returns a templated group selection list
380 * @param int $gid An optional pre-selected group
381 * @param string $label An optional label of the list
385 public static function displayGroupSelection($uid, $gid = 0, $label = '')
395 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
396 while ($group = DBA::fetch($stmt)) {
397 $display_groups[] = [
398 'name' => $group['name'],
399 'id' => $group['id'],
400 'selected' => $gid == $group['id'] ? 'true' : ''
405 Logger::info('groups: ' . print_r($display_groups, true));
408 $label = L10n::t('Default privacy group for new contacts');
411 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
413 '$groups' => $display_groups
419 * @brief Create group sidebar widget
421 * @param string $every
422 * @param string $each
423 * @param string $editmode
424 * 'standard' => include link 'Edit groups'
425 * 'extended' => include link 'Create new group'
426 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
427 * @param string $group_id
432 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
440 'text' => L10n::t('Everybody'),
442 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
449 $member_of = self::getIdsByContactId($cid);
452 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
453 while ($group = DBA::fetch($stmt)) {
454 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
456 if ($editmode == 'full') {
458 'href' => 'group/' . $group['id'],
459 'title' => L10n::t('edit'),
465 $display_groups[] = [
466 'id' => $group['id'],
468 'text' => $group['name'],
469 'href' => $each . '/' . $group['id'],
470 'edit' => $groupedit,
471 'selected' => $selected,
472 'ismember' => in_array($group['id'], $member_of),
477 // Don't show the groups on the network page when there is only one
478 if ((count($display_groups) <= 2) && ($each == 'network')) {
482 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
483 $o = Renderer::replaceMacros($tpl, [
484 '$add' => L10n::t('add'),
485 '$title' => L10n::t('Groups'),
486 '$groups' => $display_groups,
487 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
488 'grouppage' => 'group/',
489 '$edittext' => L10n::t('Edit group'),
490 '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
491 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
492 '$createtext' => L10n::t('Create a new group'),
493 '$creategroup' => L10n::t('Group Name: '),
494 '$editgroupstext' => L10n::t('Edit groups'),
495 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),