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/dba.php';
15 require_once 'include/text.php';
18 * @brief functions for interacting with the group database table
20 class Group extends BaseObject
23 * @brief Create a new contact group
25 * Note: If we found a deleted group with the same name, we restore it
31 public static function create($uid, $name)
34 if (x($uid) && x($name)) {
35 $gid = self::getIdByName($uid, $name); // check for dupes
37 // This could be a problem.
38 // Let's assume we've just created a group which we once deleted
39 // all the old members are gone, but the group remains so we don't break any security
40 // access lists. What we're doing here is reviving the dead group, but old content which
41 // was restricted to this group may now be seen by the new group members.
42 $group = dba::selectFirst('group', ['deleted'], ['id' => $gid]);
43 if (DBM::is_result($group) && $group['deleted']) {
44 dba::update('group', ['deleted' => 0], ['gid' => $gid]);
45 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);
50 $return = dba::insert('group', ['uid' => $uid, 'name' => $name]);
52 $return = dba::lastInsertId();
59 * @brief Get a list of group ids a contact belongs to
64 public static function getIdsByContactId($cid)
66 $condition = ['contact-id' => $cid];
67 $stmt = dba::select('group_member', ['gid'], $condition);
71 while ($group = dba::fetch($stmt)) {
72 $return[] = $group['gid'];
79 * @brief count unread group items
81 * Count unread items of each groups of the local user
85 * 'name' => group name
86 * 'count' => counted unseen group items
88 public static function countUnseen()
90 $stmt = dba::p("SELECT `group`.`id`, `group`.`name`,
91 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
97 WHERE `group_member`.`gid` = `group`.`id`)
100 WHERE `group`.`uid` = ?;",
105 return dba::inArray($stmt);
109 * @brief Get the group id for a user/name couple
111 * Returns false if no group has been found.
114 * @param string $name
115 * @return int|boolean
117 public static function getIdByName($uid, $name)
119 if (!$uid || !strlen($name)) {
123 $group = dba::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
124 if (DBM::is_result($group)) {
132 * @brief Mark a group as deleted
137 public static function remove($gid) {
142 $group = dba::selectFirst('group', ['uid'], ['gid' => $gid]);
143 if (!DBM::is_result($group)) {
147 // remove group from default posting lists
148 $user = dba::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
149 if (DBM::is_result($user)) {
152 if ($user['def_gid'] == $gid) {
153 $user['def_gid'] = 0;
156 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
157 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
160 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
161 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
166 dba::update('user', $user, ['uid' => $group['uid']]);
170 // remove all members
171 dba::delete('group_member', ['gid' => $gid]);
174 $return = dba::update('group', ['deleted' => 1], ['id' => $gid]);
180 * @brief Mark a group as deleted based on its name
182 * @deprecated Use Group::remove instead
185 * @param string $name
188 public static function removeByName($uid, $name) {
190 if (x($uid) && x($name)) {
191 $gid = self::getIdByName($uid, $name);
193 $return = self::remove($gid);
200 * @brief Adds a contact to a group
206 public static function addMember($gid, $cid)
208 if (!$gid || !$cid) {
212 $row_exists = dba::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
214 // Row already existing, nothing to do
217 $return = dba::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
224 * @brief Removes a contact from a group
230 public static function removeMember($gid, $cid)
232 if (!$gid || !$cid) {
236 $return = dba::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
242 * @brief Removes a contact from a group based on its name
244 * @deprecated Use Group::removeMember instead
247 * @param string $name
251 public static function removeMemberByName($uid, $name, $cid)
253 $gid = self::getIdByName($uid, $name);
255 $return = self::removeMember($gid, $cid);
261 * @brief Returns the combined list of contact ids from a group id list
263 * @param array $group_ids
264 * @param boolean $check_dead
265 * @param boolean $use_gcontact
268 public static function expand($group_ids, $check_dead = false, $use_gcontact = false)
270 if (!is_array($group_ids) || !count($group_ids)) {
274 $condition = '`gid` IN (' . substr(str_repeat("?, ", count($group_ids)), 0, -2) . ')';
276 $sql = 'SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
277 INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
278 INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
279 WHERE ' . $condition;
280 $param_arr = array_merge([$sql], $group_ids);
281 $stmt = call_user_func_array('dba::p', $param_arr);
283 $condition_array = array_merge([$condition], $group_ids);
284 $stmt = dba::select('group_member', ['contact-id'], $condition_array);
288 while($group_member = dba::fetch($stmt)) {
289 $return[] = $group_member['contact-id'];
292 if ($check_dead && !$use_gcontact) {
293 require_once 'include/acl_selectors.php';
294 $return = prune_deadguys($return);
300 * @brief Returns a templated group selection list
303 * @param int $gid An optional pre-selected group
304 * @param string $label An optional label of the list
307 public static function displayGroupSelection($uid, $gid = 0, $label = '')
311 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
320 while ($group = dba::fetch($stmt)) {
321 $display_groups[] = [
322 'name' => $group['name'],
323 'id' => $group['id'],
324 'selected' => $gid == $group['id'] ? 'true' : ''
327 logger('groups: ' . print_r($display_groups, true));
330 $label = t('Default privacy group for new contacts');
333 $o = replace_macros(get_markup_template('group_selection.tpl'), array(
335 '$groups' => $display_groups
341 * @brief Create group sidebar widget
343 * @param string $every
344 * @param string $each
345 * @param string $editmode
346 * 'standard' => include link 'Edit groups'
347 * 'extended' => include link 'Create new group'
348 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
349 * @param int $group_id
353 public static function sidebarWidget($every = 'contacts', $each = 'group', $editmode = 'standard', $group_id = 0, $cid = 0)
363 'text' => t('Everybody'),
365 'selected' => (($group_id == 0) ? 'group-selected' : ''),
370 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
372 $member_of = array();
374 $member_of = self::getIdsByContactId($cid);
377 while ($group = dba::fetch($stmt)) {
378 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
380 if ($editmode == 'full') {
382 'href' => 'group/' . $group['id'],
383 'title' => t('edit'),
389 $display_groups[] = [
390 'id' => $group['id'],
392 'text' => $group['name'],
393 'href' => $each . '/' . $group['id'],
394 'edit' => $groupedit,
395 'selected' => $selected,
396 'ismember' => in_array($group['id'], $member_of),
400 $tpl = get_markup_template('group_side.tpl');
401 $o = replace_macros($tpl, [
403 '$title' => t('Groups'),
404 '$groups' => $display_groups,
405 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
406 'grouppage' => 'group/',
407 '$edittext' => t('Edit group'),
408 '$ungrouped' => $every === 'contacts' ? t('Contacts not in any group') : '',
409 '$createtext' => t('Create a new group'),
410 '$creategroup' => t('Group Name: '),
411 '$editgroupstext' => t('Edit groups'),
412 '$form_security_token' => get_form_security_token('group_edit'),