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 = '
'; 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; } }