id = $row['ID']; $this->subtypeID = $row['SubtypeID']; $this->userID = $row['UserID']; $this->text = $row['Text']; $this->issue = $row['Cat']; $this->issueID = $row['IssueID']; $this->feelingID = $row['FeelingID']; $this->stamp = $row['Stamp']; } /************************************************************************** * CRUD Functions **************************************************************************/ protected static function create($userID, $languageID, $text) { if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid user ID: $userID"); } if (!filter_var($languageID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid language ID: $languageID"); } switch (get_called_class()) { case __NAMESPACE__ . '\ContentPost': $subtypeID = self::SUBTYPE_CONTENTPOST; break; case __NAMESPACE__ . '\ContentRepost': $subtypeID = self::SUBTYPE_CONTENTREPOST; break; case __NAMESPACE__ . '\UrlPost': $subtypeID = self::SUBTYPE_URLPOST; break; case __NAMESPACE__ . '\UrlRepost': $subtypeID = self::SUBTYPE_URLREPOST; break; default: throw new Exception('Unknown post sub-class'); } $newPostID = safe_dbwrite(" INSERT INTO tig.ProfilePosts SET SubtypeID = $subtypeID, UserID = $userID, LanguageID = $languageID, Text = '" . mysqlescape($text) . "', Stamp = NOW() "); return $newPostID; } public static function readByID($id) { if (!filter_var($id, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid ID: $id"); } $row = query("SELECT * FROM tig.ProfilePosts WHERE ID = $id"); if (!$row) { throw new \OutOfBoundsException("ID not found: $id"); } return self::readSubtype($row); } public static function readByUserID($userID, $limit, $page) { if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid User ID: $userID"); } if (!filter_var($limit, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid limit: $limit"); } if (!filter_var($page, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid before ID: $page"); } $items = array(); $offset = ($page - 1) * $limit; $rows = safe_dbread(" SELECT * FROM tig.ProfilePosts WHERE UserID = $userID ORDER BY ID DESC LIMIT $limit OFFSET $offset "); while ($row = safe_fetch_assoc($rows)) { $items[] = self::readSubtype($row); } return $items; } private function readSubtype($row) { switch ($row['SubtypeID']) { case self::SUBTYPE_CONTENTPOST: return new ContentPost($row); case self::SUBTYPE_CONTENTREPOST: return new ContentRepost($row); case self::SUBTYPE_URLPOST: return new UrlPost($row); case self::SUBTYPE_URLREPOST: return new UrlRepost($row); default: throw new \InvalidArgumentException("Invalid subtype: {$row['SubtypeID']}"); } } public function update() { safe_dbwrite(" UPDATE tig.ProfilePosts SET IssueID = " . ($this->issueID ?: 'NULL') . ", FeelingID = " . ($this->feelingID ?: 'NULL') . " WHERE ID = $this->id "); } public function delete() { safe_dbwrite("DELETE FROM tig.ProfilePosts WHERE ID = $this->id"); } /************************************************************************** * Getters \ Setters \ Checkers **************************************************************************/ public function getID() { return $this->id; } public function getSubtypeID() { return $this->subtypeID; } public function getUserID() { return $this->userID; } public function getUser() { return \TIG\Common\User\User::readByID($this->userID); } public function getIssue() { if (!$this->issueID) { return; } $issue = query("SELECT Cat FROM tig.Categories WHERE CatID = $this->issueID"); return $issue['Cat']; } public function getIssueID() { return $this->issueID; } public function setIssueID($issueID) { if (!filter_var($issueID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid issue ID: $issueID"); } $this->issueID = $issueID; } public function getFeelingID() { return $this->feelingID; } public function setFeelingID($feelingID) { if (!filter_var($feelingID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid feeling ID: $feelingID"); } $this->feelingID = $feelingID; } public static function checkText($text, $languageID, $isRequired) { $length = mb_strlen($text); if ($isRequired && $length === 0) { return self::ERROR_MISSING; } if ($length > self::TEXT_MAXLENGTH) { return self::ERROR_TOOLONG; } #if (hasSwear($text, $languageID)) { # return self::ERROR_SWEARS; #} return false; } public function getStamp() { return $this->stamp; } /************************************************************************** * Other Methods **************************************************************************/ /** * Counts the total number of posts by a user * * @return int */ public static function countPostsByUser($userID) { if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid user ID: $userID"); } $count = query(" SELECT COUNT(*) AS cnt FROM tig.ProfilePosts WHERE UserID = $userID "); return $count['cnt']; } /** * Counts the number of times the user's posts posts have been reposted by another user * * @return int */ public static function countUserPostsReposted($userID) { if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid user ID: $userID"); } $count = query(" SELECT COUNT(*) AS cnt FROM tig.ProfilePosts AS repost INNER JOIN tig.ProfilePosts AS original ON repost.RepostedPostID = original.ID WHERE original.UserID = $userID "); return $count['cnt']; } /** * Counts the number of times the user has reposted another user's profile post * * @return int */ public static function countRepostsByUser($userID) { if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid user ID: $userID"); } $count = query(" SELECT COUNT(*) AS cnt FROM tig.ProfilePosts WHERE UserID = $userID AND RepostedPostID IS NOT NULL "); return $count['cnt']; } /** * Returns a list of users that have reposted the given user's posts. * * @return int[] The IDs of users that have reposted the user's posts */ public static function getRepostersByUserID($userID) { if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid user ID: $userID"); } $reposters = array(); $reposterSet = safe_dbread(" SELECT DISTINCT repost.UserID FROM tig.ProfilePosts AS repost INNER JOIN tig.ProfilePosts AS original ON repost.RepostedPostID = original.ID WHERE original.UserID = $userID ORDER BY repost.Stamp DESC "); while ($reposter = safe_fetch_assoc($reposterSet)) { $reposters[] = $reposter['UserID']; } return $reposters; } /************************************************************************** * Abstract Methods **************************************************************************/ abstract public function getHtmlizedText(); abstract public function isRepostableByUser($userID); abstract public function repost($userID, $languageID, $text); abstract public function getRelatedPosts(); }