<?php
class Commentable extends DataObjectDecorator {
function CurrentUser(){
if ($member = Member::currentUser()) {
return $member;
}
return false;
}
// Creates unique Page ID for comments
function ItemCommentID() {
return("Page".$this->owner);
}
// Initialises the comments wrapper for use in getting and psoting comments
function CommentWrapper() {
return new Comments('4f6366f3b88f3', 'www.ipokervip.net', $this->ItemCommentID());
}
// Returns all comments for current page
function GetComments() {
require_once('../mysite/thirdparty/Comments.php');
$commentsarray = $this->CommentWrapper()->GetComments();
if ($commentsarray) {
$commentsarray2 = array();
// Loops though each comment to apply certain tweaks
foreach($commentsarray as $comment) {
$comment['user'] = DataObject::get_by_id("IPMember",$comment['user_id']); // Attaches User DataObject to comment, so nickname, avatar etc can be used in template
$comment['created'] = SS_Datetime::create('SS_Datetime', $comment['created']); // Converts the created time string into a SilverStripe Datetime object so date functions can be used on it, eg. Date.ago
$comment['comment'] = nl2br(strip_tags($comment['comment'])); // Strips HTML tags from comment, and converts linebreaks
$commentsarray2[] = $comment;
}
$comments = new DataObjectSet($commentsarray2);
$comments->sort('created', 'DESC');
return($comments);
} else {
return(false);
}
}
function CommentForm() {
$fields = new FieldSet(
new TextareaField('comment', 'Leave a comment:')
);
// Create action
$actions = new FieldSet(
new FormAction('PostComment', 'Post')
);
// Create Validators
$validator = new RequiredFields('comment');
$form = new Form($this->owner, 'CommentForm', $fields, $actions, $validator);
// Mollom Spam Protection - To implement if spam becomes a problem:
//$fieldMap = array('comment' => 'post_body');
//$protector = SpamProtectorManager::update_form($form, null, $fieldMap);
if ($this->CurrentUser()) {
return $form;
}
return false;
}
function PostComment() {
require_once('../mysite/thirdparty/Comments.php');
$this->CommentWrapper()->PostComment($this->CurrentUser()->ID, $_POST['comment'], $this->owner->Title);
Director::redirect(Director::get_current_page()->Link()."#comments");
}
}
?>