193 lines
5.6 KiB
PHP
193 lines
5.6 KiB
PHP
<?php
|
|
|
|
require_once "globals.php";
|
|
require_once "common_func.php";
|
|
require_once "usermgr.php";
|
|
|
|
$groupdb = new \SleekDB\Store(GROUPDB, DATADIR, ["timeout" => false]);
|
|
|
|
function create_group(string $groupname, string $owner, string $description = ""): bool
|
|
{
|
|
global $groupdb;
|
|
|
|
// test name uniqueness
|
|
$unique = manage_unique_in_siblings(0, $groupname);
|
|
|
|
// initialize group data
|
|
$group_data = [
|
|
"groupname" => $groupname,
|
|
"unique" => $unique,
|
|
"owner" => $owner,
|
|
"description" => $description,
|
|
"editors" => [],
|
|
"users" => [],
|
|
"games" => []
|
|
];
|
|
$groupdb->insert($group_data); // insert group
|
|
return true;
|
|
|
|
}
|
|
|
|
function get_group(string $groupid): array
|
|
{
|
|
global $groupdb;
|
|
return $groupdb->findById($groupid);
|
|
}
|
|
|
|
function update_group(array $group_data)
|
|
{
|
|
global $groupdb;
|
|
$groupdb->update($group_data);
|
|
}
|
|
|
|
function delete_group(string $groupid)
|
|
{
|
|
global $groupdb;
|
|
$group_data = get_group($groupid);
|
|
if (count($group_data) !== 0) {
|
|
foreach ($group_data["users"] as $nickname) { // remove all references to this group
|
|
change_user_group_assignments($nickname, null, $groupid);
|
|
}
|
|
}
|
|
$groupdb->deleteById($groupid);
|
|
}
|
|
|
|
function get_all_groups()
|
|
{
|
|
global $groupdb;
|
|
return $groupdb->findAll();
|
|
}
|
|
|
|
function change_group_user_assignments(string $groupid, $nickname_add, $nickname_remove)
|
|
{
|
|
$group_data = get_group($groupid);
|
|
if (count($group_data) != 0) {
|
|
// --------- UPDATE group assignments (DEV ONLY!)------
|
|
|
|
$group_data["editors"] = $group_data["editors"] ?? [];
|
|
|
|
// ---------
|
|
|
|
alter_array_contents($group_data["users"], $nickname_add, $nickname_remove); // add to local storage
|
|
alter_array_contents($group_data["editors"], null, $nickname_remove); // removed users must be excluded from the editors' list as well
|
|
update_group($group_data); // update user
|
|
if ($nickname_add !== null) {
|
|
change_user_group_assignments($nickname_add, $groupid, null); // add to user's storage remote
|
|
}
|
|
if ($nickname_remove !== null) {
|
|
change_user_group_assignments($nickname_remove, null, $groupid); // remove from user's storage
|
|
}
|
|
}
|
|
}
|
|
|
|
function change_group_game_assignments(string $groupid, $gameid_add, $gameid_remove)
|
|
{
|
|
$group_data = get_group($groupid);
|
|
if (count($group_data) != 0) {
|
|
alter_array_contents($group_data["games"], $gameid_add, $gameid_remove); // add to local storage
|
|
update_group($group_data); // update user
|
|
if ($gameid_add !== null) {
|
|
change_game_group_assignments($gameid_add, $groupid, null); // add to user's storage remote
|
|
}
|
|
if ($gameid_remove !== null) {
|
|
change_game_group_assignments($gameid_remove, null, $groupid); // remove from user's storage
|
|
}
|
|
}
|
|
}
|
|
|
|
function search_groups(string $needle): array
|
|
{
|
|
if (strlen($needle) < 3) {
|
|
return [];
|
|
}
|
|
|
|
global $groupdb;
|
|
$parts = explode("#", $needle, 2);
|
|
$groupname = $parts[0];
|
|
$group_data_array = [];
|
|
if (count($parts) === 1) {
|
|
$group_data_array = $groupdb->findBy(["groupname", "LIKE", "%$groupname%"]);
|
|
} else if (count($parts) > 1) {
|
|
$groupid = $parts[1];
|
|
$group_data_array = $groupdb->findBy([["groupname", "LIKE", "%$groupname%"], "AND", ["_id", "LIKE", "%$groupid%"]]);
|
|
}
|
|
|
|
$results = [];
|
|
foreach ($group_data_array as $group_data) {
|
|
$results[] = [
|
|
"groupname" => $group_data["groupname"],
|
|
"_id" => $group_data["_id"],
|
|
"unique" => $group_data["unique"]
|
|
];
|
|
}
|
|
return $results;
|
|
}
|
|
|
|
function manage_unique_in_siblings(int $current_gid, $groupname): bool
|
|
{
|
|
// make test on name uniqueness
|
|
global $groupdb;
|
|
$twins = $groupdb->findBy([["groupname", "=", "$groupname"], "AND", ["_id", "!=", $current_gid]]);
|
|
$unique = count($twins) == 0;
|
|
if (count($twins) === 1) { // if fails, then also indicate in the original group that its name is no longer unique
|
|
$twins[0]["unique"] = false;
|
|
update_group($twins[0]);
|
|
}
|
|
return $unique;
|
|
}
|
|
|
|
function get_groupids_by_compounds(array $compounds): array
|
|
{
|
|
global $groupdb;
|
|
$groupids = [];
|
|
foreach ($compounds as $compound) {
|
|
if (trim($compound) === "") { // skip empty entries
|
|
continue;
|
|
}
|
|
|
|
// fetch the group
|
|
$parts = explode("#", $compound);
|
|
$group_data = [];
|
|
if (count($parts) === 1) {
|
|
$fetch_cmd = ["groupname", "=", $parts[0]];
|
|
$group_data = $groupdb->findBy($fetch_cmd);
|
|
if (count($group_data) == 1) { // too many hits
|
|
$group_data = $group_data[0];
|
|
} else {
|
|
$group_data = [];
|
|
}
|
|
} else {
|
|
$group_data = $groupdb->findById($parts[1]);
|
|
}
|
|
if ($group_data !== []) {
|
|
$groupids[] = $group_data["_id"];
|
|
}
|
|
}
|
|
return $groupids;
|
|
}
|
|
|
|
function is_user_editor_to_group(string $groupid, string $nickname): bool
|
|
{
|
|
$group_data = get_group($groupid);
|
|
if (count($group_data) === 0) {
|
|
return false;
|
|
}
|
|
|
|
return in_array($nickname, $group_data["editors"]) || ($group_data["owner"] === $nickname);
|
|
}
|
|
|
|
function get_group_unique_name(string $groupid): string
|
|
{
|
|
$group_data = get_group($groupid);
|
|
if (count($group_data) !== 0) {
|
|
return $group_data["groupname"] . (!$group_data["unique"] ? "#" . $groupid : "");
|
|
}
|
|
return "";
|
|
}
|
|
|
|
function resolve_groupids(array &$groups): void
|
|
{
|
|
for ($i = 0; $i < count($groups); $i++) {
|
|
$groups[$i] = get_group_unique_name($groups[$i]);
|
|
}
|
|
} |