3 * @file src/Model/Group.php
5 namespace Friendica\Model;
7 use Friendica\BaseModule;
8 use Friendica\BaseObject;
9 use Friendica\Core\L10n;
10 use Friendica\Database\DBA;
11 use Friendica\Util\Security;
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 (DBA::isResult($group) && $group['deleted']) {
44 DBA::update('group', ['deleted' => 0], ['id' => $gid]);
45 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);
50 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
52 $return = DBA::lastInsertId();
59 * Update group information.
61 * @param int $id Group ID
62 * @param string $name Group name
64 * @return bool Was the update successful?
66 public static function update($id, $name)
68 return DBA::update('group', ['name' => $name], ['id' => $id]);
72 * @brief Get a list of group ids a contact belongs to
77 public static function getIdsByContactId($cid)
79 $condition = ['contact-id' => $cid];
80 $stmt = DBA::select('group_member', ['gid'], $condition);
84 while ($group = DBA::fetch($stmt)) {
85 $return[] = $group['gid'];
92 * @brief count unread group items
94 * Count unread items of each groups of the local user
98 * 'name' => group name
99 * 'count' => counted unseen group items
101 public static function countUnseen()
103 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
104 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
110 WHERE `group_member`.`gid` = `group`.`id`)
113 WHERE `group`.`uid` = ?;",
118 return DBA::toArray($stmt);
122 * @brief Get the group id for a user/name couple
124 * Returns false if no group has been found.
127 * @param string $name
128 * @return int|boolean
130 public static function getIdByName($uid, $name)
132 if (!$uid || !strlen($name)) {
136 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
137 if (DBA::isResult($group)) {
145 * @brief Mark a group as deleted
150 public static function remove($gid) {
155 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
156 if (!DBA::isResult($group)) {
160 // remove group from default posting lists
161 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
162 if (DBA::isResult($user)) {
165 if ($user['def_gid'] == $gid) {
166 $user['def_gid'] = 0;
169 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
170 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
173 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
174 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
179 DBA::update('user', $user, ['uid' => $group['uid']]);
183 // remove all members
184 DBA::delete('group_member', ['gid' => $gid]);
187 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
193 * @brief Mark a group as deleted based on its name
195 * @deprecated Use Group::remove instead
198 * @param string $name
201 public static function removeByName($uid, $name) {
203 if (x($uid) && x($name)) {
204 $gid = self::getIdByName($uid, $name);
206 $return = self::remove($gid);
213 * @brief Adds a contact to a group
219 public static function addMember($gid, $cid)
221 if (!$gid || !$cid) {
225 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
227 // Row already existing, nothing to do
230 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
237 * @brief Removes a contact from a group
243 public static function removeMember($gid, $cid)
245 if (!$gid || !$cid) {
249 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
255 * @brief Removes a contact from a group based on its name
257 * @deprecated Use Group::removeMember instead
260 * @param string $name
264 public static function removeMemberByName($uid, $name, $cid)
266 $gid = self::getIdByName($uid, $name);
268 $return = self::removeMember($gid, $cid);
274 * @brief Returns the combined list of contact ids from a group id list
276 * @param array $group_ids
277 * @param boolean $check_dead
280 public static function expand($group_ids, $check_dead = false)
282 if (!is_array($group_ids) || !count($group_ids)) {
286 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
289 while($group_member = DBA::fetch($stmt)) {
290 $return[] = $group_member['contact-id'];
294 Contact::pruneUnavailable($return);
301 * @brief Returns a templated group selection list
304 * @param int $gid An optional pre-selected group
305 * @param string $label An optional label of the list
308 public static function displayGroupSelection($uid, $gid = 0, $label = '')
312 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
321 while ($group = DBA::fetch($stmt)) {
322 $display_groups[] = [
323 'name' => $group['name'],
324 'id' => $group['id'],
325 'selected' => $gid == $group['id'] ? 'true' : ''
328 logger('groups: ' . print_r($display_groups, true));
331 $label = L10n::t('Default privacy group for new contacts');
334 $o = replace_macros(get_markup_template('group_selection.tpl'), [
336 '$groups' => $display_groups
342 * @brief Create group sidebar widget
344 * @param string $every
345 * @param string $each
346 * @param string $editmode
347 * 'standard' => include link 'Edit groups'
348 * 'extended' => include link 'Create new group'
349 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
350 * @param int $group_id
354 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
364 'text' => L10n::t('Everybody'),
366 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
371 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
375 $member_of = self::getIdsByContactId($cid);
378 while ($group = DBA::fetch($stmt)) {
379 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
381 if ($editmode == 'full') {
383 'href' => 'group/' . $group['id'],
384 'title' => L10n::t('edit'),
390 $display_groups[] = [
391 'id' => $group['id'],
393 'text' => $group['name'],
394 'href' => $each . '/' . $group['id'],
395 'edit' => $groupedit,
396 'selected' => $selected,
397 'ismember' => in_array($group['id'], $member_of),
401 $tpl = get_markup_template('group_side.tpl');
402 $o = replace_macros($tpl, [
403 '$add' => L10n::t('add'),
404 '$title' => L10n::t('Groups'),
405 '$groups' => $display_groups,
406 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
407 'grouppage' => 'group/',
408 '$edittext' => L10n::t('Edit group'),
409 '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
410 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
411 '$createtext' => L10n::t('Create a new group'),
412 '$creategroup' => L10n::t('Group Name: '),
413 '$editgroupstext' => L10n::t('Edit groups'),
414 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),