/administrator/components/com_zoo/classes/comment.php

https://gitlab.com/vnsoftdev/amms · PHP · 325 lines · 86 code · 42 blank · 197 comment · 7 complexity · f1743caf220a609c4ff171dfd37ad809 MD5 · raw file

  1. <?php
  2. /**
  3. * @package com_zoo
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /**
  9. * Class to deal with comments
  10. *
  11. * @package Component.Classes
  12. */
  13. class Comment {
  14. /**
  15. * Constants for different states of the comments
  16. */
  17. const STATE_UNAPPROVED = 0;
  18. const STATE_APPROVED = 1;
  19. const STATE_SPAM = 2;
  20. /**
  21. * Id of the comment
  22. *
  23. * @var int
  24. * @since 2.0
  25. */
  26. public $id;
  27. /**
  28. * The id of the parent comment
  29. *
  30. * @var int
  31. * @since 2.0
  32. */
  33. public $parent_id;
  34. /**
  35. * The id of the item the comment refers to
  36. *
  37. * @var int
  38. * @since 2.0
  39. */
  40. public $item_id;
  41. /**
  42. * Id of the User who made the comment
  43. *
  44. * @var int
  45. * @since 2.0
  46. */
  47. public $user_id;
  48. /**
  49. * The type of the user who made the comment
  50. *
  51. * @var string
  52. * @since 2.0
  53. */
  54. public $user_type;
  55. /**
  56. * The name of the author of the comment
  57. *
  58. * @var string
  59. * @since 2.0
  60. */
  61. public $author;
  62. /**
  63. * The email of the author of the comment
  64. *
  65. * @var string
  66. * @since 2.0
  67. */
  68. public $email;
  69. /**
  70. * The url of the author of the comment
  71. *
  72. * @var string
  73. * @since 2.0
  74. */
  75. public $url;
  76. /**
  77. * The ip of the author of the comment
  78. *
  79. * @var string
  80. * @since 2.0
  81. */
  82. public $ip;
  83. /**
  84. * The creation date of the comment in mysql DATETIME format
  85. *
  86. * @var string
  87. * @since 2.0
  88. */
  89. public $created;
  90. /**
  91. * The text content of the comment
  92. *
  93. * @var string
  94. * @since 2.0
  95. */
  96. public $content;
  97. /**
  98. * The state of the comment
  99. *
  100. * @var int
  101. * @since 2.0
  102. */
  103. public $state = 0;
  104. /**
  105. * A reference to the global App object
  106. *
  107. * @var App
  108. * @since 2.0
  109. */
  110. public $app;
  111. /**
  112. * The parent comment
  113. *
  114. * @var Comment
  115. * @since 2.0
  116. */
  117. protected $_parent;
  118. /**
  119. * The comment children list
  120. *
  121. * @var array
  122. * @since 2.0
  123. */
  124. protected $_children = array();
  125. /**
  126. * Evaluates user permission
  127. *
  128. * @param JUser $user User Object
  129. *
  130. * @return boolean True if user has permission
  131. *
  132. * @since 3.2
  133. */
  134. public function canManageComments($user = null) {
  135. return $this->getItem()->canManageComments($user);
  136. }
  137. /**
  138. * Get the item which the comment refers to
  139. *
  140. * @return Item The item to which the comments refers
  141. *
  142. * @since 2.0
  143. */
  144. public function getItem() {
  145. return $this->app->table->item->get($this->item_id);
  146. }
  147. /**
  148. * Get the author of the comment
  149. *
  150. * @return CommentAuthor The author of the comment
  151. *
  152. * @since 2.0
  153. */
  154. public function getAuthor() {
  155. static $authors = array();
  156. $key = md5($this->author . $this->email . $this->url . $this->user_id);
  157. if (!isset($authors[$key])) {
  158. $item = $this->getItem();
  159. $application = $item ? $item->getApplication() : null;
  160. $authors[$key] = $this->app->commentauthor->create($this->user_type, array($this->author, $this->email, $this->url, $this->user_id, $application));
  161. }
  162. return $authors[$key];
  163. }
  164. /**
  165. * Set the author of the object
  166. *
  167. * @param CommentAuthor $author The author object
  168. *
  169. * @since 2.0
  170. */
  171. public function bindAuthor(CommentAuthor $author) {
  172. $this->author = $author->name;
  173. $this->email = $author->email;
  174. $this->url = $author->url;
  175. // set params
  176. if (!$author->isGuest()) {
  177. $this->user_id = $author->user_id;
  178. $this->user_type = $author->getUserType();
  179. }
  180. }
  181. /**
  182. * Get the parent comment
  183. *
  184. * @return Comment The parent comment
  185. *
  186. * @since 2.0
  187. */
  188. public function getParent() {
  189. return $this->_parent;
  190. }
  191. /**
  192. * Set the parent comment
  193. *
  194. * @param Comment $parent The parent comment
  195. *
  196. * @since 2.0
  197. */
  198. public function setParent($parent) {
  199. $this->_parent = $parent;
  200. return $this;
  201. }
  202. /**
  203. * Get the children comments
  204. *
  205. * @return array The list of children comments
  206. *
  207. * @since 2.0
  208. */
  209. public function getChildren() {
  210. return $this->_children;
  211. }
  212. /**
  213. * Add a child to the comment
  214. *
  215. * @param Comment $child The child comment to add
  216. *
  217. * @return Comment $this for chaining support
  218. *
  219. * @since 2.0
  220. */
  221. public function addChild($child) {
  222. $this->_children[$child->id] = $child;
  223. return $this;
  224. }
  225. /**
  226. * Remove a child comment from the children list
  227. *
  228. * @param Comment $child The child to remove
  229. *
  230. * @return Comment $this for chaining support
  231. *
  232. * @since 2.0
  233. */
  234. public function removeChild($child) {
  235. unset($this->_children[$child->id]);
  236. return $this;
  237. }
  238. /**
  239. * Check if the comment has children comments
  240. *
  241. * @return boolean If the comment has children
  242. *
  243. * @since 2.0
  244. */
  245. public function hasChildren() {
  246. return !empty($this->_children);
  247. }
  248. /**
  249. * Get the comment pathway
  250. *
  251. * @return array The pathway
  252. *
  253. * @since 2.0
  254. */
  255. public function getPathway() {
  256. if ($this->_parent == null) {
  257. return array();
  258. }
  259. $pathway = $this->_parent->getPathway();
  260. $pathway[] = $this;
  261. return $pathway;
  262. }
  263. /**
  264. * Set the comment state and fires the comment:stateChanged event
  265. *
  266. * @param int $state The new state of the comment
  267. * @param boolean $save If the change should be saved to the database (default: false)
  268. */
  269. public function setState($state, $save = false) {
  270. if ($this->state != $state) {
  271. // set state
  272. $old_state = $this->state;
  273. $this->state = $state;
  274. // autosave comment ?
  275. if ($save) {
  276. $this->app->table->comment->save($this);
  277. }
  278. // fire event
  279. $this->app->event->dispatcher->notify($this->app->event->create($this, 'comment:stateChanged', compact('old_state')));
  280. }
  281. return $this;
  282. }
  283. }