1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-17 20:11:46 +02:00

Update in FieldtypeComments to trigger the appropriate Pages class hooks when comments are added/deleted

This commit is contained in:
Ryan Cramer
2020-12-11 12:04:36 -05:00
parent a8ec223b22
commit 7e1363b1f1

View File

@@ -1509,16 +1509,21 @@ class FieldtypeComments extends FieldtypeMulti {
*/ */
public function ___updateComment(Page $page, $field, Comment $comment, array $properties) { public function ___updateComment(Page $page, $field, Comment $comment, array $properties) {
if(!count($properties)) return false; if(!count($properties)) return false;
$commentID = $comment->id; $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; if(!$field instanceof Field) return false;
$table = $this->wire('database')->escapeTable($field->getTable()); $table = $database->escapeTable($field->getTable());
$sql = "UPDATE `$table` SET "; $sql = "UPDATE `$table` SET ";
$values = array(); $values = array();
foreach($properties as $property => $value) { foreach($properties as $property => $value) {
$comment->set($property, $value); $comment->set($property, $value);
$property = $this->wire('sanitizer')->fieldName($property); $property = $sanitizer->fieldName($property);
$property = $this->wire('database')->escapeCol($property); $property = $database->escapeCol($property);
if($property == 'text') $property = 'data'; if($property == 'text') $property = 'data';
if(is_null($value) && ($property == 'code' || $property == 'subcode')) { if(is_null($value) && ($property == 'code' || $property == 'subcode')) {
$sql .= "`$property`=NULL, "; $sql .= "`$property`=NULL, ";
@@ -1528,20 +1533,23 @@ class FieldtypeComments extends FieldtypeMulti {
} }
} }
$sql = rtrim($sql, ', ') . " WHERE id=:commentID AND pages_id=:pageID"; $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(':commentID', $commentID, \PDO::PARAM_INT);
$query->bindValue(':pageID', $page->id, \PDO::PARAM_INT); $query->bindValue(':pageID', $page->id, \PDO::PARAM_INT);
foreach($values as $property => $value) { foreach($values as $property => $value) {
$query->bindValue(":$property", $value); $query->bindValue(":$property", $value);
} }
$this->wire('pages')->saveFieldReady($page, $field);
try { try {
$this->triggerPageFieldSaveReady($page, $field);
$result = $query->execute(); $result = $query->execute();
$this->wire('pages')->savedField($page, $field); $this->triggerPageFieldSaved($page, $field);
} catch(\Exception $e) { } catch(\Exception $e) {
$result = false; $result = false;
if($this->wire('config')->debug) $this->error($e->getMessage(), Notice::log); if($this->wire()->config->debug) {
else $this->wire('log')->error($e->getMessage()); $this->error($e->getMessage(), Notice::log);
} else {
$this->wire()->log->error($e->getMessage());
}
} }
if($result) { if($result) {
$this->checkExistingComment($page, $field, $comment); $this->checkExistingComment($page, $field, $comment);
@@ -1587,6 +1595,7 @@ class FieldtypeComments extends FieldtypeMulti {
$sets = array('pages_id=:pages_id'); $sets = array('pages_id=:pages_id');
$binds = array('pages_id' => (int) $page->id); $binds = array('pages_id' => (int) $page->id);
$nextSort = $this->getMaxSort($page, $field) + 1; $nextSort = $this->getMaxSort($page, $field) + 1;
$approved = $comment->isApproved();
$comment->quiet(!$send); $comment->quiet(!$send);
if(!$comment->sort) $comment->sort = $nextSort; if(!$comment->sort) $comment->sort = $nextSort;
@@ -1603,6 +1612,8 @@ class FieldtypeComments extends FieldtypeMulti {
$value = $this->sleepValue($page, $field, $commentArray); $value = $this->sleepValue($page, $field, $commentArray);
if(!count($value)) return false; if(!count($value)) return false;
if($approved) $this->triggerPageFieldSaveReady($page, $field);
// use just first item, since only 1 comment // use just first item, since only 1 comment
$value = $value[0]; $value = $value[0];
$value['sort'] = $nextSort; $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 // add to loaded CommentArray for page, but only if it is already loaded in memory
if($comments && $comments instanceof CommentArray) $comments->add($comment); if($comments && $comments instanceof CommentArray) $comments->add($comment);
$this->commentAdded($page, $field, $comment); $this->commentAdded($page, $field, $comment);
if($approved) $this->triggerPageFieldSaved($page, $field);
} else { } else {
$error = "Error adding new comment on page $page field $field->name for $comment->email: $error"; $error = "Error adding new comment on page $page field $field->name for $comment->email: $error";
$this->error($error, Notice::superuser | Notice::log); $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 = '') { public function deleteComment(Page $page, Field $field, Comment $comment, $notes = '') {
$approved = $comment->isApproved();
$database = $this->wire()->database;
if($field->get('depth') > 0) { if($field->get('depth') > 0) {
foreach($comment->children() as $child) { foreach($comment->children() as $child) {
$this->deleteComment($page, $field, $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"; $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(':id', $comment->id, \PDO::PARAM_INT);
$query->bindValue(':pages_id', $page->id, \PDO::PARAM_INT); $query->bindValue(':pages_id', $page->id, \PDO::PARAM_INT);
$comments = $page->get($field->name); $comments = $page->get($field->name);
try { try {
$this->wire('pages')->saveFieldReady($page, $field); if($approved) $this->triggerPageFieldSaveReady($page, $field);
$result = $query->execute(); $result = $query->execute();
if($comments) $comments->remove($comment); if($comments) $comments->remove($comment);
$this->commentDeleted($page, $field, $comment, $notes); $this->commentDeleted($page, $field, $comment, $notes);
$this->wire('pages')->savedField($page, $field); if($approved) $this->triggerPageFieldSaved($page, $field);
} catch(\Exception $e) { } catch(\Exception $e) {
$this->error($e->getMessage()); $this->error($e->getMessage());
@@ -1794,6 +1809,32 @@ class FieldtypeComments extends FieldtypeMulti {
return $numComments; 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 * Hook called after comment has been deleted
* *