diff --git a/manager/includes/document.parser.class.inc.php b/manager/includes/document.parser.class.inc.php index 494070fd89..08689f9fb8 100755 --- a/manager/includes/document.parser.class.inc.php +++ b/manager/includes/document.parser.class.inc.php @@ -568,16 +568,41 @@ function checkPublishStatus() { @include $this->config["base_path"] . "assets/cache/sitePublishing.idx.php"; $timeNow= time() + $this->config['server_offset_time']; if ($cacheRefreshTime <= $timeNow && $cacheRefreshTime != 0) { - // now, check for documents that need publishing - $sql = "UPDATE ".$this->getFullTableName("site_content")." SET published=1, publishedon=".time()." WHERE ".$this->getFullTableName("site_content").".pub_date <= $timeNow AND ".$this->getFullTableName("site_content").".pub_date!=0 AND published=0"; - if (@ !$result= $this->db->query($sql)) { - $this->messageQuit("Execution of a query to the database failed", $sql); + // Get documents that need to be published or unpublished + $sql = array(); + $sql[] = "SELECT id"; + $sql[] = "FROM {$this->getFullTableName('site_content')}"; + $sql[] = "WHERE "; + $sql[] = "(pub_date <= {$timeNow} AND pub_date != 0 AND published = 0) "; // $unpublished == ""; + $sql[] = " OR "; + $sql[] = " (unpub_date <= $timeNow AND unpub_date != 0 AND published = 1)"; // $published == ""; + $sql = implode(' ', $sql); + + $results = $this->db->makeArray($this->db->query($sql)); + $to_publish = array(); + $to_unpublish = array(); + foreach($results as $result) { + if($result['published'] != 1) { + $to_publish[] = $result['id']; + } else { + $to_unpublish[] = $result['id']; + } + } + + if( is_array($to_publish) and !empty($to_publish)) { + $publish_query = "UPDATE ".$this->getFullTableName("site_content")." SET published=1, publishedon=".time()." WHERE id IN (".implode(',', $to_publish).")"; + $this->db->query($publish_query); + foreach($to_publish as $doc) { + $this->invokeEvent("OnDocPublished",array("docid"=>$doc)); + } } - // now, check for documents that need un-publishing - $sql= "UPDATE " . $this->getFullTableName("site_content") . " SET published=0, publishedon=0 WHERE " . $this->getFullTableName("site_content") . ".unpub_date <= $timeNow AND " . $this->getFullTableName("site_content") . ".unpub_date!=0 AND published=1"; - if (@ !$result= $this->db->query($sql)) { - $this->messageQuit("Execution of a query to the database failed", $sql); + if( is_array($to_unpublish) and !empty($to_unpublish)) { + $unpublish_query = "UPDATE ".$this->getFullTableName("site_content")." SET published=0, publishedon=0 WHERE id IN (".implode(',', $to_unpublish).")"; + $this->db->query($unpublish_query); + foreach($to_unpublish as $doc) { + $this->invokeEvent("OnDocUnPublished",array("docid"=>$doc)); + } } // clear the cache @@ -597,6 +622,7 @@ function checkPublishStatus() { closedir($handle); } + // (TODO: Merge the 2 queries below to offset the added query above) // update publish time file $timesArr= array (); $sql= "SELECT MIN(pub_date) AS minpub FROM " . $this->getFullTableName("site_content") . " WHERE pub_date>$timeNow"; diff --git a/manager/processors/save_content.processor.php b/manager/processors/save_content.processor.php index f4c81e1abc..b269370478 100755 --- a/manager/processors/save_content.processor.php +++ b/manager/processors/save_content.processor.php @@ -312,6 +312,11 @@ exit; } + //Invoke onDocPublished (onDocUnPublished could never be called here) + if($published == 1) { + $modx->invokeEvent("OnDocPublished",array("docid"=>$key)); + } + $tvChanges = array(); foreach ($tmplvars as $field => $value) { if (is_array($value)) { @@ -488,6 +493,13 @@ echo "An error occured while attempting to save the edited document. The generated SQL is: $sql ."; } + // Invoke onDocPublished/onDocUnPublished + if($was_published == 1 AND $published == 0) { + $modx->invokeEvent("OnDocUnPublished",array("docid"=>$id)); + } elseif ($was_published == 0 AND $published == 1) { + $modx->invokeEvent("OnDocPublished",array("docid"=>$id)); + } + // update template variables $rs = $modx->db->select('id, tmplvarid', $tbl_site_tmplvar_contentvalues, 'contentid='. $id); $tvIds = array ();