Source for file OnpubWSMaps.php

Documentation is available at OnpubWSMaps.php

  1. <?php
  2.  
  3. /**
  4.  * Manage website-section maps in an Onpub database.
  5.  *
  6.  * This class contains the methods to manage the data contained in an
  7.  * {@link http://onpub.com/pdfs/onpub_schema.pdf OnpubWSMaps table}. An
  8.  * OnpubWSMaps table maps website IDs to section IDs. This allows Onpub to do
  9.  * the following without any duplication of website/section data:
  10.  * - Add a single section to multiple websites.
  11.  * - Add multiple sections to a single website.
  12.  * - Specify the order of sections in a website.
  13.  *
  14.  * @author {@link mailto:corey@onpub.com Corey H.M. Taylor}
  15.  * @copyright Onpub (TM). Copyright 2012, Onpub.com.
  16.  *  {@link http://onpub.com/}
  17.  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  18.  *  Version 2
  19.  * @package OnpubAPI
  20.  */
  21. {
  22.   private $pdo;
  23.   /**
  24.    * @var bool 
  25.    */
  26.   public $enableTransactions;
  27.  
  28.   /**
  29.    * Connect to an Onpub database.
  30.    *
  31.    * All the methods in this class which query the database use the database
  32.    * connection provided by the PDO object required by this constructor.
  33.    * Currently, Onpub only supports MySQL as a database for storing content.
  34.    * Therefore, when constructing the PDO object, only the
  35.    * {@link PHP_MANUAL#ref.pdo-mysql PDO_MYSQL} driver is supported
  36.    * as a PDO {@link PHP_MANUAL#ref.pdo-mysql.connection data source}.
  37.    *
  38.    * All the methods in this class require the
  39.    * {@link http://onpub.com/pdfs/onpub_schema.pdf Onpub schema}
  40.    * to be installed in the PDO-connected database. The {@link OnpubDatabase}
  41.    * class contains methods to manage the Onpub schema. The Onpub
  42.    * schema can also be installed by clicking the "Install the schema" link
  43.    * once logged in to the Onpub web interface. The schema generally only has
  44.    * to be installed once per database.
  45.    *
  46.    * @param PDO $pdo {@link PHP_MANUAL#function.pdo-construct PHP Data Object}.
  47.    * @param bool $enableTransactions 
  48.    */
  49.   function __construct(PDO $pdo$enableTransactions TRUE)
  50.   {
  51.     $this->pdo $pdo;
  52.     $this->enableTransactions = $enableTransactions;
  53.     $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARESTRUE);
  54.   }
  55.  
  56.   /**
  57.    * @param int $websiteID Delete the map(s) with websiteID.
  58.    * @param int $sectionID Delete the map(s) with sectionID.
  59.    * @return int The number of maps deleted, 0 if no maps were deleted.
  60.    */
  61.   public function delete($websiteID$sectionID)
  62.   {
  63.     if ($websiteID && $sectionID{
  64.       $stmt $this->pdo->prepare("DELETE FROM OnpubWSMaps WHERE websiteID = :websiteID AND sectionID = :sectionID");
  65.       OnpubDatabase::verifyPrepare($this->pdo$stmtFALSE);
  66.       $stmt->bindParam(':websiteID'$websiteID);
  67.       $stmt->bindParam(':sectionID'$sectionID);
  68.     }
  69.  
  70.     if ($websiteID && !$sectionID{
  71.       $stmt $this->pdo->prepare("DELETE FROM OnpubWSMaps WHERE websiteID = :websiteID");
  72.       OnpubDatabase::verifyPrepare($this->pdo$stmtFALSE);
  73.       $stmt->bindParam(':websiteID'$websiteID);
  74.     }
  75.  
  76.     if (!$websiteID && $sectionID{
  77.       $stmt $this->pdo->prepare("DELETE FROM OnpubWSMaps WHERE sectionID = :sectionID");
  78.       OnpubDatabase::verifyPrepare($this->pdo$stmtFALSE);
  79.       $stmt->bindParam(':sectionID'$sectionID);
  80.     }
  81.  
  82.     $result $stmt->execute();
  83.     OnpubDatabase::verifyExecute($this->pdo$resultFALSE$stmt->errorInfo());
  84.  
  85.     return $stmt->rowCount();
  86.   }
  87.  
  88.   /**
  89.    * @param OnpubWSMap $wsmap Map whose ID you want to get.
  90.    * @return int The ID of the map. NULL if the map does not exist in the database.
  91.    */
  92.   public function getID(OnpubWSMap $wsmap)
  93.   {
  94.     $stmt $this->pdo->prepare("SELECT ID FROM OnpubWSMaps WHERE websiteID = :websiteID AND sectionID = :sectionID");
  95.     OnpubDatabase::verifyPrepare($this->pdo$stmtFALSE);
  96.  
  97.     $websiteID $wsmap->websiteID;
  98.     $sectionID $wsmap->sectionID;
  99.  
  100.     $stmt->bindParam(':websiteID'$websiteID);
  101.     $stmt->bindParam(':sectionID'$sectionID);
  102.     $result $stmt->execute();
  103.     OnpubDatabase::verifyExecute($this->pdo$resultFALSE$stmt->errorInfo());
  104.  
  105.     if (($row $stmt->fetch(PDO::FETCH_ASSOC))) {
  106.       return $row["ID"];
  107.     }
  108.  
  109.     return NULL;
  110.   }
  111.  
  112.   /**
  113.    * @param OnpubQueryOptions $queryOptions Database query options.
  114.    * @return array An array of {@link OnpubWSMap} objects.
  115.    */
  116.   public function select(OnpubQueryOptions $queryOptions NULL$websiteID NULL)
  117.   {
  118.     if ($queryOptions === NULL)
  119.       $queryOptions new OnpubQueryOptions();
  120.  
  121.     $query $this->selectQuery($queryOptions$websiteID);
  122.     $result $this->pdo->query($query);
  123.     OnpubDatabase::verifyQuery($this->pdo$resultFALSE);
  124.     $rows $result->fetchAll(PDO::FETCH_ASSOC);
  125.  
  126.     $wsmaps array();
  127.  
  128.     if ($rows{
  129.       foreach ($rows as $row{
  130.         $wsmap new OnpubWSMap();
  131.  
  132.         $wsmap->ID $row["ID"];
  133.         $wsmap->websiteID $row["websiteID"];
  134.         $wsmap->sectionID $row["sectionID"];
  135.         $wsmap->setCreated(new DateTime($row["created"]));
  136.         $wsmap->setModified(new DateTime($row["modified"]));
  137.  
  138.         $wsmaps[$wsmap;
  139.       }
  140.     }
  141.  
  142.     $result->closeCursor();
  143.     return $wsmaps;
  144.   }
  145.  
  146.   private function selectQuery(OnpubQueryOptions $queryOptions NULL$websiteID)
  147.   {
  148.     if ($queryOptions === NULL)
  149.       $queryOptions new OnpubQueryOptions();
  150.  
  151.     $where "";
  152.     $orderBy "";
  153.     $limit "";
  154.  
  155.     if ($queryOptions->dateLimit{
  156.       $where "WHERE created <= "
  157.         . $this->pdo->quote($queryOptions->dateLimit->format('Y-m-d H:i:s'));
  158.  
  159.       if ($websiteID{
  160.         $where .= " AND websiteID = $websiteID";
  161.       }
  162.     }
  163.     else {
  164.       if ($websiteID{
  165.         $where "WHERE websiteID = $websiteID";
  166.       }
  167.     }
  168.  
  169.     if ($queryOptions->orderBy{
  170.       $orderBy "ORDER BY " $queryOptions->orderBy;
  171.  
  172.       if ($queryOptions->order{
  173.         $orderBy .= " " $queryOptions->order;
  174.       }
  175.     }
  176.  
  177.     if ($queryOptions->getPage(&& $queryOptions->rowLimit && $queryOptions->rowLimit 0{
  178.       $limit "LIMIT "
  179.         . (($queryOptions->getPage(1$queryOptions->rowLimit","
  180.         . $queryOptions->rowLimit;
  181.     }
  182.     elseif ($queryOptions->rowLimit && $queryOptions->rowLimit 0{
  183.       $limit "LIMIT 0," $queryOptions->rowLimit;
  184.     }
  185.  
  186.     return "SELECT ID, websiteID, sectionID, created, modified FROM OnpubWSMaps $where $orderBy $limit";
  187.   }
  188.  
  189.   /**
  190.    * @param mixed $wsmaps A single {@link OnpubWSMap} object or an array of {@link OnpubWSMap} objects (to insert multiple maps at a time).
  191.    * @return mixed The ID(s) of the new map(s). An int will be returned if a single map was inserted. An array of ints will be returned if multiple maps were inserted.
  192.    * @throws PDOException if there's a database error.
  193.    */
  194.   public function insert($wsmaps)
  195.   {
  196.     $wsmapIDs array();
  197.     $isArray TRUE;
  198.  
  199.     if (!is_array($wsmaps)) {
  200.       $wsmaps array ($wsmaps);
  201.       $isArray FALSE;
  202.     }
  203.  
  204.     if ($this->enableTransactions{
  205.       $status $this->pdo->beginTransaction();
  206.       OnpubDatabase::verifyTransaction($this->pdo$status);
  207.     }
  208.  
  209.     $stmt $this->pdo->prepare("INSERT INTO OnpubWSMaps (ID, websiteID, sectionID, created, modified) VALUES (:ID, :websiteID, :sectionID, :created, :modified)");
  210.     OnpubDatabase::verifyPrepare($this->pdo$stmt$this->enableTransactions);
  211.  
  212.     foreach ($wsmaps as $wsmap{
  213.       try {
  214.         $wsmapID $this->getID($wsmap);
  215.       }
  216.       catch (PDOException $e{
  217.         if ($this->enableTransactions)
  218.           $this->pdo->rollBack();
  219.  
  220.         throw $e;
  221.       }
  222.  
  223.       if ($wsmapID{
  224.         $wsmapIDs[$wsmapID;
  225.         $wsmap->ID $wsmapID;
  226.       }
  227.       else {
  228.         $ID $wsmap->ID;
  229.         $websiteID $wsmap->websiteID;
  230.         $sectionID $wsmap->sectionID;
  231.         $created $wsmap->getCreated()->format('Y-m-d H:i:s');
  232.         $modified $wsmap->getModified()->format('Y-m-d H:i:s');
  233.  
  234.         $stmt->bindParam(':ID'$ID);
  235.         $stmt->bindParam(':websiteID'$websiteID);
  236.         $stmt->bindParam(':sectionID'$sectionID);
  237.         $stmt->bindParam(':created'$created);
  238.         $stmt->bindParam(':modified'$modified);
  239.         $result $stmt->execute();
  240.         OnpubDatabase::verifyExecute($this->pdo$result$this->enableTransactions$stmt->errorInfo());
  241.  
  242.         $wsmapIDs[$this->pdo->lastInsertId();
  243.         $wsmap->ID $this->pdo->lastInsertId();
  244.       }
  245.     }
  246.  
  247.     if ($this->enableTransactions{
  248.       $status $this->pdo->commit();
  249.       OnpubDatabase::verifyTransaction($this->pdo$status);
  250.     }
  251.  
  252.     if ($isArray{
  253.       return $wsmapIDs;
  254.     }
  255.     else {
  256.       return end($wsmapIDs);
  257.     }
  258.   }
  259. }
  260. ?>

Documentation generated on Sun, 20 May 2012 04:03:02 -0400 by phpDocumentor 1.4.4