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\Core\Logger;
11 use Friendica\Database\DBA;
12 use Friendica\Util\Security;
14 require_once 'boot.php';
15 require_once 'include/dba.php';
16 require_once 'include/text.php';
19 * @brief functions for interacting with the group database table
21 class Group extends BaseObject
24 * @brief Create a new contact group
26 * Note: If we found a deleted group with the same name, we restore it
32 public static function create($uid, $name)
35 if (x($uid) && x($name)) {
36 $gid = self::getIdByName($uid, $name); // check for dupes
38 // This could be a problem.
39 // Let's assume we've just created a group which we once deleted
40 // all the old members are gone, but the group remains so we don't break any security
41 // access lists. What we're doing here is reviving the dead group, but old content which
42 // was restricted to this group may now be seen by the new group members.
43 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
44 if (DBA::isResult($group) && $group['deleted']) {
45 DBA::update('group', ['deleted' => 0], ['id' => $gid]);
46 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);
51 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
53 $return = DBA::lastInsertId();
60 * Update group information.
62 * @param int $id Group ID
63 * @param string $name Group name
65 * @return bool Was the update successful?
67 public static function update($id, $name)
69 return DBA::update('group', ['name' => $name], ['id' => $id]);
73 * @brief Get a list of group ids a contact belongs to
78 public static function getIdsByContactId($cid)
80 $condition = ['contact-id' => $cid];
81 $stmt = DBA::select('group_member', ['gid'], $condition);
85 while ($group = DBA::fetch($stmt)) {
86 $return[] = $group['gid'];
93 * @brief count unread group items
95 * Count unread items of each groups of the local user
99 * 'name' => group name
100 * 'count' => counted unseen group items
102 public static function countUnseen()
104 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
105 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
111 WHERE `group_member`.`gid` = `group`.`id`)
114 WHERE `group`.`uid` = ?;",
119 return DBA::toArray($stmt);
123 * @brief Get the group id for a user/name couple
125 * Returns false if no group has been found.
128 * @param string $name
129 * @return int|boolean
131 public static function getIdByName($uid, $name)
133 if (!$uid || !strlen($name)) {
137 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
138 if (DBA::isResult($group)) {
146 * @brief Mark a group as deleted
151 public static function remove($gid) {
156 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
157 if (!DBA::isResult($group)) {
161 // remove group from default posting lists
162 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
163 if (DBA::isResult($user)) {
166 if ($user['def_gid'] == $gid) {
167 $user['def_gid'] = 0;
170 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
171 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
174 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
175 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
180 DBA::update('user', $user, ['uid' => $group['uid']]);
184 // remove all members
185 DBA::delete('group_member', ['gid' => $gid]);
188 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
194 * @brief Mark a group as deleted based on its name
196 * @deprecated Use Group::remove instead
199 * @param string $name
202 public static function removeByName($uid, $name) {
204 if (x($uid) && x($name)) {
205 $gid = self::getIdByName($uid, $name);
207 $return = self::remove($gid);
214 * @brief Adds a contact to a group
220 public static function addMember($gid, $cid)
222 if (!$gid || !$cid) {
226 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
228 // Row already existing, nothing to do
231 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
238 * @brief Removes a contact from a group
244 public static function removeMember($gid, $cid)
246 if (!$gid || !$cid) {
250 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
256 * @brief Removes a contact from a group based on its name
258 * @deprecated Use Group::removeMember instead
261 * @param string $name
265 public static function removeMemberByName($uid, $name, $cid)
267 $gid = self::getIdByName($uid, $name);
269 $return = self::removeMember($gid, $cid);
275 * @brief Returns the combined list of contact ids from a group id list
277 * @param array $group_ids
278 * @param boolean $check_dead
281 public static function expand($group_ids, $check_dead = false)
283 if (!is_array($group_ids) || !count($group_ids)) {
287 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
290 while($group_member = DBA::fetch($stmt)) {
291 $return[] = $group_member['contact-id'];
295 Contact::pruneUnavailable($return);
302 * @brief Returns a templated group selection list
305 * @param int $gid An optional pre-selected group
306 * @param string $label An optional label of the list
309 public static function displayGroupSelection($uid, $gid = 0, $label = '')
313 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
322 while ($group = DBA::fetch($stmt)) {
323 $display_groups[] = [
324 'name' => $group['name'],
325 'id' => $group['id'],
326 'selected' => $gid == $group['id'] ? 'true' : ''
329 Logger::log('groups: ' . print_r($display_groups, true));
332 $label = L10n::t('Default privacy group for new contacts');
335 $o = replace_macros(get_markup_template('group_selection.tpl'), [
337 '$groups' => $display_groups
343 * @brief Create group sidebar widget
345 * @param string $every
346 * @param string $each
347 * @param string $editmode
348 * 'standard' => include link 'Edit groups'
349 * 'extended' => include link 'Create new group'
350 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
351 * @param int $group_id
355 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
365 'text' => L10n::t('Everybody'),
367 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
372 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
376 $member_of = self::getIdsByContactId($cid);
379 while ($group = DBA::fetch($stmt)) {
380 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
382 if ($editmode == 'full') {
384 'href' => 'group/' . $group['id'],
385 'title' => L10n::t('edit'),
391 $display_groups[] = [
392 'id' => $group['id'],
394 'text' => $group['name'],
395 'href' => $each . '/' . $group['id'],
396 'edit' => $groupedit,
397 'selected' => $selected,
398 'ismember' => in_array($group['id'], $member_of),
402 $tpl = get_markup_template('group_side.tpl');
403 $o = replace_macros($tpl, [
404 '$add' => L10n::t('add'),
405 '$title' => L10n::t('Groups'),
406 '$groups' => $display_groups,
407 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
408 'grouppage' => 'group/',
409 '$edittext' => L10n::t('Edit group'),
410 '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
411 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
412 '$createtext' => L10n::t('Create a new group'),
413 '$creategroup' => L10n::t('Group Name: '),
414 '$editgroupstext' => L10n::t('Edit groups'),
415 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),