/** * @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() { $osections = new OnpubSections($this->pdo); $owsmaps = new OnpubWSMaps($this->pdo); $wsmap = new OnpubWSMap(); $wsmap->websiteID = $this->osection->websiteID; $wsmap->sectionID = $this->osection->ID; try { $osections->update($this->osection); if ($this->visible) { $owsmaps->insert($wsmap); } else { $owsmaps->delete($this->osection->websiteID, $this->osection->ID); } } catch (PDOException $e) { throw $e; } }
/** * Check the status of the Onpub schema. * * @return mixed The version of the schema in the database as an int. An array * of PDOException objects will be returned if the schema is incomplete or * not installed. */ public function status() { $oaamaps = new OnpubAAMaps($this->pdo); $oarticles = new OnpubArticles($this->pdo); $oauthors = new OnpubAuthors($this->pdo); $oimages = new OnpubImages($this->pdo); $osamaps = new OnpubSAMaps($this->pdo); $osections = new OnpubSections($this->pdo); $owebsites = new OnpubWebsites($this->pdo); $owsmaps = new OnpubWSMaps($this->pdo); $queryOptions = new OnpubQueryOptions($this->pdo); $queryOptions->setPage(1, 1); $exceptions = array(); $version = 0; try { $oaamaps->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $oarticles->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $oauthors->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $oimages->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $osamaps->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $osections->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $owebsites->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $owsmaps->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } if (sizeof($exceptions)) { return $exceptions; } $version = 1; return $version; }
/** * @param OnpubWebsite $website The website to be updated. * @return int 1 if the website was updated. 0 if the website does not exist in the database. */ public function update(OnpubWebsite $website) { $owsmaps = new OnpubWSMaps($this->pdo, FALSE); $now = new DateTime(); if ($this->enableTransactions) { $status = $this->pdo->beginTransaction(); OnpubDatabase::verifyTransaction($this->pdo, $status); } $stmt = $this->pdo->prepare("UPDATE OnpubWebsites SET imageID = :imageID, name = :name, url = :url, imagesURL = :imagesURL, imagesDirectory = :imagesDirectory, modified = :modified WHERE ID = :ID"); OnpubDatabase::verifyPrepare($this->pdo, $stmt, $this->enableTransactions); $ID = $website->ID; $imageID = $website->imageID; $name = OnpubDatabase::utf8Decode(trim($website->name)); $url = OnpubDatabase::utf8Decode(trim($website->url)); $imagesURL = OnpubDatabase::utf8Decode(trim($website->imagesURL)); $imagesDirectory = OnpubDatabase::utf8Decode(trim($website->imagesDirectory)); $modified = $now->format('Y-m-d H:i:s'); $stmt->bindParam(':ID', $ID); $stmt->bindParam(':imageID', $imageID); $stmt->bindParam(':name', $name); $stmt->bindParam(':url', $url); $stmt->bindParam(':imagesURL', $imagesURL); $stmt->bindParam(':imagesDirectory', $imagesDirectory); $stmt->bindParam(':modified', $modified); $result = $stmt->execute(); OnpubDatabase::verifyExecute($this->pdo, $result, $this->enableTransactions, $stmt->errorInfo()); try { $owsmaps->delete($website->ID, NULL); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } $sections = $website->sections; $wsmaps = array(); foreach ($sections as $section) { $wsmap = new OnpubWSMap(); $wsmap->websiteID = $section->websiteID; $wsmap->sectionID = $section->ID; $wsmaps[] = $wsmap; } 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); } return $stmt->rowCount(); }