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)
116 $condition = ['contact-id' => $cid];
117 $stmt = DBA::select('group_member', ['gid'], $condition);
121 while ($group = DBA::fetch($stmt)) {
122 $return[] = $group['gid'];
129 * @brief count unread group items
131 * Count unread items of each groups of the local user
135 * 'name' => group name
136 * 'count' => counted unseen group items
139 public static function countUnseen()
141 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
142 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
148 WHERE `group_member`.`gid` = `group`.`id`)
151 WHERE `group`.`uid` = ?;",
156 return DBA::toArray($stmt);
160 * @brief Get the group id for a user/name couple
162 * Returns false if no group has been found.
165 * @param string $name
166 * @return int|boolean
169 public static function getIdByName($uid, $name)
171 if (!$uid || !strlen($name)) {
175 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
176 if (DBA::isResult($group)) {
184 * @brief Mark a group as deleted
190 public static function remove($gid) {
195 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
196 if (!DBA::isResult($group)) {
200 // remove group from default posting lists
201 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
202 if (DBA::isResult($user)) {
205 if ($user['def_gid'] == $gid) {
206 $user['def_gid'] = 0;
209 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
210 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
213 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
214 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
219 DBA::update('user', $user, ['uid' => $group['uid']]);
223 // remove all members
224 DBA::delete('group_member', ['gid' => $gid]);
227 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
233 * @brief Mark a group as deleted based on its name
235 * @deprecated Use Group::remove instead
238 * @param string $name
242 public static function removeByName($uid, $name) {
244 if (!empty($uid) && !empty($name)) {
245 $gid = self::getIdByName($uid, $name);
247 $return = self::remove($gid);
254 * @brief Adds a contact to a group
261 public static function addMember($gid, $cid)
263 if (!$gid || !$cid) {
267 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
269 // Row already existing, nothing to do
272 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
279 * @brief Removes a contact from a group
286 public static function removeMember($gid, $cid)
288 if (!$gid || !$cid) {
292 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
298 * @brief Removes a contact from a group based on its name
300 * @deprecated Use Group::removeMember instead
303 * @param string $name
308 public static function removeMemberByName($uid, $name, $cid)
310 $gid = self::getIdByName($uid, $name);
312 $return = self::removeMember($gid, $cid);
318 * @brief Returns the combined list of contact ids from a group id list
321 * @param array $group_ids
322 * @param boolean $check_dead
326 public static function expand($uid, array $group_ids, $check_dead = false)
328 if (!is_array($group_ids) || !count($group_ids)) {
334 $key = array_search(self::FOLLOWERS, $group_ids);
335 if ($key !== false) {
336 $followersStmt = Contact::select(['id'], [
338 'rel' => [Contact::FOLLOWER, Contact::FRIEND],
339 'protocol' => Protocol::NATIVE_SUPPORT,
342 while($follower = DBA::fetch($followersStmt)) {
343 $return[] = $follower['id'];
346 unset($group_ids[$key]);
349 $key = array_search(self::MUTUALS, $group_ids);
350 if ($key !== false) {
351 $mutualsStmt = Contact::select(['id'], [
353 'rel' => [Contact::FRIEND],
354 'protocol' => Protocol::NATIVE_SUPPORT,
357 while($mutual = DBA::fetch($mutualsStmt)) {
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'];
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 = '')
387 $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' : ''
403 Logger::log('groups: ' . print_r($display_groups, true));
406 $label = L10n::t('Default privacy group for new contacts');
409 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
411 '$groups' => $display_groups
417 * @brief Create group sidebar widget
419 * @param string $every
420 * @param string $each
421 * @param string $editmode
422 * 'standard' => include link 'Edit groups'
423 * 'extended' => include link 'Create new group'
424 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
425 * @param string $group_id
430 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
438 'text' => L10n::t('Everybody'),
440 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
445 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
449 $member_of = self::getIdsByContactId($cid);
452 while ($group = DBA::fetch($stmt)) {
453 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
455 if ($editmode == 'full') {
457 'href' => 'group/' . $group['id'],
458 'title' => L10n::t('edit'),
464 $display_groups[] = [
465 'id' => $group['id'],
467 'text' => $group['name'],
468 'href' => $each . '/' . $group['id'],
469 'edit' => $groupedit,
470 'selected' => $selected,
471 'ismember' => in_array($group['id'], $member_of),
475 // Don't show the groups on the network page when there is only one
476 if ((count($display_groups) <= 2) && ($each == 'network')) {
480 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
481 $o = Renderer::replaceMacros($tpl, [
482 '$add' => L10n::t('add'),
483 '$title' => L10n::t('Groups'),
484 '$groups' => $display_groups,
485 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
486 'grouppage' => 'group/',
487 '$edittext' => L10n::t('Edit group'),
488 '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
489 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
490 '$createtext' => L10n::t('Create a new group'),
491 '$creategroup' => L10n::t('Group Name: '),
492 '$editgroupstext' => L10n::t('Edit groups'),
493 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),