4 * @file src/Model/Group.php
7 namespace Friendica\Model;
9 use Friendica\BaseObject;
10 use Friendica\Database\DBM;
13 require_once 'boot.php';
14 require_once 'include/text.php';
17 * @brief functions for interacting with the group database table
19 class Group extends BaseObject
22 * @brief Create a new contact group
24 * Note: If we found a deleted group with the same name, we restore it
30 public static function create($uid, $name)
33 if (x($uid) && x($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::select('group', ['deleted'], ['id' => $gid], ['limit' => 1]);
42 if (DBM::is_result($group) && $group['deleted']) {
43 dba::update('group', ['deleted' => 0], ['gid' => $gid]);
44 notice(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]);
55 * @brief Get a list of group ids a contact belongs to
57 * @todo Get rid of $uid, the contact id already bears the information
63 private static function getByContactIdForUserId($uid, $cid)
65 $stmt = dba::p('SELECT `id`
67 INNER JOIN `group_member`
68 ON `group_member`.`gid` = `group`.`id`
69 WHERE `group`.`uid` = ?
70 AND `group_member`.`contact-id` = ?',
76 if (DBM::is_result($stmt)) {
77 while($group = dba::fetch($stmt)) {
78 $return[] = $group['id'];
86 * @brief count unread group items
88 * Count unread items of each groups of the local user
92 * 'name' => group name
93 * 'count' => counted unseen group items
95 public static function countUnseen()
97 $stmt = dba::p("SELECT `group`.`id`, `group`.`name`,
98 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
104 WHERE `group_member`.`gid` = `group`.`id`
105 AND `group_member`.`uid` = ?)
108 WHERE `group`.`uid` = ?;",
114 return dba::inArray($stmt);
118 * @brief Get the group id for a user/name couple
120 * Returns false if no group has been found.
123 * @param string $name
124 * @return int|boolean
126 public static function getIdByName($uid, $name)
128 if ((! $uid) || (! strlen($name))) {
132 $group = dba::select('group', ['id'], ['uid' => $uid, 'name' => $name], ['limit' => 1]);
133 if (DBM::is_result($group)) {
141 * @brief Mark a group as deleted
146 public static function remove($gid) {
151 // remove group from default posting lists
152 $user = dba::select('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $uid], ['limit' => 1]);
153 if (DBM::is_result($user)) {
156 if ($user['def_gid'] == $gid) {
157 $user['def_gid'] = 0;
160 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
161 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
164 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
165 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
170 dba::update('user', $user, ['uid' => $uid]);
174 // remove all members
175 dba::delete('group_member', ['gid' => $gid]);
178 $return = dba::update('group', ['deleted' => 1], ['id' => $gid]);
184 * @brief Mark a group as deleted based on its name
186 * @deprecated Use Group::remove instead
192 public static function removeByName($uid, $name) {
194 if (x($uid) && x($name)) {
195 $gid = self::getIdByName($uid, $name);
197 $return = self::remove($gid);
204 * @brief Adds a contact to a group
210 public static function addMember($gid, $cid)
212 if (!$gid || !$cid) {
216 $row_exists = dba::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
218 // Row already existing, nothing to do
221 $return = dba::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
228 * @brief Removes a contact from a group
234 public static function removeMember($gid, $cid)
236 if (!$gid || !$cid) {
240 $return = dba::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
246 * @brief Removes a contact from a group based on its name
248 * @deprecated Use Group::removeMember instead
251 * @param string $name
255 public static function removeMemberByName($uid, $name, $cid)
257 $gid = self::getIdByName($uid, $name);
259 $return = self::removeMember($gid, $cid);
265 * @brief Returns the combined list of contact ids from a group id list
267 * @param array $group_ids
268 * @param boolean $check_dead
269 * @param boolean $use_gcontact
272 public static function expand($group_ids, $check_dead = false, $use_gcontact = false)
274 if (!is_array($group_ids) || !count($group_ids)) {
278 $condition = '`gid` IN (' . substr(str_repeat("?, ", count($group_ids)), 0, -2) . ')';
280 $sql = 'SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
281 INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
282 INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
283 WHERE ' . $condition;
284 $param_arr = array_merge([$sql], $group_ids);
285 $stmt = call_user_func_array('dba::p', $param_arr);
287 $condition_array = array_merge([$condition], $group_ids);
288 $stmt = dba::select('group_member', ['contact-id'], $condition_array);
292 while($group_member = dba::fetch($stmt)) {
293 $return[] = $group_member['contact-id'];
296 if ($check_dead && !$use_gcontact) {
297 require_once 'include/acl_selectors.php';
298 $return = prune_deadguys($return);
304 * @brief Returns a templated group selection list
307 * @param int $gid An optional pre-selected group
308 * @param string $label An optional label of the list
311 public static function displayGroupSelection($uid, $gid = 0, $label = '')
315 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name' => 'ASC']]);
324 while ($group = dba::fetch($stmt)) {
325 $display_groups[] = [
326 'name' => $group['name'],
327 'id' => $group['id'],
328 'selected' => $gid == $group['id'] ? 'true' : ''
331 logger('groups: ' . print_r($display_groups, true));
334 $label = t('Default privacy group for new contacts');
337 $o = replace_macros(get_markup_template('group_selection.tpl'), array(
339 '$groups' => $display_groups
345 * @brief Create group sidebar widget
347 * @param string $every
348 * @param string $each
349 * @param string $editmode
350 * 'standard' => include link 'Edit groups'
351 * 'extended' => include link 'Create new group'
352 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
353 * @param int $group_id
357 public static function sidebarWidget($every = 'contacts', $each = 'group', $editmode = 'standard', $group_id = 0, $cid = 0)
367 'text' => t('Everybody'),
369 'selected' => (($group_id == 0) ? 'group-selected' : ''),
374 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name' => 'ASC']]);
376 $member_of = array();
378 $member_of = self::getByContactIdForUserId(local_user(), $cid);
381 while ($group = dba::fetch($stmt)) {
382 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
384 if ($editmode == 'full') {
386 'href' => 'group/' . $group['id'],
387 'title' => t('edit'),
393 $display_groups[] = [
394 'id' => $group['id'],
396 'text' => $group['name'],
397 'href' => $each . '/' . $group['id'],
398 'edit' => $groupedit,
399 'selected' => $selected,
400 'ismember' => in_array($group['id'], $member_of),
404 $tpl = get_markup_template('group_side.tpl');
405 $o = replace_macros($tpl, [
407 '$title' => t('Groups'),
408 '$groups' => $display_groups,
409 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
410 'grouppage' => 'group/',
411 '$edittext' => t('Edit group'),
412 '$ungrouped' => $every === 'contacts' ? t('Contacts not in any group') : '',
413 '$createtext' => t('Create a new group'),
414 '$creategroup' => t('Group Name: '),
415 '$editgroupstext' => t('Edit groups'),
416 '$form_security_token' => get_form_security_token('group_edit'),