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\Core\Renderer;
12 use Friendica\Database\DBA;
13 use Friendica\Util\Security;
15 require_once 'boot.php';
16 require_once 'include/dba.php';
17 require_once 'include/text.php';
20 * @brief functions for interacting with the group database table
22 class Group extends BaseObject
25 * @brief Create a new contact group
27 * Note: If we found a deleted group with the same name, we restore it
33 public static function create($uid, $name)
36 if (x($uid) && x($name)) {
37 $gid = self::getIdByName($uid, $name); // check for dupes
39 // This could be a problem.
40 // Let's assume we've just created a group which we once deleted
41 // all the old members are gone, but the group remains so we don't break any security
42 // access lists. What we're doing here is reviving the dead group, but old content which
43 // was restricted to this group may now be seen by the new group members.
44 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
45 if (DBA::isResult($group) && $group['deleted']) {
46 DBA::update('group', ['deleted' => 0], ['id' => $gid]);
47 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);
52 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
54 $return = DBA::lastInsertId();
61 * Update group information.
63 * @param int $id Group ID
64 * @param string $name Group name
66 * @return bool Was the update successful?
68 public static function update($id, $name)
70 return DBA::update('group', ['name' => $name], ['id' => $id]);
74 * @brief Get a list of group ids a contact belongs to
79 public static function getIdsByContactId($cid)
81 $condition = ['contact-id' => $cid];
82 $stmt = DBA::select('group_member', ['gid'], $condition);
86 while ($group = DBA::fetch($stmt)) {
87 $return[] = $group['gid'];
94 * @brief count unread group items
96 * Count unread items of each groups of the local user
100 * 'name' => group name
101 * 'count' => counted unseen group items
103 public static function countUnseen()
105 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
106 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
112 WHERE `group_member`.`gid` = `group`.`id`)
115 WHERE `group`.`uid` = ?;",
120 return DBA::toArray($stmt);
124 * @brief Get the group id for a user/name couple
126 * Returns false if no group has been found.
129 * @param string $name
130 * @return int|boolean
132 public static function getIdByName($uid, $name)
134 if (!$uid || !strlen($name)) {
138 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
139 if (DBA::isResult($group)) {
147 * @brief Mark a group as deleted
152 public static function remove($gid) {
157 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
158 if (!DBA::isResult($group)) {
162 // remove group from default posting lists
163 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
164 if (DBA::isResult($user)) {
167 if ($user['def_gid'] == $gid) {
168 $user['def_gid'] = 0;
171 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
172 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
175 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
176 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
181 DBA::update('user', $user, ['uid' => $group['uid']]);
185 // remove all members
186 DBA::delete('group_member', ['gid' => $gid]);
189 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
195 * @brief Mark a group as deleted based on its name
197 * @deprecated Use Group::remove instead
200 * @param string $name
203 public static function removeByName($uid, $name) {
205 if (x($uid) && x($name)) {
206 $gid = self::getIdByName($uid, $name);
208 $return = self::remove($gid);
215 * @brief Adds a contact to a group
221 public static function addMember($gid, $cid)
223 if (!$gid || !$cid) {
227 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
229 // Row already existing, nothing to do
232 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
239 * @brief Removes a contact from a group
245 public static function removeMember($gid, $cid)
247 if (!$gid || !$cid) {
251 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
257 * @brief Removes a contact from a group based on its name
259 * @deprecated Use Group::removeMember instead
262 * @param string $name
266 public static function removeMemberByName($uid, $name, $cid)
268 $gid = self::getIdByName($uid, $name);
270 $return = self::removeMember($gid, $cid);
276 * @brief Returns the combined list of contact ids from a group id list
278 * @param array $group_ids
279 * @param boolean $check_dead
282 public static function expand($group_ids, $check_dead = false)
284 if (!is_array($group_ids) || !count($group_ids)) {
288 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
291 while($group_member = DBA::fetch($stmt)) {
292 $return[] = $group_member['contact-id'];
296 Contact::pruneUnavailable($return);
303 * @brief Returns a templated group selection list
306 * @param int $gid An optional pre-selected group
307 * @param string $label An optional label of the list
310 public static function displayGroupSelection($uid, $gid = 0, $label = '')
314 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
323 while ($group = DBA::fetch($stmt)) {
324 $display_groups[] = [
325 'name' => $group['name'],
326 'id' => $group['id'],
327 'selected' => $gid == $group['id'] ? 'true' : ''
330 Logger::log('groups: ' . print_r($display_groups, true));
333 $label = L10n::t('Default privacy group for new contacts');
336 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
338 '$groups' => $display_groups
344 * @brief Create group sidebar widget
346 * @param string $every
347 * @param string $each
348 * @param string $editmode
349 * 'standard' => include link 'Edit groups'
350 * 'extended' => include link 'Create new group'
351 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
352 * @param int $group_id
356 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
366 'text' => L10n::t('Everybody'),
368 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
373 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
377 $member_of = self::getIdsByContactId($cid);
380 while ($group = DBA::fetch($stmt)) {
381 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
383 if ($editmode == 'full') {
385 'href' => 'group/' . $group['id'],
386 'title' => L10n::t('edit'),
392 $display_groups[] = [
393 'id' => $group['id'],
395 'text' => $group['name'],
396 'href' => $each . '/' . $group['id'],
397 'edit' => $groupedit,
398 'selected' => $selected,
399 'ismember' => in_array($group['id'], $member_of),
403 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
404 $o = Renderer::replaceMacros($tpl, [
405 '$add' => L10n::t('add'),
406 '$title' => L10n::t('Groups'),
407 '$groups' => $display_groups,
408 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
409 'grouppage' => 'group/',
410 '$edittext' => L10n::t('Edit group'),
411 '$ungrouped' => $every === 'contact' ? L10n::t('Contacts not in any group') : '',
412 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
413 '$createtext' => L10n::t('Create a new group'),
414 '$creategroup' => L10n::t('Group Name: '),
415 '$editgroupstext' => L10n::t('Edit groups'),
416 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),