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('homeonly',__('Home page only'),1,'check'); |
---|
34 | |
---|
35 | $w->create('toppost',__('Top entries'), |
---|
36 | array('topWriterWidget','topPost')); |
---|
37 | $w->toppost->setting('title',__('Title:'), |
---|
38 | __('Top entries'),'text'); |
---|
39 | $w->toppost->setting('text',__('Text:'), |
---|
40 | '%author% (%count%)','text'); |
---|
41 | $w->toppost->setting('period',__('Period:'),'year','combo', |
---|
42 | array(__('day')=>'day',__('week')=>'week',__('month')=>'month', |
---|
43 | __('year')=>'year',__('from begining')=>'')); |
---|
44 | $w->toppost->setting('sort',__('Sort:'),'desc','combo',array( |
---|
45 | __('Ascending') => 'asc',__('Descending') => 'desc')); |
---|
46 | $w->toppost->setting('limit',__('Limit:'),'10','text'); |
---|
47 | $w->toppost->setting('homeonly',__('Home page only'),1,'check'); |
---|
48 | } |
---|
49 | |
---|
50 | public static function topCom(&$w) |
---|
51 | { |
---|
52 | global $core; |
---|
53 | |
---|
54 | if ($w->homeonly && $core->url->type != 'default') return; |
---|
55 | |
---|
56 | $rs = $core->con->select( |
---|
57 | 'SELECT COUNT(comment_id) AS comment_count, '. |
---|
58 | 'comment_author, comment_email, comment_site '. |
---|
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 | 'GROUP BY C.comment_email '. |
---|
65 | 'ORDER BY comment_count '.($w->sort == 'asc' ? 'ASC' : 'DESC'). |
---|
66 | ', comment_dt DESC '. |
---|
67 | $core->con->limit(abs((integer) $w->limit)) |
---|
68 | ); |
---|
69 | |
---|
70 | if ($rs->isEmpty()) return; |
---|
71 | |
---|
72 | $content = ''; |
---|
73 | $res = array(); |
---|
74 | $i = 0; |
---|
75 | while($rs->fetch()){ |
---|
76 | $i++; |
---|
77 | $rank = '<span class="topcomments-rank">'.$i.'</span>'; |
---|
78 | |
---|
79 | if ($rs->comment_site) { |
---|
80 | $author = '<a href="'.$rs->comment_site.'" title="'. |
---|
81 | __('Author link').'">'.$rs->comment_author.'</a>'; |
---|
82 | } else |
---|
83 | $author = $rs->comment_author; |
---|
84 | |
---|
85 | if ($rs->comment_count == 0) |
---|
86 | $count = __('no comment'); |
---|
87 | elseif($rs->comment_count == 1) |
---|
88 | $count = __('one comment'); |
---|
89 | else |
---|
90 | $count = sprintf(__('%s comments'),$rs->comment_count); |
---|
91 | |
---|
92 | $content .= '<li>'.str_replace( |
---|
93 | array('%rank%','%author%','%count%'), |
---|
94 | array($rank,$author,$count), |
---|
95 | $w->text |
---|
96 | ).'</li>'; |
---|
97 | } |
---|
98 | |
---|
99 | return |
---|
100 | '<div class="topcomments">'. |
---|
101 | ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). |
---|
102 | '<ul>'.$content.'</ul>'. |
---|
103 | '</div>'; |
---|
104 | } |
---|
105 | |
---|
106 | public static function topPost(&$w) |
---|
107 | { |
---|
108 | global $core; |
---|
109 | |
---|
110 | if ($w->homeonly && $core->url->type != 'default') return; |
---|
111 | |
---|
112 | $rs = $core->con->select( |
---|
113 | 'SELECT COUNT(P.post_id) AS post_count, '. |
---|
114 | 'U.user_id, U.user_name, U.user_firstname, U.user_displayname, U.user_email '. |
---|
115 | "FROM ".$core->prefix."post P ". |
---|
116 | 'INNER JOIN '.$core->prefix.'user U ON U.user_id = P.user_id '. |
---|
117 | "WHERE blog_id='".$core->con->escape($core->blog->id)."' ". |
---|
118 | 'AND post_status=1 AND user_status=1 '. |
---|
119 | self::period('post_dt',$w->period). |
---|
120 | 'GROUP BY U.user_id '. |
---|
121 | 'ORDER BY post_count '.($w->sort == 'asc' ? 'ASC' : 'DESC'). |
---|
122 | ', post_dt DESC '. |
---|
123 | $core->con->limit(abs((integer) $w->limit))); |
---|
124 | |
---|
125 | if ($rs->isEmpty()) return; |
---|
126 | |
---|
127 | $content = ''; |
---|
128 | $i = 0; |
---|
129 | while($rs->fetch()){ |
---|
130 | $i++; |
---|
131 | $rank = '<span class="topentries-rank">'.$i.'</span>'; |
---|
132 | |
---|
133 | $author = dcUtils::getUserCN($rs->user_id,$rs->user_name, |
---|
134 | $rs->user_firstname,$rs->user_displayname); |
---|
135 | |
---|
136 | if ($core->blog->settings->authormode_active) { |
---|
137 | $author = '<a href="'. |
---|
138 | $core->blog->url.$core->url->getBase("author").'/'.$rs->user_id.'" '. |
---|
139 | 'title="'.__('Author posts').'">'.$author.'</a>'; |
---|
140 | } elseif ($rs->user_url) { |
---|
141 | $author = '<a href="'.$rs->user_url.'" title="'. |
---|
142 | __('Author link').'">'.$author.'</a>'; |
---|
143 | } |
---|
144 | |
---|
145 | if ($rs->post_count == 0) |
---|
146 | $count = __('no post'); |
---|
147 | elseif($rs->post_count == 1) |
---|
148 | $count = __('one post'); |
---|
149 | else |
---|
150 | $count = sprintf(__('%s posts'),$rs->post_count); |
---|
151 | |
---|
152 | $content .= '<li>'.str_replace( |
---|
153 | array('%rank%','%author%','%count%'), |
---|
154 | array($rank,$author,$count), |
---|
155 | $w->text |
---|
156 | ).'</li>'; |
---|
157 | } |
---|
158 | |
---|
159 | return |
---|
160 | '<div class="topentries">'. |
---|
161 | ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). |
---|
162 | '<ul>'.$content.'</ul>'. |
---|
163 | '</div>'; |
---|
164 | } |
---|
165 | |
---|
166 | private static function period($t,$p) |
---|
167 | { |
---|
168 | $pat = '%Y-%m-%d %H:%M:%S'; |
---|
169 | switch($p) { |
---|
170 | case 'day': return |
---|
171 | "AND $t > TIMESTAMP '".dt::str($pat,time() - 3600*24)."' "; break; |
---|
172 | case 'week': return |
---|
173 | "AND $t > TIMESTAMP '".dt::str($pat,time() - 3600*24*7)."' "; break; |
---|
174 | case 'month': return |
---|
175 | "AND $t > TIMESTAMP '".dt::str($pat,time() - 3600*24*30)."' "; break; |
---|
176 | case 'year': return |
---|
177 | "AND $t > TIMESTAMP '".dt::str($pat,time() - 3600*24*30*12)."' "; break; |
---|
178 | } |
---|
179 | return ''; |
---|
180 | } |
---|
181 | } |
---|
182 | ?> |
---|