/** * @param mixed $sections A single {@link OnpubSection} object or an array of {@link OnpubSection} objects (to insert multiple sections at a time). * @return mixed The ID(s) of the new section(s). An int will be returned if a single section was inserted. An array of ints will be returned if multiple sections were inserted. * @throws PDOException if there's a database error. */ public function insert($sections, $insertWSMaps = FALSE) { $oimages = new OnpubImages($this->pdo, FALSE); $owsmaps = new OnpubWSMaps($this->pdo, FALSE); $IDs = array(); $isArray = TRUE; $wsmaps = array(); if (!is_array($sections)) { $sections = array($sections); $isArray = FALSE; } if ($this->enableTransactions) { $status = $this->pdo->beginTransaction(); OnpubDatabase::verifyTransaction($this->pdo, $status); } $stmt = $this->pdo->prepare("INSERT INTO OnpubSections (ID, imageID, websiteID, parentID, name, url, created, modified) VALUES (:ID, :imageID, :websiteID, :parentID, :name, :url, :created, :modified)"); OnpubDatabase::verifyPrepare($this->pdo, $stmt, $this->enableTransactions); foreach ($sections as $section) { if ($section->image) { try { $imageID = $oimages->insert($section->image); $section->imageID = $imageID; } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } try { $ID = $this->getID($section); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } if ($ID) { $IDs[] = $ID; $section->ID = $ID; } else { $ID = $section->ID; $imageID = $section->imageID; $websiteID = $section->websiteID; $parentID = $section->parentID; $name = OnpubDatabase::utf8Decode(trim($section->name)); $url = OnpubDatabase::utf8Decode(trim($section->url)); $created = $section->getCreated()->format('Y-m-d H:i:s'); $modified = $section->getModified()->format('Y-m-d H:i:s'); $stmt->bindParam(':ID', $ID); $stmt->bindParam(':imageID', $imageID); $stmt->bindParam(':websiteID', $websiteID); $stmt->bindParam(':parentID', $parentID); $stmt->bindParam(':name', $name); $stmt->bindParam(':url', $url); $stmt->bindParam(':created', $created); $stmt->bindParam(':modified', $modified); $result = $stmt->execute(); OnpubDatabase::verifyExecute($this->pdo, $result, $this->enableTransactions, $stmt->errorInfo()); $IDs[] = $this->pdo->lastInsertId(); $section->ID = $this->pdo->lastInsertId(); } $wsmap = new OnpubWSMap(); $wsmap->websiteID = $section->websiteID; $wsmap->sectionID = $section->ID; $wsmap->setCreated($section->getCreated()); $wsmap->setModified($section->getModified()); $wsmaps[] = $wsmap; } if ($insertWSMaps) { try { $owsmaps->insert($wsmaps); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } if ($this->enableTransactions) { $status = $this->pdo->commit(); OnpubDatabase::verifyTransaction($this->pdo, $status); } if ($isArray) { return $IDs; } else { return end($IDs); } }
public function process() { $oimages = new OnpubImages($this->pdo); $owebsites = new OnpubWebsites($this->pdo); if ($this->overwrite !== NULL) { // An image overwrite is being requested. $image = new OnpubImage(); $image->websiteID = $this->websiteID; $image->fileName = $this->overwriteFileName; try { $this->imageID = $oimages->getID($image); } catch (PDOException $e) { throw $e; } try { $website = $owebsites->get($this->websiteID); } catch (PDOException $e) { throw $e; } if ($this->overwrite) { // Overwrite the existing image file with the temp one. if (rename(addTrailingSlash($website->imagesDirectory) . $this->overwriteFileName . ONPUBGUI_TMP_IMG_SUFFIX, addTrailingSlash($website->imagesDirectory) . $this->overwriteFileName)) { // Update the image's modified timestamp in the DB. try { $image = $oimages->get($this->imageID); } catch (PDOException $e) { throw $e; } try { $oimages->update($image); } catch (PDOException $e) { throw $e; } } } else { // Remove the temporary image file. unlink(addTrailingSlash($website->imagesDirectory) . $this->overwriteFileName . ONPUBGUI_TMP_IMG_SUFFIX); } return; } if (!ini_get("file_uploads")) { $message = "File uploads are disabled in the current PHP configuration."; throw new Exception($message, ONPUBGUI_ERROR_IMAGE_TYPE); } for ($i = 0; $i < sizeof($this->imageFiles['name']); $i++) { if ($this->imageFiles['name'][$i]) { if (!$this->isValidImage($this->imageFiles['name'][$i])) { $message = "<i>" . $this->imageFiles['name'][$i] . "</i> is an unsupported image file type."; throw new Exception($message, ONPUBGUI_ERROR_IMAGE_TYPE); } $image = new OnpubImage(); $image->websiteID = $this->websiteID; $image->fileName = $this->imageFiles['name'][$i]; try { $this->imageID = $oimages->getID($image); } catch (PDOException $e) { throw $e; } try { $website = $owebsites->get($image->websiteID); } catch (PDOException $e) { throw $e; } if ($this->imageID) { // Image exists, write the file to the images directory with a temp // name, and prompt the user to overwrite existing file. if (is_uploaded_file($this->imageFiles['tmp_name'][$i])) { if (@move_uploaded_file($this->imageFiles['tmp_name'][$i], addTrailingSlash($website->imagesDirectory) . $this->imageFiles['name'][$i] . ONPUBGUI_TMP_IMG_SUFFIX)) { throw new Exception($this->imageFiles['name'][$i], ONPUBGUI_ERROR_IMAGE_EXISTS); } else { $imagesDirectory = $website->imagesDirectory; $message = "Unable to move <i>" . $this->imageFiles['tmp_name'][$i] . "</i> to <i>" . addTrailingSlash($imagesDirectory) . $this->imageFiles['name'][$i] . ONPUBGUI_TMP_IMG_SUFFIX . "</i>."; throw new Exception($message, ONPUBGUI_ERROR_MOVE_UPLOADED_FILE); } } } else { // Image does not exsist, copy to images folder and insert in to DB. if (is_uploaded_file($this->imageFiles['tmp_name'][$i])) { if (!@move_uploaded_file($this->imageFiles['tmp_name'][$i], addTrailingSlash($website->imagesDirectory) . $this->imageFiles['name'][$i])) { $imagesDirectory = $website->imagesDirectory; $message = "Unable to move <i>" . $this->imageFiles['tmp_name'][$i] . "</i> to <i>" . addTrailingSlash($imagesDirectory) . $this->imageFiles['name'][$i] . "</i>."; throw new Exception($message, ONPUBGUI_ERROR_MOVE_UPLOADED_FILE); } try { $this->imageID = $oimages->insert($image); } catch (PDOException $e) { throw $e; } } else { $imagesDirectory = $website->imagesDirectory; $message = "<i>" . $this->imageFiles['name'][$i] . "</i> file size is larger than the current PHP configuration allows."; throw new Exception($message, ONPUBGUI_ERROR_FILE_SIZE); } } } } }
/** * 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(); }
/** * @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) { $oimages = new OnpubImages($this->pdo, FALSE); $IDs = array(); $isArray = TRUE; if (!is_array($authors)) { $authors = array($authors); $isArray = FALSE; } if ($this->enableTransactions) { $status = $this->pdo->beginTransaction(); OnpubDatabase::verifyTransaction($this->pdo, $status); } $stmt = $this->pdo->prepare("INSERT INTO OnpubAuthors (ID, imageID, givenNames, familyName, displayAs, url, created, modified) VALUES (:ID, :imageID, :givenNames, :familyName, :displayAs, :url, :created, :modified)"); OnpubDatabase::verifyPrepare($this->pdo, $stmt, $this->enableTransactions); foreach ($authors as $author) { if ($author->image) { try { $imageID = $oimages->insert($author->image); $author->imageID = $imageID; } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } try { $ID = $this->getID($author); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } if ($ID) { $IDs[] = $ID; $author->ID = $ID; } else { $ID = $author->ID; $imageID = $author->imageID; $givenNames = OnpubDatabase::utf8Decode(trim($author->givenNames)); $familyName = OnpubDatabase::utf8Decode(trim($author->familyName)); $displayAs = OnpubDatabase::utf8Decode(trim($author->displayAs)); $url = OnpubDatabase::utf8Decode(trim($author->url)); $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(); OnpubDatabase::verifyExecute($this->pdo, $result, $this->enableTransactions, $stmt->errorInfo()); $IDs[] = $this->pdo->lastInsertId(); $author->ID = $this->pdo->lastInsertId(); } } if ($this->enableTransactions) { $status = $this->pdo->commit(); OnpubDatabase::verifyTransaction($this->pdo, $status); } if ($isArray) { return $IDs; } else { return end($IDs); } }