name = $name; $this->description = $description; $this->visibility = $visibility; $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 parent = ?'; $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 hasParent(): bool { return isset($this->parent); } public function getCardHTML(): string { $html = '

' . $this->name . '

'; if (isset($_SESSION['user'])) { $user = unserialize($_SESSION['user']); if ($user->getId() == $this->getOwner()->getId()) { $html .= ' '; } } $html .= '

' . $this->description . '

'; 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 { error_log('Retrieving a space from DB.'); 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 post FROM spacePosts INNER JOIN post ON spacePosts.post = post.id WHERE post.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'], $posts, $parent); } else return null; } 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; } }