shared with Droplr
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?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");
	 }

	
}

?>