CMS  Version 3.9
comment_view.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9 Copyright (c) 2010 Sonjara, Inc
10 
11 Permission is hereby granted, free of charge, to any person
12 obtaining a copy of this software and associated documentation
13 files (the "Software"), to deal in the Software without
14 restriction, including without limitation the rights to use,
15 copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the
17 Software is furnished to do so, subject to the following
18 conditions:
19 
20 The above copyright notice and this permission notice shall be
21 included in all copies or substantial portions of the Software.
22 
23 Except as contained in this notice, the name(s) of the above
24 copyright holders shall not be used in advertising or otherwise
25 to promote the sale, use or other dealings in this Software
26 without prior written authorization.
27 
28 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35 OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::using("comment", "email", "captcha", "rating", "text_lookup");
40 Fakoli::usingFeature("auto_form", "paged_list");
41 
42 /*
43  * View for displaying list of comments linked to an obj and
44  * a form for entering a new comment;
45  *
46  * @obj - object that is linked to comments - must have relation "Comments"
47  * (e.g., participant)
48  *
49  *
50  * @xrefClass - the class that links the obj to the Comment table, e.g.,
51  * ParticipantCommentXref
52  *
53  * @title - the title to display above the comment section.
54  */
55 
57 {
58  var $obj;
59  var $list;
60  var $form;
63  var $title;
64  var $usernameFormat = "{User.first_name} {User.last_name}";
65 
74  {
75  $this->obj = $obj;
76  $this->xrefClass = $xrefClass;
77  $this->component = $component;
78  $this->title = ($title) ? $title : "Comments";
79  $this->list = $this->buildCommentList($obj);
80  $this->form = $this->buildCommentForm();
81 
82  if (Settings::getValue("comment", "username_format") == "Username")
83  {
84  $mgr = new UserManager();
85  $this->usernameFormat = "{User.".$mgr->getUsernameField()."}";
86  }
87  }
88 
89  static function addCommentPanel($qs = null)
90  {
91  if (!$qs)
92  {
93  $qs = getCleanQueryString();
94  }
95  $panel = new Panel("comment_panel", "/action/comment/comment_panel?$qs");
96  $panel->draw();
97  }
98 
99  static function fromGET()
100  {
101  $adapter = CommentManager::getAdapter();
102 
103  if ($adapter)
104  {
105  $parent = $adapter->createParent();
106  return new CommentView($parent, $adapter->xrefClass, $adapter->component);
107  }
108  }
109 
111  {
112  trace("Building Comment List", 3);
113 
114  if(checkRole("admin"))
115  {
116  $comments = $obj->Comments("ORDER BY date_posted DESC");
117  }
118  else
119  {
120  $comments = $obj->Comments("Where published=1 ORDER BY date_posted DESC");
121  }
122  $list = new PagedList($comments, "comment_list", array($this, formatComment));
123  $list->CSSclass = "Standard";
124  $list->styles = "clear: both";
125  $list->paginate = false;
126 
127  return $list;
128  }
129 
130 
132  {
133  $author = ($comment->user_id) ? $comment->format($this->usernameFormat) : $comment->author;
134 
135  if($comment->published)
136  {
137  $out = "<div id='comment{$comment->comment_id}' class='message'>\n";
138  }
139  else
140  {
141  $out = "<a name='comment{$comment->comment_id}'></a><div id='comment{$comment->comment_id}' class='message_unpublished'>\n";
142  }
143  $out .=" <div class='title'>";
144 
145  if ($comment->title)
146  {
147  $out .= "<strong>{$comment->title}</strong><br/>";
148  }
149 
150  $out .= "<span class='small'>Posted by $author at {$comment->date_posted}</span>\n </div>\n <div class='message_content'>";
151 
152  if($comment->isEditable() || (checkRole("admin") && !Settings::getValue("comment","publish_automatically")))
153  {
154  $edit .= $this->drawButtons($comment);
155  }
156 
157  $out .= $comment->format("{description:stripHTML}$edit");
158  $out .= "</div></div></a>";
159  return $out;
160  }
161 
163  {
164  $edit = "<div class='button_row'>\n";
165 
166  if($comment->isEditable())
167  {
168  $edit .= "<a href='#' class='button' onclick=\"new Comment().showCommentDialog({comment_id}, '{$this->xrefClass}', '{$this->component}'); return false\"> Edit </a>&nbsp;&nbsp;";
169  }
170  if(checkRole("admin") && !Settings::getValue("comment","publish_automatically"))
171  {
172  if($comment->published)
173  {
174  $edit .= "<a href='#' class='button' onclick=\"new Comment().commentPublish({comment_id}); return false;\"> Un-Publish </a>\n";
175  }
176  else
177  {
178  $edit .= "<a href='#' class='button' onclick=\"new Comment().commentPublish({comment_id}); return false;\"> Publish </a>\n";
179  }
180  }
181  $edit .= "<div style='clear:both'></div></div>\n";
182 
183  return $edit;
184  }
185 
186  function writeScript()
187  {
188  global $user;
189 
190  $script .= $this->list->writeScript();
191 
192  if ($user || Settings::getValue("comment", "allow_anonymous_commenting"))
193  {
194  $script .= $this->form->writeScript();
195  }
196 
197  return $script;
198  }
199 
200  function drawView()
201  {
202  global $user;
203 
204  if (Settings::getValue("comment", "enable_item_rating"))
205  {
207  }
208 
209  echo "<h3>{$this->title}</h3>\n";
210  $preambleIdentifier = $user ? Settings::getValue("comment", "logged_in_preamble_text") : Settings::getValue("comment", "anonymous_preamble_text");
211 
212  $text = TextLookup::getText($preambleIdentifier);
213 
214  echo $text;
215 
216  if(count($this->list->items) > 0)
217  {
218  $this->list->drawList();
219  }
220  }
221 
222  function drawForm()
223  {
224  global $user;
225 
226  if ($user || Settings::getValue("comment", "allow_anonymous_commenting"))
227  {
228 
229  $this->form->drawForm();
230 
231  }
232  }
233 
234  function buildCommentForm()
235  {
236  trace("Building Comment Form", 3);
237 
238  global $user;
239 
240  $key_name = $this->obj->getPrimaryKey();
241  $key_value = $this->obj->get($key_name);
242 
243  $comment = new Comment();
244  if(Settings::getValue("comment", "publish_automatically"))
245  {
246  $comment->published = true;
247  }
248  $form = new AutoForm($comment, "POST", "/action/comment/comment_form?comment_id=&xref_class={$this->xrefClass}&key_name=$key_name&key_value=$key_value&xref_component={$this->component}", "Comment_form");
249  $form->ajaxSubmit("function(result) {new Comment().commentFormResult(result);}", "function() {document.id('{$form->id}_error').set('text','Failed to communicate with server'); }");
250 
251  $form->required("description");
252  $form->alias("title", "Subject", "author", "Your Name");
253  $form->alias("description", "Comment");
254  $form->hide(user_id, published);
255  $form->onSaveComplete = array($this, addXrefRecord);
256 
257  if ($user)
258  {
259  $comment->user_id = $user->get($user->primary_key);
260  $form->hide("author");
261  }
262  else
263  {
264  $comment->user_id = 0;
265  $form->required("author");
266  }
267 
268  $captchaMode = Settings::getValue("comment", "use_captcha");
269  if ($captchaMode == "Always" || ($captchaMode == "Anonymous Only" && !$user))
270  {
271  $captchaMgr = new CaptchaManager();
272  $captchaMgr->addCaptchaToForm($form);
273  }
274 
275  if (!Settings::getValue("comment", "show_subject_field")) $form->hide("title");
276 
277  return $form;
278  }
279 
281  {
282  $xref = new $this->xrefClass;
283  $xref->set($this->obj->primary_key, $this->obj->get($this->obj->primary_key));
284  $xref->set($form->data->primary_key, $form->data->get($form->data->primary_key));
285  $xref->save();
286  }
287 
288  function save()
289  {
290  $form = $this->form;
291 
292  if($form->save())
293  {
294  $xref = new $this->xrefClass;
295 
296  $obj = $this->obj;
297  $pk = $this->getMatchingPK($xref, $obj);
298 
299  $xref->set($pk, $obj->get($pk));
300  $xref->set("comment_id", $form->data->get("comment_id"));
301  $xref->save();
302  return true;
303  }
304  }
305 
315  static function onSaveNewComment($form)
316  {
317  $xref_class = $form->data->get("xref_class");
318  $xref = new $xref_class;
319 
320  $obj = $form->data;
321 
322  // The primary key name of the parent obj (e.g., Article)
323  $key_name = $obj->get("key_name");
324  $key_value = $obj->get("key_value");
325 
326  $xref->set($key_name, $key_value);
327  $xref->set("comment_id", $obj->get("comment_id"));
328 
329  if (method_exists($xref, verifyEnabled))
330  {
331  if (!$xref->verifyEnabled($key_value))
332  {
333  Fakoli::end("No SPAM for you today, Mister!");
334  }
335  }
336 
337  // Wait until after verified, if applicable, to save
338  $xref->save();
339 
340  $confirmation_message = "Thank you for your comment.";
341  if(!Settings::getValue("comment", "publish_automatically"))
342  {
343  sendEmailUsingEmailManager($xref, 'new_comment');
344  $confimation_message = "Thank you. Your comment has been submitted to the moderator.";
345  }
346 
347  $form->data->set("confirmation_message", $confirmation_message);
348  }
349 
350  function getMatchingPK($xref, $obj)
351  {
352  $pks = $obj->getPrimaryKeyList();
353  foreach($pks as $pk)
354  {
355  if ($xref->hasField($pk)) return $pk;
356  }
357 
358  throw new DataItemException("No matching foreign key in xref table");
359  }
360 
361 }
362 ?>
$edit
$out
Definition: page.inc:66
$parent
Definition: templates.inc:42
$bookmark title
Provides a central management class for event handlers and common functionality for the captcha compo...
CommentView($obj, $xrefClass, $component, $title="")
buildCommentList($obj)
static fromGET()
static onSaveNewComment($form)
Called by comment_form handler to save xref and send email for new comments.
addXrefRecord($form)
formatComment($comment)
static addCommentPanel($qs=null)
drawButtons($comment)
getMatchingPK($xref, $obj)
static usingFeature()
Uses the specified framework feature(s).
Definition: core.inc:388
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
static end($message="")
Use this method to terminate execution of a script instead of using the php keywords exit() or die().
Definition: core.inc:1149
Panel provides a reloadable panel tied to an action handler.
Definition: panel.inc:51
static drawRatingWidget($obj=null)
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
static getText($code, $obj=null, $blank=false)
Retrieves text for display on a page, given the code.
Definition: text_lookup.inc:85
Provides the interface to the user model for the application.
global $user
$captchaMode
$xref_class
$comment
$key_value
$key_name
sendEmailUsingEmailManager($sendingItem, $name, $onSendComplete="")
$image published
$feedback user_id
Definition: save.inc:14
$panel