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
60 * @todo Get rid of $uid, the contact id already bears the information
66 private static function getByContactIdForUserId($uid, $cid)
68 $condition = ['uid' => $uid, 'contact-id' => $cid];
69 $stmt = dba::select('group_member', ['gid'], $condition);
73 while ($group = dba::fetch($stmt)) {
74 $return[] = $group['gid'];
81 * @brief count unread group items
83 * Count unread items of each groups of the local user
87 * 'name' => group name
88 * 'count' => counted unseen group items
90 public static function countUnseen()
92 $stmt = dba::p("SELECT `group`.`id`, `group`.`name`,
93 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
99 WHERE `group_member`.`gid` = `group`.`id`)
102 WHERE `group`.`uid` = ?;",
108 return dba::inArray($stmt);
112 * @brief Get the group id for a user/name couple
114 * Returns false if no group has been found.
117 * @param string $name
118 * @return int|boolean
120 public static function getIdByName($uid, $name)
122 if (!$uid || !strlen($name)) {
126 $group = dba::select('group', ['id'], ['uid' => $uid, 'name' => $name], ['limit' => 1]);
127 if (DBM::is_result($group)) {
135 * @brief Mark a group as deleted
140 public static function remove($gid) {
145 // remove group from default posting lists
146 $user = dba::select('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $uid], ['limit' => 1]);
147 if (DBM::is_result($user)) {
150 if ($user['def_gid'] == $gid) {
151 $user['def_gid'] = 0;
154 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
155 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
158 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
159 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
164 dba::update('user', $user, ['uid' => $uid]);
168 // remove all members
169 dba::delete('group_member', ['gid' => $gid]);
172 $return = dba::update('group', ['deleted' => 1], ['id' => $gid]);
178 * @brief Mark a group as deleted based on its name
180 * @deprecated Use Group::remove instead
186 public static function removeByName($uid, $name) {
188 if (x($uid) && x($name)) {
189 $gid = self::getIdByName($uid, $name);
191 $return = self::remove($gid);
198 * @brief Adds a contact to a group
204 public static function addMember($gid, $cid)
206 if (!$gid || !$cid) {
210 $row_exists = dba::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
212 // Row already existing, nothing to do
215 $return = dba::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
222 * @brief Removes a contact from a group
228 public static function removeMember($gid, $cid)
230 if (!$gid || !$cid) {
234 $return = dba::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
240 * @brief Removes a contact from a group based on its name
242 * @deprecated Use Group::removeMember instead
245 * @param string $name
249 public static function removeMemberByName($uid, $name, $cid)
251 $gid = self::getIdByName($uid, $name);
253 $return = self::removeMember($gid, $cid);
259 * @brief Returns the combined list of contact ids from a group id list
261 * @param array $group_ids
262 * @param boolean $check_dead
263 * @param boolean $use_gcontact
266 public static function expand($group_ids, $check_dead = false, $use_gcontact = false)
268 if (!is_array($group_ids) || !count($group_ids)) {
272 $condition = '`gid` IN (' . substr(str_repeat("?, ", count($group_ids)), 0, -2) . ')';
274 $sql = 'SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
275 INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
276 INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
277 WHERE ' . $condition;
278 $param_arr = array_merge([$sql], $group_ids);
279 $stmt = call_user_func_array('dba::p', $param_arr);
281 $condition_array = array_merge([$condition], $group_ids);
282 $stmt = dba::select('group_member', ['contact-id'], $condition_array);
286 while($group_member = dba::fetch($stmt)) {
287 $return[] = $group_member['contact-id'];
290 if ($check_dead && !$use_gcontact) {
291 require_once 'include/acl_selectors.php';
292 $return = prune_deadguys($return);
298 * @brief Returns a templated group selection list
301 * @param int $gid An optional pre-selected group
302 * @param string $label An optional label of the list
305 public static function displayGroupSelection($uid, $gid = 0, $label = '')
309 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
318 while ($group = dba::fetch($stmt)) {
319 $display_groups[] = [
320 'name' => $group['name'],
321 'id' => $group['id'],
322 'selected' => $gid == $group['id'] ? 'true' : ''
325 logger('groups: ' . print_r($display_groups, true));
328 $label = t('Default privacy group for new contacts');
331 $o = replace_macros(get_markup_template('group_selection.tpl'), array(
333 '$groups' => $display_groups
339 * @brief Create group sidebar widget
341 * @param string $every
342 * @param string $each
343 * @param string $editmode
344 * 'standard' => include link 'Edit groups'
345 * 'extended' => include link 'Create new group'
346 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
347 * @param int $group_id
351 public static function sidebarWidget($every = 'contacts', $each = 'group', $editmode = 'standard', $group_id = 0, $cid = 0)
361 'text' => t('Everybody'),
363 'selected' => (($group_id == 0) ? 'group-selected' : ''),
368 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
370 $member_of = array();
372 $member_of = self::getByContactIdForUserId(local_user(), $cid);
375 while ($group = dba::fetch($stmt)) {
376 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
378 if ($editmode == 'full') {
380 'href' => 'group/' . $group['id'],
381 'title' => t('edit'),
387 $display_groups[] = [
388 'id' => $group['id'],
390 'text' => $group['name'],
391 'href' => $each . '/' . $group['id'],
392 'edit' => $groupedit,
393 'selected' => $selected,
394 'ismember' => in_array($group['id'], $member_of),
398 $tpl = get_markup_template('group_side.tpl');
399 $o = replace_macros($tpl, [
401 '$title' => t('Groups'),
402 '$groups' => $display_groups,
403 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
404 'grouppage' => 'group/',
405 '$edittext' => t('Edit group'),
406 '$ungrouped' => $every === 'contacts' ? t('Contacts not in any group') : '',
407 '$createtext' => t('Create a new group'),
408 '$creategroup' => t('Group Name: '),
409 '$editgroupstext' => t('Edit groups'),
410 '$form_security_token' => get_form_security_token('group_edit'),