Dotclear

source: plugins/rateIt/_public.php @ 1865

Revision 1865, 28.3 KB checked in by JcDenis, 13 years ago (diff)

rateIt 0.9.7:

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

Sites map