00001 <?
00009 abstract class BaseComment extends MyObject
00010 {
00011 public $class = '';
00012
00020 function __construct($data, $class)
00021 {
00022 parent::__construct($data, "comments_" . strtolower($class));
00023
00024 $this->class = $class;
00025 $this->english = "comment";
00026
00027 if (!class_exists($this->class))
00028 throw new Exception("Class $this->class doesn't exist!");
00029
00030
00031 $this->fullTextFields[] = 'body';
00032 }
00033
00037 function initDeletePage()
00038 {
00039 parent::initDeletePage();
00040
00041 $this->pageTitle = 'Delete Comment';
00042 }
00043
00047 function deletePost()
00048 {
00049 parent::deletePost($this->getUrl("$this->class.view?id=$this->content_id"), true);
00050 }
00051
00055 function delete()
00056 {
00057 dbExecute("
00058 UPDATE $this->tableName
00059 SET parent_id = '$this->parent_id'
00060 WHERE parent_id = '$this->id'
00061 ");
00062
00063 parent::delete();
00064 }
00065
00069 function initEditPage()
00070 {
00071 parent::initEditPage();
00072
00073 if ($this->id)
00074 $this->pageTitle = 'Edit Comment';
00075 else
00076 $this->pageTitle = 'Add New Comment';
00077
00078
00079 if (!$this->id)
00080 {
00081 $parentId = $this->params('parent_id');
00082 $contentId = $this->params('content_id');
00083
00084 if ($parentId)
00085 {
00086 $class = get_class($this);
00087 $parent = new $class($parentId);
00088
00089 $this->parent_id = $parentId;
00090 $this->content_id = $parent->content_id;
00091 }
00092 else if ($contentId)
00093 {
00094 $this->parent_id = 0;
00095 $this->content_id = $contentId;
00096 }
00097 else
00098 throw new PageError('You must supply either a parent id or a content id.');
00099 }
00100 }
00101
00105 function editFormAddFields($form)
00106 {
00107 $form->add('TextAreaField', 'body', array(
00108 "required" => true,
00109 "title" => 'Comment',
00110 "class" => 'BaseComment'
00111 ));
00112 if ($this->id)
00113 $form->addSubmit('Save Comment');
00114 else
00115 $form->addSubmit('Post Comment');
00116 }
00117
00123 function createReplyForm($contentId = null)
00124 {
00125 $form = new Form();
00126
00127 if ($this->id)
00128 {
00129 $form->action = $this->getUrl(".edit?parent_id=$this->id");
00130 $form->name = "replyForm_$this->id";
00131 $form->id = "replyForm_$this->id";
00132 }
00133 else
00134 $form->action = $this->getUrl(".edit?content_id=$contentId");
00135
00136 $form->add('TextAreaField', 'body', array(
00137 'title' => '',
00138 'required' => true,
00139 'class' => 'BaseComment',
00140 'height' => '75px',
00141 'width' => '100%'
00142 ));
00143
00144 if ($this->id)
00145 $form->addSubmit('Post Reply');
00146 else
00147 $form->addSubmit('Post New Comment');
00148
00149 return $form;
00150 }
00151
00157 function getEditPageParamXml()
00158 {
00159 $xml = <<<XML
00160 <param name="subject" desc="title of your comment. pretty important." />
00161 <param name="body" desc="comment main body" />
00162 <param name="content_id" desc="id of content in the module you're commenting on" />
00163 <param name="parent_id" desc="id of the parent comment" />
00164 XML;
00165 return $xml;
00166 }
00167
00171 function editPagePostSuccess($form)
00172 {
00173 $this->emailNotification();
00174 $this->emailReply();
00175
00176 Util::redirect($this->getUrl("$this->class.view?id=$this->content_id"));
00177 }
00178
00184 function emailNotification()
00185 {
00186
00187 $content = new $this->class($this->content_id);
00188
00189
00190 if ($this->creator->id != $content->creator->id)
00191 {
00192
00193 if ($content->creator->email_comments)
00194 {
00195 $subject = "New $this->class comment";
00196
00197 $body = $this->creator->getName() . " has posted a new comment on your $this->class.\n\n";
00198 $body .= "You can view it here:\n\n";
00199 $body .= $content->creator->getTicket(array(
00200 'module' => $this->class,
00201 'page' => 'view',
00202 'id' => $content->id
00203 )) . "\n\n";
00204 $body .= "Thank you,\nThe Happy MyKin Comment Robot\n\n";
00205 $body .= "PS. You can turn these emails off here:\n\n";
00206 $body .= $content->creator->getUrl(array('page' => 'emailprefs'), null, true);
00207
00208 $html = $this->creator->getName() . " has posted a new comment on your $this->class.<br/><br/>";
00209 $html .= $content->creator->getTicket(array(
00210 'module' => $this->class,
00211 'page' => 'view',
00212 'id' => $content->id
00213 ), 'You can view it here') . ".<br/><br/>";
00214 $html .= "Thank you,<br/>The Happy MyKin Comment Robot<br/><br/>";
00215 $html .= "PS. You can turn these emails off " . $content->creator->getLink('.emailprefs', 'here', null, true);
00216
00217 $content->creator->mail($subject, $body, $html);
00218 }
00219 }
00220 }
00221
00227 function emailReply()
00228 {
00229
00230 $content = new $this->class($this->content_id);
00231 $comClass = get_class($this);
00232 $parent = new $comClass($this->parent_id);
00233
00234
00235
00236 if ($parent->creator->id != $content->creator->id && $parent->creator->id != $this->creator->id)
00237 {
00238
00239 if ($parent->creator->email_comment_replies)
00240 {
00241 $subject = "New comment reply";
00242
00243 $body = $this->creator->getName() . " has posted a new reply to one of your $this->class comments.\n\n";
00244 $body .= "You can view it here:\n\n";
00245 $body .= $parent->creator->getTicket(array(
00246 'module' => $this->class,
00247 'page' => 'view',
00248 'id' => $content->id
00249 )) . "\n\n";
00250 $body .= "Thank you,\nThe Happy MyKin Comment Robot\n\n";
00251 $body .= "PS. You can turn these emails off here:\n\n";
00252 $body .= $parent->creator->getUrl(array('page' => 'emailprefs'), null, true);
00253
00254 $html = $this->creator->getName() . " has posted a new reply to one of your $this->class comments.<br/><br/>";
00255 $html .= $parent->creator->getTicket(array(
00256 'module' => $this->class,
00257 'page' => 'view',
00258 'id' => $content->id
00259 ), 'You can view it here') . ".<br/><br/>";
00260 $html .= "Thank you,<br/>The Happy MyKin Comment Robot<br/><br/>";
00261 $html .= "PS. You can turn these emails off " . $parent->creator->getLink('.emailprefs', 'here', null, true);
00262
00263 $parent->creator->mail($subject, $body, $html);
00264 }
00265 }
00266 }
00267
00276 function getRssItem()
00277 {
00278 $rss = parent::getRssItem();
00279
00280 if ($this->hasField('user_id'))
00281 $rss->title = 'Comment by ' . $this->creator->getName();
00282 else
00283 $rss->title = 'Comment';
00284
00285 $rss->description = $this->body;
00286
00287 return $rss;
00288 }
00289
00298 function drawComment($kids = array())
00299 {
00300 global $me;
00301
00302 if ($this->canView())
00303 {
00304 echo "<li class=\"threaded\">\n";
00305
00306
00307 $this->drawTitleBar();
00308 $this->drawCommentDetails();
00309
00310
00311 echo "<div class=\"childContainer\">\n";
00312
00313
00314 $this->drawCommentBody();
00315 $this->drawCommentReplyForm();
00316
00317
00318 $this->drawChildComments($kids, $this->id);
00319
00320 echo "</div>";
00321
00322
00323 echo "</li>";
00324 }
00325 }
00326
00330 protected function drawTitleBar()
00331 {
00332
00333 echo "<div class=\"titlebar\">";
00334
00335
00336 echo '<table class="struct"><tr><td class="titleLeft">';
00337 echo $this->creator->getIcon('tiny');
00338
00339
00340 echo $this->creator->getName(true) . " said: ";
00341
00342 echo Time::formatRelative($this->add_date);
00343
00344
00345 echo '</td><td class="titleRight">';
00346
00347
00348 echo "\t<span class=\"titleLink\" id=\"commentOptions_$this->id\">";
00349 echo "<a href=\"#\" onclick=\"return showCommentOptions('$this->id');\">options</a>";
00350 echo "</span>\n";
00351 echo "\t<span class=\"titleLink\" id=\"commentReplyLink_$this->id\"><a href=\"" . $this->getUrl(array('page' => 'edit', 'parent_id' => $this->id));
00352 echo "\" onclick=\"return showCommentReplyForm('$this->id');\">reply</a></span>\n";
00353
00354
00355 echo "</td></tr></table>";
00356
00357
00358 echo "</div>\n";
00359 }
00360
00364 protected function drawCommentDetails()
00365 {
00366
00367 echo "<div class=\"commentDetail\" id=\"comment_$this->id\" style=\"display: none;\">";
00368 echo "Posted by: " . $this->creator->getName(true) . "<br/>\n";
00369 echo "Posted: " . Time::formatRelative($this->add_date) . "<br/>\n";
00370 if ($this->edit_date != $this->add_date)
00371 echo "Last Edited on: " . Time::formatRelative($this->edit_date) . "<br/>\n";
00372 if ($this->canEdit())
00373 echo $this->getLink(".edit?id=$this->id", 'Edit Comment') . "<br/>";
00374 if ($this->canDelete())
00375 echo $this->getLink(".delete?id=$this->id", 'Delete Comment') . "<br/>";
00376 echo "</div>\n";
00377 }
00378
00382 protected function drawCommentBody()
00383 {
00384 echo "<div class=\"commentBody\">";
00385 echo Linkify::bbcode($this->body);
00386 $this->drawCommentSignature();
00387 echo "</div>";
00388 }
00389
00393 protected function drawCommentSignature()
00394 {
00395 }
00396
00400 protected function drawCommentReplyForm()
00401 {
00402
00403 echo "<div class=\"commentReply\" id=\"commentReply_$this->id\" style=\"display: none;\">\n";
00404 $form = $this->createReplyForm();
00405 $form->drawAll();
00406 echo "</div>\n";
00407 }
00408
00415 function drawChildComments($coms, $parent)
00416 {
00417
00418 if (count($coms))
00419 {
00420 if ($parent)
00421 echo "<div class=\"threaded\">";
00422
00423 echo "<ol class=\"threaded\">\n";
00424
00425 foreach ($coms AS $com)
00426
00427 if ($com->parent_id == $parent)
00428 $com->drawComment($coms);
00429 echo "</ol>\n";
00430
00431 if ($parent)
00432 echo "</div>";
00433 }
00434 }
00435
00439 function drawViewPage()
00440 {
00441 Util::redirect($this->getUrl("$this->class.view?id=$this->content_id"));
00442 }
00443
00449 function getPublicData()
00450 {
00451 $data = parent::getPublicData();
00452
00453 $data['body'] = $this->body;
00454
00455 return $data;
00456 }
00457
00463 public function getCreateFieldsArray()
00464 {
00465 $fields = parent::getCreateFieldsArray();
00466
00467 $fields['content_id'] = "content_id INT(11) default 0 not null";
00468 $fields['parent_id'] = "parent_id INT(11) default 0 not null";
00469 $fields['body'] = "body TEXT default '' not null";
00470
00471 return $fields;
00472 }
00473
00479 public function getCreateIndexesArray()
00480 {
00481 $fields = parent::getCreateIndexesArray();
00482
00483 $fields['content_id'] .= "KEY (content_id)";
00484 $fields['parent_id'] .= "KEY (parent_id)";
00485
00486 return $fields;
00487 }
00488
00492 public function save()
00493 {
00494 parent::save();
00495
00496 $obj = new $this->class($this->content_id);
00497 $obj->save();
00498 }
00499 }
00500 ?>