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\Renderer;
12 use Friendica\Database\DBA;
15 * @brief functions for interacting with the group database table
17 class Group extends BaseObject
19 const FOLLOWERS = '~';
22 public static function getByUserId($uid, $includesDeleted = false)
24 $DB = self::getApp()->getDatabase();
26 $conditions = ['uid' => $uid];
28 if (!$includesDeleted) {
29 $conditions['deleted'] = false;
32 $groupsStmt = $DB->select('group', [], $conditions);
34 return $DB->toArray($groupsStmt);
38 * @param int $group_id
42 public static function exists($group_id, $uid = null)
44 $condition = ['id' => $group_id, 'deleted' => false];
52 return DBA::exists('group', $condition);
56 * @brief Create a new contact group
58 * Note: If we found a deleted group with the same name, we restore it
65 public static function create($uid, $name)
68 if (!empty($uid) && !empty($name)) {
69 $gid = self::getIdByName($uid, $name); // check for dupes
71 // This could be a problem.
72 // Let's assume we've just created a group which we once deleted
73 // all the old members are gone, but the group remains so we don't break any security
74 // access lists. What we're doing here is reviving the dead group, but old content which
75 // was restricted to this group may now be seen by the new group members.
76 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
77 if (DBA::isResult($group) && $group['deleted']) {
78 DBA::update('group', ['deleted' => 0], ['id' => $gid]);
79 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);
84 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
86 $return = DBA::lastInsertId();
93 * Update group information.
95 * @param int $id Group ID
96 * @param string $name Group name
98 * @return bool Was the update successful?
101 public static function update($id, $name)
103 return DBA::update('group', ['name' => $name], ['id' => $id]);
107 * @brief Get a list of group ids a contact belongs to
113 public static function getIdsByContactId($cid)
115 $condition = ['contact-id' => $cid];
116 $stmt = DBA::select('group_member', ['gid'], $condition);
120 while ($group = DBA::fetch($stmt)) {
121 $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 $followersStmt = Contact::select(['id'], ['uid' => $uid, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]]);
337 while($follower = DBA::fetch($followersStmt)) {
338 $return[] = $follower['id'];
341 unset($group_ids[$key]);
344 $key = array_search(self::MUTUALS, $group_ids);
345 if ($key !== false) {
346 $mutualsStmt = Contact::select(['id'], ['uid' => $uid, 'rel' => [Contact::FRIEND]]);
348 while($mutual = DBA::fetch($mutualsStmt)) {
349 $return[] = $mutual['id'];
352 unset($group_ids[$key]);
355 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
356 while($group_member = DBA::fetch($stmt)) {
357 $return[] = $group_member['contact-id'];
361 Contact::pruneUnavailable($return);
368 * @brief Returns a templated group selection list
371 * @param int $gid An optional pre-selected group
372 * @param string $label An optional label of the list
376 public static function displayGroupSelection($uid, $gid = 0, $label = '')
378 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
387 while ($group = DBA::fetch($stmt)) {
388 $display_groups[] = [
389 'name' => $group['name'],
390 'id' => $group['id'],
391 'selected' => $gid == $group['id'] ? 'true' : ''
394 Logger::log('groups: ' . print_r($display_groups, true));
397 $label = L10n::t('Default privacy group for new contacts');
400 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
402 '$groups' => $display_groups
408 * @brief Create group sidebar widget
410 * @param string $every
411 * @param string $each
412 * @param string $editmode
413 * 'standard' => include link 'Edit groups'
414 * 'extended' => include link 'Create new group'
415 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
416 * @param string $group_id
421 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
429 'text' => L10n::t('Everybody'),
431 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
436 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
440 $member_of = self::getIdsByContactId($cid);
443 while ($group = DBA::fetch($stmt)) {
444 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
446 if ($editmode == 'full') {
448 'href' => 'group/' . $group['id'],
449 'title' => L10n::t('edit'),
455 $display_groups[] = [
456 'id' => $group['id'],
458 'text' => $group['name'],
459 'href' => $each . '/' . $group['id'],
460 'edit' => $groupedit,
461 'selected' => $selected,
462 'ismember' => in_array($group['id'], $member_of),
466 // Don't show the groups on the network page when there is only one
467 if ((count($display_groups) <= 2) && ($each == 'network')) {
471 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
472 $o = Renderer::replaceMacros($tpl, [
473 '$add' => L10n::t('add'),
474 '$title' => L10n::t('Groups'),
475 '$groups' => $display_groups,
476 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
477 'grouppage' => 'group/',
478 '$edittext' => L10n::t('Edit group'),
479 '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
480 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
481 '$createtext' => L10n::t('Create a new group'),
482 '$creategroup' => L10n::t('Group Name: '),
483 '$editgroupstext' => L10n::t('Edit groups'),
484 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),