3 * @file src/Model/Group.php
5 namespace Friendica\Model;
7 use Friendica\Core\L10n;
8 use Friendica\BaseObject;
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], ['gid' => $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 * @brief Get a list of group ids a contact belongs to
63 public 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::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
123 if (DBM::is_result($group)) {
131 * @brief Mark a group as deleted
136 public static function remove($gid) {
141 $group = dba::selectFirst('group', ['uid'], ['gid' => $gid]);
142 if (!DBM::is_result($group)) {
146 // remove group from default posting lists
147 $user = dba::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
148 if (DBM::is_result($user)) {
151 if ($user['def_gid'] == $gid) {
152 $user['def_gid'] = 0;
155 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
156 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
159 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
160 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
165 dba::update('user', $user, ['uid' => $group['uid']]);
169 // remove all members
170 dba::delete('group_member', ['gid' => $gid]);
173 $return = dba::update('group', ['deleted' => 1], ['id' => $gid]);
179 * @brief Mark a group as deleted based on its name
181 * @deprecated Use Group::remove instead
184 * @param string $name
187 public static function removeByName($uid, $name) {
189 if (x($uid) && x($name)) {
190 $gid = self::getIdByName($uid, $name);
192 $return = self::remove($gid);
199 * @brief Adds a contact to a group
205 public static function addMember($gid, $cid)
207 if (!$gid || !$cid) {
211 $row_exists = dba::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
213 // Row already existing, nothing to do
216 $return = dba::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
223 * @brief Removes a contact from a group
229 public static function removeMember($gid, $cid)
231 if (!$gid || !$cid) {
235 $return = dba::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
241 * @brief Removes a contact from a group based on its name
243 * @deprecated Use Group::removeMember instead
246 * @param string $name
250 public static function removeMemberByName($uid, $name, $cid)
252 $gid = self::getIdByName($uid, $name);
254 $return = self::removeMember($gid, $cid);
260 * @brief Returns the combined list of contact ids from a group id list
262 * @param array $group_ids
263 * @param boolean $check_dead
264 * @param boolean $use_gcontact
267 public static function expand($group_ids, $check_dead = false, $use_gcontact = false)
269 if (!is_array($group_ids) || !count($group_ids)) {
273 $condition = '`gid` IN (' . substr(str_repeat("?, ", count($group_ids)), 0, -2) . ')';
275 $sql = 'SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
276 INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
277 INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
278 WHERE ' . $condition;
279 $param_arr = array_merge([$sql], $group_ids);
280 $stmt = call_user_func_array('dba::p', $param_arr);
282 $condition_array = array_merge([$condition], $group_ids);
283 $stmt = dba::select('group_member', ['contact-id'], $condition_array);
287 while($group_member = dba::fetch($stmt)) {
288 $return[] = $group_member['contact-id'];
291 if ($check_dead && !$use_gcontact) {
292 require_once 'include/acl_selectors.php';
293 $return = prune_deadguys($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 = 0, $cid = 0)
362 'text' => L10n::t('Everybody'),
364 'selected' => (($group_id == 0) ? '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 '$createtext' => L10n::t('Create a new group'),
409 '$creategroup' => L10n::t('Group Name: '),
410 '$editgroupstext' => L10n::t('Edit groups'),
411 '$form_security_token' => get_form_security_token('group_edit'),