Dotclear

Changeset 519:c508ff281bad


Ignore:
Timestamp:
07/06/11 13:19:41 (14 years ago)
Author:
Tomtom33 <tbouron@…>
Branch:
formfilters
Message:

Cleaned and refactored list class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/admin/lib.pager.php

    r508 r519  
    1515@ingroup DC_CORE 
    1616@nosubgrouping 
    17 @brief Dotclear Pager class. 
    18  
    19 Dotclear Pager handles pagination for every admin list. 
     17@brief Dotclear items pager class. 
     18 
     19Dotclear items pager handles pagination for every admin list. 
    2020*/ 
    21 class dcPager extends pager 
     21class adminItemsPager extends pager 
    2222{ 
    2323     public function getLinks() 
     
    5656          # End 
    5757          if($this->env != $this->nb_pages) { 
    58                $htmlEnd = '<a href="'.sprintf($this->page_url,$this->nb_elements).'" class="end">'. 
     58               $htmlEnd = '<a href="'.sprintf($this->page_url,$this->nb_pages).'" class="end">'. 
    5959               $htmlEnd .= $this->html_end.'</a>'; 
    6060          } 
     
    9292@ingroup DC_CORE 
    9393@nosubgrouping 
    94 @brief Dotclear Generic column class. 
    95  
    96 Dotclear Generic column handles each column object use in adminGenericList class. 
     94@brief Dotclear items column class. 
     95 
     96Dotclear items column handles each column object use in adminItemsList class. 
    9797*/ 
    98 class adminGenericColumn 
     98class adminItemsColumn 
    9999{ 
    100100     protected $core;         /// <b>object</b> dcCore object 
     
    157157                
    158158               while (!$p) { 
    159                     if ($p->name == 'adminGenericList') { 
     159                    if ($p->name == 'adminItemsList') { 
    160160                         $find_parent = true; 
    161161                    } 
     
    166166                
    167167               if (!$p || !$f) { 
    168                     throw new Exception(__('Callback class should be inherited of adminGenericList class')); 
     168                    throw new Exception(__('Callback class should be inherited of adminItemsList class')); 
    169169               } 
    170170          } 
     
    255255@ingroup DC_CORE 
    256256@nosubgrouping 
    257 @brief abstract Generic list class. 
    258  
    259 Dotclear Generic list handles administration lists 
     257@brief abstract items list class. 
     258 
     259Dotclear items list handles administration lists 
    260260*/ 
    261 abstract class adminGenericList 
     261abstract class adminItemsList 
    262262{ 
    263263     protected $core; 
     
    265265     protected $rs_count; 
    266266     protected $columns; 
     267     protected $sortby; 
     268     protected $order; 
     269     protected $nb_per_page; 
     270     protected $page; 
    267271      
    268272     /* 
    269273     Sets columns of defined list 
    270274     */ 
    271      public function setColumns() {} 
     275     abstract function setColumns(); 
    272276      
    273277     /** 
     
    275279      
    276280     @param    core      <b>dcCore</b>       dcCore object 
    277      @param    rs        <b>recordSet</b>    Items recordSet to display 
    278      @param    rs_count  <b>int</b>          Total items number 
    279      */ 
    280      public function __construct($core,$rs,$rs_count) 
    281      { 
    282           $this->core =& $core; 
    283           $this->rs =& $rs; 
    284           $this->rs_count = $rs_count; 
     281     */ 
     282     public function __construct($core) 
     283     { 
     284           
     285          $this->core = $core; 
    285286          $this->context = get_class($this); 
    286287          $this->columns = array(); 
     
    293294          $this->html_end = __('end'); 
    294295           
     296          $this->setOptions(); 
     297           
    295298          $this->setColumns(); 
    296299           
    297           $core->callBehavior('adminListConstruct',$this); 
     300          # --BEHAVIOR-- adminItemsListConstruct 
     301          $core->callBehavior('adminItemsListConstruct',$this); 
    298302           
    299303          $this->setColumnsVisibility(); 
     304     } 
     305      
     306     /** 
     307     Apply limit, sortby and order filters to items parameters 
     308      
     309     @param    params    <b>array</b>   Items parameters 
     310      
     311     @return   <b>array</b>        Items parameters 
     312     */ 
     313     public function applyFilters($params) 
     314     { 
     315          if (!is_null($this->sortby) && !is_null($this->order)) { 
     316               foreach ($this->columns as $k => $c) { 
     317                    if ( 
     318                         $this->sortby === $c->getInfo('alias') && 
     319                         in_array($this->order,array('asc','desc')) 
     320                    ) { 
     321                         $params['order'] = $this->sortby.' '.$this->order; 
     322                         break; 
     323                    } 
     324               } 
     325          } 
     326           
     327          $params['limit'] = array((($this->page-1)*$this->nb_per_page),$this->nb_per_page); 
     328           
     329          return $params; 
     330     } 
     331      
     332     /** 
     333     Sets items and items counter 
     334      
     335     @param    rs        <b>recordSet</b>    Items recordSet to display 
     336     @param    rs_count  <b>int</b>          Total items number 
     337     */ 
     338     public function setItems($rs,$rs_count) 
     339     { 
     340          $this->rs = $rs; 
     341          $this->rs_count = $rs_count; 
    300342     } 
    301343      
     
    323365          } 
    324366           
    325           $nb_per_page = isset($_GET['nb']) ? $_GET['nb'] : 10; 
     367          $nb_per_page = !is_null($this->nb_per_page) ? $this->nb_per_page : 10; 
    326368           
    327369          return 
    328370          sprintf($block,implode('',$list)). 
    329           '<p><label for="nb">'.__('Items per page:'). 
    330           '</label>&nbsp;'.form::field('nb',3,3,$nb_per_page). 
     371          '<p><label for="nb_per_page">'.__('Items per page:'). 
     372          '</label>&nbsp;'.form::field('nb_per_page',3,3,$nb_per_page). 
    331373          '</p>'; 
    332374     } 
    333375      
    334376     /** 
     377     Returns HTML hidden fields for list options 
     378      
     379     @return   <b>string</b>       HTML hidden fields 
     380     */ 
     381     public function getFormFieldsAsHidden() 
     382     { 
     383          $res = ''; 
     384           
     385          if (!is_null($this->sortby)) { 
     386               $res .= form::hidden(array('sortby'),$this->sortby); 
     387          } 
     388          if (!is_null($this->order)) { 
     389               $res .= form::hidden(array('order'),$this->order); 
     390          } 
     391          if (!is_null($this->nb_per_page)) { 
     392               $res .= form::hidden(array('nb_per_page'),$this->nb_per_page); 
     393          } 
     394          if (!is_null($this->page)) { 
     395               $res .= form::hidden(array('page'),$this->page); 
     396          } 
     397           
     398          return $res; 
     399     } 
     400      
     401     /** 
    335402     Returns HTML code list to display 
    336403      
    337      @param    page           <b>string|int</b>   Current page 
    338      @param    nb_per_page    <b>string|int</b>   Number of items to display in each page 
    339404     @param    enclose_block  <b>string</b>       HTML wrapper of defined list 
    340405      
    341406     @return   <b>string</b>       HTML code list 
    342407     */ 
    343      public function display($page,$nb_per_page,$enclose_block = '') 
     408     public function display($enclose_block = '') 
    344409     { 
    345410          if ($this->rs->isEmpty()) 
     
    349414          else 
    350415          { 
    351                $pager = new dcPager($page,$this->rs_count,$nb_per_page,10); 
     416               $pager = new adminItemsPager($this->page,$this->rs_count,$this->nb_per_page,10); 
    352417               $pager->html_prev = $this->html_prev; 
    353418               $pager->html_next = $this->html_next; 
     
    358423               $html_block = 
    359424               '<table class="maximal clear">'. 
    360                $this->getCaption($page). 
     425               $this->getCaption($this->page). 
    361426               '<thead><tr>'; 
    362427                
     
    420485                    $html = array(); 
    421486               } 
    422                if (isset($_GET['sortby']) && $_GET['sortby'] === $alias && isset($_GET['order'])) { 
     487               if ($this->sortby === $alias && !is_null($this->order)) { 
    423488                    if (array_key_exists('class',$html)) { 
    424                          array_push($html['class'],$_GET['order']); 
     489                         array_push($html['class'],$this->order); 
    425490                    } 
    426491                    else { 
    427                          $html['class'] = array($_GET['order']); 
     492                         $html['class'] = array($this->order); 
    428493                    } 
    429494               } 
    430                $c = new adminGenericColumn($id,$alias,$title,$callback,$html,$sortable,$listable,$can_hide); 
     495               $c = new adminItemsColumn($id,$alias,$title,$callback,$html,$sortable,$listable,$can_hide); 
    431496               $this->columns[$id] = $c; 
    432497          } 
    433498          catch (Exception $e) { 
    434499               if (DC_DEBUG) { 
    435                     $this->core->error->add($e->getMessage()); 
     500                    $this->core->error->add(sprintf('[%s] %s',$id,$e->getMessage())); 
    436501               } 
    437502          } 
     
    459524      
    460525     /** 
     526     Sets options of defined list 
     527     */ 
     528     private function setOptions() 
     529     { 
     530          $opts = $_GET; 
     531           
     532          $ws = $this->core->auth->user_prefs->addWorkspace('lists'); 
     533           
     534          $user_pref = !is_null($ws->{$this->context.'_opts'}) ? unserialize($ws->{$this->context.'_opts'}) : array(); 
     535          # Sortby 
     536          $this->sortby = array_key_exists('sortby',$user_pref) ? $user_pref['sortby'] : null; 
     537          $this->sortby = array_key_exists('sortby',$opts) ? $opts['sortby'] : $this->sortby; 
     538          $user_pref['sortby'] = $this->sortby; 
     539          # Order 
     540          $this->order = array_key_exists('order',$user_pref) ? $user_pref['order'] : null; 
     541          $this->order = array_key_exists('order',$opts) ? $opts['order'] : $this->order; 
     542          $user_pref['order'] = $this->order; 
     543          # Number per page 
     544          $this->nb_per_page = array_key_exists('nb_per_page',$user_pref) ? $user_pref['nb_per_page'] : 10; 
     545          $this->nb_per_page = array_key_exists('nb_per_page',$opts) ? $opts['nb_per_page'] : $this->nb_per_page; 
     546          $user_pref['nb_per_page'] = $this->nb_per_page; 
     547           
     548          if (array_key_exists('sortby',$opts) || array_key_exists('order',$opts) || array_key_exists('nb_per_page',$opts)) { 
     549               $this->core->auth->user_prefs->lists->put($this->context.'_opts',serialize($user_pref),'string'); 
     550          } 
     551           
     552          # Page 
     553          $this->page = array_key_exists('page',$opts) ? $opts['page'] : 1; 
     554     } 
     555      
     556     /** 
    461557     Sets columns visibility of defined list 
    462558     */ 
    463559     private function setColumnsVisibility() 
    464560     { 
     561          $opts = $_GET; 
     562           
     563          # Visibility 
    465564          $ws = $this->core->auth->user_prefs->addWorkspace('lists'); 
    466565           
    467           $user_pref = !is_null($ws->{$this->context}) ? unserialize($ws->{$this->context}) : array(); 
     566          $user_pref = !is_null($ws->{$this->context.'_col'}) ? unserialize($ws->{$this->context.'_col'}) : array(); 
    468567           
    469568          foreach ($this->columns as $k => $v) { 
    470569               $visibility =  array_key_exists($k,$user_pref) ? $user_pref[$k] : true; 
    471                if (array_key_exists($this->form_trigger,$_REQUEST)) { 
     570               if (array_key_exists($this->form_trigger,$opts)) { 
    472571                    $key = sprintf($this->form_prefix,$k); 
    473                     $visibility = !array_key_exists($key,$_REQUEST) ? false : true; 
     572                    $visibility = !array_key_exists($key,$opts) ? false : true; 
    474573               } 
    475574               $v->setVisibility($visibility); 
     
    477576          } 
    478577           
    479           if (array_key_exists($this->form_trigger,$_REQUEST)) { 
    480                $this->core->auth->user_prefs->lists->put($this->context,serialize($user_pref),'string'); 
     578          if (array_key_exists($this->form_trigger,$opts)) { 
     579               $this->core->auth->user_prefs->lists->put($this->context.'_col',serialize($user_pref),'string'); 
    481580          } 
    482581     } 
     
    532631     { 
    533632          $order = 'desc'; 
    534           if (isset($_GET['sortby']) && $_GET['sortby'] === $c->getInfo('alias')) { 
    535                if (isset($_GET['order']) && $_GET['order'] === 'desc') { 
     633          if (!is_null($this->sortby) && $this->sortby === $c->getInfo('alias')) { 
     634               if (!is_null($this->order) && $this->order === $order) { 
    536635                    $order = 'asc'; 
    537636               } 
     
    558657Handle posts list on admin side 
    559658*/ 
    560 class adminPostList extends adminGenericList 
     659class adminPostList extends adminItemsList 
    561660{ 
    562661     public function setColumns() 
     
    710809Handle comments list on admin side 
    711810*/ 
    712 class adminCommentList extends adminGenericList 
     811class adminCommentList extends adminItemsList 
    713812{ 
    714813     public function setColumns() 
     
    813912Handle users list on admin side 
    814913*/ 
    815 class adminUserList extends adminGenericList 
     914class adminUserList extends adminItemsList 
    816915{ 
    817916     public function setColumns() 
     
    880979Handle blogs list on admin side 
    881980*/ 
    882 class adminBlogList extends adminGenericList 
     981class adminBlogList extends adminItemsList 
    883982{ 
    884983     public function setColumns() 
     
    888987          $this->addColumn('entries',null,__('Entries'),array('adminBlogList','getEntries'),array('class' => array('nowrap')),false); 
    889988          $this->addColumn('blogid','B.blog_id',__('Blog ID'),array('adminBlogList','getBlogId'),array('class' => array('nowrap'))); 
    890           $this->addColumn('action',null,'',array('adminBlogList','getAction'),array('class' => array('nowrap'))); 
     989          $this->addColumn('action',null,'',array('adminBlogList','getAction'),array('class' => array('nowrap')),false); 
    891990          $this->addColumn('status','blog_status',__('status'),array('adminBlogList','getStatus'),array('class' => array('nowrap'))); 
    892991     } 
Note: See TracChangeset for help on using the changeset viewer.

Sites map