implemented spaces, fixed login and signup, thought creation and space creation.
This commit is contained in:
+96
-38
@@ -10,13 +10,12 @@ class Space
|
||||
private string $name;
|
||||
private string $description;
|
||||
private int $visibility;
|
||||
private array $members;
|
||||
private array $posts;
|
||||
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)
|
||||
public function __construct(int $id, string $name, string $description, int $visibility, ?array $members, ?array $posts, ?Space $parent = null, bool $favorited = false)
|
||||
{
|
||||
$this->id = $id ?? randomId(2);
|
||||
$this->name = $name;
|
||||
$this->description = $description;
|
||||
$this->visibility = $visibility;
|
||||
@@ -24,33 +23,101 @@ class Space
|
||||
$this->posts[] = $posts;
|
||||
$this->parent = $parent;
|
||||
|
||||
/* $stmt = 'INSERT INTO spaces (spaceId, name, description, parentSpaceIdk)'; */
|
||||
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">
|
||||
<a href="spaces.php?view=show&id=' . $this->id . '" class="divLink">
|
||||
<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>
|
||||
|
||||
<div class="">
|
||||
<span>Something</span>
|
||||
';
|
||||
|
||||
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>
|
||||
</a>
|
||||
</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) {
|
||||
@@ -68,41 +135,32 @@ class Space
|
||||
$stmt = 'SELECT * FROM space WHERE id = ?';
|
||||
$rawSpace = exec_stmt($stmt, 'i', $space)->fetch_assoc();
|
||||
|
||||
if (!empty($rawSpace['parentSpace']))
|
||||
$parent = Space::retrieveFromDB($rawSpace['parentSpace'], $user);
|
||||
else
|
||||
$parent = null;
|
||||
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 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 WHERE space = ?';
|
||||
$posts = [];
|
||||
$rawPosts = exec_stmt($stmt, 'i', $rawSpace['id']);
|
||||
while ($row = $rawPosts->fetch_assoc()) {
|
||||
$posts[] = Post::retrieveFromDB($row['post']);
|
||||
}
|
||||
$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']);
|
||||
|
||||
$space = new Space($rawSpace['id'], $rawSpace['name'], $rawSpace['description'], $rawSpace['visibility'], $members, $posts, $parent);
|
||||
|
||||
foreach ($posts as $p)
|
||||
$p->setSpace($space);
|
||||
|
||||
return $space;
|
||||
return new Space($rawSpace['id'], $rawSpace['name'], $rawSpace['description'], $rawSpace['visibility'], $members, $posts, $parent);
|
||||
} else
|
||||
return null;
|
||||
}
|
||||
|
||||
public function allowUser(User $user)
|
||||
{
|
||||
$stmt = 'SELECT space FROM spaceMembers WHERE user = ?';
|
||||
$result = exec_stmt($stmt, 'i', $user->getId())->fetch_assoc();
|
||||
if ($result['space'] == $this->id)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
return in_array($user, $this->members);
|
||||
}
|
||||
|
||||
public function getPosts()
|
||||
|
||||
Reference in New Issue
Block a user