1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of topWriter, 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 | |
---|
13 | if (!defined('DC_RC_PATH')){return;} |
---|
14 | |
---|
15 | $core->addBehavior('initWidgets',array('topWriterWidget','init')); |
---|
16 | |
---|
17 | class topWriterWidget |
---|
18 | { |
---|
19 | public static function init($w) |
---|
20 | { |
---|
21 | $w->create('topcom',__('Top comments'), |
---|
22 | array('topWriterWidget','topCom')); |
---|
23 | $w->topcom->setting('title',__('Title:'), |
---|
24 | __('Top comments'),'text'); |
---|
25 | $w->topcom->setting('text',__('Text:'), |
---|
26 | '%author% (%count%)','text'); |
---|
27 | $w->topcom->setting('period',__('Period:'),'year','combo', |
---|
28 | array(__('day')=>'day',__('week')=>'week',__('month')=>'month', |
---|
29 | __('year')=>'year',__('from begining')=>'')); |
---|
30 | $w->topcom->setting('sort',__('Sort:'),'desc','combo',array( |
---|
31 | __('Ascending') => 'asc',__('Descending') => 'desc')); |
---|
32 | $w->topcom->setting('limit',__('Limit:'),'10','text'); |
---|
33 | $w->topcom->setting('exclude',__('Exclude post writer from list'),0,'check'); |
---|
34 | $w->topcom->setting('homeonly',__('Home page only'),1,'check'); |
---|
35 | |
---|
36 | $w->create('toppost',__('Top entries'), |
---|
37 | array('topWriterWidget','topPost')); |
---|
38 | $w->toppost->setting('title',__('Title:'), |
---|
39 | __('Top entries'),'text'); |
---|
40 | $w->toppost->setting('text',__('Text:'), |
---|
41 | '%author% (%count%)','text'); |
---|
42 | $w->toppost->setting('period',__('Period:'),'year','combo', |
---|
43 | array(__('day')=>'day',__('week')=>'week',__('month')=>'month', |
---|
44 | __('year')=>'year',__('from begining')=>'')); |
---|
45 | $w->toppost->setting('sort',__('Sort:'),'desc','combo',array( |
---|
46 | __('Ascending') => 'asc',__('Descending') => 'desc')); |
---|
47 | $w->toppost->setting('limit',__('Limit:'),'10','text'); |
---|
48 | $w->toppost->setting('homeonly',__('Home page only'),1,'check'); |
---|
49 | } |
---|
50 | |
---|
51 | public static function topCom($w) |
---|
52 | { |
---|
53 | global $core; |
---|
54 | |
---|
55 | if ($w->homeonly && $core->url->type != 'default') return; |
---|
56 | |
---|
57 | $req = |
---|
58 | 'SELECT COUNT(*) AS count, comment_email '. |
---|
59 | "FROM ".$core->prefix."post P, ".$core->prefix."comment C ". |
---|
60 | 'WHERE P.post_id=C.post_id '. |
---|
61 | "AND blog_id='".$core->con->escape($core->blog->id)."' ". |
---|
62 | 'AND post_status=1 AND comment_status=1 '. |
---|
63 | self::period('comment_dt',$w->period); |
---|
64 | |
---|
65 | if ($w->exclude) { |
---|
66 | $req .= |
---|
67 | 'AND comment_email NOT IN ('. |
---|
68 | ' SELECT U.user_email '. |
---|
69 | ' FROM '.$core->prefix.'user U'. |
---|
70 | ' INNER JOIN '.$core->prefix.'post P ON P.user_id = U.user_id '. |
---|
71 | " WHERE blog_id='".$core->con->escape($core->blog->id)."' ". |
---|
72 | ' GROUP BY U.user_email) '; |
---|
73 | } |
---|
74 | |
---|
75 | $req .= |
---|
76 | 'GROUP BY comment_email '. |
---|
77 | 'ORDER BY count '.($w->sort == 'asc' ? 'ASC' : 'DESC').' '. |
---|
78 | $core->con->limit(abs((integer) $w->limit)); |
---|
79 | |
---|
80 | $rs = $core->con->select($req); |
---|
81 | |
---|
82 | if ($rs->isEmpty()) return; |
---|
83 | |
---|
84 | $content = ''; |
---|
85 | $i = 0; |
---|
86 | while($rs->fetch()){ |
---|
87 | $user = $core->con->select( |
---|
88 | "SELECT * FROM ".$core->prefix."comment WHERE comment_email='".$rs->comment_email."' " |
---|
89 | ); |
---|
90 | |
---|
91 | if (!$user->comment_author) continue; |
---|
92 | |
---|
93 | $i++; |
---|
94 | $rank = '<span class="topcomments-rank">'.$i.'</span>'; |
---|
95 | |
---|
96 | if ($user->comment_site) { |
---|
97 | $author = '<a href="'.$user->comment_site.'" title="'. |
---|
98 | __('Author link').'">'.$user->comment_author.'</a>'; |
---|
99 | } |
---|
100 | else |
---|
101 | $author = $user->comment_author; |
---|
102 | |
---|
103 | if ($rs->count == 0) |
---|
104 | $count = __('no comment'); |
---|
105 | |
---|
106 | elseif ($rs->count == 1) |
---|
107 | $count = __('one comment'); |
---|
108 | |
---|
109 | else |
---|
110 | $count = sprintf(__('%s comments'),$rs->count); |
---|
111 | |
---|
112 | $content .= '<li>'.str_replace( |
---|
113 | array('%rank%','%author%','%count%'), |
---|
114 | array($rank,$author,$count), |
---|
115 | $w->text |
---|
116 | ).'</li>'; |
---|
117 | } |
---|
118 | |
---|
119 | if ($i < 1) return; |
---|
120 | |
---|
121 | return |
---|
122 | '<div class="topcomments">'. |
---|
123 | ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). |
---|
124 | '<ul>'.$content.'</ul>'. |
---|
125 | '</div>'; |
---|
126 | } |
---|
127 | |
---|
128 | public static function topPost($w) |
---|
129 | { |
---|
130 | global $core; |
---|
131 | |
---|
132 | if ($w->homeonly && $core->url->type != 'default') return; |
---|
133 | |
---|
134 | $rs = $core->con->select( |
---|
135 | 'SELECT COUNT(*) AS count, U.user_id '. |
---|
136 | "FROM ".$core->prefix."post P ". |
---|
137 | 'INNER JOIN '.$core->prefix.'user U ON U.user_id = P.user_id '. |
---|
138 | "WHERE blog_id='".$core->con->escape($core->blog->id)."' ". |
---|
139 | 'AND post_status=1 AND user_status=1 '. |
---|
140 | self::period('post_dt',$w->period). |
---|
141 | 'GROUP BY U.user_id '. |
---|
142 | 'ORDER BY count '.($w->sort == 'asc' ? 'ASC' : 'DESC').', U.user_id ASC '. |
---|
143 | $core->con->limit(abs((integer) $w->limit))); |
---|
144 | |
---|
145 | if ($rs->isEmpty()) return; |
---|
146 | |
---|
147 | $content = ''; |
---|
148 | $i = 0; |
---|
149 | while($rs->fetch()){ |
---|
150 | $user = $core->con->select( |
---|
151 | "SELECT * FROM ".$core->prefix."user WHERE user_id='".$rs->user_id."' " |
---|
152 | ); |
---|
153 | |
---|
154 | $author = dcUtils::getUserCN($user->user_id,$user->user_name, |
---|
155 | $user->user_firstname,$user->user_displayname); |
---|
156 | |
---|
157 | if (empty($author)) continue; |
---|
158 | |
---|
159 | $i++; |
---|
160 | $rank = '<span class="topentries-rank">'.$i.'</span>'; |
---|
161 | |
---|
162 | if ($core->blog->settings->authormode_active) { |
---|
163 | $author = '<a href="'. |
---|
164 | $core->blog->url.$core->url->getBase("author").'/'.$user->user_id.'" '. |
---|
165 | 'title="'.__('Author posts').'">'.$author.'</a>'; |
---|
166 | } |
---|
167 | elseif ($user->user_url) { |
---|
168 | $author = '<a href="'.$user->user_url.'" title="'. |
---|
169 | __('Author link').'">'.$author.'</a>'; |
---|
170 | } |
---|
171 | |
---|
172 | if ($rs->count == 0) |
---|
173 | $count = __('no post'); |
---|
174 | |
---|
175 | elseif ($rs->count == 1) |
---|
176 | $count = __('one post'); |
---|
177 | |
---|
178 | else |
---|
179 | $count = sprintf(__('%s posts'),$rs->count); |
---|
180 | |
---|
181 | $content .= '<li>'.str_replace( |
---|
182 | array('%rank%','%author%','%count%'), |
---|
183 | array($rank,$author,$count), |
---|
184 | $w->text |
---|
185 | ).'</li>'; |
---|
186 | } |
---|
187 | |
---|
188 | if ($i < 1) return; |
---|
189 | |
---|
190 | return |
---|
191 | '<div class="topentries">'. |
---|
192 | ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). |
---|
193 | '<ul>'.$content.'</ul>'. |
---|
194 | '</div>'; |
---|
195 | } |
---|
196 | |
---|
197 | private static function period($t,$p) |
---|
198 | { |
---|
199 | $pat = '%Y-%m-%d %H:%M:%S'; |
---|
200 | switch($p) { |
---|
201 | case 'day': return |
---|
202 | "AND $t > TIMESTAMP '".dt::str($pat,time() - 3600*24)."' "; break; |
---|
203 | case 'week': return |
---|
204 | "AND $t > TIMESTAMP '".dt::str($pat,time() - 3600*24*7)."' "; break; |
---|
205 | case 'month': return |
---|
206 | "AND $t > TIMESTAMP '".dt::str($pat,time() - 3600*24*30)."' "; break; |
---|
207 | case 'year': return |
---|
208 | "AND $t > TIMESTAMP '".dt::str($pat,time() - 3600*24*30*12)."' "; break; |
---|
209 | } |
---|
210 | return ''; |
---|
211 | } |
---|
212 | } |
---|
213 | ?> |
---|