/** * @param OnpubSection $section The section to be updated. * @return int 1 if the section was updated. 0 if the section was not updated or does not exist. */ public function update(OnpubSection $section) { $oarticles = new OnpubArticles($this->pdo, FALSE); $osamaps = new OnpubSAMaps($this->pdo, FALSE); $now = new DateTime(); $inTransaction = FALSE; if ($this->enableTransactions) { $status = $this->pdo->beginTransaction(); OnpubDatabase::verifyTransaction($this->pdo, $status); $inTransaction = TRUE; } if ($section->ID == $section->parentID) { $section->parentID = NULL; } if ($section->parentID) { $this->enableTransactions = FALSE; $parentID = $section->parentID; while ($parentID) { try { $parent = $this->get($parentID); } catch (PDOException $e) { if ($inTransaction) { $this->pdo->rollBack(); } throw $e; } if (!$parent) { $section->parentID = NULL; break; } if ($section->ID == $parent->parentID) { $section->parentID = NULL; break; } $parentID = $parent->parentID; } $this->enableTransactions = TRUE; } $stmt = $this->pdo->prepare("UPDATE OnpubSections SET imageID = :imageID, parentID = :parentID, name = :name, url = :url, modified = :modified WHERE ID = :ID"); OnpubDatabase::verifyPrepare($this->pdo, $stmt, $this->enableTransactions); $ID = $section->ID; $imageID = $section->imageID; $parentID = $section->parentID; $name = OnpubDatabase::utf8Decode(trim($section->name)); $url = OnpubDatabase::utf8Decode(trim($section->url)); $modified = $now->format('Y-m-d H:i:s'); $stmt->bindParam(':ID', $ID); $stmt->bindParam(':imageID', $imageID); $stmt->bindParam(':parentID', $parentID); $stmt->bindParam(':name', $name); $stmt->bindParam(':url', $url); $stmt->bindParam(':modified', $modified); $result = $stmt->execute(); OnpubDatabase::verifyExecute($this->pdo, $result, $this->enableTransactions, $stmt->errorInfo()); try { $osamaps->delete($section->ID, NULL); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } $articles = $section->articles; $samaps = array(); foreach ($articles as $article) { if ($article->ID) { try { $article = $oarticles->get($article->ID, new OnpubQueryOptions()); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } $samap = new OnpubSAMap(); $samap->sectionID = $section->ID; $samap->articleID = $article->ID; $samap->setCreated($article->getCreated()); $samap->setModified($article->getModified()); $samaps[] = $samap; } else { try { $articleID = $oarticles->insert($article); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } $samap = new OnpubSAMap(); $samap->sectionID = $section->ID; $samap->articleID = $articleID; $samaps[] = $samap; } } try { $osamaps->insert($samaps); } 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(); }
public function process() { $oarticles = new OnpubArticles($this->pdo); if ($this->oauthor->displayAs) { $authors = array($this->oauthor); $this->oarticle->authors = $authors; } try { $oarticles->insert($this->oarticle); } catch (PDOException $e) { throw $e; } }