/**
* @ingroup BaseObjects
* @brief BaseComment is an object used for comment threads.
*
* A class that simplifies adding, editing, deleting, replying, and displaying
* of comments. It makes adding comments to content very easy.
*/
abstract class BaseComment extends MyObject
{
public $class = '';
/**
* The constructor...
*
* @param $data see BaseObject constructor
* @param $class the name of the class the comment is on... dont put
* 'comment' in here.... unless its a comment comment.
*/
function __construct($data, $class)
{
parent::__construct($data, "comments_" . strtolower($class));
$this->class = $class;
$this->english = "comment";
if (!class_exists($this->class))
throw new Exception("Class $this->class doesn't exist!");
//add in our fulltext fields
$this->fullTextFields[] = 'body';
}
/**
* Initialize teh delete page.
*/
function initDeletePage()
{
parent::initDeletePage();
$this->pageTitle = 'Delete Comment';
}
/**
* Redirect to our original object the comment was posted on.
*/
function deletePost()
{
parent::deletePost($this->getUrl("$this->class.view?id=$this->content_id"), true);
}
/**
* do the actual deletion. also save the abandoned child comments.
*/
function delete()
{
dbExecute("
UPDATE $this->tableName
SET parent_id = '$this->parent_id'
WHERE parent_id = '$this->id'
");
parent::delete();
}
/**
* initialize the edit page.
*/
function initEditPage()
{
parent::initEditPage();
if ($this->id)
$this->pageTitle = 'Edit Comment';
else
$this->pageTitle = 'Add New Comment';
//if its new... get our ids.
if (!$this->id)
{
$parentId = $this->params('parent_id');
$contentId = $this->params('content_id');
if ($parentId)
{
$class = get_class($this);
$parent = new $class($parentId);
$this->parent_id = $parentId;
$this->content_id = $parent->content_id;
}
else if ($contentId)
{
$this->parent_id = 0;
$this->content_id = $contentId;
}
else
throw new PageError('You must supply either a parent id or a content id.');
}
}
/**
* Add our comment fields... pretty basic. body and submit button.
*/
function editFormAddFields($form)
{
$form->add('TextAreaField', 'body', array(
"required" => true,
"title" => 'Comment',
"class" => 'BaseComment'
));
if ($this->id)
$form->addSubmit('Save Comment');
else
$form->addSubmit('Post Comment');
}
/**
* creates a reply form for inline displaying with our nifty javascript.
*
* @return the reply form as a Form object.
*/
function createReplyForm($contentId = null)
{
$form = new Form();
if ($this->id)
{
$form->action = $this->getUrl(".edit?parent_id=$this->id");
$form->name = "replyForm_$this->id";
$form->id = "replyForm_$this->id";
}
else
$form->action = $this->getUrl(".edit?content_id=$contentId");
$form->add('TextAreaField', 'body', array(
'title' => '',
'required' => true,
'class' => 'BaseComment',
'height' => '75px',
'width' => '100%'
));
if ($this->id)
$form->addSubmit('Post Reply');
else
$form->addSubmit('Post New Comment');
return $form;
}
/**
* get the edit page parameters xml
*
* @return an xml string of edit page xml
*/
function getEditPageParamXml()
{
$xml = <<
";
$html .= $content->creator->getTicket(array(
'module' => $this->class,
'page' => 'view',
'id' => $content->id
), 'You can view it here') . ".
";
$html .= "Thank you,
The Happy MyKin Comment Robot
";
$html .= "PS. You can turn these emails off " . $content->creator->getLink('.emailprefs', 'here', null, true);
$content->creator->mail($subject, $body, $html);
}
}
}
/**
* if the comment was in response to another comment, will email author of
* the comment. that is if the user table has a field called
* "email_comment_replies" of type tinyint(1)
*/
function emailReply()
{
//get our stuff
$content = new $this->class($this->content_id);
$comClass = get_class($this);
$parent = new $comClass($this->parent_id);
//if the parent author is the same as content auth... they'll get an email on the content comment.
//also, dont send an email if they are the this->creator.
if ($parent->creator->id != $content->creator->id && $parent->creator->id != $this->creator->id)
{
//does the this->creator want comment notices?
if ($parent->creator->email_comment_replies)
{
$subject = "New comment reply";
$body = $this->creator->getName() . " has posted a new reply to one of your $this->class comments.\n\n";
$body .= "You can view it here:\n\n";
$body .= $parent->creator->getTicket(array(
'module' => $this->class,
'page' => 'view',
'id' => $content->id
)) . "\n\n";
$body .= "Thank you,\nThe Happy MyKin Comment Robot\n\n";
$body .= "PS. You can turn these emails off here:\n\n";
$body .= $parent->creator->getUrl(array('page' => 'emailprefs'), null, true);
$html = $this->creator->getName() . " has posted a new reply to one of your $this->class comments.
";
$html .= $parent->creator->getTicket(array(
'module' => $this->class,
'page' => 'view',
'id' => $content->id
), 'You can view it here') . ".
";
$html .= "Thank you,
The Happy MyKin Comment Robot
";
$html .= "PS. You can turn these emails off " . $parent->creator->getLink('.emailprefs', 'here', null, true);
$parent->creator->mail($subject, $body, $html);
}
}
}
/**
* this function generates the rss data for the comment.
*
* you probably wont have to define this at all. this means AUTOMAGIC
* comment rss feeds!!
*
* @return a rss item object as per BaseObject
*/
function getRssItem()
{
$rss = parent::getRssItem();
if ($this->hasField('user_id'))
$rss->title = 'Comment by ' . $this->creator->getName();
else
$rss->title = 'Comment';
$rss->description = $this->body;
return $rss;
}
/**
* draw the comment.
*
* it is called from drawChildComments. it draws its comment, then recurses
* back to drawChildComments with the kids array and its id.
*
* @param $kids is an array of comments to use.
*/
function drawComment($kids = array())
{
global $me;
if ($this->canView())
{
echo "
\n"; echo "Posted: " . Time::formatRelative($this->add_date) . "
\n"; if ($this->edit_date != $this->add_date) echo "Last Edited on: " . Time::formatRelative($this->edit_date) . "
\n"; if ($this->canEdit()) echo $this->getLink(".edit?id=$this->id", 'Edit Comment') . "
"; if ($this->canDelete()) echo $this->getLink(".delete?id=$this->id", 'Delete Comment') . "
"; echo "