Files
web/functions/space.php
T

79 lines
2.2 KiB
PHP

<?php
require_once 'core.php';
require_once 'db.php';
require_once 'user.php';
require_once 'post.php';
class Space
{
private int $spaceId;
private string $name;
private string $description;
private array $members;
private array $posts;
private ?Space $parent;
public function __construct(?int $spaceId, string $name, string $description, array $members, $posts, ?Space $parent = null)
{
$this->spaceId = $spaceId ?? randomId(2);
$this->name = $name;
$this->description = $description;
$this->members = $members;
$this->posts[] = $posts;
$this->parent = $parent;
/* $stmt = 'INSERT INTO spaces (spaceId, name, description, parentSpaceIdk)'; */
}
public static function retrieveFromDB(int $space, ?int $user): ?Space
{
if ($user) {
$stmt = 'SELECT * FROM spaceMembers WHERE space = ? AND user = ?';
$isMember = exec_stmt($stmt, 'ii', $space, $user)->fetch_assoc();
if (!$isMember)
return null;
} else {
$stmt = 'SELECT visibility FROM space WHERE id = ?';
$isOpen = exec_stmt($stmt, 'i', $space);
if (!$isOpen)
return null;
}
$stmt = 'SELECT * FROM space WHERE id = ?';
$space = exec_stmt($stmt, 'i', $space)->fetch_assoc();
if (!empty($space['parentSpace']))
$parent = Space::retrieveFromDB($space['parentSpace'], $user);
else
$parent = null;
$stmt = 'SELECT user FROM spaceMembers WHERE space = ?';
$members = exec_stmt($stmt, 'i', $space)->fetch_assoc();
$stmt = 'SELECT post FROM spacePosts WHERE space = ?';
$posts = exec_stmt($stmt, 'i', $space)->fetch_assoc();
return new Space($space['id'], $space['name'], $space['description'], $members, $posts, $parent);
}
public function __toString()
{
echo 'todo: this should be JSON';
}
public function getId(): int
{
return $this->spaceId;
}
public function getName(): string
{
return $this->name;
}
public function getDescription(): string
{
return $this->description;
}
}