Dotclear

source: inc/admin/lib.pager.php @ 246:b59b563d221b

Revision 246:b59b563d221b, 13.5 KB checked in by Tomtom33 <tbouron@…>, 14 years ago (diff)

Saved list' displayed information using user preferences

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          $ws = $this->core->auth->user_prefs->addWorkspace('lists');
162         
163          $user_pref = !is_null($ws->{$this->context}) ? unserialize($ws->{$this->context}) : array();
164         
165          foreach ($this->columns[$this->context] as $k => $v) {
166               $visibility =  array_key_exists($k,$user_pref) ? $user_pref[$k] : true;
167               if (array_key_exists($this->form_trigger,$_REQUEST)) {
168                    $key = sprintf($this->form_prefix,$k);
169                    $visibility = !array_key_exists($key,$_REQUEST) ? false : true;
170               }
171               $v->setVisibility($visibility);
172               $user_pref[$k] = $visibility;
173          }
174         
175          if (array_key_exists($this->form_trigger,$_REQUEST)) {
176               $this->core->auth->user_prefs->lists->put($this->context,serialize($user_pref),'string');
177          }
178     }
179     
180     public function getColumnsForm()
181     {
182          $block = 
183          '<h3>'.__('Displayed information').'</h3>'.
184          '<ul>%s</ul>';
185         
186          $list = array();
187         
188          foreach ($this->columns[$this->context] as $k => $v) {
189               $col_id = sprintf($this->form_prefix,$k);
190               $col_label = sprintf('<label for="%s">%s</label>',$col_id,$v->getInfo('title'));
191               $col_html = sprintf('<li class="line">%s</li>',$col_label.form::checkbox($col_id,1,$v->isVisible()));
192               
193               array_push($list,$col_html);
194          }
195         
196          return !empty($list) ? sprintf($block,implode('',$list)) : '';
197     }
198     
199     public function display($page,$nb_per_page,$enclose_block='')
200     {
201          if ($this->rs->isEmpty())
202          {
203               echo '<p><strong>'.__('No entry').'</strong></p>';
204          }
205          else
206          {
207               $pager = new pager($page,$this->rs_count,$nb_per_page,10);
208               $pager->html_prev = $this->html_prev;
209               $pager->html_next = $this->html_next;
210               $pager->var_page = 'page';
211               
212               $html_block = '<table class="clear"><tr>';
213               
214               foreach ($this->columns[$this->context] as $k => $v) {
215                    if ($v->isVisible()) {
216                         $html_block .= sprintf('<th%s>%s</th>',$v->getInfo('html'),$v->getInfo('title'));
217                    }
218               }
219               
220               $html_block .= '</tr>%s</table>';
221               
222               if ($enclose_block) {
223                    $html_block = sprintf($enclose_block,$html_block);
224               }
225               
226               echo '<p>'.__('Page(s)').' : '.$pager->getLinks().'</p>';
227               
228               $blocks = explode('%s',$html_block);
229               
230               echo $blocks[0];
231               
232               while ($this->rs->fetch())
233               {
234                    echo $this->displayLine();
235               }
236               
237               echo $blocks[1];
238               
239               echo '<p>'.__('Page(s)').' : '.$pager->getLinks().'</p>';
240          }
241     }
242     
243     private function displayLine()
244     {
245          $res = '';
246         
247          foreach ($this->columns[$this->context] as $k => $v) {
248               if ($v->isVisible()) {
249                    $c = $v->getInfo('callback');
250                    $func = $c[1];
251                    $res .= $this->{$c[1]}();
252               }
253          }
254         
255          return sprintf($this->getDefaultLine(),$res);
256     }
257     
258     protected function getDefaultLine()
259     {
260          return '<tr class="line">%s</tr>';
261     }
262}
263
264class adminPostList extends adminGenericList
265{
266     protected function getDefaultLine()
267     {
268          return
269          '<tr class="line'.($this->rs->post_status != 1 ? ' offline' : '').'"'.
270          ' id="p'.$this->rs->post_id.'">%s</tr>';
271     }
272     
273     protected function getTitle()
274     {
275          return
276          '<td class="maximal">'.
277          form::checkbox(array('entries[]'),$this->rs->post_id,'','','',!$this->rs->isEditable()).'&nbsp'.
278          '<a href="'.$this->core->getPostAdminURL($this->rs->post_type,$this->rs->post_id).'">'.
279          html::escapeHTML($this->rs->post_title).'</a></td>';
280     }
281     
282     protected function getDate()
283     {
284          return '<td class="nowrap">'.dt::dt2str(__('%Y-%m-%d %H:%M'),$this->rs->post_dt).'</td>';
285     }
286     
287     protected function getCategory()
288     {
289          if ($this->core->auth->check('categories',$this->core->blog->id)) {
290               $cat_link = '<a href="category.php?id=%s">%s</a>';
291          } else {
292               $cat_link = '%2$s';
293          }
294         
295          if ($this->rs->cat_title) {
296               $cat_title = sprintf($cat_link,$this->rs->cat_id,
297               html::escapeHTML($this->rs->cat_title));
298          } else {
299               $cat_title = __('None');
300          }
301         
302          return '<td class="nowrap">'.$cat_title.'</td>';
303     }
304     
305     protected function getAuthor()
306     {
307          return '<td class="nowrap">'.$this->rs->user_id.'</td>';
308     }
309     
310     protected function getComments()
311     {
312          return '<td class="nowrap">'.$this->rs->nb_comment.'</td>';
313     }
314     
315     protected function getTrackbacks()
316     {
317          return '<td class="nowrap">'.$this->rs->nb_trackback.'</td>';
318     }
319     
320     protected function getStatus()
321     {
322          $img = '<img alt="%1$s" title="%1$s" src="images/%2$s" />';
323          switch ($this->rs->post_status) {
324               case 1:
325                    $img_status = sprintf($img,__('published'),'check-on.png');
326                    break;
327               case 0:
328                    $img_status = sprintf($img,__('unpublished'),'check-off.png');
329                    break;
330               case -1:
331                    $img_status = sprintf($img,__('scheduled'),'scheduled.png');
332                    break;
333               case -2:
334                    $img_status = sprintf($img,__('pending'),'check-wrn.png');
335                    break;
336          }
337         
338          $protected = '';
339          if ($this->rs->post_password) {
340               $protected = sprintf($img,__('protected'),'locker.png');
341          }
342         
343          $selected = '';
344          if ($this->rs->post_selected) {
345               $selected = sprintf($img,__('selected'),'selected.png');
346          }
347         
348          $attach = '';
349          $nb_media = $this->rs->countMedia();
350          if ($nb_media > 0) {
351               $attach_str = $nb_media == 1 ? __('%d attachment') : __('%d attachments');
352               $attach = sprintf($img,sprintf($attach_str,$nb_media),'attach.png');
353          }
354         
355          return '<td class="nowrap status">'.$img_status.' '.$selected.' '.$protected.' '.$attach.'</td>';
356     }
357}
358
359class adminPostMiniList extends adminPostList{}
360
361class adminCommentList extends adminGenericList
362{
363     protected function getDefaultLine()
364     {
365          return
366          '<tr class="line'.($this->rs->comment_status != 1 ? ' offline' : '').'"'.
367          ' id="c'.$this->rs->comment_id.'">%s</tr>';
368     }
369     
370     protected function getTitle()
371     {
372          $post_url = $this->core->getPostAdminURL($this->rs->post_type,$this->rs->post_id);
373         
374          return
375          '<td class="maximal">'.
376          form::checkbox(array('comments[]'),$this->rs->comment_id,'','','',0).'&nbsp'.
377          '<a href="'.$post_url.'">'.
378          html::escapeHTML($this->rs->post_title).'</a>'.
379          ($this->rs->post_type != 'post' ? ' ('.html::escapeHTML($this->rs->post_type).')' : '').'</td>';
380     }
381     
382     protected function getDate()
383     {
384          return '<td class="nowrap">'.dt::dt2str(__('%Y-%m-%d %H:%M'),$this->rs->comment_dt).'</td>';
385     }
386     
387     protected function getAuthor()
388     {
389          global $author, $status, $sortby, $order, $nb_per_page;
390         
391          $author_url =
392          'comments.php?n='.$nb_per_page.
393          '&amp;status='.$status.
394          '&amp;sortby='.$sortby.
395          '&amp;order='.$order.
396          '&amp;author='.rawurlencode($this->rs->comment_author);
397         
398          $comment_author = html::escapeHTML($this->rs->comment_author);
399          if (mb_strlen($comment_author) > 20) {
400               $comment_author = mb_strcut($comment_author,0,17).'...';
401          }
402         
403          return '<td class="nowrap"><a href="'.$author_url.'">'.$comment_author.'</a></td>';
404     }
405     
406     protected function getType()
407     {
408          return '<td class="nowrap">'.($this->rs->comment_trackback ? __('trackback') : __('comment')).'</td>';
409     }
410     
411     protected function getStatus()
412     {
413          $img = '<img alt="%1$s" title="%1$s" src="images/%2$s" />';
414          switch ($this->rs->comment_status) {
415               case 1:
416                    $img_status = sprintf($img,__('published'),'check-on.png');
417                    break;
418               case 0:
419                    $img_status = sprintf($img,__('unpublished'),'check-off.png');
420                    break;
421               case -1:
422                    $img_status = sprintf($img,__('pending'),'check-wrn.png');
423                    break;
424               case -2:
425                    $img_status = sprintf($img,__('junk'),'junk.png');
426                    break;
427          }
428         
429          return '<td class="nowrap status">'.$img_status.'</td>';
430     }
431     
432     protected function getEdit()
433     {
434          $comment_url = 'comment.php?id='.$this->rs->comment_id;
435         
436          return
437          '<td class="nowrap status"><a href="'.$comment_url.'">'.
438          '<img src="images/edit-mini.png" alt="" title="'.__('Edit this comment').'" /></a></td>';
439     }
440}
441
442class adminUserList extends adminGenericList
443{
444     protected function getUserName()
445     {
446          $img = '<img alt="%1$s" title="%1$s" src="images/%2$s" />';
447          $img_status = '';
448         
449          $p = $this->core->getUserPermissions($this->rs->user_id);
450         
451          if (isset($p[$this->core->blog->id]['p']['admin'])) {
452               $img_status = sprintf($img,__('admin'),'admin.png');
453          }
454          if ($this->rs->user_super) {
455               $img_status = sprintf($img,__('superadmin'),'superadmin.png');
456          }
457         
458          return
459          '<td class="maximal">'.form::hidden(array('nb_post[]'),(integer) $this->rs->nb_post).
460          form::checkbox(array('user_id[]'),$this->rs->user_id).'&nbsp;'.
461          '<a href="user.php?id='.$this->rs->user_id.'">'.
462          $this->rs->user_id.'</a>&nbsp;'.$img_status.'</td>';
463     }
464     
465     protected function getFirstName()
466     {
467          return '<td class="nowrap">'.$this->rs->user_firstname.'</td>';
468     }
469     
470     protected function getLastName()
471     {
472          return '<td class="nowrap">'.$this->rs->user_name.'</td>';
473     }
474     
475     protected function getDisplayName()
476     {
477          return '<td class="nowrap">'.$this->rs->user_displayname.'</td>';
478     }
479     
480     protected function getEntries()
481     {
482          return
483          '<td class="nowrap"><a href="posts.php?user_id='.$this->rs->user_id.'">'.
484          $this->rs->nb_post.'</a></td>';
485     }
486}
487
488?>
Note: See TracBrowser for help on using the repository browser.

Sites map