X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FGroup.php;h=fa41d26467fc704034b706306a7ee81975b69467;hb=72a3ab6382551811f22391b7e82e7398628b235b;hp=b9f4c7f277d1e5b4d710ac600d3f27cf2d8ce0ae;hpb=e394143148c63b73ad719cfb60b517bb20858b74;p=friendica.git diff --git a/src/Model/Group.php b/src/Model/Group.php index b9f4c7f277..fa41d26467 100644 --- a/src/Model/Group.php +++ b/src/Model/Group.php @@ -310,6 +310,68 @@ class Group return DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]); } + /** + * Adds contacts to a group + * + * @param int $gid + * @param array $contacts + * @throws \Exception + */ + public static function addMembers(int $gid, array $contacts) + { + if (!$gid || !$contacts) { + return false; + } + + // @TODO Backward compatibility with user contacts, remove by version 2022.03 + $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]); + if (empty($group)) { + throw new HTTPException\NotFoundException('Group not found.'); + } + + foreach ($contacts as $cid) { + $cdata = Contact::getPublicAndUserContactID($cid, $group['uid']); + if (empty($cdata['user'])) { + throw new HTTPException\NotFoundException('Invalid contact.'); + } + + DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']], Database::INSERT_IGNORE); + } + } + + /** + * Removes contacts from a group + * + * @param int $gid + * @param array $contacts + * @throws \Exception + */ + public static function removeMembers(int $gid, array $contacts) + { + if (!$gid || !$contacts) { + return false; + } + + // @TODO Backward compatibility with user contacts, remove by version 2022.03 + $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]); + if (empty($group)) { + throw new HTTPException\NotFoundException('Group not found.'); + } + + $contactIds = []; + + foreach ($contacts as $cid) { + $cdata = Contact::getPublicAndUserContactID($cid, $group['uid']); + if (empty($cdata['user'])) { + throw new HTTPException\NotFoundException('Invalid contact.'); + } + + $contactIds[] = $cdata['user']; + } + + DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $contactIds]); + } + /** * Returns the combined list of contact ids from a group id list * @@ -556,7 +618,7 @@ class Group * * @param integer $id Contact ID */ - public static function getMembersForForum(int $id) + public static function updateMembersForForum(int $id) { Logger::info('Update forum members', ['id' => $id]);