chiro-canto/public/articles/Article.php

130 lines
4.6 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
// session_start();
require "$root/database/credentials.php";
// Connect the database
try {
$db = new PDO("mysql:host=$host;dbname=$database;charset=utf8",
$user,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
} catch (Exception $e) {
die("Error : ".$e->getMessage());
}
class Article
{
public $id = null;
public $publication_date = null;
public $modification_date = null;
public $title = null;
public $summary = null;
public $content = null;
public $author_id = null;
public function __construct($data=array()) {
if (isset($data['id'])) $this->id = (int) $data['id'];
if (isset($data['publication_date'])) $this->publication_date = (int) $data['publication_date'];
if (isset($data['modification_date'])) $this->modification_date = (int) $data['modification_date'];
if (isset($data['title'])) $this->title = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['title'] );
if (isset($data['summary'])) $this->summary = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['summary'] );
if (isset($data['content'])) $this->content = $data['content'];
global $db;
$req = $db->prepare('SELECT id FROM authors WHERE username=:username');
$req->execute(array(
"username"=>$_SESSION['username']
));
if ($data = $req->fetch()) {
if (isset($data['id']))
$this->author_id = $data['id'];
}
}
public function storeFormValues($params) {
$this->__construct($params);
if (isset($params['publication_date'])) {
$publication_date = explode('-', $params['publication_date']);
if(count($publication_date) == 3) {
list($y, $m, $d) = $publication_date;
$this->publication_date = mktime(0, 0, 0, $m, $d, $y);
}
}
}
public static function getById($id)
{
global $db;
$req = $db->prepare('SELECT *, UNIX_TIMESTAMP(created_on) AS publication_date FROM articles WHERE id=:id');
$req->execute(array(
"id"=>$id
));
if ($data = $req->fetch()) {
return new Article($data);
}
}
public static function getList($num = 1000000) {
global $db;
$req = $db->prepare('SELECT *, UNIX_TIMESTAMP(created_on) AS publication_date FROM articles ORDER BY created_on DESC LIMIT 5');
$req->execute();//array(
// "num_rows"=>$num
// ));
$list = array();
while ($row = $req->fetch()) {
$article = new Article($row);
$list[] = $article;
// print_r($article);
}
return $list;
}
public function insert() {
if (! is_null($this->id)) {
trigger_error("Article::insert(): Attempt to insert an Article object that already has its ID property set (to $this->id).", E_USER_ERROR);
}
global $db;
$req = $db->prepare('INSERT INTO articles (created_on, modified_on, title, summary, content, article_by) VALUES (FROM_UNIXTIME(:created_on), FROM_UNIXTIME(:modified_on), :title, :summary, :content, :article_by)');
$req->execute(array(
"created_on"=>$this->publication_date,
"modified_on"=>$this->modification_date,
"title"=>$this->title,
"summary"=>$this->summary,
"content"=>$this->content,
"article_by"=>$this->author_id
));
}
public function update() {
if (is_null($this->id))
{
trigger_error("Article::update(): Attempt to update an Article object that does not have its ID property set.", E_USER_ERROR);
}
global $db;
$req = $db->prepare('UPDATE article SET modification_date=FROM_UNIXTIMESTAMP(:modification_date), title=:title, summary=:summary, content=:content WHERE id=:id');
$req->execute(array(
"modification_date"=>$this->modification_date,
"title"=>$this->title,
"summary"=>$this->summary,
"content"=>$this->content,
"id"=>$this->id
));
}
public function delete() {
if (is_null($this->id))
{
trigger_error( "Article::delete(): Attempt to delete an Article object that does not have its ID property set.", E_USER_ERROR);
}
global $db;
$req = $db->prepare('DELETE FROM articles WHERE id=:id LIMIT 1');
$req->execute(array(
"id"=>$this->id
));
}
}
?>