Dotclear

source: plugins/rateIt/_public.php @ 1486

Revision 1486, 25.6 KB checked in by JcDenis, 14 years ago (diff)

rateIt 0.9.6:

  • Fixed DC 2.1.6 URL handlers ending
  • Added sort by sort rate by date (thanks to lottie14)
  • Cleaned some stuff
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of rateIt, a plugin for Dotclear 2.
4#
5# Copyright (c) 2009 JC Denis and contributors
6# jcdenis@gdwd.com
7#
8# Licensed under the GPL version 2.0 license.
9# A copy of this license is available in LICENSE file or at
10# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11#
12# -- END LICENSE BLOCK ------------------------------------
13
14if (!defined('DC_RC_PATH')){return;}
15
16require dirname(__FILE__).'/_widgets.php';
17
18$core->addBehavior('publicHeadContent',array('urlRateIt','publicHeadContent'));
19$core->addBehavior('publicEntryAfterContent',array('urlRateIt','publicEntryAfterContent'));
20$core->addBehavior('publicCommentAfterContent',array('urlRateIt','publicCommentAfterContent'));
21$core->addBehavior('publicEntryAfterContent',array('urlRateIt','publicGalleryAfterContent'));
22
23if (!$core->blog->settings->rateit_active) {
24
25     $core->tpl->addBlock('rateIt',array('tplRateIt','disable'));
26     $core->tpl->addBlock('rateItIf',array('tplRateIt','disable'));
27     $core->tpl->addValue('rateItLinker',array('tplRateIt','disable'));
28     $core->tpl->addValue('rateItTitle',array('tplRateIt','disable'));
29     $core->tpl->addValue('rateItTotal',array('tplRateIt','disable'));
30     $core->tpl->addValue('rateItMax',array('tplRateIt','disable'));
31     $core->tpl->addValue('rateItMin',array('tplRateIt','disable'));
32     $core->tpl->addValue('rateItNote',array('tplRateIt','disable'));
33     $core->tpl->addValue('rateItFullnote',array('tplRateIt','disable'));
34     $core->tpl->addValue('rateItQuotient',array('tplRateIt','disable'));
35
36} else {
37     $core->tpl->setPath($core->tpl->getPath(),dirname(__FILE__).'/default-templates/tpl/');
38
39     $core->tpl->addBlock('rateIt',array('tplRateIt','rateIt'));
40     $core->tpl->addBlock('rateItIf',array('tplRateIt','rateItIf'));
41     $core->tpl->addValue('rateItLinker',array('tplRateIt','rateItLinker'));
42     $core->tpl->addValue('rateItTitle',array('tplRateIt','rateItTitle'));
43     $core->tpl->addValue('rateItTotal',array('tplRateIt','rateItTotal'));
44     $core->tpl->addValue('rateItMax',array('tplRateIt','rateItMax'));
45     $core->tpl->addValue('rateItMin',array('tplRateIt','rateItMin'));
46     $core->tpl->addValue('rateItQuotient',array('tplRateIt','rateItQuotient'));
47     $core->tpl->addValue('rateItNote',array('tplRateIt','rateItNote'));
48     $core->tpl->addValue('rateItFullnote',array('tplRateIt','rateItFullnote'));
49}
50
51class urlRateIt extends dcUrlHandlers
52{
53     private static function searchRateItTplFiles($file)
54     {
55          if (strstr($file,"..") !== false)
56               return false;
57
58          $paths = $GLOBALS['core']->tpl->getPath();
59
60          foreach($paths as $path)
61          {
62               if (preg_match('/tpl(\/|)$/',$path) )
63                    $path = path::real($path.'/..');
64
65               if (file_exists($path.'/'.$file))
66                    return $path.'/'.$file;
67          }
68          return false;
69     }
70
71     public static function service($args)
72     {
73          global $core;
74          $core->rest->addFunction('rateItVote',array('rateItRest','vote'));
75          $core->rest->serve();
76          exit;
77     }
78
79     public static function postform($args)
80     {
81          global $core;
82
83          if (!$core->blog->settings->rateit_active) {
84               self::p404();
85               return;
86          }
87
88          if (!preg_match('#([^/]+)/([^/]+)$#',$args,$m)) {
89               self::p404();
90               return;
91          }
92
93          if (!isset($_POST['rateit-'.$m[1].'-'.$m[2]])) {
94               self::p404();
95               return;
96          }
97
98          $voted = false;
99          $type = $m[1];
100          $id = $m[2];
101          $note = $_POST['rateit-'.$m[1].'-'.$m[2]];
102
103          $ss = new rateIt($core);
104          $voted = $ss->voted($type,$id);
105          if (!$voted) {
106               $ss->set($type,$id,$note);
107               $voted = true;
108          }
109
110          if ($type='post') {
111               $post = $core->blog->getPosts(array('post_id'=>$id,'no_content'=>1));
112               if ($post->post_id) {
113                    http::redirect($core->blog->url.$core->url->getBase('post').'/'.$post->post_url.($voted ? '#rateit' : ''));
114                    return;
115               }
116          }
117
118          if ($type='comment') {
119               $comment = $core->blog->getComments($id);
120               if ($comment->comment_id) {
121                    http::redirect($core->blog->url.$core->url->getBase('post').'/'.$post->post_url.($voted ? '#rateit' : ''));
122                    return;
123               }
124          }
125
126          if ($type='category') {
127               $cat = $core->blog->getCategory($id);
128               if ($cat->cat_id) {
129                    http::redirect($core->blog->url.$core->url->getBase('category').'/'.$cat->cat_url.($voted ? '#rateit' : ''));
130                    return;
131               }
132          }
133
134          if ($type='tag') {
135               $objMeta = new dcMeta($core);
136               $metas = $objMeta->getMeta('tag',null,$id);
137               if ($metas->meta_id) {
138                    http::redirect($core->blog->url.$core->url->getBase('tag').'/'.$metas->meta_id.($voted ? '#rateit' : ''));
139                    return;
140               }
141          }
142
143          if ($type='gal') {
144               $gal = $core->blog->getPost(array('post_id'=>$id,'no_content'=>true));
145               if ($gal->cat_id) {
146                    http::redirect($core->blog->url.$core->url->getBase('galleries').'/'.$gal->post_url.($voted ? '#rateit' : ''));
147                    return;
148               }
149          }
150
151          if ($type='galitem') {
152               $gal = $core->blog->getPost(array('post_id'=>$id,'no_content'=>true));
153               if ($gal->cat_id) {
154                    http::redirect($core->blog->url.$core->url->getBase('gal').'/'.$gal->post_url.($voted ? '#rateit' : ''));
155                    return;
156               }
157          }
158          # --BEHAVIOR-- templateRateItRedirect
159          $core->callBehavior('templateRateItRedirect',$voted,$type,$id,$note);
160
161
162          http::redirect($core->blog->url);
163          return;
164     }
165
166     public static function files($args)
167     {
168          global $core;
169
170          if (!$core->blog->settings->rateit_active) {
171               self::p404();
172               return;
173          }
174
175          if (!preg_match('#^(.*?)$#',$args,$m)) {
176               self::p404();
177               return;
178          }
179
180          if (!($f = self::searchRateItTplFiles($m[1]))) {
181               self::p404();
182               return;
183          }
184
185          $allowed_types = array('png','jpg','jpeg','gif','css','js','swf');
186          if (!file_exists($f) || !in_array(files::getExtension($f),$allowed_types)) {
187               self::p404();
188               return;
189          }
190
191          //http::cache(array_merge(array($f),get_included_files()));
192          $type = files::getMimeType($f);
193          header('Content-Type: '.$type);
194          //header('Content-Length: '.filesize($f));
195          if ($type != "text/css" || $core->blog->settings->url_scan == 'path_info') {
196               readfile($f);
197          } else {
198               echo preg_replace('#url\((?!(http:)|/)#','url('.$core->blog->url.$core->url->getBase('rateItmodule').'/',file_get_contents($f));
199          }
200          return;
201     }
202
203     public static function publicHeadContent($core)
204     {
205          if (!$core->blog->settings->rateit_active) return;
206
207          $s = libImagePath::getSize($core,'rateIt');
208
209          echo 
210          "\n<!-- CSS for rateit --> \n".
211          "<style type=\"text/css\"> \n".
212          "div.rating-cancel,div.star-rating{float:left;width:".($s['w']+1)."px;height:".$s['h']."px;text-indent:-999em;cursor:pointer;display:block;background:transparent;overflow:hidden} \n".
213          "div.rating-cancel,div.rating-cancel a{background:url(".
214               $core->blog->url.$core->url->getBase('rateItmodule')."/img/delete.png) no-repeat 0 -16px} \n".
215          "div.star-rating,div.star-rating a{background:url(".
216               libImagePath::getUrl($core,'rateIt').") no-repeat 0 0px} \n".
217          "div.rating-cancel a,div.star-rating a{display:block;width:".$s['w']."px;height:100%;background-position:0 0px;border:0} \n".
218          "div.star-rating-on a{background-position:0 -".$s['h']."px!important} \n".
219          "div.star-rating-hover a{background-position:0 -".($s['h']*2)."px} \n".
220          "div.star-rating-readonly a{cursor:default !important} \n".
221          "div.star-rating{background:transparent!important;overflow:hidden!important} \n".
222          "</style> \n";
223
224          if (!$core->blog->settings->rateit_dispubcss) {
225
226               echo
227               "<style type=\"text/css\"> \n @import url(".
228                    $core->blog->url.$core->url->getBase('rateItmodule')."/rateit.css); \n".
229               "</style> \n";
230          }
231
232          if ($core->blog->settings->rateit_dispubjs) return;
233
234          echo 
235          "\n<!-- JS for rateit --> \n".
236          "<script type=\"text/javascript\" src=\"".
237               $core->blog->url.$core->url->getBase('rateItmodule').'/js/jquery.rating.pack.js">'.
238          "</script> \n".
239          "<script type=\"text/javascript\" src=\"".
240               $core->blog->url.$core->url->getBase('rateItmodule')."/js/jquery.rateit.js\"></script> \n".
241          "<script type=\"text/javascript\"> \n".
242          "//<![CDATA[\n".
243          " \$(function(){if(!document.getElementById){return;} \n".
244          \$.fn.rateit.defaults.service_url = '".html::escapeJS($core->blog->url.$core->url->getBase('rateItservice').'/')."'; \n".
245          \$.fn.rateit.defaults.service_func = '".html::escapeJS('rateItVote')."'; \n".
246          \$.fn.rateit.defaults.image_size = '".$s['h']."'; \n".
247          \$.fn.rateit.defaults.blog_uid = '".html::escapeJS($core->blog->uid)."'; \n".
248          \$.fn.rateit.defaults.enable_cookie = '".($core->blog->settings->rateit_userident > 0 ? '1' : '0')."'; \n".
249          \$.fn.rateit.defaults.msg_thanks = '".html::escapeJS($core->blog->settings->rateit_msgthanks)."'; \n".
250          \$('.rateit').rateit(); \n".
251          " })\n".
252          "//]]>\n".
253          "</script>\n";
254     }
255
256     public static function publicEntryAfterContent($core,$_ctx)
257     {
258          if ($core->blog->settings->rateit_active 
259           && $core->blog->settings->rateit_post_active
260           && $_ctx->exists('posts') 
261           && $_ctx->posts->post_type == 'post'
262           && (
263                $core->blog->settings->rateit_poststpl && 'post.html' == $_ctx->current_tpl 
264            || $core->blog->settings->rateit_homepoststpl && 'home.html' == $_ctx->current_tpl
265            || $core->blog->settings->rateit_tagpoststpl && 'tag.html' == $_ctx->current_tpl 
266            || $core->blog->settings->rateit_categorypoststpl && 'category.html' == $_ctx->current_tpl
267           )
268           && (
269               !$core->blog->settings->rateit_categorylimitposts
270            || $core->blog->settings->rateit_categorylimitposts == $_ctx->posts->cat_id
271           )
272          ) {
273
274               $GLOBALS['rateit_params']['type'] = 'post';
275               $GLOBALS['rateit_params']['id'] = $_ctx->posts->post_id;
276
277               echo $core->tpl->getData('rateit.html');
278
279          } else return;
280     }
281
282     public static function publicCommentAfterContent($core,$_ctx)
283     {
284          if (!$core->blog->settings->rateit_active 
285           || !$core->blog->settings->rateit_comment_active 
286           || !$core->blog->settings->rateit_commentstpl 
287           || !$_ctx->exists('comments')) return;
288
289          $GLOBALS['rateit_params']['type'] = 'comment';
290          $GLOBALS['rateit_params']['id'] = $_ctx->comments->comment_id;
291
292          echo $core->tpl->getData('rateit.html');
293     }
294
295     public static function publicGalleryAfterContent($core,$_ctx)
296     {
297          if (!$core->plugins->moduleExists('gallery')
298           || !$_ctx->exists('posts')) return;
299
300          if ($_ctx->posts->post_type == 'gal' 
301           && $core->blog->settings->rateit_gal_active 
302           && $core->blog->settings->rateit_galtpl
303           || $_ctx->posts->post_type == 'galitem' 
304           && $core->blog->settings->rateit_galitem_active
305           && $core->blog->settings->rateit_galitemtpl) {
306
307               $GLOBALS['rateit_params']['type'] = $_ctx->posts->post_type;
308               $GLOBALS['rateit_params']['id'] = $_ctx->posts->post_id;
309
310               echo $core->tpl->getData('rateit.html');
311          }
312          return;
313     }
314}
315
316class tplRateIt
317{
318     public static function disable($a,$b=null)
319     {
320          return '';
321     }
322
323     public static function rateIt($attr,$content)
324     {
325          global $core;
326
327          $type = isset($attr['type']) ? $attr['type'] : '';
328          $return = '';
329
330          if ($type == 'post') {
331
332               $return = 
333               "if (\$_ctx->exists('posts')".
334               " && \$_ctx->posts->post_type == 'post'".
335               " && \$core->blog->settings->rateit_post_active) { \n".
336               " \$rateit_params['type'] = 'post'; \n".
337               " \$rateit_params['id'] = \$_ctx->posts->post_id; \n".
338               "} \n";
339          }
340          elseif ($type == 'comment') {
341
342               $return = 
343               "if (\$_ctx->exists('comments')".
344               " && \$core->blog->settings->rateit_comment_active) { \n".
345               " \$rateit_params['type'] = 'comment'; \n".
346               " \$rateit_params['id'] = \$_ctx->comments->comment_id; \n".
347               "} \n";
348          }
349          elseif ($type == 'category') {
350
351               $return = 
352               "if (\$_ctx->exists('categories')".
353               " && \$core->blog->settings->rateit_category_active) { \n".
354               " \$rateit_params['type'] = 'category'; \n".
355               " \$rateit_params['id'] = \$_ctx->categories->cat_id; \n".
356               "} \n";
357          }
358          elseif ($type == 'tag' || $type == 'meta') {
359
360               $return = 
361               "if (\$_ctx->exists('meta')".
362               " && \$_ctx->meta->meta_type = 'tag'".
363               " && \$core->blog->settings->rateit_tag_active) { \n".
364               " \$rateit_params['type'] = 'tag'; \n".
365               " \$rateit_params['id'] = \$_ctx->meta->meta_id; \n".
366               "} \n";
367          }
368          elseif ($type == 'gal' || $type == 'galitem') {
369
370               $return = 
371               "if (\$_ctx->exists('posts') ".
372               " && \$_ctx->posts->post_type == '".$type."' ".
373               " && \$core->blog->settings->rateit_".$type."_active ".
374               " && \$core->blog->settings->rateit_".$type."tpl) { \n".
375               " \$rateit_params['type'] = '".$type."'; \n".
376               " \$rateit_params['id'] = \$_ctx->posts->post_id; \n".
377               "} \n";
378          }
379
380          # --BEHAVIOR-- templateRateIt
381          $return .= $core->callBehavior('templateRateIt',$type);
382
383
384          return
385          "<?php \n".
386          $return.
387          "if (!empty(\$rateit_params['type'])) { \n".
388          " \$rateIt = new rateIt(\$core); \n".
389          " \$rateit_voted = \$rateIt->voted(\$rateit_params['type'],\$rateit_params['id']); \n".
390          " \$_ctx->rateIt = \$rateIt->get(\$rateit_params['type'],\$rateit_params['id']); \n".
391          " ?> \n".
392          $content."\n".
393          " <?php \n".
394          " unset(\$rateit_voted); \n".
395          " \$_ctx->rateIt = null; \n".
396          "} \n".
397          "unset(\$rateit_params); \n".
398          "?> \n";
399     }
400
401     public static function rateItIf($attr,$content)
402     {
403          $operator = isset($attr['operator']) ? self::getOperator($attr['operator']) : '&&';
404
405          if (isset($attr['has_vote'])) {
406
407               $sign = (boolean) $attr['has_vote'] ? '' : '!';
408               $if[] = $sign.'(0 < $_ctx->rateIt->total)';
409          }
410
411          if (isset($attr['user_voted'])) {
412
413               $sign = (boolean) $attr['user_voted'] ? '' : '!';
414               $if[] = $sign.'$rateit_voted';
415          }
416
417          if (!empty($attr['type'])) {
418
419               if (substr($attr['type'],0,1) == '!')
420                    $if[] = '\''.substr($attr['type'],1).'\' != $_ctx->rateIt->type';
421               else
422                    $if[] = '\''.$attr['type'].'\' == $_ctx->rateIt->type';
423          }
424
425          if (empty($if))
426               return $content;
427
428          return 
429          "<?php if(".implode(' '.$operator.' ',$if).") : ?>\n".
430          $content.
431          "<?php endif; ?>\n";
432     }
433
434     public static function rateItTitle($attr)
435     {
436          global $core;
437          $f = $core->tpl->getFilters($attr);
438
439          return 
440          "<?php \n".
441          "\$title = ''; \n".
442          "if (\$_ctx->rateIt->type == 'post') \$title = __('Rate this entry'); \n".
443          "elseif (\$_ctx->rateIt->type == 'comment') \$title = __('Rate this comment'); \n".
444          "elseif (\$_ctx->rateIt->type == 'category') \$title = __('Rate this category'); \n".
445          "elseif (\$_ctx->rateIt->type == 'tag') \$title = __('Rate this tag'); \n".
446          "elseif (\$_ctx->rateIt->type == 'gal') \$title = __('Rate this gallery'); \n".
447          "elseif (\$_ctx->rateIt->type == 'galitem') \$title = __('Rate this image'); \n".
448          "else \n".
449          " \$title = \$core->callBehavior('templateRateItTitle',\$_ctx->rateIt->type,\$title); \n\n".
450          "echo ".sprintf($f,'$title')."; \n".
451          "?> \n";
452
453     }
454
455     public static function rateItLinker($attr)
456     {
457          global $core;
458          $f = $core->tpl->getFilters($attr);
459          return 
460          "<form class=\"rateit-linker\" id=\"rateit-linker-<?php echo \$_ctx->rateIt->type.'-'.\$_ctx->rateIt->id; ?>\" action=\"".
461          "<?php echo \$core->blog->url.\$core->url->getBase('rateItpostform').'/'.\$_ctx->rateIt->type.'/'.\$_ctx->rateIt->id; ?>\" method=\"post\"><p>\n".
462          "<?php for(\$i=0;\$i<\$_ctx->rateIt->quotient;\$i++){ \n".
463          " \$dis = \$rateit_voted ? ' disabled=\"disabled\"' : ''; \n".
464          " \$chk = \$_ctx->rateIt->note > \$i && \$_ctx->rateIt->note <= \$i+1 ? ' checked=\"checked\"' : ''; \n".
465          " echo '<input name=\"rateit-'.\$_ctx->rateIt->type.'-'.\$_ctx->rateIt->id.'\" class=\"rateit-'.\$_ctx->rateIt->type.'-'.\$_ctx->rateIt->id.'\" type=\"radio\" value=\"'.(\$i+1).'\"'.\$chk.\$dis.' />'; \n".
466          "} \n".
467          "if (!\$rateit_voted) { \n".
468          " echo '<input class=\"rateit-submit\" name=\"rateit_submit\" type=\"submit\" value=\"".__('Vote')."\"/>'; \n".
469          "} ?>\n".
470          "</p></form>";
471     }
472
473     public static function rateItFullnote($attr)
474     {         global $core;
475          $f = $core->tpl->getFilters($attr);
476          return '<?php echo \'<span id="rateit-fullnote-\'.$_ctx->rateIt->type.\'-\'.$_ctx->rateIt->id.\'"  class="rateit-fullnote">\'.'.sprintf($f,'$_ctx->rateIt->note."/".$_ctx->rateIt->quotient').'.\'</span>\'; ?>';
477     }
478
479     public static function rateItQuotient($attr)
480     {
481          return self::rateItValue($attr,'quotient');
482     }
483
484     public static function rateItTotal($attr)
485     {
486          $r = '';
487          if (isset($attr['totaltext']) && $attr['totaltext'] == 1) {
488               $r = 
489               "<?php \n".
490               "if (\$_ctx->rateIt->total == 0) { \n".
491               \$total = sprintf(__('no rate'),\$_ctx->rateIt->total); \n".
492               "} elseif (\$_ctx->rateIt->total == 1) {\n".
493               \$total = sprintf(__('one rate'),\$_ctx->rateIt->total); \n".
494               "} else { \n".
495               \$total = sprintf(__('%d rates'),\$_ctx->rateIt->total); \n".
496               "} \n".
497               "\$_ctx->rateIt->total = \$total; ?>\n";
498          }
499          return $r.self::rateItValue($attr,'total');
500     }
501
502     public static function rateItMax($attr)
503     {
504          return self::rateItValue($attr,'max');
505     }
506
507     public static function rateItMin($attr)
508     {
509          return self::rateItValue($attr,'min');
510     }
511
512     public static function rateItNote($attr)
513     {
514          return self::rateItValue($attr,'note');
515     }
516
517     private static function rateItValue($a,$r)
518     {
519          global $core;
520          $f = $core->tpl->getFilters($a);
521
522          if (isset($a['nospan']) && $a['nospan'] == 1)
523               return "<?php echo ".sprintf($f,'$_ctx->rateIt->'.$r)."; ?>\n";
524          else 
525               return '<?php echo \'<span id="rateit-'.$r.'-\'.$_ctx->rateIt->type.\'-\'.$_ctx->rateIt->id.\'"  class="rateit-'.$r.'">\'.'.sprintf($f,'$_ctx->rateIt->'.$r).".'</span>'; ?>\n";
526     }
527
528     protected static function getOperator($op)
529     {
530          switch (strtolower($op))
531          {
532               case 'or':
533               case '||':
534                    return '||';
535               case 'and':
536               case '&&':
537               default:
538                    return '&&';
539          }
540     }
541}
542
543class rateItPublicWidget
544{
545     public static function vote($w)
546     {
547          global $core, $_ctx; 
548
549          if (!$core->blog->settings->rateit_active) return;
550
551          if ($w->enable_post && 'post.html' == $_ctx->current_tpl 
552          && $core->blog->settings->rateit_post_active 
553          && (!$core->blog->settings->rateit_categorylimitposts
554           || $core->blog->settings->rateit_categorylimitposts == $_ctx->posts->cat_id)) {
555               $w->type = 'post';
556               $w->id = $_ctx->posts->post_id;
557               $w->title = $w->title_post;
558          }
559
560          if ($w->enable_cat && 'category.html' == $_ctx->current_tpl
561           && $core->blog->settings->rateit_category_active) {
562               $w->type = 'category';
563               $w->id = $_ctx->categories->cat_id;
564               $w->title = $w->title_cat;
565          }
566
567          if ($w->enable_tag && 'tag.html' == $_ctx->current_tpl
568           && $core->blog->settings->rateit_tag_active) {
569               $w->type = 'tag';
570               $w->id = $_ctx->meta->meta_id;
571               $w->title = $w->title_tag;
572          }
573
574          if ($w->enable_gal && strstr($_ctx->current_tpl,'gallery.html') 
575           && $core->blog->settings->rateit_gal_active) {
576               $w->type = 'gal';
577               $w->id = $_ctx->posts->post_id;
578               $w->title = $w->title_gal;
579          }
580
581          if ($w->enable_galitem && strstr($_ctx->current_tpl,'image.html')
582           && $core->blog->settings->rateit_galitem_active) {
583               $w->type = 'galitem';
584               $w->id = $_ctx->posts->post_id;
585               $w->title = $w->title_galitem;
586          }
587
588
589          # --BEHAVIOR-- parseWidgetRateItVote
590          $core->callBehavior('parseWidgetRateItVote',$w);
591
592
593          $type = $w->type;
594          $id = $w->id;
595          $title = $w->title;
596
597          if (empty($type)) return;
598
599          $rateIt = new rateIt($core);
600          $rs = $rateIt->get($type,$id);
601          $voted = $rateIt->voted($type,$id);
602
603          $res = '<div class="rateit">';
604
605          if (!empty($title))
606               $res .= '<h2>'.html::escapeHTML($title).'</h2>';
607
608          if ($w->show_fullnote == 'percent')
609               $res .= '<p><span id="rateit-fullnote-'.$type.'-'.$id.'" class="rateit-fullnote">'.round($rs->note / $rs->quotient * 100,$rs->digit).'%</span></p>';
610          elseif ($w->show_fullnote == 'full')
611               $res .= '<p><span id="rateit-fullnote-'.$type.'-'.$id.'" class="rateit-fullnote">'.$rs->note.'/'.$rs->quotient.'</span></p>';
612
613          $res .= '<form class="rateit-linker" id="rateit-linker-'.$type.'-'.$id.'" action="'.$core->blog->url.$core->url->getBase('rateItpostform').'/'.$type.'/'.$id.'" method="post"><p>';
614
615          $dis = $voted ? ' disabled="disabled"' : '';
616          for($i=0;$i<$rs->quotient;$i++) {
617               $chk = $rs->note > $i && $rs->note <= $i+1 ? ' checked="checked"' : '';
618
619               $res .= '<input name="rateit-'.$type.'-'.$id.'" class="rateit-'.$type.'-'.$id.'" type="radio" value="'.($i+1).'"'.$chk.$dis.'/>';
620          }
621
622          if (!$voted)
623               $res .= '<input type="submit" name="submit" value="'.__('Vote').'"/>';
624
625          $res .= '</p></form>';
626
627          if ($w->show_note || $w->show_vote || $w->show_higher || $w->show_lower) {
628               $res .=   '<ul>';
629               if ($w->show_note)
630                    $res .= '<li>'.__('Note:').'<span id="rateit-note-'.$type.'-'.$id.'" class="rateit-note">'.$rs->note.'</span></li>';
631               if ($w->show_vote)
632                    $res .= '<li>'.__('Votes:').'<span id="rateit-total-'.$type.'-'.$id.'" class="rateit-total">'.$rs->total.'</span></li>';
633               if ($w->show_higher)
634                    $res .= '<li>'.__('Higher:').'<span id="rateit-max-'.$type.'-'.$id.'" class="rateit-max">'.$rs->max.'</span></li>';
635               if ($w->show_lower)
636                    $res .= '<li>'.__('Lower:').'<span id="rateit-min-'.$type.'-'.$id.'" class="rateit-min">'.$rs->min.'</span></li>';
637               $res .= '</ul>';
638          }
639          return $res.'<p>&nbsp;</p></div>';
640     }
641
642     public static function rank($w)
643     {
644          global $core; 
645
646          if (!$core->blog->settings->rateit_active 
647           || $w->homeonly && $core->url->type != 'default') return;
648
649          $p = array('from'=>'','sql'=>'','columns'=>array());
650          $p['order'] = ($w->sortby && in_array($w->sortby,array('rateit_avg','rateit_total','rateit_time'))) ? 
651               $w->sortby.' ' : 'rateit_total ';
652
653          $p['order'] .= $w->sort == 'desc' ? 'DESC' : 'ASC';
654
655          if ($w->sortby != 'rateit_total')
656               $p['order'] .= ',rateit_total DESC ';
657
658          $p['limit'] = abs((integer) $w->limit);
659
660          $p['rateit_type'] = $w->type;
661
662          if ($w->type == 'post') {
663               if (!$core->blog->settings->rateit_post_active) return;
664
665               $p['columns'][] = $core->con->concat("'".$core->blog->url.$core->getPostPublicUrl('post','')."'",'P.post_url').' AS url';
666               $p['columns'][] = 'P.post_title AS title';
667               $p['groups'][] = 'P.post_url';
668               $p['groups'][] = 'P.post_title';
669               $p['from'] .= ' INNER JOIN '.$core->prefix.'post P ON CAST(P.post_id as char)=RI.rateit_id ';
670               $p['sql'] .= " AND P.post_type='post' AND P.post_status = 1 AND P.post_password IS NULL ";
671          }
672
673          if ($w->type == 'comment') {
674               if (!$core->blog->settings->rateit_comment_active) return;
675
676               $p['columns'][] = $core->con->concat("'".$core->blog->url.$core->getPostPublicUrl('post','')."'",'P.post_url').' AS url';
677               $p['columns'][] = 'P.post_title AS title';
678               $p['groups'][] = 'P.post_url';
679               $p['groups'][] = 'P.post_title';
680               $p['from'] .= ' INNER JOIN '.$core->prefix.'comment C ON CAST(C.comment_id as char)=RI.rateit_id ';
681               $p['from'] .= ' INNER JOIN '.$core->prefix.'post P ON C.comment_id = P.post_id ';
682          }
683
684          if ($w->type == 'category') {
685               if (!$core->blog->settings->rateit_category_active) return;
686
687               $p['columns'][] = $core->con->concat("'".$core->blog->url.$core->url->getBase('category')."/'",'C.cat_url').' AS url';
688               $p['columns'][] = 'C.cat_title AS title';
689               $p['groups'][] = 'C.cat_url';
690               $p['groups'][] = 'C.cat_title';
691               $p['from'] .= ' INNER JOIN '.$core->prefix.'category C ON CAST(C.cat_id as char)=RI.rateit_id ';
692          }
693
694          if ($w->type == 'tag') {
695               if (!$core->blog->settings->rateit_tag_active) return;
696
697               $p['columns'][] = $core->con->concat("'".$core->blog->url.$core->url->getBase('tag')."/'",'M.meta_id').' AS url';
698               $p['columns'][] = 'M.meta_id AS title';
699               $p['groups'][] = 'M.meta_id';
700               $p['from'] .= ' INNER JOIN '.$core->prefix.'meta M ON M.meta_id=RI.rateit_id ';
701               $p['sql'] .= "AND M.meta_type='tag' ";
702          }
703
704          if ($w->type == 'gal') {
705               if (!$core->blog->settings->rateit_gal_active) return;
706
707               $p['columns'][] = $core->con->concat("'".$core->blog->url.$core->url->getBase('gal')."/'",'P.post_url').' AS url';
708               $p['columns'][] = 'P.post_title AS title';
709               $p['groups'][] = 'P.post_url';
710               $p['groups'][] = 'P.post_title';
711               $p['from'] .= ' INNER JOIN '.$core->prefix.'post P ON CAST(P.post_id as char)=RI.rateit_id ';
712               $p['sql'] .= "AND post_type='gal' ";
713          }
714
715          if ($w->type == 'galitem') {
716               if (!$core->blog->settings->rateit_galitem_active) return;
717
718               $p['columns'][] = $core->con->concat("'".$core->blog->url.$core->url->getBase('galitem')."/'",'P.post_url').' AS url';
719               $p['columns'][] = 'P.post_title AS title';
720               $p['groups'][] = 'P.post_url';
721               $p['groups'][] = 'P.post_title';
722               $p['from'] .= ' INNER JOIN '.$core->prefix.'post P ON CAST(P.post_id as char)=RI.rateit_id ';
723               $p['sql'] .= "AND post_type='galitem' ";
724          }
725
726          $w->sql = $p;
727
728
729          # --BEHAVIOR-- parseWidgetRateItRank
730          $core->callBehavior('parseWidgetRateItRank',$w);
731
732
733          if ($w->type == '') return;
734
735          $sql = (array) $w->sql;
736          foreach($sql as $k => $v){
737               $p[$k] = $v;
738          }
739
740          $rateIt = new rateIt($core);
741          $rs = $rateIt->getRates($p);
742
743          if ($rs->isEmpty()) return;
744
745          $q = $core->blog->settings->rateit_quotient;
746          $d = $core->blog->settings->rateit_digit;
747
748          $res =
749          '<div class="rateitpostsrank rateittype'.$w->type.'">'.
750          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
751          '<ul>';
752          $i=0;
753          while ($rs->fetch()) {
754
755               $title = html::escapeHTML($rs->title);
756
757               $cut_len = abs((integer) $w->titlelen);
758               if (strlen($title) > $cut_len)
759                    $title = text::cutString($title,$cut_len).'...';
760
761               if ($rs->rateit_total == 0)
762                    $totaltext = __('no rate');
763               elseif ($rs->rateit_total == 1)
764                    $totaltext = __('one rate');
765               else
766                    $totaltext = sprintf(__('%d rates'),$rs->rateit_total);
767
768               $i++;
769               $res .= '<li>'.str_replace(array('%rank%','%title%','%note%','%quotient%','%percent%','%count%','%totaltext%'),array(
770                    '<span class="rateit-rank">'.$i.'</span>',
771                    '<a href="'.$rs->url.'">'.$title.'</a>',
772                    round($rs->rateit_avg * $q,$d),
773                    $q,
774                    floor($rs->rateit_avg * 100),
775                    $rs->rateit_total,
776                    $totaltext
777               ),
778               $w->text).'</li>';
779          }
780          $res .= '</ul></div>';
781
782          return $res;
783     }
784}
785?>
Note: See TracBrowser for help on using the repository browser.

Sites map