PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 312 lines | 83 code | 41 blank | 188 comment | 7 complexity | 52054c0531baa589273e756afa13718f MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  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. * Get the item which the comment refers to
  127. *
  128. * @return Item The item to which the comments refers
  129. *
  130. * @since 2.0
  131. */
  132. public function getItem() {
  133. return $this->app->table->item->get($this->item_id);
  134. }
  135. /**
  136. * Get the author of the comment
  137. *
  138. * @return CommentAuthor The author of the comment
  139. *
  140. * @since 2.0
  141. */
  142. public function getAuthor() {
  143. static $authors = array();
  144. $key = md5($this->author . $this->email . $this->url . $this->user_id);
  145. if (!isset($authors[$key])) {
  146. $item = $this->getItem();
  147. $application = $item ? $item->getApplication() : null;
  148. $authors[$key] = $this->app->commentauthor->create($this->user_type, array($this->author, $this->email, $this->url, $this->user_id, $application));
  149. }
  150. return $authors[$key];
  151. }
  152. /**
  153. * Set the author of the object
  154. *
  155. * @param CommentAuthor $author The author object
  156. *
  157. * @since 2.0
  158. */
  159. public function bindAuthor(CommentAuthor $author) {
  160. $this->author = $author->name;
  161. $this->email = $author->email;
  162. $this->url = $author->url;
  163. // set params
  164. if (!$author->isGuest()) {
  165. $this->user_id = $author->user_id;
  166. $this->user_type = $author->getUserType();
  167. }
  168. }
  169. /**
  170. * Get the parent comment
  171. *
  172. * @return Comment The parent comment
  173. *
  174. * @since 2.0
  175. */
  176. public function getParent() {
  177. return $this->_parent;
  178. }
  179. /**
  180. * Set the parent comment
  181. *
  182. * @param Comment $parent The parent comment
  183. *
  184. * @since 2.0
  185. */
  186. public function setParent($parent) {
  187. $this->_parent = $parent;
  188. return $this;
  189. }
  190. /**
  191. * Get the children comments
  192. *
  193. * @return array The list of children comments
  194. *
  195. * @since 2.0
  196. */
  197. public function getChildren() {
  198. return $this->_children;
  199. }
  200. /**
  201. * Add a child to the comment
  202. *
  203. * @param Comment $child The child comment to add
  204. *
  205. * @return Comment $this for chaining support
  206. *
  207. * @since 2.0
  208. */
  209. public function addChild($child) {
  210. $this->_children[$child->id] = $child;
  211. return $this;
  212. }
  213. /**
  214. * Remove a child comment from the children list
  215. *
  216. * @param Comment $child The child to remove
  217. *
  218. * @return Comment $this for chaining support
  219. *
  220. * @since 2.0
  221. */
  222. public function removeChild($child) {
  223. unset($this->_children[$child->id]);
  224. return $this;
  225. }
  226. /**
  227. * Check if the comment has children comments
  228. *
  229. * @return boolean If the comment has children
  230. *
  231. * @since 2.0
  232. */
  233. public function hasChildren() {
  234. return !empty($this->_children);
  235. }
  236. /**
  237. * Get the comment pathway
  238. *
  239. * @return array The pathway
  240. *
  241. * @since 2.0
  242. */
  243. public function getPathway() {
  244. if ($this->_parent == null) {
  245. return array();
  246. }
  247. $pathway = $this->_parent->getPathway();
  248. $pathway[] = $this;
  249. return $pathway;
  250. }
  251. /**
  252. * Set the comment state and fires the comment:stateChanged event
  253. *
  254. * @param int $state The new state of the comment
  255. * @param boolean $save If the change should be saved to the database (default: false)
  256. */
  257. public function setState($state, $save = false) {
  258. if ($this->state != $state) {
  259. // set state
  260. $old_state = $this->state;
  261. $this->state = $state;
  262. // autosave comment ?
  263. if ($save) {
  264. $this->app->table->comment->save($this);
  265. }
  266. // fire event
  267. $this->app->event->dispatcher->notify($this->app->event->create($this, 'comment:stateChanged', compact('old_state')));
  268. }
  269. return $this;
  270. }
  271. }