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
20 * @brief Create a new contact group
22 * Note: If we found a deleted group with the same name, we restore it
29 public static function create($uid, $name)
32 if (!empty($uid) && !empty($name)) {
33 $gid = self::getIdByName($uid, $name); // check for dupes
35 // This could be a problem.
36 // Let's assume we've just created a group which we once deleted
37 // all the old members are gone, but the group remains so we don't break any security
38 // access lists. What we're doing here is reviving the dead group, but old content which
39 // was restricted to this group may now be seen by the new group members.
40 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
41 if (DBA::isResult($group) && $group['deleted']) {
42 DBA::update('group', ['deleted' => 0], ['id' => $gid]);
43 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);
48 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
50 $return = DBA::lastInsertId();
57 * Update group information.
59 * @param int $id Group ID
60 * @param string $name Group name
62 * @return bool Was the update successful?
65 public static function update($id, $name)
67 return DBA::update('group', ['name' => $name], ['id' => $id]);
71 * @brief Get a list of group ids a contact belongs to
77 public static function getIdsByContactId($cid)
79 $condition = ['contact-id' => $cid];
80 $stmt = DBA::select('group_member', ['gid'], $condition);
84 while ($group = DBA::fetch($stmt)) {
85 $return[] = $group['gid'];
92 * @brief count unread group items
94 * Count unread items of each groups of the local user
98 * 'name' => group name
99 * 'count' => counted unseen group items
102 public static function countUnseen()
104 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
105 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
111 WHERE `group_member`.`gid` = `group`.`id`)
114 WHERE `group`.`uid` = ?;",
119 return DBA::toArray($stmt);
123 * @brief Get the group id for a user/name couple
125 * Returns false if no group has been found.
128 * @param string $name
129 * @return int|boolean
132 public static function getIdByName($uid, $name)
134 if (!$uid || !strlen($name)) {
138 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
139 if (DBA::isResult($group)) {
147 * @brief Mark a group as deleted
153 public static function remove($gid) {
158 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
159 if (!DBA::isResult($group)) {
163 // remove group from default posting lists
164 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
165 if (DBA::isResult($user)) {
168 if ($user['def_gid'] == $gid) {
169 $user['def_gid'] = 0;
172 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
173 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
176 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
177 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
182 DBA::update('user', $user, ['uid' => $group['uid']]);
186 // remove all members
187 DBA::delete('group_member', ['gid' => $gid]);
190 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
196 * @brief Mark a group as deleted based on its name
198 * @deprecated Use Group::remove instead
201 * @param string $name
205 public static function removeByName($uid, $name) {
207 if (!empty($uid) && !empty($name)) {
208 $gid = self::getIdByName($uid, $name);
210 $return = self::remove($gid);
217 * @brief Adds a contact to a group
224 public static function addMember($gid, $cid)
226 if (!$gid || !$cid) {
230 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
232 // Row already existing, nothing to do
235 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
242 * @brief Removes a contact from a group
249 public static function removeMember($gid, $cid)
251 if (!$gid || !$cid) {
255 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
261 * @brief Removes a contact from a group based on its name
263 * @deprecated Use Group::removeMember instead
266 * @param string $name
271 public static function removeMemberByName($uid, $name, $cid)
273 $gid = self::getIdByName($uid, $name);
275 $return = self::removeMember($gid, $cid);
281 * @brief Returns the combined list of contact ids from a group id list
283 * @param array $group_ids
284 * @param boolean $check_dead
288 public static function expand($group_ids, $check_dead = false)
290 if (!is_array($group_ids) || !count($group_ids)) {
294 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
297 while($group_member = DBA::fetch($stmt)) {
298 $return[] = $group_member['contact-id'];
302 Contact::pruneUnavailable($return);
309 * @brief Returns a templated group selection list
312 * @param int $gid An optional pre-selected group
313 * @param string $label An optional label of the list
315 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
317 public static function displayGroupSelection($uid, $gid = 0, $label = '')
319 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
328 while ($group = DBA::fetch($stmt)) {
329 $display_groups[] = [
330 'name' => $group['name'],
331 'id' => $group['id'],
332 'selected' => $gid == $group['id'] ? 'true' : ''
335 Logger::log('groups: ' . print_r($display_groups, true));
338 $label = L10n::t('Default privacy group for new contacts');
341 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
343 '$groups' => $display_groups
349 * @brief Create group sidebar widget
351 * @param string $every
352 * @param string $each
353 * @param string $editmode
354 * 'standard' => include link 'Edit groups'
355 * 'extended' => include link 'Create new group'
356 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
357 * @param string $group_id
360 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
362 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
370 'text' => L10n::t('Everybody'),
372 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
377 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
381 $member_of = self::getIdsByContactId($cid);
384 while ($group = DBA::fetch($stmt)) {
385 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
387 if ($editmode == 'full') {
389 'href' => 'group/' . $group['id'],
390 'title' => L10n::t('edit'),
396 $display_groups[] = [
397 'id' => $group['id'],
399 'text' => $group['name'],
400 'href' => $each . '/' . $group['id'],
401 'edit' => $groupedit,
402 'selected' => $selected,
403 'ismember' => in_array($group['id'], $member_of),
407 // Don't show the groups on the network page when there is only one
408 if ((count($display_groups) <= 2) && ($each == 'network')) {
412 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
413 $o = Renderer::replaceMacros($tpl, [
414 '$add' => L10n::t('add'),
415 '$title' => L10n::t('Groups'),
416 '$groups' => $display_groups,
417 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
418 'grouppage' => 'group/',
419 '$edittext' => L10n::t('Edit group'),
420 '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
421 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
422 '$createtext' => L10n::t('Create a new group'),
423 '$creategroup' => L10n::t('Group Name: '),
424 '$editgroupstext' => L10n::t('Edit groups'),
425 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),