chiro-canto/public/articles/Article.php

122 lines
4.5 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
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'];
if ( isset( $data['author_id'] ) ) $this->author_id = $data['author_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(publication_date) 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;
}
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 (publication_date, modification_date, title, summary, content) VALUES (FROM_UNIXTIME(:publication_date), FROM_UNIXTIME(:modification_date), :title, :summary, :content, :author_id');
$req->execute(array(
"publication_date"=>$this->publication_date,
"modification_date"=>$this->modification_date,
"title"=>$this->title,
"summary"=>$this->summary,
"content"=>$this->content,
"author_id"=>$this->author_id
));
$this->id = $db->lastInsertedId();
}
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
));
}
}
?>