Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
comment.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/page/page.inc';
18 
19 if (!defined('SQ_COMMENT_HTML_ALLOW')) {
20  define ('SQ_COMMENT_HTML_ALLOW', 0);
21 }
22 
23 if (!defined('SQ_COMMENT_HTML_ESCAPE')) {
24  define ('SQ_COMMENT_HTML_ESCAPE', 1);
25 }
26 
27 if (!defined('SQ_COMMENT_HTML_STRIP')) {
28  define ('SQ_COMMENT_HTML_STRIP', 2);
29 }
30 
42 class Comment extends Page
43 {
44 
45 
56  protected function _getName($short_name=FALSE, $contextid=NULL)
57  {
58  // No context specified, using the current context
59  if ($contextid === NULL) {
60  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
61  }//end if
62 
63  // Obtain the attribute value for Name from the specified Context
64  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
65  if (empty($values) === TRUE) {
66  return strip_tags(parent::_getName($short_name, $contextid));
67  } else {
68  return strip_tags($values[$this->id]);
69  }
70 
71  }//end _getName()
72 
73 
82  {
83  return $this->attr('name');
84 
85  }//end getAssetAttributeNameKeywordReplacement()
86 
87 
96  {
97  return $this->attr('comment');
98 
99  }//end getAssetAttributeCommentKeywordReplacement()
100 
101 
111  {
112  $length = $GLOBALS['SQ_SYSTEM']->getUserPrefs($this->type(), 'SQ_COMMENT_MAX_LENGTH');
113  return $length;
114 
115  }//end getCommentMaxLengthKeywordReplacement()
116 
117 
125  {
126  $length = $GLOBALS['SQ_SYSTEM']->getUserPrefs($this->type(), 'SQ_COMMENT_MAX_RATING');
127  return $length;
128 
129  }//end getCommentMaxRatingKeywordReplacement()
130 
131 
139  {
140  $rating = $this->attr('rating');
141  if ($rating == -1) return;
142 
143  return $this->_getRatingScaledInteger();
144 
145  }//end getCommentRatingIntegerKeywordReplacement()
146 
147 
155  {
156  $rating = $this->attr('rating');
157  if ($rating == -1) return;
158 
159  return round($this->_getRatingScaled(), 2);
160 
161  }//end getCommentRatingKeywordReplacement()
162 
163 
170  function _getRatingScaled()
171  {
172  $rating_scale = $this->_getRatingLimit();
173  $rating_absolute = $this->attr('rating');
174 
175  if (empty($rating_scale)) return 0;
176 
177  $rating = $rating_absolute/100 * $rating_scale;
178 
179  return $rating;
180 
181  }//end _getRatingScaled()
182 
183 
191  {
192  return round($this->_getRatingScaled());
193 
194  }//end _getRatingScaledInteger()
195 
196 
206  {
207  if (empty($integer) || !is_numeric($integer) || $integer < 0) {
208  return 0;
209  }
210 
211  $rating_scale = $this->_getRatingLimit();
212 
213  if (empty($rating_scale)) return 0;
214 
215  if ($integer > $rating_scale) return 100;
216 
217  $percentage = $integer/$rating_scale * 100;
218 
219  return $percentage;
220 
221  }//end _convertIntegerRatingToPercent()
222 
223 
230  function _getRatingLimit()
231  {
232  $rating_max = $GLOBALS['SQ_SYSTEM']->getUserPrefs($this->type(), 'SQ_COMMENT_MAX_RATING');
233  return $rating_max;
234 
235  }//end _getRatingLimit()
236 
237 
245  {
246  $max_rating = $this->_getRatingLimit();
247  $rating = $this->attr('rating');
248  if ($rating == -1) return;
249 
250  $rating = $this->_getRatingScaledInteger();
251 
252  $image_location_prefix = sq_web_path('data').'/asset_types/comment/files';
253 
254  $image_rated = $GLOBALS['SQ_SYSTEM']->getUserPrefs($this->type(), 'SQ_COMMENT_IMG_RATED');
255  if (empty($image_rated)) {
256  $image_rated = $image_location_prefix.'/star.png';
257  }
258 
259  $image_blank = $GLOBALS['SQ_SYSTEM']->getUserPrefs($this->type(), 'SQ_COMMENT_IMG_BLANK');
260  if (empty($image_blank)) {
261  $image_blank = $image_location_prefix.'/star_grey.png';
262  }
263 
264  $html = '';
265 
266  for ($i = 0; $i < $max_rating; $i++) {
267  if ($i < $rating) {
268  $alt = '*';
269  $image = $image_rated;
270  } else {
271  $alt = '.';
272  $image = $image_blank;
273  }
274  $html .= '<img src="'.htmlspecialchars($image).'" alt="'.$alt.'" />';
275  }
276 
277  return $html;
278 
279  }//end getCommentRatingImageHtmlBlockKeywordReplacement()
280 
281 
289  {
290  $rating = $this->attr('rating');
291  if ($rating == -1) return;
292 
293  return round($rating, 2);
294 
295  }//end getCommentRatingPercentageKeywordReplacement()
296 
297 
305  {
306  $keywords = parent::getAvailableKeywords();
307 
308  $keywords['comment_max_length'] = translate('news_comment_max_length');
309  $keywords['comment_max_rating'] = 'Max Rating Value';
310  $keywords['comment_rating'] = 'Rating Assigned to the comment (possibly a decimal)';
311  $keywords['comment_rating_integer'] = 'Rating Assigned to the comment as integer';
312  $keywords['comment_rating_percentage'] = 'Rating Assigned to the comment as percent';
313  $keywords['comment_rating_image_html_block'] = 'Rating Assigned to the comment in form of images';
314 
315  return $keywords;
316 
317  }//end getAvailableKeywords()
318 
319 
328  function attr($name)
329  {
330  if (in_array($name, Array('name', 'short_name', 'comment'))) {
331  return $this->_formatValueBasedOnPreference(parent::attr($name));
332  } else if (in_array($name, Array('name_raw', 'short_name_raw', 'comment_raw'))) {
333  return parent::attr(str_replace('_raw', '', $name));
334  }
335  return parent::attr($name);
336 
337  }//end attr()
338 
339 
352  function setAttrValue($name, $value)
353  {
354  $caller = array_get_index(debug_backtrace(), 1);
355  if (array_get_index($caller, 'class') == 'page_asset_builder') {
356  if (in_array($name, Array('name', 'short_name', 'comment'))) {
357  return TRUE;
358  }
359  }
360 
361  switch ($name) {
362  case 'comment':
363  $length = $GLOBALS['SQ_SYSTEM']->getUserPrefs($this->type(), 'SQ_COMMENT_MAX_LENGTH');
364  if (!empty($length) && (strlen($value) > $length)) {
365  $value = substr($value, 0, $length);
366  }
367  $value = trim($value);
368  break;
369 
370  case 'name':
371  $value = trim($value);
372  break;
373  }
374 
375  return parent::setAttrValue($name, $value);
376 
377  }//end setAttrValue()
378 
379 
389  {
390  $html_conf = $GLOBALS['SQ_SYSTEM']->getUserPrefs($this->type(), 'SQ_COMMENT_HTML_CONF');
391 
392  switch ($html_conf) {
393  case SQ_COMMENT_HTML_STRIP:
394  $value = nl2br(strip_tags($value));
395  break;
396 
397  case SQ_COMMENT_HTML_ESCAPE:
398  $value = nl2br(htmlspecialchars($value));
399  break;
400 
401  case SQ_COMMENT_HTML_ALLOW:
402  // nothing at the moment
403  break;
404  }
405 
406  $disable_keyword_conf = $GLOBALS['SQ_SYSTEM']->getUserPrefs($this->type(), 'SQ_COMMENT_DISABLE_KEYWORD_REPLACEMENTS');
407 
408  if ($disable_keyword_conf) {
409  require_once SQ_FUDGE_PATH.'/general/text.inc';
410  $keywords = extract_keywords($value);
411 
412  foreach ($keywords as $keyword) {
413  $value = str_replace('%'.$keyword.'%', $keyword, $value);
414  }
415  }//end if disable keyword replacements
416 
417  return $value;
418 
419  }//end _formatValueBasedOnPreference()
420 
421 
422 }//end class
423 
424 ?>