3 * @file src/Model/Group.php
6 namespace Friendica\Model;
8 use Friendica\BaseModule;
9 use Friendica\BaseObject;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\Renderer;
14 use Friendica\Database\DBA;
17 * @brief functions for interacting with the group database table
19 class Group extends BaseObject
21 const FOLLOWERS = '~';
24 public static function getByUserId($uid, $includesDeleted = false)
26 $conditions = ['uid' => $uid];
28 if (!$includesDeleted) {
29 $conditions['deleted'] = false;
32 return DBA::selectToArray('group', [], $conditions);
36 * @param int $group_id
40 public static function exists($group_id, $uid = null)
42 $condition = ['id' => $group_id, 'deleted' => false];
50 return DBA::exists('group', $condition);
54 * @brief Create a new contact group
56 * Note: If we found a deleted group with the same name, we restore it
63 public static function create($uid, $name)
66 if (!empty($uid) && !empty($name)) {
67 $gid = self::getIdByName($uid, $name); // check for dupes
69 // This could be a problem.
70 // Let's assume we've just created a group which we once deleted
71 // all the old members are gone, but the group remains so we don't break any security
72 // access lists. What we're doing here is reviving the dead group, but old content which
73 // was restricted to this group may now be seen by the new group members.
74 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
75 if (DBA::isResult($group) && $group['deleted']) {
76 DBA::update('group', ['deleted' => 0], ['id' => $gid]);
77 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);
82 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
84 $return = DBA::lastInsertId();
91 * Update group information.
93 * @param int $id Group ID
94 * @param string $name Group name
96 * @return bool Was the update successful?
99 public static function update($id, $name)
101 return DBA::update('group', ['name' => $name], ['id' => $id]);
105 * @brief Get a list of group ids a contact belongs to
111 public static function getIdsByContactId($cid)
115 $stmt = DBA::select('group_member', ['gid'], ['contact-id' => $cid]);
116 while ($group = DBA::fetch($stmt)) {
117 $return[] = $group['gid'];
125 * @brief count unread group items
127 * Count unread items of each groups of the local user
131 * 'name' => group name
132 * 'count' => counted unseen group items
135 public static function countUnseen()
137 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
138 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
144 WHERE `group_member`.`gid` = `group`.`id`)
147 WHERE `group`.`uid` = ?;",
152 return DBA::toArray($stmt);
156 * @brief Get the group id for a user/name couple
158 * Returns false if no group has been found.
161 * @param string $name
162 * @return int|boolean
165 public static function getIdByName($uid, $name)
167 if (!$uid || !strlen($name)) {
171 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
172 if (DBA::isResult($group)) {
180 * @brief Mark a group as deleted
186 public static function remove($gid)
192 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
193 if (!DBA::isResult($group)) {
197 // remove group from default posting lists
198 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
199 if (DBA::isResult($user)) {
202 if ($user['def_gid'] == $gid) {
203 $user['def_gid'] = 0;
206 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
207 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
210 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
211 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
216 DBA::update('user', $user, ['uid' => $group['uid']]);
220 // remove all members
221 DBA::delete('group_member', ['gid' => $gid]);
224 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
230 * @brief Mark a group as deleted based on its name
233 * @param string $name
236 * @deprecated Use Group::remove instead
239 public static function removeByName($uid, $name)
242 if (!empty($uid) && !empty($name)) {
243 $gid = self::getIdByName($uid, $name);
245 $return = self::remove($gid);
252 * @brief Adds a contact to a group
259 public static function addMember($gid, $cid)
261 if (!$gid || !$cid) {
265 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
267 // Row already existing, nothing to do
270 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
277 * @brief Removes a contact from a group
284 public static function removeMember($gid, $cid)
286 if (!$gid || !$cid) {
290 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
296 * @brief Removes a contact from a group based on its name
299 * @param string $name
303 * @deprecated Use Group::removeMember instead
306 public static function removeMemberByName($uid, $name, $cid)
308 $gid = self::getIdByName($uid, $name);
310 $return = self::removeMember($gid, $cid);
316 * @brief Returns the combined list of contact ids from a group id list
319 * @param array $group_ids
320 * @param boolean $check_dead
324 public static function expand($uid, array $group_ids, $check_dead = false)
326 if (!is_array($group_ids) || !count($group_ids)) {
332 $key = array_search(self::FOLLOWERS, $group_ids);
333 if ($key !== false) {
334 $followers = Contact::selectToArray(['id'], [
336 'rel' => [Contact::FOLLOWER, Contact::FRIEND],
337 'network' => Protocol::SUPPORT_PRIVATE,
340 foreach ($followers as $follower) {
341 $return[] = $follower['id'];
344 unset($group_ids[$key]);
347 $key = array_search(self::MUTUALS, $group_ids);
348 if ($key !== false) {
349 $mutuals = Contact::selectToArray(['id'], [
351 'rel' => [Contact::FRIEND],
352 'network' => Protocol::SUPPORT_PRIVATE,
355 foreach ($mutuals as $mutual) {
356 $return[] = $mutual['id'];
359 unset($group_ids[$key]);
362 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
363 while ($group_member = DBA::fetch($stmt)) {
364 $return[] = $group_member['contact-id'];
369 Contact::pruneUnavailable($return);
376 * @brief Returns a templated group selection list
379 * @param int $gid An optional pre-selected group
380 * @param string $label An optional label of the list
384 public static function displayGroupSelection($uid, $gid = 0, $label = '')
394 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
395 while ($group = DBA::fetch($stmt)) {
396 $display_groups[] = [
397 'name' => $group['name'],
398 'id' => $group['id'],
399 'selected' => $gid == $group['id'] ? 'true' : ''
404 Logger::info('Got groups', $display_groups);
407 $label = L10n::t('Default privacy group for new contacts');
410 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
412 '$groups' => $display_groups
418 * @brief Create group sidebar widget
420 * @param string $every
421 * @param string $each
422 * @param string $editmode
423 * 'standard' => include link 'Edit groups'
424 * 'extended' => include link 'Create new group'
425 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
426 * @param string $group_id
431 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
439 'text' => L10n::t('Everybody'),
441 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
448 $member_of = self::getIdsByContactId($cid);
451 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
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),
476 // Don't show the groups on the network page when there is only one
477 if ((count($display_groups) <= 2) && ($each == 'network')) {
481 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
482 $o = Renderer::replaceMacros($tpl, [
483 '$add' => L10n::t('add'),
484 '$title' => L10n::t('Groups'),
485 '$groups' => $display_groups,
486 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
487 'grouppage' => 'group/',
488 '$edittext' => L10n::t('Edit group'),
489 '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
490 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
491 '$createtext' => L10n::t('Create a new group'),
492 '$creategroup' => L10n::t('Group Name: '),
493 '$editgroupstext' => L10n::t('Edit groups'),
494 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),