196 lines
5.9 KiB
PHP
196 lines
5.9 KiB
PHP
<?php
|
|
require_once 'core.php';
|
|
require_once 'db.php';
|
|
require_once 'user.php';
|
|
require_once 'post.php';
|
|
|
|
class Space
|
|
{
|
|
private int $id;
|
|
private string $name;
|
|
private string $description;
|
|
private int $visibility;
|
|
private ?array $members;
|
|
private ?array $posts;
|
|
private ?Space $parent;
|
|
|
|
public function __construct(int $id, string $name, string $description, int $visibility, ?array $members, ?array $posts, ?Space $parent = null, bool $favorited = false)
|
|
{
|
|
$this->name = $name;
|
|
$this->description = $description;
|
|
$this->visibility = $visibility;
|
|
$this->members = $members;
|
|
$this->posts[] = $posts;
|
|
$this->parent = $parent;
|
|
|
|
if ($id < 0) {
|
|
$this->id = $id * -1;
|
|
error_log('ID: ' . $this->id);
|
|
|
|
$stmt = 'INSERT INTO space (id, name, description, visibility, parentSpace) VALUES (?, ?, ?, ?, ?)';
|
|
exec_stmt($stmt, 'issii', $this->id, $name, $description, $visibility, (isset($parent) ? $parent->getId() : null));
|
|
error_log('Added space in DB');
|
|
} else
|
|
$this->id = $id;
|
|
}
|
|
|
|
public static function exists(int $id): bool
|
|
{
|
|
$stmt = 'SELECT count(id) FROM space WHERE id = ?';
|
|
$result = exec_stmt($stmt, 'i', $id)->fetch_assoc();
|
|
|
|
if ($result['count(id)'] == 1)
|
|
return true;
|
|
else if ($result['count(id)'] > 1)
|
|
return true;
|
|
else
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function addUserToSpace(int $user, int $space, int $role = 3, bool $favorited = false)
|
|
{
|
|
$stmt = 'INSERT INTO spaceMembers (user, space, role, favorited) VALUES (?, ?, ?, ?)';
|
|
exec_stmt($stmt, 'iiii', $user, $space, $role, $favorited);
|
|
}
|
|
|
|
public function getSubSpaces(): ?array
|
|
{
|
|
$stmt = 'SELECT id FROM space WHERE parentSpace = ?';
|
|
$rawSubSpaces = exec_stmt($stmt, 'i', $this->id);
|
|
|
|
$subSpaces = [];
|
|
foreach ($rawSubSpaces as $ss) {
|
|
$subSpaces[] = Space::retrieveFromDB($ss['id']);
|
|
}
|
|
|
|
return $subSpaces;
|
|
}
|
|
|
|
public function getParentSpace(): ?Space
|
|
{
|
|
return $this->parent;
|
|
}
|
|
|
|
public function getCardHTML(): string
|
|
{
|
|
$html = '
|
|
<div class="full_width std_border padding center top_margin bottom_margin rel" id="space-' . $this->id . '">
|
|
<a href="spaces.php?view=show&id=' . $this->id . '" class="fillDiv spaceCard"></a>
|
|
<div class="row space_between full_width">
|
|
<div class="column std_width">
|
|
<h4>' . $this->name . '</h4>
|
|
</div>
|
|
|
|
';
|
|
|
|
if (isset($_SESSION['user'])) {
|
|
$user = unserialize($_SESSION['user']);
|
|
if ($user->getId() == $this->getOwner()->getId()) {
|
|
$html .= '
|
|
<div class="dropdown">
|
|
<i class="nf nf-md-dots_horizontal"></i>
|
|
<div class="sm_border padding glass dropdownContent">
|
|
<a class="underline link" onclick="deleteSpace(' . $this->id . ')">Delete</a>
|
|
</div>
|
|
</div>
|
|
';
|
|
}
|
|
}
|
|
|
|
$html .= '
|
|
</div>
|
|
<div class="row full_width">
|
|
<p>' . $this->description . '</p>
|
|
</div>
|
|
</div>
|
|
';
|
|
|
|
return $html;
|
|
}
|
|
|
|
public function getOwner(): User
|
|
{
|
|
$stmt = 'SELECT user FROM spaceMembers WHERE space = ? AND role = ?';
|
|
$user = exec_stmt($stmt, 'ii', $this->id, 1)->fetch_assoc();
|
|
|
|
return User::retrieveFromDB($user['user']);
|
|
}
|
|
|
|
public static function retrieveFromDB(int $space, int $user = -1): ?Space
|
|
{
|
|
if ($user != -1) {
|
|
$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 = ?';
|
|
$rawSpace = exec_stmt($stmt, 'i', $space)->fetch_assoc();
|
|
|
|
if ($rawSpace) {
|
|
if (!empty($rawSpace['parentSpace']))
|
|
$parent = Space::retrieveFromDB($rawSpace['parentSpace'], $user);
|
|
else
|
|
$parent = null;
|
|
|
|
$stmt = 'SELECT user FROM spaceMembers WHERE space = ?';
|
|
$members = [];
|
|
$rawMembers = exec_stmt($stmt, 'i', $rawSpace['id']);
|
|
foreach ($rawMembers as $m)
|
|
$members[] = User::retrieveFromDB($m['user']);
|
|
|
|
$stmt = 'SELECT post FROM spacePosts INNER JOIN post ON spacePosts.post = post.id WHERE space = ? ORDER BY post.createdAt DESC';
|
|
$posts = [];
|
|
$rawPosts = exec_stmt($stmt, 'i', $rawSpace['id']);
|
|
while ($row = $rawPosts->fetch_assoc())
|
|
$posts[] = Post::retrieveFromDB($row['post']);
|
|
|
|
return new Space($rawSpace['id'], $rawSpace['name'], $rawSpace['description'], $rawSpace['visibility'], $members, $posts, $parent);
|
|
} else
|
|
return null;
|
|
}
|
|
|
|
public function allowUser(User $user)
|
|
{
|
|
return in_array($user, $this->members);
|
|
}
|
|
|
|
public function getPosts()
|
|
{
|
|
return $this->posts;
|
|
}
|
|
|
|
public function __toString()
|
|
{
|
|
echo 'todo: this should be JSON';
|
|
}
|
|
|
|
public function getId(): int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getName(): string
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
public function getDescription(): string
|
|
{
|
|
return $this->description;
|
|
}
|
|
|
|
public function getVisibility(): int
|
|
{
|
|
return $this->visibility;
|
|
}
|
|
}
|