Dotclear

source: inc/admin/lib.pager.php @ 326:3138de12835e

Revision 326:3138de12835e, 13.8 KB checked in by Tomtom33 <tbouron@…>, 14 years ago (diff)

Let's start fresh for a better world

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2010 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14class adminGenericColumn
15{
16     protected $core;
17     protected $rs;
18     protected $id;
19     protected $title;
20     protected $callback;
21     protected $html;
22     protected $visibility;
23     
24     public function __construct($id,$title,$callback,$html = null)
25     {
26          if (!is_string($id) || $id === '') {
27               throw new Exception(__('Invalid column ID'));
28          }
29         
30          if (!is_string($title)) {
31               throw new Exception(__('Invalid column title'));
32          }
33         
34          if (is_null($html) || !is_string($html)) {
35               $html = '';
36          }
37          if (!empty($html)) {
38               $html = ' '.$html;
39          }
40         
41          try {
42               if (!is_array($callback) || count($callback) < 2) {
43                    throw new Exception(__('Callback parameter should be an array'));
44               }
45               $r = new ReflectionClass($callback[0]);
46               $f = $r->getMethod($callback[1]);
47               $p = $r->getParentClass();
48               if (!$p || $p->name != 'adminGenericList') {
49                    throw new Exception(__('Callback class should be inherited of adminGenericList class'));
50               }
51          }
52          catch (Exception $e) {
53               throw new Exception(sprintf(__('Invalid column callback: %s'),$e->getMessage()));
54          }
55         
56          $this->id = $id;
57          $this->title = $title;
58          $this->callback = $callback;
59          $this->html = $html;
60          $this->visibility = true;
61     }
62     
63     public function getInfo($k)
64     {
65          return property_exists(get_class($this),$k) ? $this->{$k} : null;
66     }
67     
68     public function setVisibility($visibility)
69     {
70          if (is_bool($visibility)) {
71               $this->visibility = $visibility;
72          }
73     }
74     
75     public function isVisible()
76     {
77          return $this->visibility;
78     }
79}
80
81class adminGenericList
82{
83     protected $core;
84     protected $rs;
85     protected $rs_count;
86     protected $columns;
87     
88     public function __construct($core,$rs,$rs_count)
89     {
90          $this->core =& $core;
91          $this->rs =& $rs;
92          $this->rs_count = $rs_count;
93          $this->context = get_class($this);
94          $this->columns = array();
95          $this->form_prefix = 'col_%s';
96          $this->form_trigger = 'add_filter';
97         
98          $this->html_prev = __('&#171;prev.');
99          $this->html_next = __('next&#187;');
100         
101          # Post columns
102          $this->addColumn('adminPostList','title',__('Title'),array('adminPostList','getTitle'));
103          $this->addColumn('adminPostList','date',__('Date'),array('adminPostList','getDate'));
104          $this->addColumn('adminPostList','category',__('Category'),array('adminPostList','getCategory'));
105          $this->addColumn('adminPostList','author',__('Author'),array('adminPostList','getAuthor'));
106          $this->addColumn('adminPostList','comment',__('Comments'),array('adminPostList','getComments'));
107          $this->addColumn('adminPostList','trackback',__('Trackbacks'),array('adminPostList','getTrackbacks'));
108          $this->addColumn('adminPostList','status',__('Status'),array('adminPostList','getStatus'));
109         
110          # Post (mini list) columns
111          $this->addColumn('adminPostMiniList','title',__('Title'),array('adminPostList','getTitle'));
112          $this->addColumn('adminPostMiniList','date',__('Date'),array('adminPostList','getDate'));
113          $this->addColumn('adminPostMiniList','author',__('Author'),array('adminPostList','getAuthor'));
114          $this->addColumn('adminPostMiniList','status',__('Status'),array('adminPostList','getStatus'));
115         
116          # Comment columns
117          $this->addColumn('adminCommentList','title',__('Title'),array('adminCommentList','getTitle'));
118          $this->addColumn('adminCommentList','date',__('Date'),array('adminCommentList','getDate'));
119          $this->addColumn('adminCommentList','author',__('Author'),array('adminCommentList','getAuthor'));
120          $this->addColumn('adminCommentList','type',__('Type'),array('adminCommentList','getType'));
121          $this->addColumn('adminCommentList','status',__('Status'),array('adminCommentList','getStatus'));
122          $this->addColumn('adminCommentList','edit','',array('adminCommentList','getEdit'));
123         
124          # User columns
125          $this->addColumn('adminUserList','username',__('Username'),array('adminUserList','getUserName'));
126          $this->addColumn('adminUserList','firstname',__('First name'),array('adminUserList','getFirstName'));
127          $this->addColumn('adminUserList','lastname',__('Last name'),array('adminUserList','getLastName'));
128          $this->addColumn('adminUserList','displayname',__('Display name'),array('adminUserList','getDisplayName'));
129          $this->addColumn('adminUserList','entries',__('Entries'),array('adminUserList','getEntries'));
130         
131          $core->callBehavior('adminGenericListConstruct',$this);
132         
133          $this->setColumnsVisibility();
134     }
135     
136     public function addColumn($context,$id,$title,$callback,$html = null)
137     {
138          try {
139               if (!array_key_exists($context,$this->columns)) {
140                    $this->columns[$context] = array();
141               }
142               
143               $c = new adminGenericColumn($id,$title,$callback,$html);
144               $this->columns[$context][$c->getInfo('id')] = $c;
145          }
146          catch (Exception $e) {
147               if (DC_DEBUG) {
148                    $this->core->error->add($e->getMessage());
149               }
150          }
151     }
152     
153     public function setColumnsVisibility()
154     {
155          $ws = $this->core->auth->user_prefs->addWorkspace('lists');
156         
157          $user_pref = !is_null($ws->{$this->context}) ? unserialize($ws->{$this->context}) : array();
158         
159          foreach ($this->columns[$this->context] as $k => $v) {
160               $visibility =  array_key_exists($k,$user_pref) ? $user_pref[$k] : true;
161               if (array_key_exists($this->form_trigger,$_REQUEST)) {
162                    $key = sprintf($this->form_prefix,$k);
163                    $visibility = !array_key_exists($key,$_REQUEST) ? false : true;
164               }
165               $v->setVisibility($visibility);
166               $user_pref[$k] = $visibility;
167          }
168         
169          if (array_key_exists($this->form_trigger,$_REQUEST)) {
170               $this->core->auth->user_prefs->lists->put($this->context,serialize($user_pref),'string');
171          }
172     }
173     
174     public function getColumnsForm()
175     {
176          $block = 
177          '<h3>'.__('Displayed information').'</h3>'.
178          '<ul>%s</ul>';
179         
180          $list = array();
181         
182          foreach ($this->columns[$this->context] as $k => $v) {
183               $col_id = sprintf($this->form_prefix,$k);
184               $col_label = sprintf('<label for="%s">%s</label>',$col_id,$v->getInfo('title'));
185               $col_html = sprintf('<li class="line">%s</li>',$col_label.form::checkbox($col_id,1,$v->isVisible()));
186               
187               array_push($list,$col_html);
188          }
189         
190          return !empty($list) ? sprintf($block,implode('',$list)) : '';
191     }
192     
193     public function display($page,$nb_per_page,$enclose_block='')
194     {
195          if ($this->rs->isEmpty())
196          {
197               echo '<p><strong>'.__('No entry').'</strong></p>';
198          }
199          else
200          {
201               $pager = new pager($page,$this->rs_count,$nb_per_page,10);
202               $pager->html_prev = $this->html_prev;
203               $pager->html_next = $this->html_next;
204               $pager->var_page = 'page';
205               
206               $html_block = '<table class="maximal clear"><tr>';
207               
208               foreach ($this->columns[$this->context] as $k => $v) {
209                    if ($v->isVisible()) {
210                         $html_block .= sprintf('<th%s>%s</th>',$v->getInfo('html'),$v->getInfo('title'));
211                    }
212               }
213               
214               $html_block .= '</tr>%s</table>';
215               
216               if ($enclose_block) {
217                    $html_block = sprintf($enclose_block,$html_block);
218               }
219               
220               echo '<p>'.__('Page(s)').' : '.$pager->getLinks().'</p>';
221               
222               $blocks = explode('%s',$html_block);
223               
224               echo $blocks[0];
225               
226               while ($this->rs->fetch())
227               {
228                    echo $this->displayLine();
229               }
230               
231               echo $blocks[1];
232               
233               echo '<p>'.__('Page(s)').' : '.$pager->getLinks().'</p>';
234          }
235     }
236     
237     private function displayLine()
238     {
239          $res = '';
240         
241          foreach ($this->columns[$this->context] as $k => $v) {
242               if ($v->isVisible()) {
243                    $c = $v->getInfo('callback');
244                    $func = $c[1];
245                    $res .= $this->{$c[1]}();
246               }
247          }
248         
249          return sprintf($this->getDefaultLine(),$res);
250     }
251     
252     protected function getDefaultLine()
253     {
254          return '<tr class="line">%s</tr>';
255     }
256}
257
258class adminPostList extends adminGenericList
259{
260     protected function getDefaultLine()
261     {
262          return
263          '<tr class="line'.($this->rs->post_status != 1 ? ' offline' : '').'"'.
264          ' id="p'.$this->rs->post_id.'">%s</tr>';
265     }
266     
267     protected function getTitle()
268     {
269          return
270          '<td class="maximal">'.
271          form::checkbox(array('entries[]'),$this->rs->post_id,'','','',!$this->rs->isEditable()).'&nbsp'.
272          '<a href="'.$this->core->getPostAdminURL($this->rs->post_type,$this->rs->post_id).'">'.
273          html::escapeHTML($this->rs->post_title).'</a></td>';
274     }
275     
276     protected function getDate()
277     {
278          return '<td class="nowrap">'.dt::dt2str(__('%Y-%m-%d %H:%M'),$this->rs->post_dt).'</td>';
279     }
280     
281     protected function getCategory()
282     {
283          if ($this->core->auth->check('categories',$this->core->blog->id)) {
284               $cat_link = '<a href="category.php?id=%s">%s</a>';
285          } else {
286               $cat_link = '%2$s';
287          }
288         
289          if ($this->rs->cat_title) {
290               $cat_title = sprintf($cat_link,$this->rs->cat_id,
291               html::escapeHTML($this->rs->cat_title));
292          } else {
293               $cat_title = __('None');
294          }
295         
296          return '<td class="nowrap">'.$cat_title.'</td>';
297     }
298     
299     protected function getAuthor()
300     {
301          return '<td class="nowrap">'.$this->rs->user_id.'</td>';
302     }
303     
304     protected function getComments()
305     {
306          return '<td class="nowrap">'.$this->rs->nb_comment.'</td>';
307     }
308     
309     protected function getTrackbacks()
310     {
311          return '<td class="nowrap">'.$this->rs->nb_trackback.'</td>';
312     }
313     
314     protected function getStatus()
315     {
316          $img = '<img alt="%1$s" title="%1$s" src="images/%2$s" />';
317          switch ($this->rs->post_status) {
318               case 1:
319                    $img_status = sprintf($img,__('published'),'check-on.png');
320                    break;
321               case 0:
322                    $img_status = sprintf($img,__('unpublished'),'check-off.png');
323                    break;
324               case -1:
325                    $img_status = sprintf($img,__('scheduled'),'scheduled.png');
326                    break;
327               case -2:
328                    $img_status = sprintf($img,__('pending'),'check-wrn.png');
329                    break;
330          }
331         
332          $protected = '';
333          if ($this->rs->post_password) {
334               $protected = sprintf($img,__('protected'),'locker.png');
335          }
336         
337          $selected = '';
338          if ($this->rs->post_selected) {
339               $selected = sprintf($img,__('selected'),'selected.png');
340          }
341         
342          $attach = '';
343          $nb_media = $this->rs->countMedia();
344          if ($nb_media > 0) {
345               $attach_str = $nb_media == 1 ? __('%d attachment') : __('%d attachments');
346               $attach = sprintf($img,sprintf($attach_str,$nb_media),'attach.png');
347          }
348         
349          return '<td class="nowrap status">'.$img_status.' '.$selected.' '.$protected.' '.$attach.'</td>';
350     }
351}
352
353class adminPostMiniList extends adminPostList
354{
355     protected function getTitle()
356     {
357          return
358          '<td class="maximal">'.
359          form::checkbox(array('entries[]'),$this->rs->post_id,'','','',!$this->rs->isEditable()).'&nbsp'.
360          '<a href="'.$this->core->getPostAdminURL($this->rs->post_type,$this->rs->post_id).'" '.
361          'title="'.html::escapeHTML($this->rs->getURL()).'">'.
362          html::escapeHTML($this->rs->post_title).'</a></td>';
363     }
364}
365
366class adminCommentList extends adminGenericList
367{
368     protected function getDefaultLine()
369     {
370          return
371          '<tr class="line'.($this->rs->comment_status != 1 ? ' offline' : '').'"'.
372          ' id="c'.$this->rs->comment_id.'">%s</tr>';
373     }
374     
375     protected function getTitle()
376     {
377          $post_url = $this->core->getPostAdminURL($this->rs->post_type,$this->rs->post_id);
378         
379          return
380          '<td class="maximal">'.
381          form::checkbox(array('comments[]'),$this->rs->comment_id,'','','',0).'&nbsp'.
382          '<a href="'.$post_url.'">'.
383          html::escapeHTML($this->rs->post_title).'</a>'.
384          ($this->rs->post_type != 'post' ? ' ('.html::escapeHTML($this->rs->post_type).')' : '').'</td>';
385     }
386     
387     protected function getDate()
388     {
389          return '<td class="nowrap">'.dt::dt2str(__('%Y-%m-%d %H:%M'),$this->rs->comment_dt).'</td>';
390     }
391     
392     protected function getAuthor()
393     {
394          global $author, $status, $sortby, $order, $nb_per_page;
395         
396          $author_url =
397          'comments.php?n='.$nb_per_page.
398          '&amp;status='.$status.
399          '&amp;sortby='.$sortby.
400          '&amp;order='.$order.
401          '&amp;author='.rawurlencode($this->rs->comment_author);
402         
403          $comment_author = html::escapeHTML($this->rs->comment_author);
404          if (mb_strlen($comment_author) > 20) {
405               $comment_author = mb_strcut($comment_author,0,17).'...';
406          }
407         
408          return '<td class="nowrap"><a href="'.$author_url.'">'.$comment_author.'</a></td>';
409     }
410     
411     protected function getType()
412     {
413          return '<td class="nowrap">'.($this->rs->comment_trackback ? __('trackback') : __('comment')).'</td>';
414     }
415     
416     protected function getStatus()
417     {
418          $img = '<img alt="%1$s" title="%1$s" src="images/%2$s" />';
419          switch ($this->rs->comment_status) {
420               case 1:
421                    $img_status = sprintf($img,__('published'),'check-on.png');
422                    break;
423               case 0:
424                    $img_status = sprintf($img,__('unpublished'),'check-off.png');
425                    break;
426               case -1:
427                    $img_status = sprintf($img,__('pending'),'check-wrn.png');
428                    break;
429               case -2:
430                    $img_status = sprintf($img,__('junk'),'junk.png');
431                    break;
432          }
433         
434          return '<td class="nowrap status">'.$img_status.'</td>';
435     }
436     
437     protected function getEdit()
438     {
439          $comment_url = 'comment.php?id='.$this->rs->comment_id;
440         
441          return
442          '<td class="nowrap status"><a href="'.$comment_url.'">'.
443          '<img src="images/edit-mini.png" alt="" title="'.__('Edit this comment').'" /></a></td>';
444     }
445}
446
447class adminUserList extends adminGenericList
448{
449     protected function getUserName()
450     {
451          $img = '<img alt="%1$s" title="%1$s" src="images/%2$s" />';
452          $img_status = '';
453         
454          $p = $this->core->getUserPermissions($this->rs->user_id);
455         
456          if (isset($p[$this->core->blog->id]['p']['admin'])) {
457               $img_status = sprintf($img,__('admin'),'admin.png');
458          }
459          if ($this->rs->user_super) {
460               $img_status = sprintf($img,__('superadmin'),'superadmin.png');
461          }
462         
463          return
464          '<td class="maximal">'.form::hidden(array('nb_post[]'),(integer) $this->rs->nb_post).
465          form::checkbox(array('user_id[]'),$this->rs->user_id).'&nbsp;'.
466          '<a href="user.php?id='.$this->rs->user_id.'">'.
467          $this->rs->user_id.'</a>&nbsp;'.$img_status.'</td>';
468     }
469     
470     protected function getFirstName()
471     {
472          return '<td class="nowrap">'.$this->rs->user_firstname.'</td>';
473     }
474     
475     protected function getLastName()
476     {
477          return '<td class="nowrap">'.$this->rs->user_name.'</td>';
478     }
479     
480     protected function getDisplayName()
481     {
482          return '<td class="nowrap">'.$this->rs->user_displayname.'</td>';
483     }
484     
485     protected function getEntries()
486     {
487          return
488          '<td class="nowrap"><a href="posts.php?user_id='.$this->rs->user_id.'">'.
489          $this->rs->nb_post.'</a></td>';
490     }
491}
492
493?>
Note: See TracBrowser for help on using the repository browser.

Sites map