Dotclear

source: inc/admin/class.dc.list.php @ 1491:2b9253624dbe

Revision 1491:2b9253624dbe, 5.8 KB checked in by Dsls, 12 years ago (diff)

Implemented last merges from default, post.php is now more flexible.
added twig function : form_field_attr

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2013 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 -----------------------------------------
12
13class dcItemList extends dcForm {
14
15     protected $columns;
16     protected $entries;
17     protected $filterset;
18     protected $fetcher;
19     protected $selection;
20     protected $current_page;
21     protected $nb_items;
22     protected $nb_items_per_page;
23     protected $nb_pages;
24     protected $page;
25     protected $sortby;
26     protected $order;
27
28
29     public static function __init__($env) {
30          $env->getExtension('dc_form')->addTemplate('@forms/lists_layout.html.twig');
31          $env->addFunction(
32               new Twig_SimpleFunction(
33                    'listitems',
34                    'dcItemList::renderList',
35                    array(
36                         'is_safe' => array('html'),
37                         'needs_context' => true,
38                         'needs_environment' => true
39          )));
40     }
41
42     public static function renderList($env,$context,$name,$attributes=array())
43     {
44          $context['listname']=$name;
45          echo $env->getExtension('dc_form')->renderWidget(
46               'entrieslist',
47               $context
48          );
49     }
50     /**
51     Inits dcItemList object
52     
53     @param    core      <b>dcCore</b>       Dotclear core reference
54     @param    form_prefix    <b>string</b>       form prefix to use for parameters
55     */
56     public function __construct($core,$name,$filterset,$fetcher,$action,$form_prefix="f_") {
57          parent::__construct($core,$name,$action,'POST');
58          $this->entries = array();
59          $this->columns = array();
60          $this->selection = new dcFieldCheckbox('entries',NULL,array('multiple' => true));
61          $this->addField($this->selection);
62          $this->filterset = $filterset;
63          $this->fetcher = $fetcher;
64     }
65
66     public function setup() {
67          $this
68               ->addField(new dcFieldCombo('action','',array(), array(
69                    'label' => __('Selected entries action:'))))
70               ->addField(new dcFieldSubmit('ok',__('ok'), array()));
71          $columns_combo = array();
72          foreach ($this->columns as $c) {
73               $columns_combo[$c->getID()] = $c->getName();
74          }
75          $this->sortby = new dcFilterCombo(
76               'sortby',
77               __('Sort By'), 
78               __('Sort by'), 'sortby', $columns_combo,array('singleval'=> true,'static' => true));
79          $this->filterset->addFilter($this->sortby);
80          $order_combo = array('asc' => __('Ascending'),'desc' => __('Descending'));
81          $this->order = new dcFilterCombo(
82               'order',
83               __('Order'), 
84               __('Order'), 'orderby', $order_combo,array('singleval'=> true, 'static' => true));
85          $limit = new dcFilterText(
86               'limit',
87               __('Limit'), __('Limit'), 'limit',array('singleval'=> true,'static' =>true));
88          $this->filterset->addFilter($this->order);
89          $this->filterset->addFilter($limit);
90          $this->filterset->setup();
91          parent::setup();
92          $this->nb_items_per_page = $limit->getFields()->getValue();
93          if ($this->nb_items_per_page == 0)
94               $this->nb_items_per_page = 10;
95          $this->fetchEntries();
96
97     }
98
99     protected function fetchEntries() {
100          $params = new ArrayObject();
101          $this->filterset->applyFilters($params);
102          $this->nb_items = $this->fetcher->getEntriesCount($params);
103          $this->nb_pages = round($this->nb_items / $this->nb_items_per_page) + 1;
104          if (isset($_GET['page'])) {
105               $this->page = (int)$_GET['page'];
106          } else {
107               $this->page = 1;
108          }
109          if ($this->page > $this->nb_pages) {
110               $this->page = $this->nb_pages;
111          }
112          $offset = $this->nb_items_per_page*($this->page-1);
113          $params['order'] = $this->getOrder();
114          $entries = $this->fetcher->getEntries($params,$offset,$this->nb_items_per_page);
115          $this->setEntries($entries);
116
117     }
118
119     public function setEntries($entries) {
120          $this->entries = $entries;
121          $this->core->tpl->addGlobal('list_'.$this->name,$this->getContext());
122          foreach ($this->entries as $e) {
123               $this->selection->addValue($e->post_id);
124          }
125     }
126
127     public function getContext() {
128          $ccontext = new ArrayObject();
129          foreach ($this->columns as $c) {
130               $c->appendEditLines($ccontext);
131          }
132          $page = $this->page;
133          $nb_pages = $this->nb_pages;
134          $nb_pages_around = 2;
135          $pages = array(1);
136          if ($page>$nb_pages_around+2) {
137               $pages[]='...';
138          }
139          for ($p=max(2,$page-$nb_pages_around); 
140               $p<=min($page+$nb_pages_around,$nb_pages-1); $p++) {
141               $pages[]=$p;
142          }
143          if ($page<$nb_pages-$nb_pages_around-1) {
144               $pages[]='...';
145          }
146          $pages[] = $nb_pages;
147
148
149          return array(
150               'url' => array('',$this->filterset->getURLParams()),
151               'cols' => $ccontext,
152               'entries' => $this->entries,
153               'nb_entries' => $this->nb_items,
154               'page' => $page,
155               'pages_links' => $pages);
156     }
157
158     public function addColumn($c) {
159          $this->columns[$c->getID()] = $c;
160          $c->setForm($this);
161          return $this;
162     }
163
164     public function getOrder() {
165          $id = $this->sortby->getFields()->getValue();
166          return $this->columns[$id]->getColID().' '.$this->order->getFields()->getValue();
167     }
168
169     public function setFilterSet($fs) {
170          $this->filterset = $fs;
171     }
172
173}
174
175class dcColumn {
176     protected $form;
177     protected $id;
178     protected $name;
179     protected $sortable;
180     protected $col_id;
181
182     public function __construct($id, $name, $col_id,$attributes=array()) {
183          $this->id = $id;
184          $this->name = $name;
185          $this->col_id = $col_id;
186     }
187
188     public function getName() {
189          return $this->name;
190     }
191
192     public function getID() {
193          return $this->id;
194     }
195
196     public function getColID(){
197          return $this->col_id;
198     }
199
200     public function setForm($f){
201          $this->form = $f;
202     }
203
204     public function appendEditLines($line) {
205          $line[] = array (
206               'name' => $this->name,
207               'col_id' => $this->col_id,
208               'widget' => 'col_'.$this->id);
209     }
210
211}
212
213abstract class dcListFetcher {
214     protected $core;
215     public function __construct($core) {
216          $this->core = $core;
217     }
218
219     abstract function getEntries($params,$offset,$limit);
220     abstract function getEntriesCount($params);
221}
222
223dcItemList::__init__($GLOBALS['core']->tpl);
224
225?>
Note: See TracBrowser for help on using the repository browser.

Sites map