Dotclear

source: inc/admin/lib.pager.php @ 242:57cb73269f1e

Revision 242:57cb73269f1e, 13.1 KB checked in by Tomtom33 <tbouron@…>, 14 years ago (diff)

Makes custom lists work

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

Sites map