topicId = $topicId;
$this->name = $name;
}
public function getId(): int
{
return $this->topicId;
}
public function getName(): string
{
return $this->name;
}
}
// -- Post Data Models (as previously defined, with setReadCount in Post) --
class Post
{
private int $space;
private ?int $id;
private User $creator;
private int $postType;
private ?int $status;
private int $seenCount;
private ?DateTime $createdAt;
private ?DateTime $updatedAt;
public function __construct(int $space = 2025, ?int $id = null, User $creator, int $postType, int $status)
{
error_log('Instantiating a post');
$this->space = $space;
$this->id = $id;
$this->creator = $creator;
$this->postType = $postType;
$this->status = $status;
$this->seenCount = 0;
}
public static function exists(int $id): bool
{
$stmt = 'SELECT count(id) FROM post WHERE id = ?';
$result = exec_stmt($stmt, 'i', $id)->fetch_assoc();
if ($result['count(id)'] == 1)
return true;
return false;
}
public static function retrieveFromDB(int $id): ?Post
{
$stmt = 'SELECT * FROM post WHERE id = ?';
$post = exec_stmt($stmt, 'i', $id)->fetch_assoc();
if ($post) {
$creator = User::retrieveFromDB($post['creator']);
switch ($post['postType']) {
case 1:
return new Essay($post['space'], $id, $creator, 1, $post['status'], $post['title'], $post['markdown'], $post['excerpt']);
case 2:
return new Moment($post['space'], $id, $creator, 2, $post['status'], $post['images'], $post['caption']);
case 3:
return new Thought($post['space'], $id, $creator, 3, $post['status'], $post['thought']);
}
}
return null;
}
public static function getComposeHTML(): string
{
$user = unserialize($_SESSION['user']);
// TODO: Need to store the current space in SESSION and use that to auto-select a Space
// TODO: List the user's spaces or let them search for a space or quick select a favorited space
$html = '
New Thought
Thought
Moment
Essay
';
return $html;
}
public static function getEditHTML($id)
{
$user = unserialize($_SESSION['user']);
$post = Post::retrieveFromDB($id);
if (!$post)
return '';
// TODO: Need to plumb this into the DB via AJAX.
// TODO: Need to store the current space in SESSION and use that to auto-select a Space
// TODO: List the user's spaces or let them search for a space or quick select a favorited space
$html = '
';
return $html;
}
public function setId(int $id): void
{
$this->id = $id;
}
public function getId(): int
{
return $this->id;
}
public function getCreator(): User
{
return $this->creator;
}
public function getPostType(): int
{
return $this->postType;
}
public function getSeenCount(): int
{
return $this->seenCount;
}
public function getCreatedAt(): ?DateTime
{
return $this->createdAt;
}
public function getUpdatedAt(): ?DateTime
{
return $this->updatedAt;
}
public function getStatus(): ?DateTime
{
return $this->status;
}
public function incrementSeenCount(): void
{
$this->seenCount++;
}
public function setStatus(?int $status): void
{
$this->status = $status;
}
public function setSpace(int $space): void
{
$this->space = $space;
}
public function getSpace(): int
{
if (!isset($this->space)) {
$stmt = 'SELECT space from spacePosts WHERE post = ?';
$rawSpace = exec_stmt($stmt, 'i', $this->id)->fetch_assoc();
}
return $this->space;
}
}
class Essay extends Post
{
private string $title;
private string $markdown;
private string $excerpt;
/* private array $files; */
public function __construct(int $space, ?int $id = null, User $creator, int $postType, int $status, string $title, string $markdown, string $excerpt)
{
parent::__construct($space, $id, $creator, 1, $postType, $status);
$this->title = $title;
$this->excerpt = $excerpt;
$this->markdown = $markdown;
if ($id < 0) {
parent::setId($id * -1);
$stmt = 'INSERT INTO post (id, space, creator, postType, status, title, markdown, excerpt) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
$space = isset($space) ? $space : $creator->getHomeSpace()->getId();
exec_stmt($stmt, 'iiiiisss', $id * -1, $space, $creator->getId(), 1, 2, $title, $markdown, $excerpt);
$stmt = 'INSERT INTO spacePosts (post, space) VALUES (?, ?)';
exec_stmt($stmt, 'ii', $id * -1, $space);
}
}
public static function edit(int $id, string $title, string $markdown, string $excerpt, int $space)
{
$post = Post::retrieveFromDB($id);
if (!is_a($post, 'Essay'))
return;
$post->setTitle($title);
$post->setMarkdown($markdown);
$post->setExcerpt($excerpt);
$post->setSpace($space);
$stmt = 'UPDATE post SET title = ?, markdown = ?, excerpt = ?, space = ? WHERE id = ?';
exec_stmt($stmt, 'sssii', $title, $markdown, $excerpt, $space, $id);
}
public function getCardHTML(): ?string
{
/* @' . parent::getCreator()->getUsername() . ' */
$html = '
';
return $html;
}
public function setTitle(string $title)
{
$this->title = $title;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setMarkdown(string $markdown)
{
$this->markdown = $markdown;
}
public function getMarkdown(): ?string
{
return $this->markdown;
}
public function getHTML(): string
{
$parsedown = new Parsedown();
$parsedown->setSafeMode(true);
return $parsedown->parse($this->markdown);
}
public function setExcerpt(string $excerpt)
{
$this->excerpt = $excerpt;
}
public function getExcerpt(): ?string
{
return $this->excerpt;
}
}
class Moment extends Post
{
private ?string $caption;
private $image;
public function __construct(int $space, ?int $id = null, User $creator, int $postType, int $status, $image, string $caption)
{
parent::__construct($space, $id, $creator, $postType, $status);
$this->caption = $caption;
$this->image = $image;
if ($id < 0) {
if (!isset($image['tmp_name']))
return;
mkdir($_ENV['IMG_UPLOAD'] . ($id * -1));
move_uploaded_file($image['tmp_name'], $_ENV['IMG_UPLOAD'] . ($id * -1) . '/' . $image['name']);
$this->image = $image['name'];
$stmt = 'INSERT INTO post (id, space, creator, postType, status, images, caption) VALUES (?, ?, ?, ?, ?, ?, ?)';
parent::setId($id * -1);
$space = isset($space) ? $space : $creator->getHomeSpace()->getId();
exec_stmt($stmt, 'iiiiiss', $id * -1, $space, $creator->getId(), 2, 2, $this->image, $this->caption);
$stmt = 'INSERT INTO spacePosts (post, space) VALUES (?, ?)';
exec_stmt($stmt, 'ii', $id * -1, $space);
}
}
public function getCardHTML(): ?string
{
$html = '
@' . parent::getCreator()->getUsername() . '
';
if (isset($_SESSION['user'])) {
$user = unserialize($_SESSION['user']);
if ($user->getId() == parent::getCreator()->getId()) {
$html .= '
';
}
}
// TODO: If multiple photos, use components/core/slideshow.php
$html .= '
' . $this->caption . '
';
return $html;
}
public static function edit(int $id, string $caption, int $space)
{
$post = Post::retrieveFromDB($id);
if (!is_a($post, 'Moment'))
return;
$post->setCaption($caption);
$post->setSpace($space);
$stmt = 'UPDATE post SET caption = ?, space = ? WHERE id = ?';
exec_stmt($stmt, 'sii', $caption, $space, $id);
}
public function getCaption(): ?string
{
return $this->caption;
}
public function getImages(): ?array
{
return $this->image;
}
public function setCaption(?string $caption): void
{
$this->caption = $caption;
}
public function setImages(?array $image): void
{
$this->image = $image;
}
}
class Thought extends Post
{
private ?string $thought;
public function __construct(int $space, ?int $id = null, User $creator, int $postType, int $status, string $thought)
{
parent::__construct($space, $id, $creator, $postType, $status);
$this->thought = $thought;
if ($id < 0) {
$stmt = 'INSERT INTO post (id, space, creator, postType, status, thought) VALUES (?, ?, ?, ?, ?, ?)';
parent::setId($id * -1);
$space = isset($space) ? $space : $creator->getHomeSpace()->getId();
exec_stmt($stmt, 'iiiiis', $id * -1, $space, $creator->getId(), 3, 2, $this->thought);
$stmt = 'INSERT INTO spacePosts (post, space) VALUES (?, ?)';
exec_stmt($stmt, 'ii', $id * -1, $space);
}
}
public static function edit(int $id, string $thought, int $space)
{
error_log('Updating post ' . $id . ' to have thought = ' . $thought);
$post = Post::retrieveFromDB($id);
if (!is_a($post, 'Thought'))
return;
$post->setThought($thought);
$post->setSpace($space);
$stmt = 'UPDATE post SET thought = ?, space = ? WHERE id = ?';
exec_stmt($stmt, 'sii', $thought, $space, $id);
}
public function getCardHTML(): ?string
{
$html = '
';
return $html;
}
public function getThought(): ?string
{
return $this->thought;
}
public function setThought(?string $thought): void
{
$this->thought = $thought;
}
}