diff --git a/app/bootstrap.php b/app/bootstrap.php index c955a74..e0d0ac2 100644 --- a/app/bootstrap.php +++ b/app/bootstrap.php @@ -19,17 +19,17 @@ require_once('../app/library.php'); /// Autoloader to get all our classes. const MAP = [ - 'Article' => 'classes/Article.php', + 'Article' => 'classes/Article.php', 'ArticleComment' => 'classes/ArticleComment.php', 'EpisodeComment' => 'classes/EpisodeComment.php', - 'Project' => 'classes/Project.php', - 'Show' => 'classes/Show.php', - 'User' => 'classes/User.php', + 'Project' => 'classes/Project.php', + 'Show' => 'classes/Show.php', + 'User' => 'classes/User.php', - 'DatabaseModule' => 'modules/DatabaseModule.php', + 'Database' => 'modules/Database.php', 'CommunityModule' => 'modules/CommunityModule.php', - 'DisplayModule' => 'modules/DisplayModule.php', - 'ParserModule' => 'modules/ParserModule.php', + 'DisplayModule' => 'modules/DisplayModule.php', + 'ParserModule' => 'modules/ParserModule.php', 'CommunityHub' => 'hubs/CommunityHub.php' ]; diff --git a/app/classes/Article.php b/app/classes/Article.php index 4cec490..fba5427 100755 --- a/app/classes/Article.php +++ b/app/classes/Article.php @@ -1,67 +1,39 @@ ID = $id; + $getArticle = $db->Handle->prepare('SELECT * FROM ms_articles WHERE id = :id'); + $getArticle->bindValue(':id', $id, PDO::PARAM_INT); + $getArticle->execute(); - public function __construct($id) { + $ArticleInfo = $getArticle->fetch(); - $this->ID = $id; + $getArticle->closeCursor(); - $this->getArticle($id); - - } - - - private function getArticle($id) { - - $db = new DatabaseModule(); - - $getArticle = $db->Handle->prepare('SELECT * FROM ms_articles WHERE id = :id'); - $getArticle->bindValue(':id', $id, PDO::PARAM_INT); - $getArticle->execute(); - - $ArticleInfo = $getArticle->fetch(); - - $getArticle->closeCursor(); - - - $this->Title = $ArticleInfo['articleName']; - $this->Cover = $ArticleInfo['articleCover']; - $this->Author = $ArticleInfo['articleAuthor']; - $this->Content = $ArticleInfo['articleContent']; - $this->PostDate = betterDate($ArticleInfo['articleDate']); - $this->Comments = $ArticleInfo['articleComments']; - $this->Description = $ArticleInfo['articleDescription']; - - } - - - public function __get($what) { - - if(property_exists($this, $what)) { - - return $this->{$what}; - - } else { - - return null; - - } - - } - - - public function update($what) { - - } + $this->Title = $ArticleInfo['articleName']; + $this->Cover = $ArticleInfo['articleCover']; + $this->Author = $ArticleInfo['articleAuthor']; + $this->Content = $ArticleInfo['articleContent']; + $this->PostDate = betterDate($ArticleInfo['articleDate']); + $this->Comments = $ArticleInfo['articleComments']; + $this->Description = $ArticleInfo['articleDescription']; } -?> + + public function __get($what) + { + return property_exists($this, $what) ? $this->{$what} : null; + } +} diff --git a/app/modules/Database.php b/app/modules/Database.php new file mode 100755 index 0000000..bbe8e79 --- /dev/null +++ b/app/modules/Database.php @@ -0,0 +1,59 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + ]; + + try { + self::$c = new PDO("sqlite:$path", null, null, $opts); + } catch (PDOException $e) { + throw $e; + } + } + + /* --- Module Functions - the meat and purpose of the module. --- */ + /* Create Table - for creating a table when need be. + * @param string $name - the name of the table to be created. + * @param string $columns - a long string containing the columns to be made. + */ + public static function createTable($name, $columns) { + // No need for creating tables for the time being. + } + + /* 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 + { + $query = "SELECT $retrieve FROM $from $condition"; + if (!empty($orderby)) { $query .= " $orderby"; } + if (!empty($limit)) { $query .= " $limit"; } + + $selectQuery = self::$c->prepare($query); + $selectQuery->execute($data); + return $selectQuery->fetchAll(); + } + + public function count(string $from, string $condition = ''): mixed + { + $query = "SELECT COUNT(*) FROM $from"; + if (!empty($condition)) { $query .= " $condition"; } + return self::$c->query($query)->fetch(); + } +} diff --git a/app/modules/DatabaseModule.php b/app/modules/DatabaseModule.php deleted file mode 100755 index 70d1677..0000000 --- a/app/modules/DatabaseModule.php +++ /dev/null @@ -1,109 +0,0 @@ -getMessage() . "."); - } - - $DBHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $DBHandle->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); - - $this->Handle = $DBHandle; - } - - public function createUserHandle() { - $DBUsername = 'root'; - $DBPassword = 'root'; - $DBName = 'database'; - - try { - $DBHandle = new PDO('mysql: host=localhost; dbname=' . $DBName, $DBUsername, $DBPassword); - } catch(PDOException $ex) { - die("Oops, we failed to the connect to the database. The error: " . $ex->getMessage() . "."); - } - - $DBHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $DBHandle->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); - - $this->userHandle = $DBHandle; - } - - public function createFHandle() { - $DBUsername = 'root'; - $DBPassword = 'root'; - $DBName = 'database'; - - try { - $DBHandle = new PDO('mysql: host=localhost; dbname=' . $DBName, $DBUsername, $DBPassword); - } catch(PDOException $ex) { - die("Oops, we failed to the connect to the database. The error: " . $ex->getMessage() . "."); - } - - $DBHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $DBHandle->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); - - $this->FHandle = $DBHandle; - } - - public function __get($what) { - if(property_exists($this, $what)) { - return $this->{$what}; - } else { - return null; - } - } - - /* --- Module Functions - the meat and purpose of the module. --- */ - /* Create Table - for creating a table when need be. - * @param string $name - the name of the table to be created. - * @param string $columns - a long string containing the columns to be made. - */ - public function createTable($name, $columns) { - // No need for creating tables for the time being. - } - - /* Select Data - used for getting data from the database. - * @return array $selectedRos - an array containing the data. - */ - public function getData($from, $retrieve, $condition, $limit, $orderby) { - $Handle = $this->Handle; - - $query = 'SELECT ' . $retrieve . ' FROM ' . $from . ' ' . $condition . ' ' . $orderby . ' ' . $limit ; - - $selectQuery = $Handle->prepare($query); - $selectQuery->execute(); - $selectedRows = $selectQuery->fetchAll(); - - return $selectedRows; - } - - public function countRows($which, $condition) { - $Handle = $this->Handle; - - $query = 'SELECT COUNT(*) FROM ' . $which . ' ' . $condition; - $count = $Handle->query($query); - - return $count->fetch(); - } - } -?>