Source for file OnpubSections.php

Documentation is available at OnpubSections.php

  1. <?php
  2.  
  3. /**
  4.  * Manage sections in an Onpub database.
  5.  *
  6.  * @author {@link mailto:corey@onpub.com Corey H.M. Taylor}
  7.  * @copyright Onpub (TM). Copyright 2012, Onpub.com.
  8.  *  {@link http://onpub.com/}
  9.  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  10.  *  Version 2
  11.  * @package OnpubAPI
  12.  */
  13. {
  14.   private $pdo;
  15.   /**
  16.    * @var bool 
  17.    */
  18.   public $enableTransactions;
  19.  
  20.   /**
  21.    * Connect to an Onpub database.
  22.    *
  23.    * All the methods in this class which query the database use the database
  24.    * connection provided by the PDO object required by this constructor.
  25.    * Currently, Onpub only supports MySQL as a database for storing content.
  26.    * Therefore, when constructing the PDO object, only the
  27.    * {@link PHP_MANUAL#ref.pdo-mysql PDO_MYSQL} driver is supported
  28.    * as a PDO {@link PHP_MANUAL#ref.pdo-mysql.connection data source}.
  29.    *
  30.    * All the methods in this class require the
  31.    * {@link http://onpub.com/pdfs/onpub_schema.pdf Onpub schema}
  32.    * to be installed in the PDO-connected database. The {@link OnpubDatabase}
  33.    * class contains methods to manage the Onpub schema. The Onpub
  34.    * schema can also be installed by clicking the "Install the schema" link
  35.    * once logged in to the Onpub web interface. The schema generally only has
  36.    * to be installed once per database.
  37.    *
  38.    * @param PDO $pdo {@link PHP_MANUAL#function.pdo-construct PHP Data Object}.
  39.    * @param bool $enableTransactions 
  40.    */
  41.   function __construct(PDO $pdo$enableTransactions TRUE)
  42.   {
  43.     $this->pdo $pdo;
  44.     $this->enableTransactions = $enableTransactions;
  45.     $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARESTRUE);
  46.   }
  47.  
  48.   /**
  49.    * @return int 
  50.    */
  51.   public function count()
  52.   {
  53.     $query $this->countQuery();
  54.     $result $this->pdo->query($query);
  55.     OnpubDatabase::verifyQuery($this->pdo$resultFALSE);
  56.  
  57.     if (!($row $result->fetch(PDO::FETCH_ASSOC))) {
  58.       return 0;
  59.     }
  60.  
  61.     $result->closeCursor();
  62.     return $row["count"];
  63.   }
  64.  
  65.   private function countQuery()
  66.   {
  67.     return "SELECT COUNT(ID) AS count FROM OnpubSections";
  68.   }
  69.  
  70.   /**
  71.    * @param int $ID ID of the section to delete.
  72.    * @return int 1 if the section was deleted, 0 if the section does not exist in the database.
  73.    */
  74.   public function delete($ID)
  75.   {
  76.     $osamaps new OnpubSAMaps($this->pdoFALSE);
  77.     $owsmaps new OnpubWSMaps($this->pdoFALSE);
  78.  
  79.     if ($this->enableTransactions{
  80.       $status $this->pdo->beginTransaction();
  81.       OnpubDatabase::verifyTransaction($this->pdo$status);
  82.     }
  83.  
  84.     try {
  85.       $osamaps->delete($IDNULL);
  86.     }
  87.     catch (PDOException $e{
  88.       if ($this->enableTransactions)
  89.         $this->pdo->rollBack();
  90.  
  91.       throw $e;
  92.     }
  93.  
  94.     try {
  95.       $owsmaps->delete(NULL$ID);
  96.     }
  97.     catch (PDOException $e{
  98.       if ($this->enableTransactions)
  99.         $this->pdo->rollBack();
  100.  
  101.       throw $e;
  102.     }
  103.  
  104.     $stmt $this->pdo->prepare("DELETE FROM OnpubSections WHERE ID = :ID");
  105.     OnpubDatabase::verifyPrepare($this->pdo$stmt$this->enableTransactions);
  106.  
  107.     $stmt->bindParam(':ID'$ID);
  108.  
  109.     $result $stmt->execute();
  110.     OnpubDatabase::verifyExecute($this->pdo$result$this->enableTransactions$stmt->errorInfo());
  111.  
  112.     if ($this->enableTransactions{
  113.       $status $this->pdo->commit();
  114.       OnpubDatabase::verifyTransaction($this->pdo$status);
  115.     }
  116.  
  117.     return $stmt->rowCount();
  118.   }
  119.  
  120.   /**
  121.    * @param string $keywords Keyword(s) to search for.
  122.    * @return array All the sections which were found as an array of {@link OnpubSection} objects.
  123.    */
  124.   public function search($keywordsOnpubQueryOptions $queryOptions NULL)
  125.   {
  126.     if ($queryOptions === NULL)
  127.       $queryOptions new OnpubQueryOptions();
  128.  
  129.     $query $this->searchQuery($keywords$queryOptions);
  130.     $result $this->pdo->query($query);
  131.     OnpubDatabase::verifyQuery($this->pdo$resultFALSE);
  132.  
  133.     $rows $result->fetchAll(PDO::FETCH_ASSOC);
  134.  
  135.     $sections array();
  136.  
  137.     if ($rows{
  138.       foreach ($rows as $row{
  139.         $section new OnpubSection();
  140.  
  141.         $section->ID $row["ID"];
  142.         $section->imageID $row["imageID"];
  143.         $section->websiteID $row["websiteID"];
  144.         $section->parentID $row["parentID"];
  145.         $section->name $row["name"];
  146.         $section->url $row["url"];
  147.         $section->setCreated(new DateTime($row["created"]));
  148.         $section->setModified(new DateTime($row["modified"]));
  149.  
  150.         $sections[$section;
  151.       }
  152.     }
  153.  
  154.     $result->closeCursor();
  155.     return $sections;
  156.   }
  157.  
  158.   private function searchQuery($keywordsOnpubQueryOptions $queryOptions NULL)
  159.   {
  160.     if ($queryOptions === NULL)
  161.       $queryOptions new OnpubQueryOptions();
  162.  
  163.     $keywords $this->pdo->quote(OnpubDatabase::utf8Decode(trim($keywords)));
  164.  
  165.     if ($queryOptions->orderBy{
  166.       $orderBy $queryOptions->orderBy;
  167.     }
  168.     else {
  169.       $orderBy "";
  170.     }
  171.  
  172.     if ($queryOptions->order{
  173.       $order $queryOptions->order;
  174.     }
  175.     else {
  176.       $order "";
  177.     }
  178.  
  179.     if ($orderBy{
  180.       if ($order{
  181.         return "SELECT ID, imageID, websiteID, parentID, name, url, created, modified FROM OnpubSections WHERE ID RLIKE $keywords OR name RLIKE $keywords OR modified RLIKE $keywords AND parentID IS NULL ORDER BY $orderBy $order";
  182.       }
  183.       else {
  184.         return "SELECT ID, imageID, websiteID, parentID, name, url, created, modified FROM OnpubSections WHERE ID RLIKE $keywords OR name RLIKE $keywords OR modified RLIKE $keywords AND parentID IS NULL ORDER BY $orderBy";
  185.       }
  186.     }
  187.     else {
  188.       return "SELECT ID, imageID, websiteID, parentID, name, url, created, modified FROM OnpubSections WHERE ID RLIKE $keywords OR name RLIKE $keywords OR modified RLIKE $keywords AND parentID IS NULL";
  189.     }
  190.   }
  191.  
  192.   /**
  193.    * @param OnpubQueryOptions $queryOptions Database query options.
  194.    * @return array An array of {@link OnpubSection} objects.
  195.    */
  196.   public function select(OnpubQueryOptions $queryOptions NULL$websiteID NULL$flatSectionList TRUE$parentID NULL)
  197.   {
  198.     if ($queryOptions === NULL)
  199.       $queryOptions new OnpubQueryOptions();
  200.  
  201.     if ($this->enableTransactions{
  202.       $status $this->pdo->beginTransaction();
  203.       OnpubDatabase::verifyTransaction($this->pdo$status);
  204.     }
  205.  
  206.     $query $this->selectQuery($queryOptions$websiteID$parentID);
  207.     $result $this->pdo->query($query);
  208.     OnpubDatabase::verifyQuery($this->pdo$result$this->enableTransactions);
  209.     $rows $result->fetchAll(PDO::FETCH_ASSOC);
  210.  
  211.     $sections array();
  212.  
  213.     if ($rows{
  214.       foreach ($rows as $row{
  215.         $section new OnpubSection();
  216.  
  217.         $section->ID $row["ID"];
  218.         $section->imageID $row["imageID"];
  219.         $section->websiteID $row["websiteID"];
  220.         $section->parentID $row["parentID"];
  221.         $section->name $row["name"];
  222.         $section->url $row["url"];
  223.         $section->setCreated(new DateTime($row["created"]));
  224.         $section->setModified(new DateTime($row["modified"]));
  225.  
  226.         $sections[$section;
  227.       }
  228.     }
  229.  
  230.     if ($parentID{
  231.       if ($this->enableTransactions{
  232.         $status $this->pdo->commit();
  233.         OnpubDatabase::verifyTransaction($this->pdo$status);
  234.       }
  235.  
  236.       $result->closeCursor();
  237.       return $sections;
  238.     }
  239.  
  240.     $sectionsassoc array();
  241.  
  242.     for ($i 0$i sizeof($sections)$i++{
  243.       $sectionsassoc[$sections[$i]->ID$sections[$i];
  244.     }
  245.  
  246.     for ($i 0$i sizeof($sections)$i++{
  247.       $section $sections[$i];
  248.       $parent NULL;
  249.  
  250.       while ($section->parentID{
  251.         if (isset($sectionsassoc[$section->parentID])) {
  252.           $parent $sectionsassoc[$section->parentID];
  253.         }
  254.         else {
  255.           // Here's where we have to lookup section from DB.
  256.           $query $this->getQuery($section->parentID);
  257.           $result $this->pdo->query($query);
  258.           OnpubDatabase::verifyQuery($this->pdo$result$this->enableTransactions);
  259.  
  260.           $row $result->fetch(PDO::FETCH_ASSOC);
  261.  
  262.           $parent new OnpubSection();
  263.  
  264.           if ($row{
  265.             $parent->ID $row["ID"];
  266.             $parent->imageID $row["imageID"];
  267.             $parent->websiteID $row["websiteID"];
  268.             $parent->parentID $row["parentID"];
  269.             $parent->name $row["name"];
  270.             $parent->url $row["url"];
  271.             $parent->setCreated(new DateTime($row["created"]));
  272.             $parent->setModified(new DateTime($row["modified"]));
  273.  
  274.             $result->closeCursor();
  275.           }
  276.         }
  277.  
  278.         $section->parent $parent;
  279.  
  280.         $found FALSE;
  281.  
  282.         foreach ($parent->sections as $s{
  283.           if ($s->ID == $section->ID{
  284.             $found TRUE;
  285.           }
  286.         }
  287.  
  288.         if (!$found{
  289.           $parent->sections[$section;
  290.         }
  291.  
  292.         $section $parent;
  293.       }
  294.     }
  295.  
  296.     $sections array();
  297.  
  298.     foreach ($sectionsassoc as $s{
  299.       if ($flatSectionList{
  300.         $sections[$s;
  301.       }
  302.       else {
  303.         if (!$s->parentID{
  304.           $sections[$s;
  305.         }
  306.       }
  307.     }
  308.  
  309.     if ($this->enableTransactions{
  310.       $status $this->pdo->commit();
  311.       OnpubDatabase::verifyTransaction($this->pdo$status);
  312.     }
  313.  
  314.     $result->closeCursor();
  315.     return $sections;
  316.   }
  317.  
  318.   private function selectQuery(OnpubQueryOptions $queryOptions NULL$websiteID$parentID)
  319.   {
  320.     if ($queryOptions === NULL)
  321.       $queryOptions new OnpubQueryOptions();
  322.  
  323.     $where "";
  324.     $orderBy "";
  325.     $limit "";
  326.  
  327.     if ($queryOptions->dateLimit{
  328.       $where "WHERE created <= "
  329.         . $this->pdo->quote($queryOptions->dateLimit->format('Y-m-d H:i:s'));
  330.  
  331.       if ($websiteID{
  332.         $where .= " AND websiteID = $websiteID";
  333.       }
  334.       elseif ($parentID{
  335.         $where .= " AND parentID = $parentID";
  336.       }
  337.     }
  338.     else {
  339.       if ($websiteID{
  340.         $where "WHERE websiteID = $websiteID";
  341.       }
  342.       elseif ($parentID{
  343.         $where "WHERE parentID = $parentID";
  344.       }
  345.     }
  346.  
  347.     if ($queryOptions->orderBy{
  348.       $orderBy "ORDER BY " $queryOptions->orderBy;
  349.  
  350.       if ($queryOptions->order{
  351.         $orderBy .= " " $queryOptions->order;
  352.       }
  353.     }
  354.  
  355.     if ($queryOptions->getPage(&& $queryOptions->rowLimit && $queryOptions->rowLimit 0{
  356.       $limit "LIMIT "
  357.         . (($queryOptions->getPage(1$queryOptions->rowLimit","
  358.         . $queryOptions->rowLimit;
  359.     }
  360.     elseif ($queryOptions->rowLimit && $queryOptions->rowLimit 0{
  361.       $limit "LIMIT 0," $queryOptions->rowLimit;
  362.     }
  363.  
  364.     return "SELECT ID, imageID, websiteID, parentID, name, url, created, modified FROM OnpubSections $where $orderBy $limit";
  365.   }
  366.  
  367.   /**
  368.    * @param int $ID ID of the section to get.
  369.    * @return OnpubSection An {@link OnpubSection} object. NULL if the section does not exist in the database.
  370.    */
  371.   public function get($IDOnpubQueryOptions $queryOptions NULL)
  372.   {
  373.     if ($queryOptions === NULL)
  374.       $queryOptions new OnpubQueryOptions();
  375.  
  376.     $query $this->getQuery($ID$queryOptions);
  377.     $result $this->pdo->query($query);
  378.     OnpubDatabase::verifyQuery($this->pdo$resultFALSE);
  379.  
  380.     if (!($rows $result->fetchAll(PDO::FETCH_ASSOC))) {
  381.       return NULL;
  382.     }
  383.  
  384.     $row $rows[0];
  385.  
  386.     $section new OnpubSection();
  387.  
  388.     $section->ID $row["ID"];
  389.     $section->imageID $row["imageID"];
  390.     $section->websiteID $row["websiteID"];
  391.     $section->parentID $row["parentID"];
  392.     $section->name $row["name"];
  393.     $section->url $row["url"];
  394.     $section->setCreated(new DateTime($row["created"]));
  395.     $section->setModified(new DateTime($row["modified"]));
  396.  
  397.     if (!$queryOptions->includeArticles{
  398.       return $section;
  399.     }
  400.  
  401.     if ($row["imageID"]{
  402.       $image new OnpubImage();
  403.  
  404.       $image->ID $row["imageID"];
  405.       $image->websiteID $row["websiteID"];
  406.       $image->fileName $row["fileName"];
  407.       $image->description $row["description"];
  408.       $image->setCreated(new DateTime($row["imageCreated"]));
  409.       $image->setModified(new DateTime($row["imageModified"]));
  410.  
  411.       $section->image $image;
  412.     }
  413.  
  414.     $articlesassoc array();
  415.  
  416.     foreach ($rows as $row{
  417.       $articleID $row["articleID"];
  418.  
  419.       if ($articleID{
  420.         if ($queryOptions->includeContent{
  421.           $content $row["content"];
  422.         }
  423.         else {
  424.           $content "";
  425.         }
  426.  
  427.         $article new OnpubArticle();
  428.  
  429.         $article->ID $articleID;
  430.         $article->imageID $row["articleImageID"];
  431.         $article->title $row["title"];
  432.         $article->content $content;
  433.         $article->url $row["articleURL"];
  434.         $article->setCreated(new DateTime($row["articleCreated"]));
  435.         $article->setModified(new DateTime($row["articleModified"]));
  436.  
  437.         if (isset($articlesassoc["$articleID"])) {
  438.           $article $articlesassoc["$articleID"];
  439.         }
  440.         else {
  441.           $articlesassoc["$articleID"$article;
  442.         }
  443.  
  444.         if ($row["articleImageID"]{
  445.           $image new OnpubImage();
  446.  
  447.           $image->ID $row["articleImageID"];
  448.           $image->websiteID $row["articleImageWebsiteID"];
  449.           $image->fileName $row["articleImageFileName"];
  450.           $image->description $row["articleImageDescription"];
  451.           $image->url $row["articleImageURL"];
  452.           $image->setCreated(new DateTime($row["articleImageCreated"]));
  453.           $image->setModified(new DateTime($row["articleImageModified"]));
  454.  
  455.           $article->image $image;
  456.         }
  457.  
  458.         $articles $section->articles;
  459.         $exists FALSE;
  460.  
  461.         foreach ($articles as $a{
  462.           if ($a->ID == $article->ID{
  463.             $exists TRUE;
  464.           }
  465.         }
  466.  
  467.         if ($exists == FALSE{
  468.           $articles[$article;
  469.           $section->articles $articles;
  470.         }
  471.       }
  472.     }
  473.  
  474.     $authorsassoc array();
  475.  
  476.     reset ($rows);
  477.  
  478.     foreach ($rows as $row{
  479.       $authorID $row["authorID"];
  480.       $articleID $row["articleID"];
  481.  
  482.       if ($authorID && $articleID{
  483.         $author new OnpubAuthor();
  484.  
  485.         $author->ID $authorID;
  486.         $author->imageID $row["authorImageID"];
  487.         $author->givenNames $row["givenNames"];
  488.         $author->familyName $row["familyName"];
  489.         $author->displayAs $row["displayAs"];
  490.         $author->setCreated(new DateTime($row["authorCreated"]));
  491.         $author->setModified(new DateTime($row["authorModified"]));
  492.  
  493.         if (isset($authorsassoc["$authorID"])) {
  494.           $author $authorsassoc["$authorID"];
  495.         }
  496.         else {
  497.           $authorsassoc["$authorID"$author;
  498.         }
  499.  
  500.         if ($row["authorImageID"]{
  501.           $image new OnpubImage();
  502.  
  503.           $image->ID $row["authorImageID"];
  504.           $image->websiteID $row["authorImageWebsiteID"];
  505.           $image->fileName $row["authorImageFileName"];
  506.           $image->description $row["authorImageDescription"];
  507.           $image->setCreated(new DateTime($row["authorImageCreated"]));
  508.           $image->setModified(new DateTime($row["authorImageModified"]));
  509.  
  510.           $author->image $image;
  511.         }
  512.  
  513.         $article $articlesassoc["$articleID"];
  514.         $authors $article->authors;
  515.         $exists FALSE;
  516.  
  517.         foreach ($authors as $a{
  518.           if ($a->ID == $author->ID{
  519.             $exists TRUE;
  520.           }
  521.         }
  522.  
  523.         if ($exists == FALSE{
  524.           $authors[$author;
  525.           $article->authors $authors;
  526.         }
  527.       }
  528.     }
  529.  
  530.     $result->closeCursor();
  531.     return $section;
  532.   }
  533.  
  534.   private function getQuery($IDOnpubQueryOptions $queryOptions NULL)
  535.   {
  536.     if ($queryOptions === NULL)
  537.       $queryOptions new OnpubQueryOptions();
  538.  
  539.     if ($queryOptions->orderBy{
  540.       $orderBy $queryOptions->orderBy;
  541.       $orderBy "articles.$orderBy";
  542.     }
  543.     else {
  544.       $orderBy "samaps.ID";
  545.     }
  546.  
  547.     if ($queryOptions->order{
  548.       $order $queryOptions->order;
  549.     }
  550.     else {
  551.       $order "";
  552.     }
  553.  
  554.     $where "WHERE sections.ID = $ID";
  555.  
  556.     if ($queryOptions->dateLimit{
  557.       $dateLimit $queryOptions->dateLimit->format('Y-m-d H:i:s');
  558.       $where .= " AND articles.created <= '$dateLimit'";
  559.     }
  560.  
  561.     if ($queryOptions->includeContent{
  562.       $articleColumns "articles.ID AS articleID, articles.imageID AS articleImageID, title, content, articles.url AS articleURL, articles.created AS articleCreated, articles.modified AS articleModified";
  563.     }
  564.     else {
  565.       $articleColumns "articles.ID AS articleID, articles.imageID AS articleImageID, title, articles.url AS articleURL, articles.created AS articleCreated, articles.modified AS articleModified";
  566.     }
  567.  
  568.     if ($queryOptions->includeArticles{
  569.       return
  570.         "SELECT sections.ID, sections.imageID, sections.websiteID, sections.parentID, sections.name, sections.url, sections.url, sections.created, sections.modified, sectionimages.fileName, sectionimages.description, sectionimages.created AS imageCreated, sectionimages.modified AS imageModified, $articleColumns, articleimages.websiteID AS articleImageWebsiteID, articleimages.fileName AS articleImageFileName, articleimages.description AS articleImageDescription, articleimages.url AS articleImageURL, articleimages.created AS articleImageCreated, articleimages.modified AS articleImageModified, authors.ID AS authorID, authors.imageID AS authorImageID, authors.givenNames, authors.familyName, authors.displayAs, authors.created AS authorCreated, authors.modified AS authorModified, authorimages.websiteID AS authorImageWebsiteID, authorimages.fileName AS authorImageFileName, authorimages.description AS authorImageDescription, authorimages.url AS authorImageURL, authorimages.created AS authorImageCreated, authorimages.modified AS authorImageModified FROM OnpubSections AS sections LEFT JOIN OnpubImages AS sectionimages ON sections.imageID = sectionimages.ID LEFT JOIN OnpubSAMaps AS samaps ON sections.ID = samaps.sectionID LEFT JOIN OnpubArticles AS articles ON samaps.articleID = articles.ID LEFT JOIN OnpubImages AS articleimages ON articles.imageID = articleimages.ID LEFT JOIN OnpubAAMaps AS aamaps ON articles.ID = aamaps.articleID LEFT JOIN OnpubAuthors AS authors ON aamaps.authorID = authors.ID LEFT JOIN OnpubImages AS authorimages ON authors.imageID = authorimages.ID $where ORDER BY $orderBy $order";
  571.     }
  572.     else {
  573.       return "SELECT ID, imageID, websiteID, parentID, name, url, created, modified FROM OnpubSections WHERE ID = $ID";
  574.     }
  575.   }
  576.  
  577.   /**
  578.    * @param OnpubSection $section Section whose ID you want to get.
  579.    * @return int The ID of the section. NULL if the section does not exist in the database.
  580.    */
  581.   public function getID(OnpubSection $section)
  582.   {
  583.     if ($section->parentID{
  584.       $stmt $this->pdo->prepare("SELECT ID FROM OnpubSections WHERE websiteID = :websiteID AND parentID = :parentID AND name = :name");
  585.     }
  586.     else {
  587.       $stmt $this->pdo->prepare("SELECT ID FROM OnpubSections WHERE websiteID = :websiteID AND parentID IS NULL AND name = :name");
  588.     }
  589.  
  590.     OnpubDatabase::verifyPrepare($this->pdo$stmtFALSE);
  591.  
  592.     $websiteID $section->websiteID;
  593.     $parentID $section->parentID;
  594.     $name OnpubDatabase::utf8Decode(trim($section->name));
  595.  
  596.     $stmt->bindParam(':websiteID'$websiteID);
  597.  
  598.     if ($parentID{
  599.       $stmt->bindParam(':parentID'$parentID);
  600.     }
  601.  
  602.     $stmt->bindParam(':name'$name);
  603.     $result $stmt->execute();
  604.     OnpubDatabase::verifyExecute($this->pdo$resultFALSE$stmt->errorInfo());
  605.  
  606.     if (($row $stmt->fetch(PDO::FETCH_ASSOC))) {
  607.       return $row["ID"];
  608.     }
  609.  
  610.     return NULL;
  611.   }
  612.  
  613.   /**
  614.    * @param mixed $sections A single {@link OnpubSection} object or an array of {@link OnpubSection} objects (to insert multiple sections at a time).
  615.    * @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.
  616.    * @throws PDOException if there's a database error.
  617.    */
  618.   public function insert($sections$insertWSMaps FALSE)
  619.   {
  620.     $oimages new OnpubImages($this->pdoFALSE);
  621.     $owsmaps new OnpubWSMaps($this->pdoFALSE);
  622.     $IDs array();
  623.     $isArray TRUE;
  624.     $wsmaps array();
  625.  
  626.     if (!is_array($sections)) {
  627.       $sections array ($sections);
  628.       $isArray FALSE;
  629.     }
  630.  
  631.     if ($this->enableTransactions{
  632.       $status $this->pdo->beginTransaction();
  633.       OnpubDatabase::verifyTransaction($this->pdo$status);
  634.     }
  635.  
  636.     $stmt $this->pdo->prepare("INSERT INTO OnpubSections (ID, imageID, websiteID, parentID, name, url, created, modified) VALUES (:ID, :imageID, :websiteID, :parentID, :name, :url, :created, :modified)");
  637.     OnpubDatabase::verifyPrepare($this->pdo$stmt$this->enableTransactions);
  638.  
  639.     foreach ($sections as $section{
  640.       if ($section->image{
  641.         try {
  642.           $imageID $oimages->insert($section->image);
  643.           $section->imageID $imageID;
  644.         }
  645.         catch (PDOException $e{
  646.           if ($this->enableTransactions)
  647.             $this->pdo->rollBack();
  648.  
  649.           throw $e;
  650.         }
  651.       }
  652.  
  653.       try {
  654.         $ID $this->getID($section);
  655.       }
  656.       catch (PDOException $e{
  657.         if ($this->enableTransactions)
  658.           $this->pdo->rollBack();
  659.  
  660.         throw $e;
  661.       }
  662.  
  663.       if ($ID{
  664.         $IDs[$ID;
  665.         $section->ID $ID;
  666.       }
  667.       else {
  668.         $ID $section->ID;
  669.         $imageID $section->imageID;
  670.         $websiteID $section->websiteID;
  671.         $parentID $section->parentID;
  672.         $name OnpubDatabase::utf8Decode(trim($section->name));
  673.         $url OnpubDatabase::utf8Decode(trim($section->url));
  674.         $created $section->getCreated()->format('Y-m-d H:i:s');
  675.         $modified $section->getModified()->format('Y-m-d H:i:s');
  676.  
  677.         $stmt->bindParam(':ID'$ID);
  678.         $stmt->bindParam(':imageID'$imageID);
  679.         $stmt->bindParam(':websiteID'$websiteID);
  680.         $stmt->bindParam(':parentID'$parentID);
  681.         $stmt->bindParam(':name'$name);
  682.         $stmt->bindParam(':url'$url);
  683.         $stmt->bindParam(':created'$created);
  684.         $stmt->bindParam(':modified'$modified);
  685.         $result $stmt->execute();
  686.         OnpubDatabase::verifyExecute($this->pdo$result$this->enableTransactions$stmt->errorInfo());
  687.  
  688.         $IDs[$this->pdo->lastInsertId();
  689.         $section->ID $this->pdo->lastInsertId();
  690.       }
  691.  
  692.       $wsmap new OnpubWSMap();
  693.  
  694.       $wsmap->websiteID $section->websiteID;
  695.       $wsmap->sectionID $section->ID;
  696.       $wsmap->setCreated($section->getCreated());
  697.       $wsmap->setModified($section->getModified());
  698.  
  699.       $wsmaps[$wsmap;
  700.     }
  701.  
  702.     if ($insertWSMaps{
  703.       try {
  704.         $owsmaps->insert($wsmaps);
  705.       }
  706.       catch (PDOException $e{
  707.         if ($this->enableTransactions)
  708.           $this->pdo->rollBack();
  709.  
  710.         throw $e;
  711.       }
  712.     }
  713.  
  714.     if ($this->enableTransactions{
  715.       $status $this->pdo->commit();
  716.       OnpubDatabase::verifyTransaction($this->pdo$status);
  717.     }
  718.  
  719.     if ($isArray{
  720.       return $IDs;
  721.     }
  722.     else {
  723.       return end($IDs);
  724.     }
  725.   }
  726.  
  727.   /**
  728.    * @param OnpubSection $section The section to be updated.
  729.    * @return int 1 if the section was updated. 0 if the section was not updated or does not exist.
  730.    */
  731.   public function update(OnpubSection $section)
  732.   {
  733.     $oarticles new OnpubArticles($this->pdoFALSE);
  734.     $osamaps new OnpubSAMaps($this->pdoFALSE);
  735.     $now new DateTime();
  736.     $inTransaction FALSE;
  737.  
  738.     if ($this->enableTransactions{
  739.       $status $this->pdo->beginTransaction();
  740.       OnpubDatabase::verifyTransaction($this->pdo$status);
  741.       $inTransaction TRUE;
  742.     }
  743.  
  744.     if ($section->ID == $section->parentID{
  745.       $section->parentID NULL;
  746.     }
  747.  
  748.     if ($section->parentID{
  749.       $this->enableTransactions = FALSE;
  750.       $parentID $section->parentID;
  751.  
  752.       while ($parentID{
  753.         try {
  754.           $parent $this->get($parentID);
  755.         }
  756.         catch (PDOException $e{
  757.           if ($inTransaction)
  758.             $this->pdo->rollBack();
  759.  
  760.           throw $e;
  761.         }
  762.  
  763.         if (!$parent{
  764.           $section->parentID NULL;
  765.           break;
  766.         }
  767.  
  768.         if ($section->ID == $parent->parentID{
  769.           $section->parentID NULL;
  770.           break;
  771.         }
  772.  
  773.         $parentID $parent->parentID;
  774.       }
  775.  
  776.       $this->enableTransactions = TRUE;
  777.     }
  778.  
  779.     $stmt $this->pdo->prepare("UPDATE OnpubSections SET imageID = :imageID, parentID = :parentID, name = :name, url = :url, modified = :modified WHERE ID = :ID");
  780.     OnpubDatabase::verifyPrepare($this->pdo$stmt$this->enableTransactions);
  781.  
  782.     $ID $section->ID;
  783.     $imageID $section->imageID;
  784.     $parentID $section->parentID;
  785.     $name OnpubDatabase::utf8Decode(trim($section->name));
  786.     $url OnpubDatabase::utf8Decode(trim($section->url));
  787.     $modified $now->format('Y-m-d H:i:s');
  788.  
  789.     $stmt->bindParam(':ID'$ID);
  790.     $stmt->bindParam(':imageID'$imageID);
  791.     $stmt->bindParam(':parentID'$parentID);
  792.     $stmt->bindParam(':name'$name);
  793.     $stmt->bindParam(':url'$url);
  794.     $stmt->bindParam(':modified'$modified);
  795.     $result $stmt->execute();
  796.     OnpubDatabase::verifyExecute($this->pdo$result$this->enableTransactions$stmt->errorInfo());
  797.  
  798.     try {
  799.       $osamaps->delete($section->IDNULL);
  800.     }
  801.     catch (PDOException $e{
  802.       if ($this->enableTransactions)
  803.         $this->pdo->rollBack();
  804.  
  805.       throw $e;
  806.     }
  807.  
  808.     $articles $section->articles;
  809.     $samaps array();
  810.  
  811.     foreach ($articles as $article{
  812.       if ($article->ID{
  813.         try {
  814.           $article $oarticles->get($article->IDnew OnpubQueryOptions());
  815.         }
  816.         catch (PDOException $e{
  817.           if ($this->enableTransactions)
  818.             $this->pdo->rollBack();
  819.  
  820.           throw $e;
  821.         }
  822.  
  823.         $samap new OnpubSAMap();
  824.  
  825.         $samap->sectionID $section->ID;
  826.         $samap->articleID $article->ID;
  827.         $samap->setCreated($article->getCreated());
  828.         $samap->setModified($article->getModified());
  829.  
  830.         $samaps[$samap;
  831.       }
  832.       else {
  833.         try {
  834.           $articleID $oarticles->insert($article);
  835.         }
  836.         catch (PDOException $e{
  837.           if ($this->enableTransactions)
  838.             $this->pdo->rollBack();
  839.  
  840.           throw $e;
  841.         }
  842.  
  843.         $samap new OnpubSAMap();
  844.  
  845.         $samap->sectionID $section->ID;
  846.         $samap->articleID $articleID;
  847.  
  848.         $samaps[$samap;
  849.       }
  850.     }
  851.  
  852.     try {
  853.       $osamaps->insert($samaps);
  854.     }
  855.     catch (PDOException $e{
  856.       if ($this->enableTransactions)
  857.         $this->pdo->rollBack();
  858.  
  859.       throw $e;
  860.     }
  861.  
  862.     if ($this->enableTransactions{
  863.       $status $this->pdo->commit();
  864.       OnpubDatabase::verifyTransaction($this->pdo$status);
  865.     }
  866.  
  867.     return $stmt->rowCount();
  868.   }
  869. }
  870. ?>

Documentation generated on Fri, 08 Feb 2013 04:02:24 -0500 by phpDocumentor 1.4.4