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;
13 use Friendica\Util\Security;
16 * @brief functions for interacting with the group database table
18 class Group extends BaseObject
21 * @brief Create a new contact group
23 * Note: If we found a deleted group with the same name, we restore it
30 public static function create($uid, $name)
33 if (!empty($uid) && !empty($name)) {
34 $gid = self::getIdByName($uid, $name); // check for dupes
36 // This could be a problem.
37 // Let's assume we've just created a group which we once deleted
38 // all the old members are gone, but the group remains so we don't break any security
39 // access lists. What we're doing here is reviving the dead group, but old content which
40 // was restricted to this group may now be seen by the new group members.
41 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
42 if (DBA::isResult($group) && $group['deleted']) {
43 DBA::update('group', ['deleted' => 0], ['id' => $gid]);
44 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);
49 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
51 $return = DBA::lastInsertId();
58 * Update group information.
60 * @param int $id Group ID
61 * @param string $name Group name
63 * @return bool Was the update successful?
66 public static function update($id, $name)
68 return DBA::update('group', ['name' => $name], ['id' => $id]);
72 * @brief Get a list of group ids a contact belongs to
78 public static function getIdsByContactId($cid)
80 $condition = ['contact-id' => $cid];
81 $stmt = DBA::select('group_member', ['gid'], $condition);
85 while ($group = DBA::fetch($stmt)) {
86 $return[] = $group['gid'];
93 * @brief count unread group items
95 * Count unread items of each groups of the local user
99 * 'name' => group name
100 * 'count' => counted unseen group items
103 public static function countUnseen()
105 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
106 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
112 WHERE `group_member`.`gid` = `group`.`id`)
115 WHERE `group`.`uid` = ?;",
120 return DBA::toArray($stmt);
124 * @brief Get the group id for a user/name couple
126 * Returns false if no group has been found.
129 * @param string $name
130 * @return int|boolean
133 public static function getIdByName($uid, $name)
135 if (!$uid || !strlen($name)) {
139 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
140 if (DBA::isResult($group)) {
148 * @brief Mark a group as deleted
154 public static function remove($gid) {
159 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
160 if (!DBA::isResult($group)) {
164 // remove group from default posting lists
165 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
166 if (DBA::isResult($user)) {
169 if ($user['def_gid'] == $gid) {
170 $user['def_gid'] = 0;
173 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
174 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
177 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
178 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
183 DBA::update('user', $user, ['uid' => $group['uid']]);
187 // remove all members
188 DBA::delete('group_member', ['gid' => $gid]);
191 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
197 * @brief Mark a group as deleted based on its name
199 * @deprecated Use Group::remove instead
202 * @param string $name
206 public static function removeByName($uid, $name) {
208 if (!empty($uid) && !empty($name)) {
209 $gid = self::getIdByName($uid, $name);
211 $return = self::remove($gid);
218 * @brief Adds a contact to a group
225 public static function addMember($gid, $cid)
227 if (!$gid || !$cid) {
231 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
233 // Row already existing, nothing to do
236 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
243 * @brief Removes a contact from a group
250 public static function removeMember($gid, $cid)
252 if (!$gid || !$cid) {
256 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
262 * @brief Removes a contact from a group based on its name
264 * @deprecated Use Group::removeMember instead
267 * @param string $name
272 public static function removeMemberByName($uid, $name, $cid)
274 $gid = self::getIdByName($uid, $name);
276 $return = self::removeMember($gid, $cid);
282 * @brief Returns the combined list of contact ids from a group id list
284 * @param array $group_ids
285 * @param boolean $check_dead
289 public static function expand($group_ids, $check_dead = false)
291 if (!is_array($group_ids) || !count($group_ids)) {
295 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
298 while($group_member = DBA::fetch($stmt)) {
299 $return[] = $group_member['contact-id'];
303 Contact::pruneUnavailable($return);
310 * @brief Returns a templated group selection list
313 * @param int $gid An optional pre-selected group
314 * @param string $label An optional label of the list
316 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
318 public static function displayGroupSelection($uid, $gid = 0, $label = '')
322 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
331 while ($group = DBA::fetch($stmt)) {
332 $display_groups[] = [
333 'name' => $group['name'],
334 'id' => $group['id'],
335 'selected' => $gid == $group['id'] ? 'true' : ''
338 Logger::log('groups: ' . print_r($display_groups, true));
341 $label = L10n::t('Default privacy group for new contacts');
344 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
346 '$groups' => $display_groups
352 * @brief Create group sidebar widget
354 * @param string $every
355 * @param string $each
356 * @param string $editmode
357 * 'standard' => include link 'Edit groups'
358 * 'extended' => include link 'Create new group'
359 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
360 * @param string $group_id
363 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
365 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
373 'text' => L10n::t('Everybody'),
375 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
380 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
384 $member_of = self::getIdsByContactId($cid);
387 while ($group = DBA::fetch($stmt)) {
388 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
390 if ($editmode == 'full') {
392 'href' => 'group/' . $group['id'],
393 'title' => L10n::t('edit'),
399 $display_groups[] = [
400 'id' => $group['id'],
402 'text' => $group['name'],
403 'href' => $each . '/' . $group['id'],
404 'edit' => $groupedit,
405 'selected' => $selected,
406 'ismember' => in_array($group['id'], $member_of),
410 // Don't show the groups on the network page when there is only one
411 if ((count($display_groups) <= 2) && ($each == 'network')) {
415 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
416 $o = Renderer::replaceMacros($tpl, [
417 '$add' => L10n::t('add'),
418 '$title' => L10n::t('Groups'),
419 '$groups' => $display_groups,
420 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
421 'grouppage' => 'group/',
422 '$edittext' => L10n::t('Edit group'),
423 '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
424 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
425 '$createtext' => L10n::t('Create a new group'),
426 '$creategroup' => L10n::t('Group Name: '),
427 '$editgroupstext' => L10n::t('Edit groups'),
428 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),