From 7e1363b1f1dc42030c7fe3afe8530abc27e8ad6f Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 11 Dec 2020 12:04:36 -0500 Subject: [PATCH] Update in FieldtypeComments to trigger the appropriate Pages class hooks when comments are added/deleted --- .../FieldtypeComments.module | 67 +++++++++++++++---- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/wire/modules/Fieldtype/FieldtypeComments/FieldtypeComments.module b/wire/modules/Fieldtype/FieldtypeComments/FieldtypeComments.module index 1cd9d7bc..4d2c52df 100644 --- a/wire/modules/Fieldtype/FieldtypeComments/FieldtypeComments.module +++ b/wire/modules/Fieldtype/FieldtypeComments/FieldtypeComments.module @@ -1509,16 +1509,21 @@ class FieldtypeComments extends FieldtypeMulti { */ public function ___updateComment(Page $page, $field, Comment $comment, array $properties) { if(!count($properties)) return false; + $commentID = $comment->id; - if(!is_object($field)) $field = $this->wire('fields')->get($this->wire('sanitizer')->fieldName($field)); + $database = $this->wire()->database; + $sanitizer = $this->wire()->sanitizer; + $fields = $this->wire()->fields; + + if(!is_object($field)) $field = $fields->get($sanitizer->fieldName($field)); if(!$field instanceof Field) return false; - $table = $this->wire('database')->escapeTable($field->getTable()); + $table = $database->escapeTable($field->getTable()); $sql = "UPDATE `$table` SET "; $values = array(); foreach($properties as $property => $value) { $comment->set($property, $value); - $property = $this->wire('sanitizer')->fieldName($property); - $property = $this->wire('database')->escapeCol($property); + $property = $sanitizer->fieldName($property); + $property = $database->escapeCol($property); if($property == 'text') $property = 'data'; if(is_null($value) && ($property == 'code' || $property == 'subcode')) { $sql .= "`$property`=NULL, "; @@ -1528,20 +1533,23 @@ class FieldtypeComments extends FieldtypeMulti { } } $sql = rtrim($sql, ', ') . " WHERE id=:commentID AND pages_id=:pageID"; - $query = $this->wire('database')->prepare($sql); + $query = $database->prepare($sql); $query->bindValue(':commentID', $commentID, \PDO::PARAM_INT); $query->bindValue(':pageID', $page->id, \PDO::PARAM_INT); foreach($values as $property => $value) { $query->bindValue(":$property", $value); } - $this->wire('pages')->saveFieldReady($page, $field); try { + $this->triggerPageFieldSaveReady($page, $field); $result = $query->execute(); - $this->wire('pages')->savedField($page, $field); + $this->triggerPageFieldSaved($page, $field); } catch(\Exception $e) { $result = false; - if($this->wire('config')->debug) $this->error($e->getMessage(), Notice::log); - else $this->wire('log')->error($e->getMessage()); + if($this->wire()->config->debug) { + $this->error($e->getMessage(), Notice::log); + } else { + $this->wire()->log->error($e->getMessage()); + } } if($result) { $this->checkExistingComment($page, $field, $comment); @@ -1587,6 +1595,7 @@ class FieldtypeComments extends FieldtypeMulti { $sets = array('pages_id=:pages_id'); $binds = array('pages_id' => (int) $page->id); $nextSort = $this->getMaxSort($page, $field) + 1; + $approved = $comment->isApproved(); $comment->quiet(!$send); if(!$comment->sort) $comment->sort = $nextSort; @@ -1603,6 +1612,8 @@ class FieldtypeComments extends FieldtypeMulti { $value = $this->sleepValue($page, $field, $commentArray); if(!count($value)) return false; + if($approved) $this->triggerPageFieldSaveReady($page, $field); + // use just first item, since only 1 comment $value = $value[0]; $value['sort'] = $nextSort; @@ -1653,6 +1664,7 @@ class FieldtypeComments extends FieldtypeMulti { // add to loaded CommentArray for page, but only if it is already loaded in memory if($comments && $comments instanceof CommentArray) $comments->add($comment); $this->commentAdded($page, $field, $comment); + if($approved) $this->triggerPageFieldSaved($page, $field); } else { $error = "Error adding new comment on page $page field $field->name for $comment->email: $error"; $this->error($error, Notice::superuser | Notice::log); @@ -1673,25 +1685,28 @@ class FieldtypeComments extends FieldtypeMulti { */ public function deleteComment(Page $page, Field $field, Comment $comment, $notes = '') { + $approved = $comment->isApproved(); + $database = $this->wire()->database; + if($field->get('depth') > 0) { foreach($comment->children() as $child) { $this->deleteComment($page, $field, $child); } } - $table = $this->wire('database')->escapeTable($field->getTable()); + $table = $database->escapeTable($field->getTable()); $sql = "DELETE FROM `$table` WHERE id=:id AND pages_id=:pages_id"; - $query = $this->wire('database')->prepare($sql); + $query = $database->prepare($sql); $query->bindValue(':id', $comment->id, \PDO::PARAM_INT); $query->bindValue(':pages_id', $page->id, \PDO::PARAM_INT); $comments = $page->get($field->name); try { - $this->wire('pages')->saveFieldReady($page, $field); + if($approved) $this->triggerPageFieldSaveReady($page, $field); $result = $query->execute(); if($comments) $comments->remove($comment); $this->commentDeleted($page, $field, $comment, $notes); - $this->wire('pages')->savedField($page, $field); + if($approved) $this->triggerPageFieldSaved($page, $field); } catch(\Exception $e) { $this->error($e->getMessage()); @@ -1793,6 +1808,32 @@ class FieldtypeComments extends FieldtypeMulti { return $numComments; } + + /** + * Trigger hooks for Pages::saveFieldReady() and pages::savePageOrFieldReady() + * + * @param Page $page + * @param Field $field + * + */ + protected function triggerPageFieldSaveReady(Page $page, Field $field) { + $pages = $this->wire()->pages; + $pages->saveFieldReady($page, $field); + $pages->savePageOrFieldReady($page, $field->name); + } + + /** + * Trigger hooks for Pages::savedField() and pages::savedPageOrField() + * + * @param Page $page + * @param Field $field + * + */ + protected function triggerPageFieldSaved(Page $page, Field $field) { + $pages = $this->wire()->pages; + $pages->savedField($page, $field); + $pages->savedPageOrField($page, array($field->name)); + } /** * Hook called after comment has been deleted