Dotclear

source: plugins/topWriter/_widgets.php @ 3257

Revision 3257, 8.2 KB checked in by JcDenis, 10 years ago (diff)

Switch to DC 2.6, add widgets options, clean code

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3#
4# This file is part of topWriter, a plugin for Dotclear 2.
5#
6# Copyright (c) 2009-2013 Jean-Christian Denis and contributors
7# contact@jcdenis.fr http://jcd.lv
8#
9# Licensed under the GPL version 2.0 license.
10# A copy of this license is available in LICENSE file or at
11# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
12#
13# -- END LICENSE BLOCK ------------------------------------
14
15if (!defined('DC_RC_PATH')) {
16
17     return null;
18}
19
20$core->addBehavior('initWidgets', array('topWriterWidget', 'init'));
21
22class topWriterWidget
23{
24     public static function init($w)
25     {
26          $w->create(
27               'topcom',
28               __('Top comments'),
29               array('topWriterWidget', 'topCom'),
30               null,
31               __('List users who write more posts')
32          );
33          $w->topcom->setting(
34               'title',
35               __('Title:'),
36               __('Top comments'),
37               'text'
38          );
39          $w->topcom->setting(
40               'text',
41               __('Text:'),
42               '%author% (%count%)',
43               'text'
44          );
45          $w->topcom->setting(
46               'period',
47               __('Period:'),
48               'year',
49               'combo',
50               array(
51                    __('day')           => 'day',
52                    __('week')          => 'week',
53                    __('month')         => 'month',
54                    __('year')          => 'year',
55                    __('from begining') => ''
56               )
57          );
58          $w->topcom->setting(
59               'sort',
60               __('Sort:'),
61               'desc',
62               'combo',
63               array(
64                    __('Ascending')     => 'asc',
65                    __('Descending')    => 'desc'
66               )
67          );
68          $w->topcom->setting(
69               'limit',
70               __('Limit:'),
71               '10',
72               'text'
73          );
74          $w->topcom->setting(
75               'exclude',
76               __('Exclude post writer from list'),
77               0,
78               'check'
79          );
80          $w->topcom->setting(
81               'content_only',
82               __('Content only'),
83               0,
84               'check'
85          );
86          $w->topcom->setting(
87               'class',
88               __('CSS class:'),
89               ''
90          );
91          $w->topcom->setting(
92               'homeonly',
93               __('Display on:'),
94               1,
95               'combo',
96               array(
97                    __('All pages')               => 0,
98                    __('Home page only')          => 1,
99                    __('Except on home page')     => 2
100               )
101          );
102
103          $w->create(
104               'toppost',
105               __('Top entries'),
106               array('topWriterWidget', 'topPost'),
107               null,
108               __('List users who write more comments')
109          );
110          $w->toppost->setting(
111               'title',
112               __('Title:'),
113               __('Top entries'),
114               'text'
115          );
116          $w->toppost->setting(
117               'text',
118               __('Text:'),
119               '%author% (%count%)',
120               'text'
121          );
122          $w->toppost->setting(
123               'period',
124               __('Period:'),
125               'year',
126               'combo',
127               array(
128                    __('day')           => 'day',
129                    __('week')          => 'week',
130                    __('month')         => 'month',
131                    __('year')          => 'year',
132                    __('from begining') => ''
133               )
134          );
135          $w->toppost->setting(
136               'sort',
137               __('Sort:'),'desc',
138               'combo',
139               array(
140                    __('Ascending')     => 'asc',
141                    __('Descending')    => 'desc'
142               )
143          );
144          $w->toppost->setting(
145               'limit',
146               __('Limit:'),
147               '10',
148               'text'
149          );
150          $w->toppost->setting(
151               'content_only',
152               __('Content only'),
153               0,
154               'check'
155          );
156          $w->toppost->setting(
157               'class',
158               __('CSS class:'),
159               ''
160          );
161          $w->toppost->setting(
162               'homeonly',
163               __('Display on:'),
164               1,
165               'combo',
166               array(
167                    __('All pages')               => 0,
168                    __('Home page only')          => 1,
169                    __('Except on home page')     => 2
170               )
171          );
172     }
173
174     public static function topCom($w)
175     {
176          global $core;
177
178          if ($w->homeonly == 1 && $core->url->type != 'default'
179           || $w->homeonly == 2 && $core->url->type == 'default'
180          ) {
181               return null;
182          }
183
184          $req =
185          'SELECT COUNT(*) AS count, comment_email '.
186          "FROM ".$core->prefix."post P,  ".$core->prefix."comment C ".
187          'WHERE P.post_id=C.post_id '.
188          "AND blog_id='".$core->con->escape($core->blog->id)."' ".
189          'AND post_status=1 AND comment_status=1 '.
190          self::period('comment_dt',$w->period);
191
192          if ($w->exclude) {
193               $req .= 
194               'AND comment_email NOT IN ('.
195               ' SELECT U.user_email '.
196               ' FROM '.$core->prefix.'user U'.
197               ' INNER JOIN '.$core->prefix.'post P ON P.user_id = U.user_id '.
198               " WHERE blog_id='".$core->con->escape($core->blog->id)."' ".
199               ' GROUP BY U.user_email) ';
200          }
201
202          $req .=
203          'GROUP BY comment_email '.
204          'ORDER BY count '.($w->sort == 'asc' ? 'ASC' : 'DESC').' '.
205          $core->con->limit(abs((integer) $w->limit));
206
207          $rs = $core->con->select($req);
208
209          if ($rs->isEmpty()) {
210
211               return null;
212          }
213
214          $content = '';
215          $i = 0;
216          while($rs->fetch()) {
217               $user = $core->con->select(
218                    "SELECT * FROM ".$core->prefix."comment ".
219                    "WHERE comment_email='".$rs->comment_email."' ".
220                    'ORDER BY comment_dt DESC'
221               );
222
223               if (!$user->comment_author) {
224                    continue;
225               }
226
227               $i++;
228               $rank = '<span class="topcomments-rank">'.$i.'</span>';
229
230               if ($user->comment_site) {
231                    $author = '<a href="'.$user->comment_site.'" title="'.
232                         __('Author link').'">'.$user->comment_author.'</a>';
233               }
234               else {
235                    $author = $user->comment_author;
236               }
237               $author = '<span class="topcomments-author">'.$author.'</span>';
238
239               if ($rs->count == 0) {
240                    $count = __('no comment');
241               }
242               else {
243                    $count = sprintf(__('one comment', '%s comments', $rs->count), $rs->count);
244               }
245
246               $content .= '<li>'.str_replace(
247                    array('%rank%', '%author%', '%count%'),
248                    array($rank, $author, $count),
249                    $w->text
250               ).'</li>';
251          }
252
253          if ($i < 1) {
254
255               return null;
256          }
257
258          return 
259          ($w->content_only ? '' : '<div class="topcomments'.
260          ($w->class ? ' '.html::escapeHTML($w->class) : '').'"">').
261          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
262          '<ul>'.$content.'</ul>'.
263          ($w->content_only ? '' : '</div>');
264     }
265     
266     public static function topPost($w)
267     {
268          global $core;
269
270          if ($w->homeonly == 1 && $core->url->type != 'default'
271           || $w->homeonly == 2 && $core->url->type == 'default'
272          ) {
273               return null;
274          }
275
276          $rs = $core->con->select(
277          'SELECT COUNT(*) AS count, U.user_id '.
278          "FROM ".$core->prefix."post P ".
279          'INNER JOIN '.$core->prefix.'user U ON U.user_id = P.user_id '.
280          "WHERE blog_id='".$core->con->escape($core->blog->id)."' ".
281          'AND post_status=1 AND user_status=1 '.
282          self::period('post_dt',$w->period).
283          'GROUP BY U.user_id '.
284          'ORDER BY count '.($w->sort == 'asc' ? 'ASC' : 'DESC').', U.user_id ASC '.
285          $core->con->limit(abs((integer) $w->limit)));
286
287          if ($rs->isEmpty()) {
288
289               return null;
290          }
291
292          $content = '';
293          $i = 0;
294          while($rs->fetch()) {
295               $user = $core->con->select(
296                    "SELECT * FROM ".$core->prefix."user WHERE user_id='".$rs->user_id."' "
297               );
298
299               $author = dcUtils::getUserCN($user->user_id,$user->user_name,
300                    $user->user_firstname,$user->user_displayname);
301
302               if (empty($author)) {
303                    continue;
304               }
305
306               $i++;
307               $rank = '<span class="topentries-rank">'.$i.'</span>';
308
309               $core->blog->settings->addNamespace('authormode');
310               if ($core->blog->settings->authormode->authormode_active) {
311                    $author = '<a href="'.
312                         $core->blog->url.$core->url->getBase("author").'/'.$user->user_id.'" '.
313                         'title="'.__('Author posts').'">'.$author.'</a>';
314               }
315               elseif ($user->user_url) {
316                    $author = '<a href="'.$user->user_url.'" title="'.
317                         __('Author link').'">'.$author.'</a>';
318               }
319               $author = '<span class="topentries-author">'.$author.'</span>';
320
321               if ($rs->count == 0) {
322                    $count = __('no post');
323               }
324               else {
325                    $count = sprintf(__('one post', '%s posts', $rs->count), $rs->count);
326               }
327
328               $content .= '<li>'.str_replace(
329                    array('%rank%', '%author%', '%count%'),
330                    array($rank, $author, $count),
331                    $w->text
332               ).'</li>';
333          }
334
335          if ($i < 1) {
336
337               return null;
338          }
339
340          return 
341          ($w->content_only ? '' : '<div class="topentries'.
342          ($w->class ? ' '.html::escapeHTML($w->class) : '').'"">').
343          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
344          '<ul>'.$content.'</ul>'.
345          ($w->content_only ? '' : '</div>');
346     }
347
348     private static function period($t,$p)
349     {
350          $pat = '%Y-%m-%d %H:%M:%S';
351          switch($p) {
352               case 'day':
353
354               return
355               "AND $t > TIMESTAMP '".dt::str($pat, time() - 3600*24)."' ";
356               break;
357               
358               case 'week':
359
360               return 
361               "AND $t > TIMESTAMP '".dt::str($pat, time() - 3600*24*7)."' ";
362               break;
363
364               case 'month':
365
366               return
367               "AND $t > TIMESTAMP '".dt::str($pat, time() - 3600*24*30)."' ";
368               break;
369
370               case 'year':
371
372               return
373               "AND $t > TIMESTAMP '".dt::str($pat, time() - 3600*24*30*12)."' ";
374               break;
375          }
376
377          return '';
378     }
379}
Note: See TracBrowser for help on using the repository browser.

Sites map