Advertisement
Guest User

The Article Class

a guest
Feb 21st, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.64 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This class handle the article objects of the cms
  4.  */
  5.  
  6. class Article{
  7.     public $id = null; //@var int: article ID
  8.     public $pubDate = null; //@var int: date of publication, store as Unix Timestamp
  9.     public $title = null; //@var string: title of article
  10.     public $summary = null; //@var string: summary of the article
  11.     public $content = null; //@var string: html content of article
  12.    
  13.     /*
  14.      * Constructor
  15.      */
  16.     public function __construct( $data=array() ) {
  17.         if( isset( $data['id']) )
  18.             $this->id = (int)$data['id'];
  19.         if( isset( $data['pubDate']) )
  20.             $this->pubDate = (int)$data['pubDate'];
  21.         if ( isset( $data['title'] ) )
  22.             // filter out characters, for security purpose
  23.             $this->title = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['title'] );
  24.         if ( isset( $data['summary'] ) )
  25.             // filter out characters, for security purpose
  26.             $this->title = preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['summary'] );
  27.         if ( isset ($data['content']) )
  28.             $this->content = $data['content'];
  29.     }
  30.    
  31.     /*
  32.      * Store values from a form into the object's properties
  33.      * @formData the POST data from the form
  34.      */
  35.     public function storeFormValues($formData=array()) {
  36.         // stores all the parameters
  37.         $this->__construct($formData);
  38.        
  39.         // stores the publication data in appropriate form
  40.         if (isset($formData['pubDate'])) {
  41.             // split the string, because pubdate is in form YYYY-M-D   
  42.             $pubDate = explode('-', $formValue['pubDate']);
  43.             if (count($pubDate) == 3)
  44.                 list($y, $m, $d) = $pubDate;
  45.             // convert to unix timestamp
  46.             $this->pubDate = mkTime(0, 0, 0, $m, $d, $y);
  47.         }
  48.     }
  49.    
  50.     /*
  51.      * Get article object by id
  52.      * This function is static due to the need of returning an article without
  53.      * having to have an existing article object
  54.      * @param $id int the id of the article
  55.      */
  56.     public static function getArticleById($id) {
  57.         // create a new PDO object 
  58.         $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
  59.         // sql command
  60.         // :id is a place holder for article id (security purpose)
  61.         $sql = "SELECT *, UNIX_TIMESTAMP(pubDate) AS pubDate FROM artciles WHERE id = :id";
  62.         // prepare a PDO statement
  63.         $st = $conn->prepare($sql);
  64.         // bind the id value to the statement
  65.         $st->bindValue(":id", $id, PDO::PARAM_INT);
  66.         // execute the sql command
  67.         $st->execute();
  68.         // retrieve the row data
  69.         $row = $st->fetch();
  70.         // if data found then return data
  71.         if ($row)
  72.             return new Article($row);
  73.         // close the connection
  74.         $conn = null;
  75.     }
  76.    
  77.     /*
  78.      * Get all articles in the db
  79.      * @return: array contains article objects
  80.      */
  81.     public static function getAllArticles() {
  82.         // create a new PDO object
  83.         $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
  84.         // sql command
  85.         $sql = "SELECT *, UNIX_TIMESTAMP(pubDate) as pubDate FROM articles";
  86.         $articleList = array();
  87.         foreach ($conn->query($sql) as $row)
  88.             // create a new article and add it to the list
  89.             $article = new Article($row);
  90.             $articleList[] = $article;
  91.     }
  92.    
  93.     /*
  94.      * Inserts the current article object into the database
  95.      * If the article object already has an id, don't insert it into the db
  96.      * since db already has it!
  97.      */
  98.     public function insert() {
  99.         // if the article already has an id, don't insert!
  100.         if (!is_null($this->id)) trigger_error("Article::insert(): Cannot insert an article that already has an id", E_USER_ERROR);
  101.        
  102.         // create a new PDO object
  103.         $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
  104.         // sql statement with placeholders
  105.         $sql = "INSERT INTO articles (pubDate, title, summary, content) VALUES ( FROM_UNIXTIME(:pubDate), :title, :summary, :content)";
  106.         // prepare pdo statement
  107.         $st = $conn->prepare($sql);
  108.         // bind necessary values
  109.         $st->bindValue(":pubDate", $this->pubDate, PDO::PARAM_STR);
  110.         $st->bindValue(":title", $this->title, PDO::PARAM_STR);
  111.         $st->bindValue(":summary", $this->summary, PDO::PARAM_STR);
  112.         $st->bindValue(":content", $this->content, PDO::PARAM_STR);
  113.         // excute
  114.         $st->execute();
  115.         // get id
  116.         $this->id = $conn->lastInsertId();
  117.         // close the connection
  118.         $conn = null;
  119.     }
  120.  
  121.     /*
  122.      * Update an article record in the database
  123.      */
  124.     public function update() {
  125.         // if the article doesn't have an id, trigger error
  126.         if (is_null($this->id)) trigger_error("Article::update(): Cannot update article without an id", E_USER_ERROR);
  127.        
  128.         // create a new PDO object
  129.         $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
  130.         // sql query with placeholders
  131.         $sql = "UPDATE articles SET pubDate = FROM_UNIXTIME(:pubDate) title=:title, summary=:summary, content=:content WHERE id=:id";
  132.         // prepare pdo statement
  133.         $st = $conn->prepare($sql);
  134.         // bind necessary values
  135.         $st->bindValue(":pubDate", $this->pubDate, PDO::PARAM_STR);
  136.         $st->bindValue(":title", $this->title, PDO::PARAM_STR);
  137.         $st->bindValue(":summary", $this->summary, PDO::PARAM_STR);
  138.         $st->bindValue(":content", $this->content, PDO::PARAM_STR);
  139.         $st->bindValue(":id", $this->id, PDO::PARAM_INT);
  140.         // excute
  141.         $st->execute();
  142.         // close the connection
  143.         $conn = null;
  144.     }
  145.    
  146.     /*
  147.      * Delete the current article object from the db
  148.      */
  149.     public function delete(){
  150.         // if the article doesn't have an id, trigger error
  151.         if (is_null($this->id)) trigger_error("Article::delete(): Cannot delete article without an id", E_USER_ERROR);
  152.        
  153.         // create a new PDO object
  154.         $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
  155.         // sql query, LIMIT 1 is for safety reason to make sure only 1 article is deleted at a time
  156.         $sql = "DELETE FROM articles WHERE id=:id LIMIT 1";
  157.         $st = $conn->prepare($sql);
  158.         $st->bindValue(":id", $this->$id, PDO::PARAM_INT);
  159.         $st->execute();
  160.         $conn = null;
  161.     }
  162. }
  163. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement