PageRenderTime 16ms CodeModel.GetById 7ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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