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]);
51 $return = dba::lastInsertId();
58 * @brief Get a list of group ids a contact belongs to
63 private static function getIdsByContactId($cid)
65 $condition = ['contact-id' => $cid];
66 $stmt = dba::select('group_member', ['gid'], $condition);
70 while ($group = dba::fetch($stmt)) {
71 $return[] = $group['gid'];
78 * @brief count unread group items
80 * Count unread items of each groups of the local user
84 * 'name' => group name
85 * 'count' => counted unseen group items
87 public static function countUnseen()
89 $stmt = dba::p("SELECT `group`.`id`, `group`.`name`,
90 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
96 WHERE `group_member`.`gid` = `group`.`id`)
99 WHERE `group`.`uid` = ?;",
104 return dba::inArray($stmt);
108 * @brief Get the group id for a user/name couple
110 * Returns false if no group has been found.
113 * @param string $name
114 * @return int|boolean
116 public static function getIdByName($uid, $name)
118 if (!$uid || !strlen($name)) {
122 $group = dba::select('group', ['id'], ['uid' => $uid, 'name' => $name], ['limit' => 1]);
123 if (DBM::is_result($group)) {
131 * @brief Mark a group as deleted
136 public static function remove($gid) {
141 // remove group from default posting lists
142 $user = dba::select('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $uid], ['limit' => 1]);
143 if (DBM::is_result($user)) {
146 if ($user['def_gid'] == $gid) {
147 $user['def_gid'] = 0;
150 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
151 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
154 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
155 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
160 dba::update('user', $user, ['uid' => $uid]);
164 // remove all members
165 dba::delete('group_member', ['gid' => $gid]);
168 $return = dba::update('group', ['deleted' => 1], ['id' => $gid]);
174 * @brief Mark a group as deleted based on its name
176 * @deprecated Use Group::remove instead
182 public static function removeByName($uid, $name) {
184 if (x($uid) && x($name)) {
185 $gid = self::getIdByName($uid, $name);
187 $return = self::remove($gid);
194 * @brief Adds a contact to a group
200 public static function addMember($gid, $cid)
202 if (!$gid || !$cid) {
206 $row_exists = dba::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
208 // Row already existing, nothing to do
211 $return = dba::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
218 * @brief Removes a contact from a group
224 public static function removeMember($gid, $cid)
226 if (!$gid || !$cid) {
230 $return = dba::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
236 * @brief Removes a contact from a group based on its name
238 * @deprecated Use Group::removeMember instead
241 * @param string $name
245 public static function removeMemberByName($uid, $name, $cid)
247 $gid = self::getIdByName($uid, $name);
249 $return = self::removeMember($gid, $cid);
255 * @brief Returns the combined list of contact ids from a group id list
257 * @param array $group_ids
258 * @param boolean $check_dead
259 * @param boolean $use_gcontact
262 public static function expand($group_ids, $check_dead = false, $use_gcontact = false)
264 if (!is_array($group_ids) || !count($group_ids)) {
268 $condition = '`gid` IN (' . substr(str_repeat("?, ", count($group_ids)), 0, -2) . ')';
270 $sql = 'SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
271 INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
272 INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
273 WHERE ' . $condition;
274 $param_arr = array_merge([$sql], $group_ids);
275 $stmt = call_user_func_array('dba::p', $param_arr);
277 $condition_array = array_merge([$condition], $group_ids);
278 $stmt = dba::select('group_member', ['contact-id'], $condition_array);
282 while($group_member = dba::fetch($stmt)) {
283 $return[] = $group_member['contact-id'];
286 if ($check_dead && !$use_gcontact) {
287 require_once 'include/acl_selectors.php';
288 $return = prune_deadguys($return);
294 * @brief Returns a templated group selection list
297 * @param int $gid An optional pre-selected group
298 * @param string $label An optional label of the list
301 public static function displayGroupSelection($uid, $gid = 0, $label = '')
305 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
314 while ($group = dba::fetch($stmt)) {
315 $display_groups[] = [
316 'name' => $group['name'],
317 'id' => $group['id'],
318 'selected' => $gid == $group['id'] ? 'true' : ''
321 logger('groups: ' . print_r($display_groups, true));
324 $label = t('Default privacy group for new contacts');
327 $o = replace_macros(get_markup_template('group_selection.tpl'), array(
329 '$groups' => $display_groups
335 * @brief Create group sidebar widget
337 * @param string $every
338 * @param string $each
339 * @param string $editmode
340 * 'standard' => include link 'Edit groups'
341 * 'extended' => include link 'Create new group'
342 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
343 * @param int $group_id
347 public static function sidebarWidget($every = 'contacts', $each = 'group', $editmode = 'standard', $group_id = 0, $cid = 0)
357 'text' => t('Everybody'),
359 'selected' => (($group_id == 0) ? 'group-selected' : ''),
364 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
366 $member_of = array();
368 $member_of = self::getIdsByContactId($cid);
371 while ($group = dba::fetch($stmt)) {
372 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
374 if ($editmode == 'full') {
376 'href' => 'group/' . $group['id'],
377 'title' => t('edit'),
383 $display_groups[] = [
384 'id' => $group['id'],
386 'text' => $group['name'],
387 'href' => $each . '/' . $group['id'],
388 'edit' => $groupedit,
389 'selected' => $selected,
390 'ismember' => in_array($group['id'], $member_of),
394 $tpl = get_markup_template('group_side.tpl');
395 $o = replace_macros($tpl, [
397 '$title' => t('Groups'),
398 '$groups' => $display_groups,
399 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
400 'grouppage' => 'group/',
401 '$edittext' => t('Edit group'),
402 '$ungrouped' => $every === 'contacts' ? t('Contacts not in any group') : '',
403 '$createtext' => t('Create a new group'),
404 '$creategroup' => t('Group Name: '),
405 '$editgroupstext' => t('Edit groups'),
406 '$form_security_token' => get_form_security_token('group_edit'),