Move "classes" to "models" and update with new DB handler

This commit is contained in:
Sky Johnson 2024-06-29 12:26:14 -05:00
parent 1501634925
commit d011816617
13 changed files with 234 additions and 394 deletions

View File

@ -19,12 +19,12 @@ require_once('../app/library.php');
/// Autoloader to get all our classes.
const MAP = [
'Article' => 'classes/Article.php',
'ArticleComment' => 'classes/ArticleComment.php',
'EpisodeComment' => 'classes/EpisodeComment.php',
'Project' => 'classes/Project.php',
'Show' => 'classes/Show.php',
'User' => 'classes/User.php',
'Article' => 'models/Article.php',
'ArticleComment' => 'models/ArticleComment.php',
'EpisodeComment' => 'models/EpisodeComment.php',
'Project' => 'models/Project.php',
'Show' => 'models/Show.php',
'User' => 'models/User.php',
'Database' => 'modules/Database.php',
'CommunityModule' => 'modules/CommunityModule.php',
@ -39,3 +39,6 @@ spl_autoload_register(function ($class) {
require_once '../app/' . MAP[$class];
return true;
});
// Initialize the database handler
Database::init('../app/database.db');

View File

@ -1,59 +0,0 @@
<?php
class ArticleComment {
private $ID;
private $Author;
private $Content;
private $PostDate;
private $ArticleID;
public function __construct($id) {
$this->ID = $id;
$this->getArticleComment($id);
}
private function getArticleComment($id) {
$db = new DatabaseModule();
$getArticle = $db->Handle->prepare('SELECT * FROM ms_articlecomments WHERE id = :id');
$getArticle->bindValue(':id', $id, PDO::PARAM_INT);
$getArticle->execute();
$ArticleInfo = $getArticle->fetch();
$getArticle->closeCursor();
$this->Author = $ArticleInfo['commentAuthor'];
$this->Content = $ArticleInfo['commentContent'];
$this->PostDate = betterDate($ArticleInfo['commentDate']);
$this->ArticleID = $ArticleInfo['articleID'];
}
public function __get($what) {
if(property_exists($this, $what)) {
return $this->{$what};
} else {
return null;
}
}
public function update($what) {
}
}
?>

View File

@ -1,60 +0,0 @@
<?php
class EpisodeComment {
private $ID;
private $Author;
private $Content;
private $PostDate;
private $ShowID;
public function __construct($id) {
$this->ID = $id;
$this->getComment($id);
}
private function getComment($id) {
$db = new DatabaseModule();
$get = $db->Handle->prepare('SELECT * FROM episodecomments WHERE id = :id');
$get->bindValue(':id', $id, PDO::PARAM_INT);
$get->execute();
$data = $get->fetch();
$get->closeCursor();
$this->Author = $data['commentAuthor'];
$this->Content = $data['commentContent'];
$this->PostDate = betterDate($data['commentDate']);
$this->ArticleID = $data['showID'];
}
public function __get($what) {
if(property_exists($this, $what)) {
return $this->{$what};
} else {
return null;
}
}
public function update($what) {
}
}
?>

View File

@ -1,56 +0,0 @@
<?php
class Project {
/* -- User info variables; used for the functions here -- */
private $ID;
private $Type;
private $Desc;
private $Title;
private $Cover;
private $LastUpdate;
/* -- User class constructor -- */
public function __construct($id) {
$this->ID = $id;
$this->getProject($id);
}
/* -- Used in the constructor to access the DB and get all the user's info and populate the variables -- */
private function getProject($id) {
// Open database connection
$db = new DatabaseModule();
// Get user information from the DB
$getProject = $db->Handle->prepare('SELECT * FROM ms_projects WHERE id = :id');
$getProject->bindValue(':id', $id, PDO::PARAM_INT); // bind $id to the placeholder
$getProject->execute();
$ProjectInfo = $getProject->fetch(); // get the results from the query
$getProject->closeCursor(); // close the SELECT query from continuing its search
// Populate the variables
$this->ID = $ProjectInfo["id"];
$this->Type = $ProjectInfo["type"];
$this->Desc = $ProjectInfo["desc"];
$this->Title = $ProjectInfo["title"];
$this->Cover = $ProjectInfo["cover"];
$this->lastUpdate = betterDate($ProjectInfo["lastUpdate"]);
}
/* -- Returns whatever info needed at the moment -- */
public function __get($what) {
if(property_exists($this, $what)) {
return $this->{$what};
} else {
return null;
}
}
/* -- Updates a value in the DB belonging to the user -- */
public function update($what) {
}
}
?>

View File

@ -1,108 +0,0 @@
<?php
class Show {
private $ID;
private $Title;
private $ShowID;
private $Thumbnail;
private $Description;
private $EpisodeArray;
private $EpisodeComments;
private $db;
private $ThumbPath = "http://localhost:8888//assets/images/Thumbs/Show/";
public function __construct($id) {
$DBM = new DatabaseModule();
$this->db = $DBM->Handle;
$this->ShowID = $id;
$this->CommentBox = GetTemplate('comments/commentbox');
$this->CommentForm = GetTemplate('comments/commentform');
$this->getData();
$this->GetEpisodeList();
}
private function getData() {
$get = $this->db->prepare('SELECT * FROM ms_projects WHERE showid = :id');
$get->bindValue(':id', $this->ShowID, PDO::PARAM_INT); // bind $id to the placeholder
$get->execute();
$data = $get->fetch();
$get->closeCursor();
$this->ID = $data["id"];
$this->Title = $data["title"];
$this->Thumbnail = $this->ThumbPath . $data["thumbnail"];
$this->Description = $data["desc"];
}
public function __get($what) {
if(property_exists($this, $what)) {
return $this->{$what};
} else {
return null;
}
}
public function GetEpisodeList() {
$get = $this->db->prepare("SELECT * FROM episodes WHERE `show` = :s ORDER BY id DESC");
$get->bindValue(':s', $this->ShowID, PDO::PARAM_INT);
$get->execute();
$episodes = $get->fetchAll();
$get->closeCursor();
$this->EpisodeArray = $episodes;
}
public function GetEpisodeComments($id) {
$get = $this->db->prepare('SELECT * FROM episodecomments WHERE showID = :id');
$get->bindValue(':id', $id, PDO::PARAM_INT);
$get->execute();
$data = $get->fetchAll();
$list = array();
foreach($data as $ID) {
$comment = new EpisodeComment($ID['id']);
$poster = new User($comment->Author);
$list[] = ParseTemplate($this->CommentBox, array('a' => $poster->Avatar, 'u' => $poster->Username, 'c' => BBCode(magicClean($comment->Content)), 'd' => $comment->PostDate));
}
return $list;
}
}
?>

View File

@ -1,95 +0,0 @@
<?php
class User {
/* -- User info variables; used for the functions here -- */
private $Email;
private $Title;
private $UserID;
private $Avatar;
private $Gender;
private $Badges;
private $AboutMe;
private $WhatsUp;
private $Username;
private $BirthDay;
private $JoinDate;
private $PostCount;
private $UserTitle;
private $Reputation;
private $isVerified;
private $MemberLevel;
/* User Database Handle */
private $DB = null;
/* ------------------------------------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------------------------------------- */
public function __construct($id) {
// Open database connection
$DM = new DatabaseModule();
$DM->createUserHandle();
$this->DB = $DM->userHandle;
$this->UserID = $id;
$this->getUser($id);
}
/* ------------------------------------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------------------------------------- */
private function getUser($id) {
$db = $this->DB;
// Get username and ID from the database
$getUser = $db->prepare('SELECT * FROM ms_users WHERE id = :id');
$getUser->bindValue(':id', $id, PDO::PARAM_INT); // bind $id to the placeholder
$getUser->execute();
$User = $getUser->fetch(); // get the results from the query
$getUser->closeCursor(); // close the SELECT query from continuing its search
// Populate the variable(s)
$this->Username = $User['username'];
$this->Title = $User['title'];
$this->Email = $User['email'];
$this->Gender = $User['gender'];
$this->Badges = $User['badges'];
$this->AboutMe = $User['blurb'];
$this->WhatsUp = $User['status'];
$this->BirthDay = $User['bday'];
$this->JoinDate = $User['joindate'];
$this->PostCount = $User['posts'];
$this->Avatar = $User['avatar'];
$this->Reputation = $User['reputation'];
$this->MemberLevel = $User['mlevel'];
}
/* ------------------------------------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------------------------------------- */
public function __get($what) {
if(property_exists($this, $what)) {
return $this->{$what};
} else {
return null;
}
}
public function update($what) {
}
}
?>

View File

@ -15,13 +15,9 @@ class Article
{
$this->ID = $id;
$getArticle = $db->Handle->prepare('SELECT * FROM ms_articles WHERE id = :id');
$getArticle->bindValue(':id', $id, PDO::PARAM_INT);
$getArticle->execute();
$result = Database::select('articles', '*', 'WHERE id = :id', data: [':id' => $id]);
$ArticleInfo = $getArticle->fetch();
$getArticle->closeCursor();
$ArticleInfo = $result->fetch();
$this->Title = $ArticleInfo['articleName'];
$this->Cover = $ArticleInfo['articleCover'];

29
app/models/ArticleComment.php Executable file
View File

@ -0,0 +1,29 @@
<?php
class ArticleComment
{
private $ID;
private $Author;
private $Content;
private $PostDate;
private $ArticleID;
public function __construct($id)
{
$this->ID = $id;
$result = Database::select('articlecomments', '*', 'WHERE id = :id', data: [':id' => $id]);
$ArticleInfo = $result->fetch();
$this->Author = $ArticleInfo['commentAuthor'];
$this->Content = $ArticleInfo['commentContent'];
$this->PostDate = betterDate($ArticleInfo['commentDate']);
$this->ArticleID = $ArticleInfo['articleID'];
}
public function __get($what)
{
return property_exists($this, $what) ? $this->{$what} : null;
}
}

29
app/models/EpisodeComment.php Executable file
View File

@ -0,0 +1,29 @@
<?php
class EpisodeComment
{
private $ID;
private $Author;
private $Content;
private $PostDate;
private $ShowID;
public function __construct($id)
{
$this->ID = $id;
$result = Database::select('episodecomments', '*', 'WHERE id = :id', data: [':id' => $id]);
$data = $result->fetch();
$this->Author = $data['commentAuthor'];
$this->Content = $data['commentContent'];
$this->PostDate = betterDate($data['commentDate']);
$this->ShowID = $data['showID'];
}
public function __get($what)
{
return property_exists($this, $what) ? $this->{$what} : null;
}
}

32
app/models/Project.php Executable file
View File

@ -0,0 +1,32 @@
<?php
class Project
{
private $ID;
private $Type;
private $Desc;
private $Title;
private $Cover;
private $LastUpdate;
public function __construct($id)
{
$this->ID = $id;
$result = Database::select('projects', '*', 'WHERE id = :id', data: [':id' => $id]);
$data = $result->fetch();
$this->ID = $data["id"];
$this->Type = $data["type"];
$this->Desc = $data["desc"];
$this->Title = $data["title"];
$this->Cover = $data["cover"];
$this->LastUpdate = betterDate($data["lastUpdate"]);
}
public function __get($what)
{
return property_exists($this, $what) ? $this->{$what} : null;
}
}

81
app/models/Show.php Executable file
View File

@ -0,0 +1,81 @@
<?php
class Show
{
private $ID;
private $Title;
private $ShowID;
private $Thumbnail;
private $Description;
private $EpisodeArray;
private $EpisodeComments;
private $ThumbPath = "/assets/images/Thumbs/Show/";
public string $CommentBox;
public string $CommentForm;
public function __construct($id)
{
$this->ShowID = $id;
$this->CommentForm = render('comments/commentform');
$this->getData();
$this->GetEpisodeList();
}
private function getData()
{
$get = Database::select('projects', '*', 'WHERE showid = :id', data: [':id' => $this->ShowID]);
$data = $get->fetch();
$this->ID = $data["id"];
$this->Title = $data["title"];
$this->Thumbnail = $this->ThumbPath . $data["thumbnail"];
$this->Description = $data["desc"];
}
public function __get($what)
{
return property_exists($this, $what) ? $this->{$what} : null;
}
public function GetEpisodeList()
{
$get = Database::select('episodes', '*', 'WHERE `show` = :s', 'id DESC', data: [':s' => $this->ShowID]);
$episodes = $get->fetchAll();
$this->EpisodeArray = $episodes;
}
public function GetEpisodeComments($id)
{
$get = Database::select('episodecomments', '*', 'WHERE showID = :id', data: [':id' => $id]);
$data = $get->fetchAll();
$list = [];
foreach ($data as $ID) {
$comment = new EpisodeComment($ID['id']);
$poster = new User($comment->Author);
$list[] = render('comments/commentbox', [
'a' => $poster->Avatar,
'u' => $poster->Username,
'c' => BBCode(magicClean($comment->Content)),
'd' => $comment->PostDate
]);
}
return $list;
}
}

48
app/models/User.php Executable file
View File

@ -0,0 +1,48 @@
<?php
class User
{
private $Email;
private $Title;
private $UserID;
private $Avatar;
private $Gender;
private $Badges;
private $AboutMe;
private $WhatsUp;
private $Username;
private $BirthDay;
private $JoinDate;
private $PostCount;
private $UserTitle;
private $Reputation;
private $isVerified;
private $MemberLevel;
public function __construct($id)
{
$this->UserID = $id;
$result = Database::select('users', '*', 'WHERE id = :id', data: [':id' => $id]);
$User = $result->fetch();
$this->Username = $User['username'];
$this->Title = $User['title'];
$this->Email = $User['email'];
$this->Gender = $User['gender'];
$this->Badges = $User['badges'];
$this->AboutMe = $User['blurb'];
$this->WhatsUp = $User['status'];
$this->BirthDay = $User['bday'];
$this->JoinDate = $User['joindate'];
$this->PostCount = $User['posts'];
$this->Avatar = $User['avatar'];
$this->Reputation = $User['reputation'];
$this->MemberLevel = $User['mlevel'];
}
public function __get($what)
{
return property_exists($this, $what) ? $this->{$what} : null;
}
}

View File

@ -39,15 +39,15 @@ class Database
/* Select Data - used for getting data from the database.
* @return array $selectedRos - an array containing the data.
*/
public static function select(string $from, string $retrieve, string $condition, string $orderby = '', string $limit = '', array $data = []): array
public static function select(string $from, string $retrieve, string $condition, string $orderby = '', int $limit = 0, array $data = [])
{
$query = "SELECT $retrieve FROM $from $condition";
if (!empty($orderby)) { $query .= " $orderby"; }
if (!empty($limit)) { $query .= " $limit"; }
if (!empty($orderby)) { $query .= " ORDER BY $orderby"; }
if ($limit > 0) { $query .= " LIMIT $limit"; }
$selectQuery = self::$c->prepare($query);
$selectQuery->execute($data);
return $selectQuery->fetchAll();
return $selectQuery;
}
public function count(string $from, string $condition = ''): mixed