/** * @param int $ID ID of the section to get. * @return OnpubSection An {@link OnpubSection} object. NULL if the section does not exist in the database. */ public function get($ID, OnpubQueryOptions $queryOptions = NULL) { if ($queryOptions === NULL) { $queryOptions = new OnpubQueryOptions(); } $query = $this->getQuery($ID, $queryOptions); $result = $this->pdo->query($query); OnpubDatabase::verifyQuery($this->pdo, $result, FALSE); if (!($rows = $result->fetchAll(PDO::FETCH_ASSOC))) { return NULL; } $row = $rows[0]; $section = new OnpubSection(); $section->ID = $row["ID"]; $section->imageID = $row["imageID"]; $section->websiteID = $row["websiteID"]; $section->parentID = $row["parentID"]; $section->name = $row["name"]; $section->url = $row["url"]; $section->setCreated(new DateTime($row["created"])); $section->setModified(new DateTime($row["modified"])); if (!$queryOptions->includeArticles) { return $section; } if ($row["imageID"]) { $image = new OnpubImage(); $image->ID = $row["imageID"]; $image->websiteID = $row["websiteID"]; $image->fileName = $row["fileName"]; $image->description = $row["description"]; $image->setCreated(new DateTime($row["imageCreated"])); $image->setModified(new DateTime($row["imageModified"])); $section->image = $image; } $articlesassoc = array(); foreach ($rows as $row) { $articleID = $row["articleID"]; if ($articleID) { if ($queryOptions->includeContent) { $content = $row["content"]; } else { $content = ""; } $article = new OnpubArticle(); $article->ID = $articleID; $article->imageID = $row["articleImageID"]; $article->title = $row["title"]; $article->content = $content; $article->url = $row["articleURL"]; $article->setCreated(new DateTime($row["articleCreated"])); $article->setModified(new DateTime($row["articleModified"])); if (isset($articlesassoc["{$articleID}"])) { $article = $articlesassoc["{$articleID}"]; } else { $articlesassoc["{$articleID}"] = $article; } if ($row["articleImageID"]) { $image = new OnpubImage(); $image->ID = $row["articleImageID"]; $image->websiteID = $row["articleImageWebsiteID"]; $image->fileName = $row["articleImageFileName"]; $image->description = $row["articleImageDescription"]; $image->url = $row["articleImageURL"]; $image->setCreated(new DateTime($row["articleImageCreated"])); $image->setModified(new DateTime($row["articleImageModified"])); $article->image = $image; } $articles = $section->articles; $exists = FALSE; foreach ($articles as $a) { if ($a->ID == $article->ID) { $exists = TRUE; } } if ($exists == FALSE) { $articles[] = $article; $section->articles = $articles; } } } $authorsassoc = array(); reset($rows); foreach ($rows as $row) { $authorID = $row["authorID"]; $articleID = $row["articleID"]; if ($authorID && $articleID) { $author = new OnpubAuthor(); $author->ID = $authorID; $author->imageID = $row["authorImageID"]; $author->givenNames = $row["givenNames"]; $author->familyName = $row["familyName"]; $author->displayAs = $row["displayAs"]; $author->setCreated(new DateTime($row["authorCreated"])); $author->setModified(new DateTime($row["authorModified"])); if (isset($authorsassoc["{$authorID}"])) { $author = $authorsassoc["{$authorID}"]; } else { $authorsassoc["{$authorID}"] = $author; } if ($row["authorImageID"]) { $image = new OnpubImage(); $image->ID = $row["authorImageID"]; $image->websiteID = $row["authorImageWebsiteID"]; $image->fileName = $row["authorImageFileName"]; $image->description = $row["authorImageDescription"]; $image->setCreated(new DateTime($row["authorImageCreated"])); $image->setModified(new DateTime($row["authorImageModified"])); $author->image = $image; } $article = $articlesassoc["{$articleID}"]; $authors = $article->authors; $exists = FALSE; foreach ($authors as $a) { if ($a->ID == $author->ID) { $exists = TRUE; } } if ($exists == FALSE) { $authors[] = $author; $article->authors = $authors; } } } $result->closeCursor(); return $section; }
/** * @param int $ID ID of the website to get. * @return OnpubWebsite An {@link OnpubWebsite} object. NULL if the website does not exist in the database. */ public function get($ID, OnpubQueryOptions $queryOptions = NULL, $flatArticleList = FALSE) { if ($queryOptions === NULL) { $queryOptions = new OnpubQueryOptions(); } $query = $this->getQuery($ID, $queryOptions); $result = $this->pdo->query($query); OnpubDatabase::verifyQuery($this->pdo, $result, FALSE); if (!($rows = $result->fetchAll(PDO::FETCH_ASSOC))) { return NULL; } $row = $rows[0]; $website = new OnpubWebsite(); $website->ID = $row["ID"]; $website->imageID = $row["imageID"]; $website->name = $row["name"]; $website->url = $row["url"]; $website->imagesURL = $row["imagesURL"]; $website->imagesDirectory = $row["imagesDirectory"]; $website->setCreated(new DateTime($row["created"])); $website->setModified(new DateTime($row["modified"])); if ($row["imageID"]) { $image = new OnpubImage(); $image->ID = $row["imageID"]; $image->websiteID = $row["ID"]; $image->fileName = $row["fileName"]; $image->description = $row["description"]; $image->setCreated(new DateTime($row["imageCreated"])); $image->setModified(new DateTime($row["imageModified"])); $website->image = $image; } if (!$queryOptions->includeSections) { return $website; } $osections = new OnpubSections($this->pdo); $sections = array(); $sectionsassoc = array(); foreach ($rows as $row) { $sectionID = $row["sectionID"]; $ID = $row["ID"]; if ($sectionID) { $section = new OnpubSection(); $section->ID = $sectionID; $section->imageID = $row["sectionImageID"]; $section->websiteID = $row["sectionWebsiteID"]; $section->parentID = $row["sectionParentID"]; $section->name = $row["sectionName"]; $section->url = $row["sectionURL"]; $section->setCreated(new DateTime($row["sectionCreated"])); $section->setModified(new DateTime($row["sectionModified"])); if (isset($sectionsassoc["{$sectionID}"])) { $section = $sectionsassoc["{$sectionID}"]; } else { $sectionsassoc["{$sectionID}"] = $section; } if ($row["sectionImageID"]) { $image = new OnpubImage(); $image->ID = $row["sectionImageID"]; $image->websiteID = $row["sectionImageWebsiteID"]; $image->fileName = $row["sectionImageFileName"]; $image->description = $row["sectionImageDescription"]; $image->setCreated(new DateTime($row["sectionImageCreated"])); $image->setModified(new DateTime($row["sectionImageModified"])); $section->image = $image; } $sections = $website->sections; $exists = FALSE; foreach ($sections as $s) { if ($s->ID == $section->ID) { $exists = TRUE; } } if ($exists == FALSE) { $sections[] = $section; $website->sections = $sections; } } } // An array to track the subsections indexes in the sections array. $subsections = array(); // Loop through all sections and link subsections to their parents and // vice-versa. for ($i = 0; $i < sizeof($sections); $i++) { $section = $sections[$i]; if ($section->parentID) { $parentID = $section->parentID; if (isset($sectionsassoc["{$parentID}"])) { $parentSection = $sectionsassoc["{$parentID}"]; $parentSection->sections[] = $section; $section->parent = $parentSection; } $subsections[] = $i; } } // Unset subsections from the original sections array now that they are // linked to their parent sections. foreach ($subsections as $subsection) { unset($sections[$subsection]); } // Array now might not be offset from 0 for sections. Re-index from 0. $website->sections = array_values($sections); if (!$queryOptions->includeArticles) { return $website; } $articlesassoc = array(); reset($rows); foreach ($rows as $row) { $articleID = $row["articleID"]; $sectionID = $row["sectionID"]; if ($articleID && $sectionID) { if ($queryOptions->includeContent) { $content = $row["content"]; } else { $content = ""; } $article = new OnpubArticle(); $article->ID = $articleID; $article->imageID = $row["articleImageID"]; $article->title = $row["title"]; $article->content = $content; $article->url = $row["articleURL"]; $article->setCreated(new DateTime($row["articleCreated"])); $article->setModified(new DateTime($row["articleModified"])); if (isset($articlesassoc["{$articleID}"])) { $article = $articlesassoc["{$articleID}"]; } else { $article->sectionIDs[] = $sectionID; $articlesassoc["{$articleID}"] = $article; } if ($row["articleImageID"]) { $image = new OnpubImage(); $image->ID = $row["articleImageID"]; $image->websiteID = $row["articleImageWebsiteID"]; $image->fileName = $row["articleImageFileName"]; $image->description = $row["articleImageDescription"]; $image->url = $row["articleImageURL"]; $image->setCreated(new DateTime($row["articleImageCreated"])); $image->setModified(new DateTime($row["articleImageModified"])); $article->image = $image; } $section = $sectionsassoc["{$sectionID}"]; $articles = $section->articles; $exists = FALSE; foreach ($articles as $a) { if ($a->ID == $article->ID) { $exists = TRUE; } } if ($exists == FALSE) { $articles[] = $article; $section->articles = $articles; } } } if ($flatArticleList) { $articles = array(); foreach ($articlesassoc as $article) { $articles[] = $article; } $website->articles = $articles; } $authorsassoc = array(); reset($rows); foreach ($rows as $row) { $authorID = $row["authorID"]; $articleID = $row["articleID"]; if ($authorID && $articleID) { $author = new OnpubAuthor(); $author->ID = $authorID; $author->imageID = $row["authorImageID"]; $author->givenNames = $row["givenNames"]; $author->familyName = $row["familyName"]; $author->displayAs = $row["displayAs"]; $author->setCreated(new DateTime($row["authorCreated"])); $author->setModified(new DateTime($row["authorModified"])); if (isset($authorsassoc["{$authorID}"])) { $author = $authorsassoc["{$authorID}"]; } else { $authorsassoc["{$authorID}"] = $author; } if ($row["authorImageID"]) { $image = new OnpubImage(); $image->ID = $row["authorImageID"]; $image->websiteID = $row["authorImageWebsiteID"]; $image->fileName = $row["authorImageFileName"]; $image->description = $row["authorImageDescription"]; $image->setCreated(new DateTime($row["authorImageCreated"])); $image->setModified(new DateTime($row["authorImageModified"])); $author->image = $image; } $article = $articlesassoc["{$articleID}"]; $authors = $article->authors; $exists = FALSE; foreach ($authors as $a) { if ($a->ID == $author->ID) { $exists = TRUE; } } if ($exists == FALSE) { $authors[] = $author; $article->authors = $authors; } } } $result->closeCursor(); return $website; }
/** * Update an article already in the database. * * If you set the article's sectionIDs to NULL, it will be unmapped from * any sections it was previously mapped to. * * @param OnpubArticle $article The article to be updated. * @param bool $overwriteAAMaps False by default. If set to TRUE, the * article-author maps for this article will be deleted and recreated, if * applicable. * @return int 1 if the article was updated. 0 if the article does not exist in the database. */ public function update(OnpubArticle $article, $overwriteAAMaps = FALSE) { $oaamaps = new OnpubAAMaps($this->pdo, FALSE); $oauthors = new OnpubAuthors($this->pdo, FALSE); $osamaps = new OnpubSAMaps($this->pdo, FALSE); $oimages = new OnpubImages($this->pdo, FALSE); $now = new DateTime(); if ($this->enableTransactions) { $status = $this->pdo->beginTransaction(); OnpubDatabase::verifyTransaction($this->pdo, $status); } $stmt = $this->pdo->prepare("UPDATE OnpubArticles SET imageID = :imageID, title = :title, content = :content, url = :url, created = :created, modified = :modified WHERE ID = :ID"); OnpubDatabase::verifyPrepare($this->pdo, $stmt, $this->enableTransactions); if ($article->image) { try { $imageID = $oimages->insert($article->image); $article->imageID = $imageID; } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } $ID = $article->ID; $imageID = $article->imageID; $title = OnpubDatabase::utf8Decode(trim($article->title)); $content = OnpubDatabase::utf8Decode(trim($article->content)); $url = OnpubDatabase::utf8Decode(trim($article->url)); $created = $article->getCreated()->format('Y-m-d H:i:s'); $modified = $now->format('Y-m-d H:i:s'); $stmt->bindParam(':ID', $ID); $stmt->bindParam(':imageID', $imageID); $stmt->bindParam(':title', $title); $stmt->bindParam(':content', $content); $stmt->bindParam(':url', $url); $stmt->bindParam(':created', $created); $stmt->bindParam(':modified', $modified); $result = $stmt->execute(); OnpubDatabase::verifyExecute($this->pdo, $result, $this->enableTransactions, $stmt->errorInfo()); if ($overwriteAAMaps) { try { $oaamaps->delete($article->ID, NULL); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } $authors = $article->authors; foreach ($authors as $author) { if ($author->ID) { try { $oauthors->update($author); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } else { try { $oauthors->insert($author); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } try { $aamap = new OnpubAAMap(); $aamap->articleID = $article->ID; $aamap->authorID = $author->ID; $oaamaps->insert($aamap); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } $sectionIDs = $article->sectionIDs; if ($sectionIDs === NULL) { try { $samaps = $osamaps->delete(NULL, $article->ID); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } elseif (sizeof($sectionIDs)) { $queryOptions = new OnpubQueryOptions(); $queryOptions->orderBy = "ID"; $queryOptions->order = "ASC"; try { $samaps = $osamaps->select($queryOptions, NULL, $article->ID); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } // Unmap sections not included in $sectionIDs. foreach ($samaps as $samap) { if (!in_array($samap->sectionID, $sectionIDs)) { try { $osamaps->delete($samap->sectionID, $article->ID); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } } foreach ($sectionIDs as $sectionID) { $samap = new OnpubSAMap(); $samap->sectionID = $sectionID; $samap->articleID = $article->ID; $samap->setCreated($article->getCreated()); $samap->setModified($article->getModified()); try { $samapID = $osamaps->getID($samap); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } if ($samapID) { $samap->ID = $samapID; try { $osamaps->update($samap); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } else { try { $osamaps->insert($samap); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } } } if ($this->enableTransactions) { $status = $this->pdo->commit(); OnpubDatabase::verifyTransaction($this->pdo, $status); } return $stmt->rowCount(); }
$displayAs = $_POST['displayAs']; } else { $displayAs = ""; } if (isset($_POST['imageID'])) { $imageID = $_POST['imageID']; if (!$imageID) { $imageID = NULL; } } else { $imageID = NULL; } $odate = new DateTime(); $odate->setDate($_POST['createdYear'], $_POST['createdMonth'], $_POST['createdDay']); $odate->setTime($_POST['createdHour'], $_POST['createdMinute'], $_POST['createdSecond']); $oarticle = new OnpubArticle(); $oarticle->ID = $_POST['articleID']; $oarticle->imageID = $imageID; $oarticle->title = $_POST['title']; $oarticle->content = $_POST['content']; $oarticle->url = $_POST['url']; $oarticle->setCreated($odate); $oarticle->sectionIDs = $sectionIDs; if ($displayAs !== "") { $oauthor = new OnpubAuthor(); if ($displayAs == $_POST['lastDisplayAs']) { $oauthor->ID = $authorID; } if ($_POST['authorImageID']) { $oauthor->imageID = $_POST['authorImageID']; }