Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This class handle the article objects of the cms
- */
- class Article{
- public $id = null; //@var int: article ID
- public $pubDate = null; //@var int: date of publication, store as Unix Timestamp
- public $title = null; //@var string: title of article
- public $summary = null; //@var string: summary of the article
- public $content = null; //@var string: html content of article
- /*
- * Constructor
- */
- public function __construct( $data=array() ) {
- if( isset( $data['id']) )
- $this->id = (int)$data['id'];
- if( isset( $data['pubDate']) )
- $this->pubDate = (int)$data['pubDate'];
- if ( isset( $data['title'] ) )
- // filter out characters, for security purpose
- $this->title = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['title'] );
- if ( isset( $data['summary'] ) )
- // filter out characters, for security purpose
- $this->title = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['summary'] );
- if ( isset ($data['content']) )
- $this->content = $data['content'];
- }
- /*
- * Store values from a form into the object's properties
- * @formData the POST data from the form
- */
- public function storeFormValues($formData=array()) {
- // stores all the parameters
- $this->__construct($formData);
- // stores the publication data in appropriate form
- if (isset($formData['pubDate'])) {
- // split the string, because pubdate is in form YYYY-M-D
- $pubDate = explode('-', $formValue['pubDate']);
- if (count($pubDate) == 3)
- list($y, $m, $d) = $pubDate;
- // convert to unix timestamp
- $this->pubDate = mkTime(0, 0, 0, $m, $d, $y);
- }
- }
- /*
- * Get article object by id
- * This function is static due to the need of returning an article without
- * having to have an existing article object
- * @param $id int the id of the article
- */
- public static function getArticleById($id) {
- // create a new PDO object
- $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
- // sql command
- // :id is a place holder for article id (security purpose)
- $sql = "SELECT *, UNIX_TIMESTAMP(pubDate) AS pubDate FROM artciles WHERE id = :id";
- // prepare a PDO statement
- $st = $conn->prepare($sql);
- // bind the id value to the statement
- $st->bindValue(":id", $id, PDO::PARAM_INT);
- // execute the sql command
- $st->execute();
- // retrieve the row data
- $row = $st->fetch();
- // if data found then return data
- if ($row)
- return new Article($row);
- // close the connection
- $conn = null;
- }
- /*
- * Get all articles in the db
- * @return: array contains article objects
- */
- public static function getAllArticles() {
- // create a new PDO object
- $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
- // sql command
- $sql = "SELECT *, UNIX_TIMESTAMP(pubDate) as pubDate FROM articles";
- $articleList = array();
- foreach ($conn->query($sql) as $row)
- // create a new article and add it to the list
- $article = new Article($row);
- $articleList[] = $article;
- }
- /*
- * Inserts the current article object into the database
- * If the article object already has an id, don't insert it into the db
- * since db already has it!
- */
- public function insert() {
- // if the article already has an id, don't insert!
- if (!is_null($this->id)) trigger_error("Article::insert(): Cannot insert an article that already has an id", E_USER_ERROR);
- // create a new PDO object
- $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
- // sql statement with placeholders
- $sql = "INSERT INTO articles (pubDate, title, summary, content) VALUES ( FROM_UNIXTIME(:pubDate), :title, :summary, :content)";
- // prepare pdo statement
- $st = $conn->prepare($sql);
- // bind necessary values
- $st->bindValue(":pubDate", $this->pubDate, PDO::PARAM_STR);
- $st->bindValue(":title", $this->title, PDO::PARAM_STR);
- $st->bindValue(":summary", $this->summary, PDO::PARAM_STR);
- $st->bindValue(":content", $this->content, PDO::PARAM_STR);
- // excute
- $st->execute();
- // get id
- $this->id = $conn->lastInsertId();
- // close the connection
- $conn = null;
- }
- /*
- * Update an article record in the database
- */
- public function update() {
- // if the article doesn't have an id, trigger error
- if (is_null($this->id)) trigger_error("Article::update(): Cannot update article without an id", E_USER_ERROR);
- // create a new PDO object
- $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
- // sql query with placeholders
- $sql = "UPDATE articles SET pubDate = FROM_UNIXTIME(:pubDate) title=:title, summary=:summary, content=:content WHERE id=:id";
- // prepare pdo statement
- $st = $conn->prepare($sql);
- // bind necessary values
- $st->bindValue(":pubDate", $this->pubDate, PDO::PARAM_STR);
- $st->bindValue(":title", $this->title, PDO::PARAM_STR);
- $st->bindValue(":summary", $this->summary, PDO::PARAM_STR);
- $st->bindValue(":content", $this->content, PDO::PARAM_STR);
- $st->bindValue(":id", $this->id, PDO::PARAM_INT);
- // excute
- $st->execute();
- // close the connection
- $conn = null;
- }
- /*
- * Delete the current article object from the db
- */
- public function delete(){
- // if the article doesn't have an id, trigger error
- if (is_null($this->id)) trigger_error("Article::delete(): Cannot delete article without an id", E_USER_ERROR);
- // create a new PDO object
- $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
- // sql query, LIMIT 1 is for safety reason to make sure only 1 article is deleted at a time
- $sql = "DELETE FROM articles WHERE id=:id LIMIT 1";
- $st = $conn->prepare($sql);
- $st->bindValue(":id", $this->$id, PDO::PARAM_INT);
- $st->execute();
- $conn = null;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement