PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/code/dataobjects/Comment.php

https://github.com/lenix/silverstripe-comments
PHP | 247 lines | 135 code | 44 blank | 68 comment | 25 complexity | a414b3823e30f7ef3a2273d445e15286 MD5 | raw file
  1. <?php
  2. /**
  3. * Represents a single comment object.
  4. *
  5. * @package comments
  6. */
  7. class Comment extends DataObject {
  8. static $db = array(
  9. "Name" => "Varchar(200)",
  10. "Comment" => "Text",
  11. "Email" => "Varchar(200)",
  12. "URL" => "Varchar(255)",
  13. "BaseClass" => "Varchar(200)",
  14. "Moderated" => "Boolean",
  15. "IsSpam" => "Boolean",
  16. 'NeedsModeration' => 'Boolean',
  17. );
  18. static $has_one = array(
  19. "Parent" => "DataObject",
  20. "Author" => "Member"
  21. );
  22. static $has_many = array();
  23. static $many_many = array();
  24. static $defaults = array(
  25. "Moderated" => true
  26. );
  27. static $casting = array(
  28. "RSSTitle" => "Varchar",
  29. );
  30. /**
  31. * Migrates the old {@link PageComment} objects to {@link Comment}
  32. */
  33. public function requireDefaultRecords() {
  34. parent::requireDefaultRecords();
  35. if(DB::getConn()->hasTable('PageComment')) {
  36. $comments = DB::query("SELECT * FROM \"PageComment\"");
  37. if($comments) {
  38. while($pageComment = $comments->nextRecord()) {
  39. // create a new comment from the older page comment
  40. $comment = new Comment($pageComment);
  41. // set the variables which have changed
  42. $comment->BaseClass = 'SiteTree';
  43. $comment->URL = (isset($pageComment['CommenterURL'])) ? $pageComment['CommenterURL'] : "";
  44. $comment->write();
  45. }
  46. }
  47. DB::alteration_message("Migrated PageComment to Comment","changed");
  48. DB::getConn()->dontRequireTable('PageComment');
  49. }
  50. }
  51. /**
  52. * Return a link to this comment
  53. *
  54. * @return string link to this comment.
  55. */
  56. public function Link($action = "") {
  57. return $this->Parent()->Link($action) . '#' . $this->Permalink();
  58. }
  59. /**
  60. * Returns the permalink for this {@link Comment}. Inserted into
  61. * the ID tag of the comment
  62. *
  63. * @return string
  64. */
  65. public function Permalink() {
  66. $prefix = Commenting::get_config_value($this->BaseClass, 'comment_permalink_prefix');
  67. return $prefix . $this->ID;
  68. }
  69. /**
  70. * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields
  71. */
  72. function fieldLabels($includerelations = true) {
  73. $labels = parent::fieldLabels($includerelations);
  74. $labels['Name'] = _t('Comment.NAME', 'Author Name');
  75. $labels['Comment'] = _t('Comment.COMMENT', 'Comment');
  76. $labels['IsSpam'] = _t('Comment.ISSPAM', 'Spam?');
  77. $labels['NeedsModeration'] = _t('Comment.NEEDSMODERATION', 'Needs Moderation?');
  78. return $labels;
  79. }
  80. /**
  81. * Returns the parent {@link DataObject} this comment is attached too
  82. *
  83. * @return DataObject
  84. */
  85. public function getParent() {
  86. if(!$this->BaseClass) $this->BaseClass = "SiteTree";
  87. return DataObject::get_by_id($this->BaseClass, $this->ParentID);
  88. }
  89. /**
  90. * This method is called just before this object is
  91. * written to the database.
  92. *
  93. * Specifically, make sure "http://" exists at the start
  94. * of the URL, if it doesn't have https:// or http://
  95. */
  96. public function onBeforeWrite() {
  97. parent::onBeforeWrite();
  98. $url = $this->CommenterURL;
  99. if($url) {
  100. if(strtolower(substr($url, 0, 8)) != 'https://' && strtolower(substr($url, 0, 7)) != 'http://') {
  101. $this->CommenterURL = 'http://' . $url;
  102. }
  103. }
  104. }
  105. /**
  106. * @todo needs to compare to the new {@link Commenting} configuration API
  107. *
  108. * @return Boolean
  109. */
  110. function canCreate($member = null) {
  111. return false;
  112. }
  113. /**
  114. * Checks for association with a page,
  115. * and {@link SiteTree->ProvidePermission} flag being set to TRUE.
  116. * Note: There's an additional layer of permission control
  117. * in {@link PageCommentInterface}.
  118. *
  119. * @param Member $member
  120. * @return Boolean
  121. */
  122. function canView($member = null) {
  123. if(!$member) $member = Member::currentUser();
  124. // Standard mechanism for accepting permission changes from decorators
  125. $extended = $this->extendedCan('canView', $member);
  126. if($extended !== null) return $extended;
  127. $page = $this->getParent();
  128. return (
  129. ($page && $page->ProvideComments)
  130. || (bool)Permission::checkMember($member, 'CMS_ACCESS_CommentAdmin')
  131. );
  132. }
  133. /**
  134. * Checks for "CMS_ACCESS_CommentAdmin" permission codes
  135. * and {@link canView()}.
  136. *
  137. * @param Member $member
  138. * @return Boolean
  139. */
  140. function canEdit($member = null) {
  141. if(!$member) $member = Member::currentUser();
  142. // Standard mechanism for accepting permission changes from decorators
  143. $extended = $this->extendedCan('canEdit', $member);
  144. if($extended !== null) return $extended;
  145. if(!$this->canView($member)) return false;
  146. return (bool)Permission::checkMember($member, 'CMS_ACCESS_CommentAdmin');
  147. }
  148. /**
  149. * Checks for "CMS_ACCESS_CommentAdmin" permission codes
  150. * and {@link canEdit()}.
  151. *
  152. * @param Member $member
  153. * @return Boolean
  154. */
  155. function canDelete($member = null) {
  156. if(!$member) $member = Member::currentUser();
  157. // Standard mechanism for accepting permission changes from decorators
  158. $extended = $this->extendedCan('canDelete', $member);
  159. if($extended !== null) return $extended;
  160. return $this->canEdit($member);
  161. }
  162. /************************************ Review the following */
  163. function getRSSName() {
  164. if($this->Name) {
  165. return $this->Name;
  166. } elseif($this->Author()) {
  167. return $this->Author()->getName();
  168. }
  169. }
  170. function DeleteLink() {
  171. return ($this->canDelete()) ? "PageComment_Controller/deletecomment/$this->ID" : false;
  172. }
  173. function CommentTextWithLinks() {
  174. $pattern = '|([a-zA-Z]+://)([a-zA-Z0-9?&%.;:/=+_-]*)|is';
  175. $replace = '<a rel="nofollow" href="$1$2">$1$2</a>';
  176. return preg_replace($pattern, $replace, $this->Comment);
  177. }
  178. function SpamLink() {
  179. return ($this->canEdit() && !$this->IsSpam) ? "PageComment_Controller/reportspam/$this->ID" : false;
  180. }
  181. function HamLink() {
  182. return ($this->canEdit() && $this->IsSpam) ? "PageComment_Controller/reportham/$this->ID" : false;
  183. }
  184. function ApproveLink() {
  185. return ($this->canEdit() && $this->NeedsModeration) ? "PageComment_Controller/approve/$this->ID" : false;
  186. }
  187. function SpamClass() {
  188. if($this->getField('IsSpam')) {
  189. return 'spam';
  190. } else if($this->getField('NeedsModeration')) {
  191. return 'unmoderated';
  192. } else {
  193. return 'notspam';
  194. }
  195. }
  196. function RSSTitle() {
  197. return sprintf(
  198. _t('PageComment.COMMENTBY', "Comment by '%s' on %s", PR_MEDIUM, 'Name, Page Title'),
  199. Convert::raw2xml($this->getRSSName()),
  200. $this->Parent()->Title
  201. );
  202. }
  203. }