Source for file OnpubAuthors.php
Documentation is available at OnpubAuthors.php
* Manage authors in an Onpub database.
* @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);
$query =
$this->countQuery();
$result =
$this->pdo->query($query);
if (!($row =
$result->fetch(PDO::FETCH_ASSOC))) {
private function countQuery()
return "SELECT COUNT(ID) AS count FROM OnpubAuthors";
* @param int $ID ID of the author to get.
* @return OnpubAuthor An {@link OnpubAuthor} object. NULL if the author does not exist in the database.
$query =
$this->getQuery($ID);
$result =
$this->pdo->query($query);
if (!($row =
$result->fetch(PDO::FETCH_ASSOC))) {
$author->ID =
$row["ID"];
$author->imageID =
$row["imageID"];
$author->givenNames =
$row["givenNames"];
$author->familyName =
$row["familyName"];
$author->displayAs =
$row["displayAs"];
$author->url =
$row["url"];
$author->setCreated(new DateTime($row["created"]));
$author->setModified(new DateTime($row["modified"]));
private function getQuery($ID)
return "SELECT * FROM OnpubAuthors WHERE ID = $ID";
* @param OnpubAuthor $author Author whose ID you want to get.
* @return int The ID of the author. NULL if the author does not exist in the database.
public function getID(OnpubAuthor $author)
$stmt =
$this->pdo->prepare("SELECT ID FROM OnpubAuthors WHERE displayAs = :displayAs");
$stmt->bindParam(':displayAs', $displayAs);
$result =
$stmt->execute();
if (($row =
$stmt->fetch(PDO::FETCH_ASSOC))) {
* @param OnpubQueryOptions $queryOptions Database query options.
* @return array An array of {@link OnpubAuthor} objects.
public function select(OnpubQueryOptions $queryOptions =
NULL)
if ($queryOptions ===
NULL)
$query =
$this->selectQuery($queryOptions);
$result =
$this->pdo->query($query);
$rows =
$result->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$author->ID =
$row["ID"];
$author->imageID =
$row["imageID"];
$author->givenNames =
$row["givenNames"];
$author->familyName =
$row["familyName"];
$author->displayAs =
$row["displayAs"];
$author->url =
$row["url"];
$author->setCreated(new DateTime($row["created"]));
$author->setModified(new DateTime($row["modified"]));
private function selectQuery(OnpubQueryOptions $queryOptions =
NULL)
if ($queryOptions ===
NULL)
if ($queryOptions->dateLimit) {
$where =
"WHERE created <= "
.
$this->pdo->quote($queryOptions->dateLimit->format('Y-m-d H:i:s'));
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, imageID, givenNames, familyName, displayAs, url, created, modified FROM OnpubAuthors $where $orderBy $limit";
* @param mixed $authors A single {@link OnpubAuthor} object or an array of {@link OnpubAuthor} objects (to insert multiple authors at a time).
* @return mixed The ID(s) of the new author(s). An int will be returned if a single author was inserted. An array of ints will be returned if multiple authors were inserted.
* @throws PDOException if there's a database error.
public function insert($authors)
$authors =
array ($authors);
$status =
$this->pdo->beginTransaction();
$stmt =
$this->pdo->prepare("INSERT INTO OnpubAuthors (ID, imageID, givenNames, familyName, displayAs, url, created, modified) VALUES (:ID, :imageID, :givenNames, :familyName, :displayAs, :url, :created, :modified)");
foreach ($authors as $author) {
$imageID =
$oimages->insert($author->image);
$author->imageID =
$imageID;
catch
(PDOException $e) {
$ID =
$this->getID($author);
catch
(PDOException $e) {
$imageID =
$author->imageID;
$created =
$author->getCreated()->format('Y-m-d H:i:s');
$modified =
$author->getModified()->format('Y-m-d H:i:s');
$stmt->bindParam(':ID', $ID);
$stmt->bindParam(':imageID', $imageID);
$stmt->bindParam(':givenNames', $givenNames);
$stmt->bindParam(':familyName', $familyName);
$stmt->bindParam(':displayAs', $displayAs);
$stmt->bindParam(':url', $url);
$stmt->bindParam(':created', $created);
$stmt->bindParam(':modified', $modified);
$result =
$stmt->execute();
$IDs[] =
$this->pdo->lastInsertId();
$author->ID =
$this->pdo->lastInsertId();
$status =
$this->pdo->commit();
* @param OnpubAuthor $author The author to be updated.
* @return int 1 if the author was updated. 0 if the author does not exist in the database.
public function update(OnpubAuthor $author)
$status =
$this->pdo->beginTransaction();
$stmt =
$this->pdo->prepare("UPDATE OnpubAuthors SET imageID = :imageID, givenNames = :givenNames, familyName = :familyName, displayAs = :displayAs, url = :url, modified = :modified WHERE ID = :ID");
$imageID =
$author->imageID;
$modified =
$now->format('Y-m-d H:i:s');
$stmt->bindParam(':ID', $ID);
$stmt->bindParam(':imageID', $imageID);
$stmt->bindParam(':givenNames', $givenNames);
$stmt->bindParam(':familyName', $familyName);
$stmt->bindParam(':displayAs', $displayAs);
$stmt->bindParam(':url', $url);
$stmt->bindParam(':modified', $modified);
$result =
$stmt->execute();
$status =
$this->pdo->commit();
return $stmt->rowCount();
Documentation generated on Fri, 08 Feb 2013 04:02:19 -0500 by phpDocumentor 1.4.4