Source for file OnpubImages.php

Documentation is available at OnpubImages.php

  1. <?php
  2.  
  3. /**
  4.  * Manage images in an Onpub database.
  5.  *
  6.  * @author {@link mailto:corey@onpub.com Corey H.M. Taylor}
  7.  * @copyright Onpub (TM). Copyright 2012, Onpub.com.
  8.  *  {@link http://onpub.com/}
  9.  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  10.  *  Version 2
  11.  * @package OnpubAPI
  12.  */
  13. {
  14.   private $pdo;
  15.   /**
  16.    * @var bool 
  17.    */
  18.   public $enableTransactions;
  19.  
  20.   /**
  21.    * Connect to an Onpub database.
  22.    *
  23.    * All the methods in this class which query the database use the database
  24.    * connection provided by the PDO object required by this constructor.
  25.    * Currently, Onpub only supports MySQL as a database for storing content.
  26.    * Therefore, when constructing the PDO object, only the
  27.    * {@link PHP_MANUAL#ref.pdo-mysql PDO_MYSQL} driver is supported
  28.    * as a PDO {@link PHP_MANUAL#ref.pdo-mysql.connection data source}.
  29.    *
  30.    * All the methods in this class require the
  31.    * {@link http://onpub.com/pdfs/onpub_schema.pdf Onpub schema}
  32.    * to be installed in the PDO-connected database. The {@link OnpubDatabase}
  33.    * class contains methods to manage the Onpub schema. The Onpub
  34.    * schema can also be installed by clicking the "Install the schema" link
  35.    * once logged in to the Onpub web interface. The schema generally only has
  36.    * to be installed once per database.
  37.    *
  38.    * @param PDO $pdo {@link PHP_MANUAL#function.pdo-construct PHP Data Object}.
  39.    * @param bool $enableTransactions 
  40.    */
  41.   function __construct(PDO $pdo$enableTransactions TRUE)
  42.   {
  43.     $this->pdo $pdo;
  44.     $this->enableTransactions = $enableTransactions;
  45.     $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARESTRUE);
  46.   }
  47.  
  48.   /**
  49.    * @return int 
  50.    */
  51.   public function count()
  52.   {
  53.     $query $this->countQuery();
  54.     $result $this->pdo->query($query);
  55.     OnpubDatabase::verifyQuery($this->pdo$resultFALSE);
  56.  
  57.     if (!($row $result->fetch(PDO::FETCH_ASSOC))) {
  58.       return 0;
  59.     }
  60.  
  61.     $result->closeCursor();
  62.     return $row["count"];
  63.   }
  64.  
  65.   private function countQuery()
  66.   {
  67.     return "SELECT COUNT(ID) AS count FROM OnpubImages";
  68.   }
  69.  
  70.   /**
  71.    * @param mixed $images A single {@link OnpubImage} object or an array of {@link OnpubImage} objects (to insert multiple images at a time).
  72.    * @return mixed The ID(s) of the new image(s). An int will be returned if a single image was inserted. An array of ints will be returned if multiple images were inserted.
  73.    * @throws PDOException if there's a database error.
  74.    */
  75.   public function insert($images)
  76.   {
  77.     $IDs array();
  78.     $isArray TRUE;
  79.  
  80.     if (!is_array($images)) {
  81.       $images array ($images);
  82.       $isArray FALSE;
  83.     }
  84.  
  85.     if ($this->enableTransactions{
  86.       $status $this->pdo->beginTransaction();
  87.       OnpubDatabase::verifyTransaction($this->pdo$status);
  88.     }
  89.  
  90.     $stmt $this->pdo->prepare("INSERT INTO OnpubImages (ID, websiteID, fileName, description, url, created, modified) VALUES (:ID, :websiteID, :fileName, :description, :url, :created, :modified)");
  91.     OnpubDatabase::verifyPrepare($this->pdo$stmt$this->enableTransactions);
  92.  
  93.     foreach ($images as $image{
  94.       try {
  95.         $ID $this->getID($image);
  96.       }
  97.       catch (PDOException $e{
  98.         if ($this->enableTransactions)
  99.           $this->pdo->rollBack();
  100.  
  101.         throw $e;
  102.       }
  103.  
  104.       if ($ID{
  105.         $IDs[$ID;
  106.         $image->ID $ID;
  107.       }
  108.       else {
  109.         $ID $image->ID;
  110.         $websiteID $image->websiteID;
  111.         $fileName OnpubDatabase::utf8Decode(trim($image->fileName));
  112.         $description OnpubDatabase::utf8Decode(trim($image->description));
  113.         $url OnpubDatabase::utf8Decode(trim($image->url));
  114.         $created $image->getCreated()->format('Y-m-d H:i:s');
  115.         $modified $image->getModified()->format('Y-m-d H:i:s');
  116.  
  117.         $stmt->bindParam(':ID'$ID);
  118.         $stmt->bindParam(':websiteID'$websiteID);
  119.         $stmt->bindParam(':fileName'$fileName);
  120.         $stmt->bindParam(':description'$description);
  121.         $stmt->bindParam(':url'$url);
  122.         $stmt->bindParam(':created'$created);
  123.         $stmt->bindParam(':modified'$modified);
  124.         $result $stmt->execute();
  125.         OnpubDatabase::verifyExecute($this->pdo$result$this->enableTransactions$stmt->errorInfo());
  126.  
  127.         $IDs[$this->pdo->lastInsertId();
  128.         $image->ID $this->pdo->lastInsertId();
  129.       }
  130.     }
  131.  
  132.     if ($this->enableTransactions{
  133.       $status $this->pdo->commit();
  134.       OnpubDatabase::verifyTransaction($this->pdo$status);
  135.     }
  136.  
  137.     if ($isArray{
  138.       return $IDs;
  139.     }
  140.     else {
  141.       return end($IDs);
  142.     }
  143.   }
  144.  
  145.   /**
  146.    * @param int $ID ID of the image to get.
  147.    * @return OnpubImage An {@link OnpubImage} object. NULL if the image does not exist in the database.
  148.    */
  149.   public function get($ID)
  150.   {
  151.     $query $this->getQuery($ID);
  152.     $result $this->pdo->query($query);
  153.     OnpubDatabase::verifyQuery($this->pdo$resultFALSE);
  154.  
  155.     if (!($row $result->fetch(PDO::FETCH_ASSOC))) {
  156.       return NULL;
  157.     }
  158.  
  159.     $website new OnpubWebsite();
  160.  
  161.     $website->ID $row["websiteID"];
  162.     $website->imageID $row["websiteImageID"];
  163.     $website->name $row["websiteName"];
  164.     $website->url $row["websiteURL"];
  165.     $website->imagesURL $row["websiteImagesURL"];
  166.     $website->imagesDirectory $row["websiteImagesDirectory"];
  167.     $website->setCreated(new DateTime($row["websiteCreated"]));
  168.     $website->setModified(new DateTime($row["websiteModified"]));
  169.  
  170.     $image new OnpubImage();
  171.  
  172.     $image->ID $row["ID"];
  173.     $image->websiteID $row["websiteID"];
  174.     $image->fileName $row["fileName"];
  175.     $image->description $row["description"];
  176.     $image->url $row["url"];
  177.     $image->setCreated(new DateTime($row["created"]));
  178.     $image->setModified(new DateTime($row["modified"]));
  179.     $image->website $website;
  180.  
  181.     $result->closeCursor();
  182.     return $image;
  183.   }
  184.  
  185.   private function getQuery($ID)
  186.   {
  187.     return "SELECT images.ID, images.websiteID, images.fileName, " .
  188.            "images.description, images.url, images.created, images.modified, " .
  189.            "website.imageID as websiteImageID, website.name as websiteName, " .
  190.            "website.url as websiteURL, website.imagesURL as websiteImagesURL, " .
  191.            "website.imagesDirectory as websiteImagesDirectory, website.created " .
  192.            "as websiteCreated, website.modified as websiteModified FROM " .
  193.            "OnpubImages as images LEFT JOIN OnpubWebsites as website ON " .
  194.            "images.websiteID = website.ID WHERE images.ID = $ID";
  195.   }
  196.  
  197.   /**
  198.    * @param OnpubImage $image Image whose ID you want to get.
  199.    * @return int The ID of the image. NULL if the image does not exist in the database.
  200.    */
  201.   public function getID(OnpubImage $image)
  202.   {
  203.     $stmt $this->pdo->prepare("SELECT ID FROM OnpubImages WHERE websiteID = :websiteID AND fileName = :fileName");
  204.     OnpubDatabase::verifyPrepare($this->pdo$stmtFALSE);
  205.  
  206.     $websiteID $image->websiteID;
  207.     $fileName OnpubDatabase::utf8Decode(trim($image->fileName));
  208.  
  209.     $stmt->bindParam(':websiteID'$websiteID);
  210.     $stmt->bindParam(':fileName'$fileName);
  211.     $result $stmt->execute();
  212.     OnpubDatabase::verifyExecute($this->pdo$resultFALSE$stmt->errorInfo());
  213.  
  214.     if (($row $stmt->fetch(PDO::FETCH_ASSOC))) {
  215.       return $row["ID"];
  216.     }
  217.  
  218.     return NULL;
  219.   }
  220.  
  221.   /**
  222.    * @param OnpubQueryOptions $queryOptions Database query options.
  223.    * @return array An array of {@link OnpubImage} objects.
  224.    */
  225.   public function select(OnpubQueryOptions $queryOptions NULL)
  226.   {
  227.     if ($queryOptions === NULL)
  228.       $queryOptions new OnpubQueryOptions();
  229.  
  230.     $query $this->selectQuery($queryOptions);
  231.     $result $this->pdo->query($query);
  232.     OnpubDatabase::verifyQuery($this->pdo$resultFALSE);
  233.     $rows $result->fetchAll(PDO::FETCH_ASSOC);
  234.  
  235.     $images array();
  236.  
  237.     if ($rows{
  238.       foreach ($rows as $row{
  239.         $website new OnpubWebsite();
  240.  
  241.         $website->ID $row["websiteID"];
  242.         $website->imageID $row["websiteImageID"];
  243.         $website->name $row["websiteName"];
  244.         $website->url $row["websiteURL"];
  245.         $website->imagesURL $row["websiteImagesURL"];
  246.         $website->imagesDirectory $row["websiteImagesDirectory"];
  247.         $website->setCreated(new DateTime($row["websiteCreated"]));
  248.         $website->setModified(new DateTime($row["websiteModified"]));
  249.  
  250.         $image new OnpubImage();
  251.  
  252.         $image->ID $row["ID"];
  253.         $image->websiteID $row["websiteID"];
  254.         $image->fileName $row["fileName"];
  255.         $image->description $row["description"];
  256.         $image->url $row["url"];
  257.         $image->setCreated(new DateTime($row["created"]));
  258.         $image->setModified(new DateTime($row["modified"]));
  259.         $image->website $website;
  260.  
  261.         $images[$image;
  262.       }
  263.     }
  264.  
  265.     $result->closeCursor();
  266.     return $images;
  267.   }
  268.  
  269.   private function selectQuery(OnpubQueryOptions $queryOptions NULL)
  270.   {
  271.     if ($queryOptions === NULL)
  272.       $queryOptions new OnpubQueryOptions();
  273.  
  274.     $where "";
  275.     $orderBy "";
  276.     $limit "";
  277.  
  278.     if ($queryOptions->dateLimit{
  279.       $where "WHERE images.created <= "
  280.         . $this->pdo->quote($queryOptions->dateLimit->format('Y-m-d H:i:s'));
  281.     }
  282.  
  283.     if ($queryOptions->orderBy{
  284.       $orderBy "ORDER BY " $queryOptions->orderBy;
  285.  
  286.       if ($queryOptions->order{
  287.         $orderBy .= " " $queryOptions->order;
  288.       }
  289.     }
  290.  
  291.     if ($queryOptions->getPage(&& $queryOptions->rowLimit && $queryOptions->rowLimit 0{
  292.       $limit "LIMIT "
  293.         . (($queryOptions->getPage(1$queryOptions->rowLimit","
  294.         . $queryOptions->rowLimit;
  295.     }
  296.     elseif ($queryOptions->rowLimit && $queryOptions->rowLimit 0{
  297.       $limit "LIMIT 0," $queryOptions->rowLimit;
  298.     }
  299.  
  300.     return "SELECT images.ID, images.websiteID, images.fileName, " .
  301.            "images.description, images.url, images.created, images.modified, " .
  302.            "website.imageID as websiteImageID, website.name as websiteName, " .
  303.            "website.url as websiteURL, website.imagesURL as websiteImagesURL, " .
  304.            "website.imagesDirectory as websiteImagesDirectory, website.created " .
  305.            "as websiteCreated, website.modified as websiteModified FROM " .
  306.            "OnpubImages as images LEFT JOIN OnpubWebsites as website ON " .
  307.            "images.websiteID = website.ID $where $orderBy $limit";
  308.   }
  309.  
  310.   /**
  311.    * @param string $keywords Keyword(s) to search for.
  312.    * @return array All the images which were found as an array of {@link OnpubImage} objects.
  313.    */
  314.   public function search($keywordsOnpubQueryOptions $queryOptions NULL)
  315.   {
  316.     if ($queryOptions === NULL)
  317.       $queryOptions new OnpubQueryOptions();
  318.  
  319.     $query $this->searchQuery($keywords$queryOptions);
  320.     $result $this->pdo->query($query);
  321.     OnpubDatabase::verifyQuery($this->pdo$resultFALSE);
  322.  
  323.     $rows $result->fetchAll(PDO::FETCH_ASSOC);
  324.  
  325.     $images array();
  326.  
  327.     if ($rows{
  328.       foreach ($rows as $row{
  329.         $image new OnpubImage();
  330.  
  331.         $image->ID $row["ID"];
  332.         $image->websiteID $row["websiteID"];
  333.         $image->fileName $row["fileName"];
  334.         $image->description $row["description"];
  335.         $image->url $row["url"];
  336.         $image->setCreated(new DateTime($row["created"]));
  337.         $image->setModified(new DateTime($row["modified"]));
  338.  
  339.         $images[$image;
  340.       }
  341.     }
  342.  
  343.     $result->closeCursor();
  344.     return $images;
  345.   }
  346.  
  347.   private function searchQuery($keywordsOnpubQueryOptions $queryOptions NULL)
  348.   {
  349.     if ($queryOptions === NULL)
  350.       $queryOptions new OnpubQueryOptions();
  351.  
  352.     $keywords $this->pdo->quote(OnpubDatabase::utf8Decode(trim($keywords)));
  353.  
  354.     if ($queryOptions->orderBy{
  355.       $orderBy $queryOptions->orderBy;
  356.     }
  357.     else {
  358.       $orderBy "";
  359.     }
  360.  
  361.     if ($queryOptions->order{
  362.       $order $queryOptions->order;
  363.     }
  364.     else {
  365.       $order "";
  366.     }
  367.  
  368.     if ($orderBy{
  369.       if ($order{
  370.         return "SELECT ID, websiteID, fileName ,description, url, created, modified FROM OnpubImages WHERE ID RLIKE $keywords OR fileName RLIKE $keywords OR description RLIKE $keywords OR modified RLIKE $keywords ORDER BY $orderBy $order";
  371.       }
  372.       else {
  373.         return "SELECT ID, websiteID, fileName ,description, url, created, modified FROM OnpubImages WHERE ID RLIKE $keywords OR fileName RLIKE $keywords OR description RLIKE $keywords OR modified RLIKE $keywords ORDER BY $orderBy";
  374.       }
  375.     }
  376.     else {
  377.       return "SELECT ID, websiteID, fileName ,description, url, created, modified FROM OnpubImages WHERE ID RLIKE $keywords OR fileName RLIKE $keywords OR description RLIKE $keywords OR modified RLIKE $keywords";
  378.     }
  379.   }
  380.  
  381.   /**
  382.    * @param OnpubImage $image The image to be updated.
  383.    * @return int 1 if the image was updated. 0 if the image does not exist in the database.
  384.    */
  385.   public function update(OnpubImage $image)
  386.   {
  387.     $now new DateTime();
  388.  
  389.     $stmt $this->pdo->prepare("UPDATE OnpubImages SET fileName = :fileName, description = :description, url = :url, modified = :modified WHERE ID = :ID");
  390.     OnpubDatabase::verifyPrepare($this->pdo$stmt$this->enableTransactions);
  391.  
  392.     $ID $image->ID;
  393.     $fileName OnpubDatabase::utf8Decode(trim($image->fileName));
  394.     $description OnpubDatabase::utf8Decode(trim($image->description));
  395.     $url OnpubDatabase::utf8Decode(trim($image->url));
  396.     $modified $now->format('Y-m-d H:i:s');
  397.  
  398.     $stmt->bindParam(':ID'$ID);
  399.     $stmt->bindParam(':fileName'$fileName);
  400.     $stmt->bindParam(':description'$description);
  401.     $stmt->bindParam(':url'$url);
  402.     $stmt->bindParam(':modified'$modified);
  403.     $result $stmt->execute();
  404.     OnpubDatabase::verifyExecute($this->pdo$result$this->enableTransactions$stmt->errorInfo());
  405.  
  406.     return $stmt->rowCount();
  407.   }
  408.  
  409.   /**
  410.    * @param int $ID ID of the image to delete.
  411.    * @return int 1 if the image was deleted, 0 if the image does not exist in the database.
  412.    */
  413.   public function delete($ID)
  414.   {
  415.     $stmt $this->pdo->prepare("DELETE FROM OnpubImages WHERE ID = :ID");
  416.     OnpubDatabase::verifyPrepare($this->pdo$stmtFALSE);
  417.  
  418.     $stmt->bindParam(':ID'$ID);
  419.     $result $stmt->execute();
  420.     OnpubDatabase::verifyExecute($this->pdo$resultFALSE$stmt->errorInfo());
  421.  
  422.     return $stmt->rowCount();
  423.   }
  424.  
  425.   public static function getThumbURL($phpThumbParams$onpub_dir_phpthumb '../api/phpThumb/')
  426.   {
  427.     global $PHPTHUMB_CONFIG;
  428.  
  429.     if ($PHPTHUMB_CONFIG['high_security_enabled']{
  430.       return $onpub_dir_phpthumb 'phpThumb.php?' $phpThumbParams '&hash=' md5($phpThumbParams $PHPTHUMB_CONFIG['high_security_password']);
  431.     }
  432.  
  433.     return $onpub_dir_phpthumb 'phpThumb.php?' $phpThumbParams;
  434.   }
  435. }
  436. ?>

Documentation generated on Fri, 08 Feb 2013 04:02:21 -0500 by phpDocumentor 1.4.4