Rework DatabaseModule into Database static class
This commit is contained in:
parent
d56016f8cc
commit
1501634925
|
@ -19,17 +19,17 @@ require_once('../app/library.php');
|
||||||
/// Autoloader to get all our classes.
|
/// Autoloader to get all our classes.
|
||||||
|
|
||||||
const MAP = [
|
const MAP = [
|
||||||
'Article' => 'classes/Article.php',
|
'Article' => 'classes/Article.php',
|
||||||
'ArticleComment' => 'classes/ArticleComment.php',
|
'ArticleComment' => 'classes/ArticleComment.php',
|
||||||
'EpisodeComment' => 'classes/EpisodeComment.php',
|
'EpisodeComment' => 'classes/EpisodeComment.php',
|
||||||
'Project' => 'classes/Project.php',
|
'Project' => 'classes/Project.php',
|
||||||
'Show' => 'classes/Show.php',
|
'Show' => 'classes/Show.php',
|
||||||
'User' => 'classes/User.php',
|
'User' => 'classes/User.php',
|
||||||
|
|
||||||
'DatabaseModule' => 'modules/DatabaseModule.php',
|
'Database' => 'modules/Database.php',
|
||||||
'CommunityModule' => 'modules/CommunityModule.php',
|
'CommunityModule' => 'modules/CommunityModule.php',
|
||||||
'DisplayModule' => 'modules/DisplayModule.php',
|
'DisplayModule' => 'modules/DisplayModule.php',
|
||||||
'ParserModule' => 'modules/ParserModule.php',
|
'ParserModule' => 'modules/ParserModule.php',
|
||||||
|
|
||||||
'CommunityHub' => 'hubs/CommunityHub.php'
|
'CommunityHub' => 'hubs/CommunityHub.php'
|
||||||
];
|
];
|
||||||
|
|
|
@ -1,67 +1,39 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
class Article {
|
class Article
|
||||||
|
{
|
||||||
|
private $ID;
|
||||||
|
private $Title;
|
||||||
|
private $Cover;
|
||||||
|
private $Author;
|
||||||
|
private $Content;
|
||||||
|
private $PostDate;
|
||||||
|
private $Comments;
|
||||||
|
private $Description;
|
||||||
|
|
||||||
private $ID;
|
public function __construct(int $id)
|
||||||
private $Title;
|
{
|
||||||
private $Cover;
|
$this->ID = $id;
|
||||||
private $Author;
|
|
||||||
private $Content;
|
|
||||||
private $PostDate;
|
|
||||||
private $Comments;
|
|
||||||
private $Description;
|
|
||||||
|
|
||||||
|
$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);
|
$this->Title = $ArticleInfo['articleName'];
|
||||||
|
$this->Cover = $ArticleInfo['articleCover'];
|
||||||
}
|
$this->Author = $ArticleInfo['articleAuthor'];
|
||||||
|
$this->Content = $ArticleInfo['articleContent'];
|
||||||
|
$this->PostDate = betterDate($ArticleInfo['articleDate']);
|
||||||
private function getArticle($id) {
|
$this->Comments = $ArticleInfo['articleComments'];
|
||||||
|
$this->Description = $ArticleInfo['articleDescription'];
|
||||||
$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) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
?>
|
|
||||||
|
public function __get($what)
|
||||||
|
{
|
||||||
|
return property_exists($this, $what) ? $this->{$what} : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
59
app/modules/Database.php
Executable file
59
app/modules/Database.php
Executable file
|
@ -0,0 +1,59 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
--- The Database Module
|
||||||
|
--- This module is for the general CRUD methods.
|
||||||
|
--- This module was crafted by Skylear. : )
|
||||||
|
--- Copyright (c) Mad Splash, 2014, all rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Database
|
||||||
|
{
|
||||||
|
/* --- Variables - data the module needs to work with. --- */
|
||||||
|
private static PDO $c;
|
||||||
|
|
||||||
|
/* --- Constructor - creates the module's instance. --- */
|
||||||
|
public static function init(string $path, array $opts = [])
|
||||||
|
{
|
||||||
|
$opts = !empty($opts) ? $opts : [
|
||||||
|
PDO::ATTR_ERRMODE => 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,109 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
--- The Database Module
|
|
||||||
--- This module is for the general CRUD methods.
|
|
||||||
--- This module was crafted by Skylear. : )
|
|
||||||
--- Copyright (c) Mad Splash, 2014, all rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
class DatabaseModule {
|
|
||||||
/* --- Variables - data the module needs to work with. --- */
|
|
||||||
private $Handle = null;
|
|
||||||
private $userHandle = null;
|
|
||||||
private $FHandle = null;
|
|
||||||
|
|
||||||
/* --- Constructor - creates the module's instance. --- */
|
|
||||||
public function __construct() {
|
|
||||||
$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->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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
?>
|
|
Loading…
Reference in New Issue
Block a user