Source for file OnpubSAMaps.php
Documentation is available at OnpubSAMaps.php
* Manage section-article maps in an Onpub database.
* This class contains the methods to manage the data contained in an
* {@link http://onpub.com/pdfs/onpub_schema.pdf OnpubSAMaps table}. An
* OnpubSAMaps table maps section IDs to article IDs. This allows Onpub to do
* the following without any duplication of section/article data:
* - Add a single article to multiple sections.
* - Add multiple articles to a single section.
* - Specify the order of articles in a section.
* @author {@link mailto:corey@onpub.com Corey H.M. Taylor}
* @copyright Onpub (TM). Copyright 2012, Onpub.com.
* {@link http://onpub.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* Connect to an Onpub database.
* All the methods in this class which query the database use the database
* connection provided by the PDO object required by this constructor.
* Currently, Onpub only supports MySQL as a database for storing content.
* Therefore, when constructing the PDO object, only the
* {@link PHP_MANUAL#ref.pdo-mysql PDO_MYSQL} driver is supported
* as a PDO {@link PHP_MANUAL#ref.pdo-mysql.connection data source}.
* All the methods in this class require the
* {@link http://onpub.com/pdfs/onpub_schema.pdf Onpub schema}
* to be installed in the PDO-connected database. The {@link OnpubDatabase}
* class contains methods to manage the Onpub schema. The Onpub
* schema can also be installed by clicking the "Install the schema" link
* once logged in to the Onpub web interface. The schema generally only has
* to be installed once per database.
* @param PDO $pdo A {@link PHP_MANUAL#function.pdo-construct PHP Data Object}.
* @param bool $enableTransactions
function __construct(PDO $pdo, $enableTransactions =
TRUE)
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);
* @param int $sectionID Delete the map(s) with sectionID.
* @param int $articleID Delete the map(s) with articleID.
* @return int The number of maps deleted, 0 if no maps were deleted.
public function delete($sectionID, $articleID)
if ($sectionID &&
$articleID) {
$stmt =
$this->pdo->prepare("DELETE FROM OnpubSAMaps WHERE sectionID = :sectionID AND articleID = :articleID");
$stmt->bindParam(':sectionID', $sectionID);
$stmt->bindParam(':articleID', $articleID);
if ($sectionID &&
!$articleID) {
$stmt =
$this->pdo->prepare("DELETE FROM OnpubSAMaps WHERE sectionID = :sectionID");
$stmt->bindParam(':sectionID', $sectionID);
if (!$sectionID &&
$articleID) {
$stmt =
$this->pdo->prepare("DELETE FROM OnpubSAMaps WHERE articleID = :articleID");
$stmt->bindParam(':articleID', $articleID);
$result =
$stmt->execute();
return $stmt->rowCount();
* @param OnpubSAMap $samap Map whose ID you want to get.
* @return int The ID of the map. NULL if the map does not exist in the database.
public function getID(OnpubSAMap $samap)
$stmt =
$this->pdo->prepare("SELECT ID FROM OnpubSAMaps WHERE sectionID = :sectionID AND articleID = :articleID");
$sectionID =
$samap->sectionID;
$articleID =
$samap->articleID;
$stmt->bindParam(':sectionID', $sectionID);
$stmt->bindParam(':articleID', $articleID);
$result =
$stmt->execute();
if (($row =
$stmt->fetch(PDO::FETCH_ASSOC))) {
* @param OnpubQueryOptions $queryOptions Database query options.
* @return array An array of {@link OnpubSAMap} objects.
public function select(OnpubQueryOptions $queryOptions =
NULL, $sectionID =
NULL, $articleID =
NULL)
if ($queryOptions ===
NULL)
$query =
$this->selectQuery($queryOptions, $sectionID, $articleID);
$result =
$this->pdo->query($query);
$rows =
$result->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$samap->sectionID =
$row["sectionID"];
$samap->articleID =
$row["articleID"];
$samap->setCreated(new DateTime($row["created"]));
$samap->setModified(new DateTime($row["modified"]));
private function selectQuery(OnpubQueryOptions $queryOptions =
NULL, $sectionID =
NULL, $articleID =
NULL)
if ($queryOptions ===
NULL)
if ($queryOptions->dateLimit) {
$where =
"WHERE created <= "
.
$this->pdo->quote($queryOptions->dateLimit->format('Y-m-d H:i:s'));
$where .=
" AND sectionID = $sectionID";
$where .=
" AND articleID = $articleID";
$where =
"WHERE sectionID = $sectionID";
$where =
"WHERE articleID = $articleID";
if ($queryOptions->orderBy) {
$orderBy =
"ORDER BY " .
$queryOptions->orderBy;
if ($queryOptions->order) {
$orderBy .=
" " .
$queryOptions->order;
if ($queryOptions->getPage() &&
$queryOptions->rowLimit &&
$queryOptions->rowLimit >
0) {
.
(($queryOptions->getPage() -
1) *
$queryOptions->rowLimit) .
","
.
$queryOptions->rowLimit;
elseif ($queryOptions->rowLimit &&
$queryOptions->rowLimit >
0) {
$limit =
"LIMIT 0," .
$queryOptions->rowLimit;
return "SELECT ID, sectionID, articleID, created, modified FROM OnpubSAMaps $where $orderBy $limit";
* @param mixed $samaps A single {@link OnpubSAMap} object or an array of {@link OnpubSAMap} objects (to insert multiple maps at a time).
* @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.
* @throws PDOException if there's a database error.
public function insert($samaps)
$samaps =
array ($samaps);
$status =
$this->pdo->beginTransaction();
$stmt =
$this->pdo->prepare("INSERT INTO OnpubSAMaps (ID, sectionID, articleID, created, modified) VALUES (:ID, :sectionID, :articleID, :created, :modified)");
foreach ($samaps as $samap) {
$samapID =
$this->getID($samap);
catch
(PDOException $e) {
$sectionID =
$samap->sectionID;
$articleID =
$samap->articleID;
$created =
$samap->getCreated()->format('Y-m-d H:i:s');
$modified =
$samap->getModified()->format('Y-m-d H:i:s');
$stmt->bindParam(':ID', $ID);
$stmt->bindParam(':sectionID', $sectionID);
$stmt->bindParam(':articleID', $articleID);
$stmt->bindParam(':created', $created);
$stmt->bindParam(':modified', $modified);
$result =
$stmt->execute();
$samapIDs[] =
$this->pdo->lastInsertId();
$samap->ID =
$this->pdo->lastInsertId();
$status =
$this->pdo->commit();
* @param OnpubSAMap $samap The map to be updated.
* @return int 1 if the map was updated. 0 if the map does not exist in the database.
public function update(OnpubSAMap $samap)
$stmt =
$this->pdo->prepare("UPDATE OnpubSAMaps SET sectionID = :sectionID, articleID = :articleID, created = :created, modified = :modified WHERE ID = :ID");
$sectionID =
$samap->sectionID;
$articleID =
$samap->articleID;
$created =
$samap->getCreated()->format('Y-m-d H:i:s');
$modified =
$samap->getModified()->format('Y-m-d H:i:s');
$stmt->bindParam(':ID', $ID);
$stmt->bindParam(':sectionID', $sectionID);
$stmt->bindParam(':articleID', $articleID);
$stmt->bindParam(':created', $created);
$stmt->bindParam(':modified', $modified);
$result =
$stmt->execute();
return $stmt->rowCount();
Documentation generated on Fri, 08 Feb 2013 04:02:22 -0500 by phpDocumentor 1.4.4