Dotclear

source: plugins/rateIt/_widgets.php @ 1307

Revision 1307, 7.1 KB checked in by JcDenis, 14 years ago (diff)

rateIt 0.5:

  • fixed PostgreSQL compatibility
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
16$core->addBehavior('initWidgets',array('rateItWidget','initVote'));
17$core->addBehavior('initWidgets',array('rateItWidget','initRank'));
18
19class rateItWidget
20{
21     public static function initVote(&$w)
22     {
23          global $core;
24
25          $w->create('rateit',__('Rating'),
26               array('rateItWidget','parseVote'));
27          $w->rateit->setting('enable_post',__('Enable vote for entries'),
28               1,'check');
29          $w->rateit->setting('title_post',__('Title for entries:'),
30               __('Rate this entry'),'text');
31
32          # --BEHAVIOR-- initWidgetRateItVote
33          $core->callBehavior('initWidgetRateItVote',$w);
34
35          $w->rateit->setting('show_fullnote',__('Show full note'),'full','combo',
36               array(__('Hidden')=>'hide',__('Full note (5/20)')=>'full',__('Percent (25%)')=>'percent'));
37          $w->rateit->setting('show_note',__('Show note'),
38               1,'check');
39          $w->rateit->setting('show_vote',__('Show the count of vote'),
40               1,'check');
41          $w->rateit->setting('show_higher',__('Show the highest rate'),
42               1,'check');
43          $w->rateit->setting('show_lower',__('Show the lowest rate'),
44               1,'check');
45          $w->rateit->setting('type','type','','hidden');
46          $w->rateit->setting('id','id','0','hidden');
47          $w->rateit->setting('title','title','rateIt','hidden');
48     }
49
50     public static function parseVote(&$w)
51     {
52          global $core, $_ctx; 
53
54          if (!$core->blog->settings->rateit_active) return;
55
56          if ($w->enable_post && 'post.html' == $_ctx->current_tpl) {
57               $w->type = 'post';
58               $w->id = $_ctx->posts->post_id;
59               $w->title = $w->title_post;
60          }
61
62          # --BEHAVIOR-- parseWidgetRateItVote
63          $core->callBehavior('parseWidgetRateItVote',$w);
64
65          $type = $w->type;
66          $id = $w->id;
67          $title = $w->title;
68
69          if (empty($type)) return;
70
71          $rateIt = new rateIt($core);
72          $rs = $rateIt->get($type,$id);
73          $voted = $rateIt->voted($type,$id);
74
75          $res = '<div class="rateitwidget">';
76
77          if (!empty($title))
78               $res .= '<h2>'.html::escapeHTML($title).'</h2>';
79
80          if ($w->show_fullnote == 'percent')
81               $res .= '<p><span id="rateit-fullnote-'.$type.'-'.$id.'" class="rateit-fullnote">'.round($rs->note / $rs->quotient * 100,$rs->digit).'%</span></p>';
82          elseif ($w->show_fullnote == 'full')
83               $res .= '<p><span id="rateit-fullnote-'.$type.'-'.$id.'" class="rateit-fullnote">'.$rs->note.'/'.$rs->quotient.'</span></p>';
84
85          $res .= '<form class="rateit-linker" id="raiteitwidget-linker-'.$type.'-'.$id.'" action="'.$core->blog->url.'rateitnow/'.$type.'/'.$id.'/" method="post"><p>';
86
87          $dis = $voted ? ' disabled="disabled"' : '';
88          for($i=0;$i<$rs->quotient;$i++) {
89               $chk = $rs->note > $i && $rs->note <= $i+1 ? ' checked="checked"' : '';
90
91               $res .= '<input name="rateit-'.$type.'-'.$id.'" class="rateit-'.$type.'-'.$id.'" type="radio" value="'.($i+1).'"'.$chk.$dis.'/>';
92          }
93
94          if (!$voted)
95               $res .= '<input type="submit" name="submit" value="'.__('Vote').'"/>';
96
97          $res .= '</p></form>';
98
99          if ($w->show_note || $w->show_vote || $w->show_higher || $w->show_lower) {
100               $res .=   '<ul>';
101               if ($w->show_note)
102                    $res .= '<li>'.__('Note:').'<span id="rateitwidget-note-'.$type.'-'.$id.'" class="rateit-note">'.$rs->note.'</span></li>';
103               if ($w->show_vote)
104                    $res .= '<li>'.__('Votes:').'<span id="rateitwidget-total-'.$type.'-'.$id.'" class="rateit-total">'.$rs->total.'</span></li>';
105               if ($w->show_higher)
106                    $res .= '<li>'.__('Higher:').'<span id="rateitwidget-max-'.$type.'-'.$id.'" class="rateit-max">'.$rs->max.'</span></li>';
107               if ($w->show_lower)
108                    $res .= '<li>'.__('Lower:').'<span id="rateitwidget-min-'.$type.'-'.$id.'" class="rateit-min">'.$rs->min.'</span></li>';
109               $res .= '</ul>';
110          }
111          return $res.'<p>&nbsp;</p></div>';
112     }
113
114     public static function initRank(&$w)
115     {
116          global $core;
117          $w->create('rateitrank',__('Top rating'),
118               array('rateItWidget','parseRank'));
119          $w->rateitrank->setting('title',__('Title:'),
120               __('Top rated entries'),'text');
121
122          $types = new ArrayObject();
123
124          # --BEHAVIOR-- initWidgetRateItRank
125          $core->callBehavior('initWidgetRateItRank',$types);
126
127          $types[] = array(__('entries')=>'post');
128          $types = (array) $types;
129          $combo = array();
130          foreach($types as $k => $v){
131               $combo = array_merge($v,$combo);
132          }
133
134          $w->rateitrank->setting('type',__('Type:'),'post','combo',$combo);
135         
136          $w->rateitrank->setting('limit',__('Length:'),3,'combo',array(
137               1=>1,2=>2,3=>3,4=>4,5=>5,10=>10,15=>15,20=>20));
138          $w->rateitrank->setting('sortby',__('Order by:'),'rateit_avg','combo',array(
139               __('Note') => 'rateit_avg',
140               __('Votes') => 'rateit_total'));
141          $w->rateitrank->setting('sort',__('Sort:'),'desc','combo',array(
142               __('Ascending') => 'asc',
143               __('Descending') => 'desc'));
144          $w->rateitrank->setting('text',__('Text'),'%rank% %title% (%note%/%quotient%)','text');
145          $w->rateitrank->setting('homeonly',__('Home page only'),1,'check');
146          $w->rateitrank->setting('sql','sql','','hidden');
147     }
148
149     public static function parseRank(&$w)
150     {
151          global $core; 
152
153          if (!$core->blog->settings->rateit_active) return;
154
155          if ($w->homeonly && $core->url->type != 'default') return;
156
157          $p = array('from'=>'','sql'=>'','columns'=>array());
158          $p['order'] = ($w->sortby && in_array($w->sortby,array('rateit_avg','rateit_total'))) ? 
159               $w->sortby.' ' : 'rateit_total ';
160
161          $p['order'] .= $w->sort == 'desc' ? 'desc' : 'asc';
162
163          $p['limit'] = abs((integer) $w->limit);
164
165          $p['rateit_type'] = $w->type;
166
167          if ($w->type == 'post') {
168               $p['columns'][] = $core->con->concat("'".$core->blog->url.$core->getPostPublicUrl('post','')."'",'P.post_url').' AS url';
169               $p['columns'][] = 'P.post_title AS title';
170               $p['groups'][] = 'P.post_url';
171               $p['groups'][] = 'P.post_title';
172               $p['from'] .= ' INNER JOIN '.$core->prefix.'post P ON CAST(P.post_id as char)=RI.rateit_id ';
173               $p['sql'] .= ' AND P.post_status = 1 AND P.post_password IS NULL ';
174          }
175          $w->sql = $p;
176
177          # --BEHAVIOR-- parseWidgetRateItRank
178          $core->callBehavior('parseWidgetRateItRank',$w);
179
180          if ($w->type == '') return;
181
182          $sql = (array) $w->sql;
183          foreach($sql as $k => $v){
184               $p[$k] = $v;
185          }
186
187          $rateIt = new rateIt($core);
188          $rs = $rateIt->getRates($p);
189
190          if ($rs->isEmpty()) return;
191
192          $q = $core->blog->settings->rateit_quotient;
193          $d = $core->blog->settings->rateit_digit;
194
195          $res =
196          '<div class="rateitpostsrank">'.
197          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
198          '<ul>';
199          $i=0;
200          while ($rs->fetch()) {
201               $i++;
202               $res .= '<li>'.str_replace(array('%rank%','%title%','%note%','%quotient%','%percent%','%count%'),array(
203                    '<span class="rateit-rank">'.$i.'</span>',
204                    '<a href="'.$rs->url.'">'.
205                         html::escapeHTML($rs->title).'</a>',
206                    round($rs->rateit_avg * $q,$d),
207                    $q,
208                    floor($rs->rateit_avg * 100),
209                    $rs->rateit_total
210               ),
211               $w->text).'</li>';
212          }
213          $res .= '</ul></div>';
214
215          return $res;
216     }
217}
218?>
Note: See TracBrowser for help on using the repository browser.

Sites map