CMS  Version 3.9
comment_field_renderer.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::usingFile("framework/field_renderers.inc");
40 
73 class CommentFieldRenderer extends FieldRenderer
74 {
75  var $comment;
76  var $list;
77  var $xrefClass;
78  var $component;
79  var $publishedFormat = "published/unpublished";
80  var $publishRoles = "admin";
81 
82  function CommentFieldRenderer(&$parent, $field, $xrefClass, $component, $comments = null)
83  {
84  $this->FieldRenderer($parent);
85  $this->hideLabel = true;
86  $this->parent->add($this, $field);
87  $this->xrefClass = $xrefClass;
88  $this->component = $component;
89  $this->field = $field;
90 
91  $this->list = $this->buildCommentList($comments);
92  $this->comment = $this->createComment();
93  $this->addField($this->field);
94 
95  $this->addCommentFields();
96 
97  $this->getPublishedFormat();
98  }
99 
100  function addCommentFields()
101  {
102  $this->addTitleField();
103  $this->addAuthorField();
104  $this->addCommentField();
105  }
106 
107  function createComment()
108  {
109  global $user;
110 
111  $comment = new Comment();
112  if(!checkRole("admin"))
113  {
114  $comment->published = true;
115  }
116 
117  if($user)
118  {
119  $comment->user_id = $user->user_id;
120  }
121 
122  return $comment;
123  }
124 
125  function addTitleField()
126  {
127  $field = "comment_title";
128  $this->addField($field);
129  $this->parent->add(new StringFieldRenderer($this->parent), $field);
130  }
131 
138  function subjectSelectField($options, $label = "Subject")
139  {
140  $field = "comment_title";
141  $this->addField($field);
142  $this->parent->override($field, $label, new SelectFieldRenderer($this->parent, $field, $label, $options));
143  }
144 
151  function addField($field)
152  {
153  $filter = $this->parent->data->filter;
154  if ($filter && $filter->isExcluded($field))
155  {
156  if($filter->type == "InclusionFilter")
157  {
158  $filter->add($field);
159  }
160  else
161  {
162  $filter->remove($field);
163  }
164  }
165  }
166 
167  function addCommentField()
168  {
169  $field = "comment_message";
170  $this->parent->add(new TextFieldRenderer($this->parent), $field);
171  $this->parent->alias($field, "Comment");
172  $this->addField($field);
173  }
174 
175  function addAuthorField()
176  {
177  $field = "comment_author";
178  global $user;
179 
180  if(!$user)
181  {
182  $this->parent->add(new StringFieldRenderer($this->parent, $field));
183  $this->addField($field);
184  }
185  }
186 
187  function addPublishedField($label = "Publish")
188  {
189  $rend = new BooleanFieldRenderer($this->parent);
190  $this->parent->add($rend, "comment_published");
191  $this->parent->override("comment_published", $label, $rend);
192  $this->addField("comment_published");
193  }
194 
195  function addGroup($title = "")
196  {
197  if(!$title) $title = "Comments";
198  $this->parent->group("Comments", $this->field, "comment_title", "comment_message", "comment_published");
199  }
200 
201  function getCommentDialog()
202  {
203  return "comment_dialog";
204  }
205 
206  function buildCommentList($comments)
207  {
208  if(!is_array($comments))
209  {
210  $comments = $this->getComments();
211  }
212 
214  $list = new PagedList($comments, "{$this->parent->id}_{$field}", array($this, formatComment));
215  $list->styles = "clear: both";
216  $list->CSSclass = "comment";
217  $list->paginate = false;
218 
219  return $list;
220  }
221 
222  function getComments()
223  {
224  if(!checkRole("admin"))
225  {
226  $constraint = "WHERE published=true";
227  }
228  return $this->parent->data->Comments("$constraint ORDER BY date_posted DESC");
229  }
230 
234  function getPublishedFormat()
235  {
236  $fn = array($this->xrefClass, getPublishedFormat);
237  if(is_callable($fn))
238  {
239  $this->publishedFormat = call_user_func($fn);
240  }
241  }
242 
243  function getPublishRoles()
244  {
245  $fn = array($this->xrefClass, getPublishRoles);
246  if(is_callable($fn))
247  {
248  $this->publishRoles = call_user_func($fn);
249  }
250  }
251 
252  function formatComment($comment)
253  {
255  $author = ($comment->user_id) ? "{User.first_name} {User.last_name}" : "{author}";
256 
257  $class = ($comment->published) ? "message" : "message_unpublished";
258  $out .= "<div id='comment_{comment_id}' class='$class'>\n";
259  $out .=" <div class='title'>";
260 
261  if ($comment->title)
262  {
263  $out .= "<strong><span id='comment_{comment_id}_title'>{title:prettify}</span></strong><br/>\n";
264  }
265 
266  $published = (checkRole($this->publishRoles)) ? "{published:{$this->publishedFormat}}" : "";
267  $out .= "<span class='small'>Posted by $author at {date_posted:short}</span>&nbsp;&nbsp{$published}&nbsp;\n </div>\n";
268  $out .= "<div class='message_content'><span id='comment_{comment_id}_message'>{description:stripHTML}</span>";
269 
270  if($comment->isEditable() && !$this->parent->isReadOnly($field))
271  {
272  $out .= "&nbsp;<a href='#' onclick=\"commentMgr.showCommentDialog({comment_id}); return false\">(edit)</a>";
273  }
274 
275  $out .= "</div></div>";
276 
277  $out = $comment->format($out);
278 
279  return $out;
280  }
281 
282  function renderScript($field)
283  {
284  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field) || array_key_exists($field, $this->parent->hidden)) return "";
285 
286  //echo $this->list->writeScript();
287 
288  $commentDialog = $this->getCommentDialog();
289 
290 ?>
291 <script type="text/javascript" src="/components/comment/js/comment_manager.js"></script>
292 <script type="text/javascript">
293 var commentMgr;
294 
295 window.addEvent('domready', function()
296 {
297  commentMgr = new CommentManager(
298  '<?php echo $this->component ?>',
299  '<?php echo $commentDialog ?>'
300  );
301 });
302 
303 </script>
304 <?
305  }
306 
307  function renderField($field)
308  {
309  $this->list->listCSS = $this->listCSS;
310 
311  if(count($this->list->items) > 0)
312  {
313  $this->list->drawList();
314  }
315  // title and comment fields rendered by parent
316  }
317 
318  function getMatchingPK($xref, $obj)
319  {
320  $pks = $obj->getPrimaryKeyList();
321  foreach($pks as $pk)
322  {
323  if ($xref->hasField($pk)) return $pk;
324  }
325 
326  throw new DataItemException("No matching foreign key in xref table");
327  }
328 
329  function postProcess($field = "")
330  {
331  $commentField = "comment_message";
332  $titleField = "comment_title";
333  $authorField = "comment_author";
334  trace("CommentFieldRenderer postProcess title field $titleField value ". $_POST[$titleField], 3);
335 
336  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($commentField) || array_key_exists($commentField, $this->parent->hidden)) return "";
337 
338  $xref = new $this->xrefClass;
339 
340  $obj = $this->parent->data;
341  $pk = $this->getMatchingPK($xref, $obj);
342 
343  $commentMessage = $_POST[$commentField];
344 
345  if(!$commentMessage)
346  {
347  trace("CommentFieldRenderer no comment entered in comment field {$commentField}", 3);
348  return;
349  }
351  $comment->title = $_POST[$titleField];
352  $comment->description = $commentMessage;
353 
354  if(isset($_POST["comment_published"]))
355  {
356  $comment->published = $_POST["comment_published"];
357  }
358  else
359  {
360  $comment->published = $comment->published;
361  }
362 
363  $author = $_POST[$authorField];
364  if($author)
365  {
366  $comment->author = $author;
367  }
368  $comment->save();
369 
370  $xref->set($pk, $obj->get($pk));
371  $xref->set("comment_id", $comment->comment_id);
372  $xref->save();
373  }
374 
375  function renderReadOnly($comment)
376  {
377  $this->readOnly = true;
378  $this->renderField($this->field);
379  }
380 }?>
$constraint
$_POST["owner_id"]
Definition: blog_form.inc:54
$component
Definition: help.inc:38
$out
Definition: page.inc:66
$parent
Definition: templates.inc:42
$filter
Definition: update.inc:44
$published
Definition: update.inc:38
static usingFile()
Uses the specified framework file(s) from the framework directory.
Definition: core.inc:369
global $user
$comment
$list
Definition: list_images.inc:41
$openData field
$typeSelect hideLabel