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
22 * @param int $group_id
26 public static function exists($group_id, $uid = null)
28 $condition = ['id' => $group_id, 'deleted' => false];
36 return DBA::exists('group', $condition);
40 * @brief Create a new contact group
42 * Note: If we found a deleted group with the same name, we restore it
49 public static function create($uid, $name)
52 if (!empty($uid) && !empty($name)) {
53 $gid = self::getIdByName($uid, $name); // check for dupes
55 // This could be a problem.
56 // Let's assume we've just created a group which we once deleted
57 // all the old members are gone, but the group remains so we don't break any security
58 // access lists. What we're doing here is reviving the dead group, but old content which
59 // was restricted to this group may now be seen by the new group members.
60 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
61 if (DBA::isResult($group) && $group['deleted']) {
62 DBA::update('group', ['deleted' => 0], ['id' => $gid]);
63 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);
68 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
70 $return = DBA::lastInsertId();
77 * Update group information.
79 * @param int $id Group ID
80 * @param string $name Group name
82 * @return bool Was the update successful?
85 public static function update($id, $name)
87 return DBA::update('group', ['name' => $name], ['id' => $id]);
91 * @brief Get a list of group ids a contact belongs to
97 public static function getIdsByContactId($cid)
99 $condition = ['contact-id' => $cid];
100 $stmt = DBA::select('group_member', ['gid'], $condition);
104 while ($group = DBA::fetch($stmt)) {
105 $return[] = $group['gid'];
112 * @brief count unread group items
114 * Count unread items of each groups of the local user
118 * 'name' => group name
119 * 'count' => counted unseen group items
122 public static function countUnseen()
124 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
125 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
131 WHERE `group_member`.`gid` = `group`.`id`)
134 WHERE `group`.`uid` = ?;",
139 return DBA::toArray($stmt);
143 * @brief Get the group id for a user/name couple
145 * Returns false if no group has been found.
148 * @param string $name
149 * @return int|boolean
152 public static function getIdByName($uid, $name)
154 if (!$uid || !strlen($name)) {
158 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
159 if (DBA::isResult($group)) {
167 * @brief Mark a group as deleted
173 public static function remove($gid) {
178 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
179 if (!DBA::isResult($group)) {
183 // remove group from default posting lists
184 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
185 if (DBA::isResult($user)) {
188 if ($user['def_gid'] == $gid) {
189 $user['def_gid'] = 0;
192 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
193 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
196 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
197 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
202 DBA::update('user', $user, ['uid' => $group['uid']]);
206 // remove all members
207 DBA::delete('group_member', ['gid' => $gid]);
210 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
216 * @brief Mark a group as deleted based on its name
218 * @deprecated Use Group::remove instead
221 * @param string $name
225 public static function removeByName($uid, $name) {
227 if (!empty($uid) && !empty($name)) {
228 $gid = self::getIdByName($uid, $name);
230 $return = self::remove($gid);
237 * @brief Adds a contact to a group
244 public static function addMember($gid, $cid)
246 if (!$gid || !$cid) {
250 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
252 // Row already existing, nothing to do
255 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
262 * @brief Removes a contact from a group
269 public static function removeMember($gid, $cid)
271 if (!$gid || !$cid) {
275 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
281 * @brief Removes a contact from a group based on its name
283 * @deprecated Use Group::removeMember instead
286 * @param string $name
291 public static function removeMemberByName($uid, $name, $cid)
293 $gid = self::getIdByName($uid, $name);
295 $return = self::removeMember($gid, $cid);
301 * @brief Returns the combined list of contact ids from a group id list
303 * @param array $group_ids
304 * @param boolean $check_dead
308 public static function expand($group_ids, $check_dead = false)
310 if (!is_array($group_ids) || !count($group_ids)) {
314 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
317 while($group_member = DBA::fetch($stmt)) {
318 $return[] = $group_member['contact-id'];
322 Contact::pruneUnavailable($return);
329 * @brief Returns a templated group selection list
332 * @param int $gid An optional pre-selected group
333 * @param string $label An optional label of the list
335 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
337 public static function displayGroupSelection($uid, $gid = 0, $label = '')
339 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
348 while ($group = DBA::fetch($stmt)) {
349 $display_groups[] = [
350 'name' => $group['name'],
351 'id' => $group['id'],
352 'selected' => $gid == $group['id'] ? 'true' : ''
355 Logger::log('groups: ' . print_r($display_groups, true));
358 $label = L10n::t('Default privacy group for new contacts');
361 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
363 '$groups' => $display_groups
369 * @brief Create group sidebar widget
371 * @param string $every
372 * @param string $each
373 * @param string $editmode
374 * 'standard' => include link 'Edit groups'
375 * 'extended' => include link 'Create new group'
376 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
377 * @param string $group_id
380 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
382 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
390 'text' => L10n::t('Everybody'),
392 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
397 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
401 $member_of = self::getIdsByContactId($cid);
404 while ($group = DBA::fetch($stmt)) {
405 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
407 if ($editmode == 'full') {
409 'href' => 'group/' . $group['id'],
410 'title' => L10n::t('edit'),
416 $display_groups[] = [
417 'id' => $group['id'],
419 'text' => $group['name'],
420 'href' => $each . '/' . $group['id'],
421 'edit' => $groupedit,
422 'selected' => $selected,
423 'ismember' => in_array($group['id'], $member_of),
427 // Don't show the groups on the network page when there is only one
428 if ((count($display_groups) <= 2) && ($each == 'network')) {
432 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
433 $o = Renderer::replaceMacros($tpl, [
434 '$add' => L10n::t('add'),
435 '$title' => L10n::t('Groups'),
436 '$groups' => $display_groups,
437 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
438 'grouppage' => 'group/',
439 '$edittext' => L10n::t('Edit group'),
440 '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
441 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
442 '$createtext' => L10n::t('Create a new group'),
443 '$creategroup' => L10n::t('Group Name: '),
444 '$editgroupstext' => L10n::t('Edit groups'),
445 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),