ENHANCEMENT: Addition of Depth and Lineage to the Comment model, helper methods to enable threading

This commit is contained in:
Gordon Anderson 2015-02-07 12:13:13 +07:00
parent a88fc21cde
commit 726478af0e

View File

@ -16,11 +16,16 @@ class Comment extends DataObject {
"Moderated" => "Boolean", "Moderated" => "Boolean",
"IsSpam" => "Boolean", "IsSpam" => "Boolean",
"ParentID" => "Int", "ParentID" => "Int",
'AllowHtml' => "Boolean" 'AllowHtml' => "Boolean",
"ParentID" => "Int",
"Depth" => 'Int',
'Lineage' => 'Varchar(255)',
'MarkedAsDeleted' => 'Boolean'
); );
private static $has_one = array( private static $has_one = array(
"Author" => "Member" "Author" => "Member",
"ParentComment" => 'Comment'
); );
private static $default_sort = '"Created" DESC'; private static $default_sort = '"Created" DESC';
@ -33,6 +38,11 @@ class Comment extends DataObject {
"Moderated" => 1, "Moderated" => 1,
"IsSpam" => 0 "IsSpam" => 0
); );
private static $indexes = array(
'Depth' => true,
'Lineage' => true
);
private static $casting = array( private static $casting = array(
'AuthorName' => 'Varchar', 'AuthorName' => 'Varchar',
@ -56,13 +66,37 @@ class Comment extends DataObject {
'IsSpam' => 'Is Spam' 'IsSpam' => 'Is Spam'
); );
public function onBeforeWrite() { public function onAfterWrite() {
parent::onBeforeWrite(); parent::onAfterWrite();
// Sanitize HTML, because its expected to be passed to the template unescaped later if (!$this->LineageFixed) {
if($this->AllowHtml) { // Sanitize HTML, because its expected to be passed to the template unescaped later
$this->Comment = $this->purifyHtml($this->Comment); if($this->AllowHtml) {
$this->Comment = $this->purifyHtml($this->Comment);
}
// Calculate depth and lineage from parent comment
if ($this->ParentCommentID == 0) {
$this->Depth = 1;
$this->Lineage = $this->paddedNumber($this->ID);
} else {
$pc = $this->ParentComment();
$this->Depth = $pc->Depth + 1;
$this->Lineage = ($pc->Lineage).$this->paddedNumber($this->ID);
}
$this->LineageFixed = true;
$this->write();
} }
}
private function paddedNumber($i) {
// fixme, use config
return str_pad($i, 5, '0', STR_PAD_LEFT);
} }
/** /**
@ -92,6 +126,31 @@ class Comment extends DataObject {
DB::alteration_message("Migrated PageComment to Comment","changed"); DB::alteration_message("Migrated PageComment to Comment","changed");
DB::getConn()->dontRequireTable('PageComment'); DB::getConn()->dontRequireTable('PageComment');
} }
DB::query('UPDATE Comment set Depth=1 where ParentCommentID = 0;');
// add depth to comments missing this value
$maxthreaddepth = Commenting::get_config_value($this->Class, 'maximum_thread_comment_depth');
for ($i=1; $i < $maxthreaddepth; $i++) {
$sql = "UPDATE Comment c1\n".
"INNER JOIN Comment c2\n".
"ON c1.ID = c2.ParentCommentID\n".
"SET c2.Depth=".($i+1)." WHERE c1.Depth=".$i.";";
DB::query($sql);
}
DB::alteration_message("Updated missing depth values from comment hierarchy","changed");
// now fix any missing lineage
for ($i=1; $i < $maxthreaddepth; $i++) {
$comments = Comment::get()->filter('Depth',$i)->where('Lineage is NULL');
foreach ($comments as $comment) {
$comment->write();
DB::alteration_message("Lineage fixed for comment ".$comment->write(),"changed");
}
}
} }
/** /**
@ -333,6 +392,21 @@ class Comment extends DataObject {
)))); ))));
} }
} }
/**
* @return string
*/
public function ViewCommentLink() {
if($this->canView() && $this->Moderated) {
$token = SecurityToken::inst();
return DBField::create_field("Varchar", Director::absoluteURL($token->addToUrl(sprintf(
"CommentingController/viewcomment/%s", 'COMMENTID'
))));
} else {
return 'lolnope';
}
}
/** /**
* @return string * @return string
@ -418,4 +492,17 @@ class Comment extends DataObject {
return $gravatar; return $gravatar;
} }
/*
Check if this comment can be replied to
- check threading is enabled
- comment must have been moderated
*/
public function CanReply() {
$threaded = Commenting::get_config_value($this->BaseClass, 'thread_comments');
$maxdepth = Commenting::get_config_value($this->BaseClass, 'maximum_thread_comment_depth');
$disabled = $this->Disabled;
$moderated = $this->Moderated;
return $threaded && $this->Moderated && $this->Depth < $maxdepth && !$disabled && $this->Moderated;
}
} }