orm to frontend connected. for a time, i'll be trying multiple solutions at once.

This commit is contained in:
2025-05-16 16:41:07 -06:00
parent 228cf1137b
commit a93c30c40f
38 changed files with 1442 additions and 2407 deletions
+78
View File
@@ -0,0 +1,78 @@
<?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;
}
}