3 * @file src/Model/Group.php
5 namespace Friendica\Model;
7 use Friendica\BaseObject;
8 use Friendica\Core\L10n;
9 use Friendica\Database\DBA;
11 require_once 'boot.php';
12 require_once 'include/dba.php';
13 require_once 'include/text.php';
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
29 public static function create($uid, $name)
32 if (x($uid) && x($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?
64 public static function update($id, $name)
66 return DBA::update('group', ['name' => $name], ['id' => $id]);
70 * @brief Get a list of group ids a contact belongs to
75 public static function getIdsByContactId($cid)
77 $condition = ['contact-id' => $cid];
78 $stmt = DBA::select('group_member', ['gid'], $condition);
82 while ($group = DBA::fetch($stmt)) {
83 $return[] = $group['gid'];
90 * @brief count unread group items
92 * Count unread items of each groups of the local user
96 * 'name' => group name
97 * 'count' => counted unseen group items
99 public static function countUnseen()
101 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
102 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
108 WHERE `group_member`.`gid` = `group`.`id`)
111 WHERE `group`.`uid` = ?;",
116 return DBA::toArray($stmt);
120 * @brief Get the group id for a user/name couple
122 * Returns false if no group has been found.
125 * @param string $name
126 * @return int|boolean
128 public static function getIdByName($uid, $name)
130 if (!$uid || !strlen($name)) {
134 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
135 if (DBA::isResult($group)) {
143 * @brief Mark a group as deleted
148 public static function remove($gid) {
153 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
154 if (!DBA::isResult($group)) {
158 // remove group from default posting lists
159 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
160 if (DBA::isResult($user)) {
163 if ($user['def_gid'] == $gid) {
164 $user['def_gid'] = 0;
167 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
168 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
171 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
172 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
177 DBA::update('user', $user, ['uid' => $group['uid']]);
181 // remove all members
182 DBA::delete('group_member', ['gid' => $gid]);
185 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
191 * @brief Mark a group as deleted based on its name
193 * @deprecated Use Group::remove instead
196 * @param string $name
199 public static function removeByName($uid, $name) {
201 if (x($uid) && x($name)) {
202 $gid = self::getIdByName($uid, $name);
204 $return = self::remove($gid);
211 * @brief Adds a contact to a group
217 public static function addMember($gid, $cid)
219 if (!$gid || !$cid) {
223 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
225 // Row already existing, nothing to do
228 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
235 * @brief Removes a contact from a group
241 public static function removeMember($gid, $cid)
243 if (!$gid || !$cid) {
247 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
253 * @brief Removes a contact from a group based on its name
255 * @deprecated Use Group::removeMember instead
258 * @param string $name
262 public static function removeMemberByName($uid, $name, $cid)
264 $gid = self::getIdByName($uid, $name);
266 $return = self::removeMember($gid, $cid);
272 * @brief Returns the combined list of contact ids from a group id list
274 * @param array $group_ids
275 * @param boolean $check_dead
278 public static function expand($group_ids, $check_dead = false)
280 if (!is_array($group_ids) || !count($group_ids)) {
284 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
287 while($group_member = DBA::fetch($stmt)) {
288 $return[] = $group_member['contact-id'];
292 Contact::pruneUnavailable($return);
299 * @brief Returns a templated group selection list
302 * @param int $gid An optional pre-selected group
303 * @param string $label An optional label of the list
306 public static function displayGroupSelection($uid, $gid = 0, $label = '')
310 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
319 while ($group = DBA::fetch($stmt)) {
320 $display_groups[] = [
321 'name' => $group['name'],
322 'id' => $group['id'],
323 'selected' => $gid == $group['id'] ? 'true' : ''
326 logger('groups: ' . print_r($display_groups, true));
329 $label = L10n::t('Default privacy group for new contacts');
332 $o = replace_macros(get_markup_template('group_selection.tpl'), [
334 '$groups' => $display_groups
340 * @brief Create group sidebar widget
342 * @param string $every
343 * @param string $each
344 * @param string $editmode
345 * 'standard' => include link 'Edit groups'
346 * 'extended' => include link 'Create new group'
347 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
348 * @param int $group_id
352 public static function sidebarWidget($every = 'contacts', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
362 'text' => L10n::t('Everybody'),
364 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
369 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
373 $member_of = self::getIdsByContactId($cid);
376 while ($group = DBA::fetch($stmt)) {
377 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
379 if ($editmode == 'full') {
381 'href' => 'group/' . $group['id'],
382 'title' => L10n::t('edit'),
388 $display_groups[] = [
389 'id' => $group['id'],
391 'text' => $group['name'],
392 'href' => $each . '/' . $group['id'],
393 'edit' => $groupedit,
394 'selected' => $selected,
395 'ismember' => in_array($group['id'], $member_of),
399 $tpl = get_markup_template('group_side.tpl');
400 $o = replace_macros($tpl, [
401 '$add' => L10n::t('add'),
402 '$title' => L10n::t('Groups'),
403 '$groups' => $display_groups,
404 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
405 'grouppage' => 'group/',
406 '$edittext' => L10n::t('Edit group'),
407 '$ungrouped' => $every === 'contacts' ? L10n::t('Contacts not in any group') : '',
408 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
409 '$createtext' => L10n::t('Create a new group'),
410 '$creategroup' => L10n::t('Group Name: '),
411 '$editgroupstext' => L10n::t('Edit groups'),
412 '$form_security_token' => get_form_security_token('group_edit'),