3 * @file src/Model/Group.php
5 namespace Friendica\Model;
7 use Friendica\BaseObject;
8 use Friendica\Core\L10n;
9 use Friendica\Database\DBM;
12 require_once 'boot.php';
13 require_once 'include/dba.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::selectFirst('group', ['deleted'], ['id' => $gid]);
42 if (DBM::is_result($group) && $group['deleted']) {
43 dba::update('group', ['deleted' => 0], ['id' => $gid]);
44 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);
49 $return = dba::insert('group', ['uid' => $uid, 'name' => $name]);
51 $return = dba::lastInsertId();
58 * Update group information.
60 * @param int $id Group ID
61 * @param string $name Group name
63 * @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
76 public static function getIdsByContactId($cid)
78 $condition = ['contact-id' => $cid];
79 $stmt = dba::select('group_member', ['gid'], $condition);
83 while ($group = dba::fetch($stmt)) {
84 $return[] = $group['gid'];
91 * @brief count unread group items
93 * Count unread items of each groups of the local user
97 * 'name' => group name
98 * 'count' => counted unseen group items
100 public static function countUnseen()
102 $stmt = dba::p("SELECT `group`.`id`, `group`.`name`,
103 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
109 WHERE `group_member`.`gid` = `group`.`id`)
112 WHERE `group`.`uid` = ?;",
117 return dba::inArray($stmt);
121 * @brief Get the group id for a user/name couple
123 * Returns false if no group has been found.
126 * @param string $name
127 * @return int|boolean
129 public static function getIdByName($uid, $name)
131 if (!$uid || !strlen($name)) {
135 $group = dba::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
136 if (DBM::is_result($group)) {
144 * @brief Mark a group as deleted
149 public static function remove($gid) {
154 $group = dba::selectFirst('group', ['uid'], ['id' => $gid]);
155 if (!DBM::is_result($group)) {
159 // remove group from default posting lists
160 $user = dba::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
161 if (DBM::is_result($user)) {
164 if ($user['def_gid'] == $gid) {
165 $user['def_gid'] = 0;
168 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
169 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
172 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
173 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
178 dba::update('user', $user, ['uid' => $group['uid']]);
182 // remove all members
183 dba::delete('group_member', ['gid' => $gid]);
186 $return = dba::update('group', ['deleted' => 1], ['id' => $gid]);
192 * @brief Mark a group as deleted based on its name
194 * @deprecated Use Group::remove instead
197 * @param string $name
200 public static function removeByName($uid, $name) {
202 if (x($uid) && x($name)) {
203 $gid = self::getIdByName($uid, $name);
205 $return = self::remove($gid);
212 * @brief Adds a contact to a group
218 public static function addMember($gid, $cid)
220 if (!$gid || !$cid) {
224 $row_exists = dba::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
226 // Row already existing, nothing to do
229 $return = dba::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
236 * @brief Removes a contact from a group
242 public static function removeMember($gid, $cid)
244 if (!$gid || !$cid) {
248 $return = dba::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
254 * @brief Removes a contact from a group based on its name
256 * @deprecated Use Group::removeMember instead
259 * @param string $name
263 public static function removeMemberByName($uid, $name, $cid)
265 $gid = self::getIdByName($uid, $name);
267 $return = self::removeMember($gid, $cid);
273 * @brief Returns the combined list of contact ids from a group id list
275 * @param array $group_ids
276 * @param boolean $check_dead
277 * @param boolean $use_gcontact
280 public static function expand($group_ids, $check_dead = false, $use_gcontact = false)
282 if (!is_array($group_ids) || !count($group_ids)) {
286 $condition = '`gid` IN (' . substr(str_repeat("?, ", count($group_ids)), 0, -2) . ')';
288 $sql = 'SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
289 INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
290 INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
291 WHERE ' . $condition;
292 $param_arr = array_merge([$sql], $group_ids);
293 $stmt = call_user_func_array('dba::p', $param_arr);
295 $condition_array = array_merge([$condition], $group_ids);
296 $stmt = dba::select('group_member', ['contact-id'], $condition_array);
300 while($group_member = dba::fetch($stmt)) {
301 $return[] = $group_member['contact-id'];
304 if ($check_dead && !$use_gcontact) {
305 Contact::pruneUnavailable($return);
311 * @brief Returns a templated group selection list
314 * @param int $gid An optional pre-selected group
315 * @param string $label An optional label of the list
318 public static function displayGroupSelection($uid, $gid = 0, $label = '')
322 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
331 while ($group = dba::fetch($stmt)) {
332 $display_groups[] = [
333 'name' => $group['name'],
334 'id' => $group['id'],
335 'selected' => $gid == $group['id'] ? 'true' : ''
338 logger('groups: ' . print_r($display_groups, true));
341 $label = L10n::t('Default privacy group for new contacts');
344 $o = replace_macros(get_markup_template('group_selection.tpl'), [
346 '$groups' => $display_groups
352 * @brief Create group sidebar widget
354 * @param string $every
355 * @param string $each
356 * @param string $editmode
357 * 'standard' => include link 'Edit groups'
358 * 'extended' => include link 'Create new group'
359 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
360 * @param int $group_id
364 public static function sidebarWidget($every = 'contacts', $each = 'group', $editmode = 'standard', $group_id = 0, $cid = 0)
374 'text' => L10n::t('Everybody'),
376 'selected' => (($group_id == 0) ? 'group-selected' : ''),
381 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
385 $member_of = self::getIdsByContactId($cid);
388 while ($group = dba::fetch($stmt)) {
389 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
391 if ($editmode == 'full') {
393 'href' => 'group/' . $group['id'],
394 'title' => L10n::t('edit'),
400 $display_groups[] = [
401 'id' => $group['id'],
403 'text' => $group['name'],
404 'href' => $each . '/' . $group['id'],
405 'edit' => $groupedit,
406 'selected' => $selected,
407 'ismember' => in_array($group['id'], $member_of),
411 $tpl = get_markup_template('group_side.tpl');
412 $o = replace_macros($tpl, [
413 '$add' => L10n::t('add'),
414 '$title' => L10n::t('Groups'),
415 '$groups' => $display_groups,
416 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
417 'grouppage' => 'group/',
418 '$edittext' => L10n::t('Edit group'),
419 '$ungrouped' => $every === 'contacts' ? L10n::t('Contacts not in any group') : '',
420 '$createtext' => L10n::t('Create a new group'),
421 '$creategroup' => L10n::t('Group Name: '),
422 '$editgroupstext' => L10n::t('Edit groups'),
423 '$form_security_token' => get_form_security_token('group_edit'),