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` = ?;",
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::select('group', ['id'], ['uid' => $uid, 'name' => $name], ['limit' => 1]);
124 if (DBM::is_result($group)) {
132 * @brief Mark a group as deleted
137 public static function remove($gid) {
142 // remove group from default posting lists
143 $user = dba::select('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $uid], ['limit' => 1]);
144 if (DBM::is_result($user)) {
147 if ($user['def_gid'] == $gid) {
148 $user['def_gid'] = 0;
151 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
152 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
155 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
156 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
161 dba::update('user', $user, ['uid' => $uid]);
165 // remove all members
166 dba::delete('group_member', ['gid' => $gid]);
169 $return = dba::update('group', ['deleted' => 1], ['id' => $gid]);
175 * @brief Mark a group as deleted based on its name
177 * @deprecated Use Group::remove instead
183 public static function removeByName($uid, $name) {
185 if (x($uid) && x($name)) {
186 $gid = self::getIdByName($uid, $name);
188 $return = self::remove($gid);
195 * @brief Adds a contact to a group
201 public static function addMember($gid, $cid)
203 if (!$gid || !$cid) {
207 $row_exists = dba::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
209 // Row already existing, nothing to do
212 $return = dba::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
219 * @brief Removes a contact from a group
225 public static function removeMember($gid, $cid)
227 if (!$gid || !$cid) {
231 $return = dba::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
237 * @brief Removes a contact from a group based on its name
239 * @deprecated Use Group::removeMember instead
242 * @param string $name
246 public static function removeMemberByName($uid, $name, $cid)
248 $gid = self::getIdByName($uid, $name);
250 $return = self::removeMember($gid, $cid);
256 * @brief Returns the combined list of contact ids from a group id list
258 * @param array $group_ids
259 * @param boolean $check_dead
260 * @param boolean $use_gcontact
263 public static function expand($group_ids, $check_dead = false, $use_gcontact = false)
265 if (!is_array($group_ids) || !count($group_ids)) {
269 $condition = '`gid` IN (' . substr(str_repeat("?, ", count($group_ids)), 0, -2) . ')';
271 $sql = 'SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
272 INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
273 INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
274 WHERE ' . $condition;
275 $param_arr = array_merge([$sql], $group_ids);
276 $stmt = call_user_func_array('dba::p', $param_arr);
278 $condition_array = array_merge([$condition], $group_ids);
279 $stmt = dba::select('group_member', ['contact-id'], $condition_array);
283 while($group_member = dba::fetch($stmt)) {
284 $return[] = $group_member['contact-id'];
287 if ($check_dead && !$use_gcontact) {
288 require_once 'include/acl_selectors.php';
289 $return = prune_deadguys($return);
295 * @brief Returns a templated group selection list
298 * @param int $gid An optional pre-selected group
299 * @param string $label An optional label of the list
302 public static function displayGroupSelection($uid, $gid = 0, $label = '')
306 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
315 while ($group = dba::fetch($stmt)) {
316 $display_groups[] = [
317 'name' => $group['name'],
318 'id' => $group['id'],
319 'selected' => $gid == $group['id'] ? 'true' : ''
322 logger('groups: ' . print_r($display_groups, true));
325 $label = t('Default privacy group for new contacts');
328 $o = replace_macros(get_markup_template('group_selection.tpl'), array(
330 '$groups' => $display_groups
336 * @brief Create group sidebar widget
338 * @param string $every
339 * @param string $each
340 * @param string $editmode
341 * 'standard' => include link 'Edit groups'
342 * 'extended' => include link 'Create new group'
343 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
344 * @param int $group_id
348 public static function sidebarWidget($every = 'contacts', $each = 'group', $editmode = 'standard', $group_id = 0, $cid = 0)
358 'text' => t('Everybody'),
360 'selected' => (($group_id == 0) ? 'group-selected' : ''),
365 $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
367 $member_of = array();
369 $member_of = self::getIdsByContactId($cid);
372 while ($group = dba::fetch($stmt)) {
373 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
375 if ($editmode == 'full') {
377 'href' => 'group/' . $group['id'],
378 'title' => t('edit'),
384 $display_groups[] = [
385 'id' => $group['id'],
387 'text' => $group['name'],
388 'href' => $each . '/' . $group['id'],
389 'edit' => $groupedit,
390 'selected' => $selected,
391 'ismember' => in_array($group['id'], $member_of),
395 $tpl = get_markup_template('group_side.tpl');
396 $o = replace_macros($tpl, [
398 '$title' => t('Groups'),
399 '$groups' => $display_groups,
400 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
401 'grouppage' => 'group/',
402 '$edittext' => t('Edit group'),
403 '$ungrouped' => $every === 'contacts' ? t('Contacts not in any group') : '',
404 '$createtext' => t('Create a new group'),
405 '$creategroup' => t('Group Name: '),
406 '$editgroupstext' => t('Edit groups'),
407 '$form_security_token' => get_form_security_token('group_edit'),