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 | |
---|
13 | class dcItemList extends dcForm { |
---|
14 | |
---|
15 | protected $columns; |
---|
16 | protected $columns_combo; |
---|
17 | protected $entries; |
---|
18 | protected $filterset; |
---|
19 | protected $fetcher; |
---|
20 | protected $selection; |
---|
21 | protected $current_page; |
---|
22 | protected $nb_items; |
---|
23 | protected $nb_items_per_page; |
---|
24 | protected $nb_pages; |
---|
25 | protected $page; |
---|
26 | protected $sortby; |
---|
27 | protected $order; |
---|
28 | |
---|
29 | |
---|
30 | public static function __init__($env) { |
---|
31 | $env->getExtension('dc_form')->addTemplate('@forms/lists_layout.html.twig'); |
---|
32 | $env->addFunction( |
---|
33 | new Twig_SimpleFunction( |
---|
34 | 'listitems', |
---|
35 | 'dcItemList::renderList', |
---|
36 | array( |
---|
37 | 'is_safe' => array('html'), |
---|
38 | 'needs_context' => true, |
---|
39 | 'needs_environment' => true |
---|
40 | ))); |
---|
41 | } |
---|
42 | |
---|
43 | public static function renderList($env,$context,$name,$attributes=array()) |
---|
44 | { |
---|
45 | $context['listname']=$name; |
---|
46 | echo $env->getExtension('dc_form')->renderWidget( |
---|
47 | 'entrieslist', |
---|
48 | $context |
---|
49 | ); |
---|
50 | } |
---|
51 | /** |
---|
52 | Inits dcItemList object |
---|
53 | |
---|
54 | @param core <b>dcCore</b> Dotclear core reference |
---|
55 | @param form_prefix <b>string</b> form prefix to use for parameters |
---|
56 | */ |
---|
57 | public function __construct($core,$name,$fetcher,$action,$form_prefix="f_") { |
---|
58 | parent::__construct($core,$name,$action,'POST'); |
---|
59 | $this->entries = array(); |
---|
60 | $this->columns = array(); |
---|
61 | $this->selection = new dcFieldCheckbox('entries',NULL,array('multiple' => true)); |
---|
62 | $this->addField($this->selection); |
---|
63 | $this->fetcher = $fetcher; |
---|
64 | $this->filterset = null; |
---|
65 | } |
---|
66 | |
---|
67 | protected function setupFilterset() { |
---|
68 | foreach ($this->columns as $c) { |
---|
69 | $this->filterset->addfilter($c->getFilter()); |
---|
70 | } |
---|
71 | $this->sortby = new dcFilterCombo( |
---|
72 | 'sortby', |
---|
73 | __('Sort By'), |
---|
74 | __('Sort by'), 'sortby', $this->columns_combo,array('singleval'=> true,'static' => true)); |
---|
75 | $this->filterset->addFilter($this->sortby); |
---|
76 | $order_combo = array('asc' => __('Ascending'),'desc' => __('Descending')); |
---|
77 | $this->order = new dcFilterCombo( |
---|
78 | 'order', |
---|
79 | __('Order'), |
---|
80 | __('Order'), 'orderby', $order_combo,array('singleval'=> true, 'static' => true)); |
---|
81 | $limit = new dcFilterText( |
---|
82 | 'limit', |
---|
83 | __('Limit'), __('Limit'), 'limit',array('singleval'=> true,'static' =>true)); |
---|
84 | $this->filterset->addFilter($this->order); |
---|
85 | $this->filterset->addFilter($limit); |
---|
86 | $this->filterset->setup(); |
---|
87 | $this->nb_items_per_page = $limit->getFields()->getValue(); |
---|
88 | |
---|
89 | } |
---|
90 | |
---|
91 | public function setup() { |
---|
92 | $this |
---|
93 | ->addField(new dcFieldCombo('action','',array(), array( |
---|
94 | 'label' => __('Selected entries action:')))) |
---|
95 | ->addField(new dcFieldSubmit('ok',__('ok'), array())); |
---|
96 | $this->columns_combo = array(); |
---|
97 | foreach ($this->columns as $c) { |
---|
98 | $this->columns_combo[$c->getID()] = $c->getName(); |
---|
99 | } |
---|
100 | if ($this->filterset !== null) { |
---|
101 | $this->setupFilterset(); |
---|
102 | } |
---|
103 | parent::setup(); |
---|
104 | if ($this->nb_items_per_page == 0) |
---|
105 | $this->nb_items_per_page = 10; |
---|
106 | $this->fetchEntries(); |
---|
107 | |
---|
108 | } |
---|
109 | |
---|
110 | protected function fetchEntries() { |
---|
111 | $params = new ArrayObject(); |
---|
112 | if ($this->filterset != null) { |
---|
113 | $this->filterset->applyFilters($params); |
---|
114 | } |
---|
115 | $this->nb_items = $this->fetcher->getEntriesCount($params); |
---|
116 | $this->nb_pages = round($this->nb_items / $this->nb_items_per_page) + 1; |
---|
117 | if (isset($_GET['page'])) { |
---|
118 | $this->page = (int)$_GET['page']; |
---|
119 | } else { |
---|
120 | $this->page = 1; |
---|
121 | } |
---|
122 | if ($this->page > $this->nb_pages) { |
---|
123 | $this->page = $this->nb_pages; |
---|
124 | } |
---|
125 | $offset = $this->nb_items_per_page*($this->page-1); |
---|
126 | $params['order'] = $this->getOrder(); |
---|
127 | $entries = $this->fetcher->getEntries($params,$offset,$this->nb_items_per_page); |
---|
128 | $this->setEntries($entries); |
---|
129 | |
---|
130 | } |
---|
131 | |
---|
132 | public function setEntries($entries) { |
---|
133 | $this->entries = $entries; |
---|
134 | $this->core->tpl->addGlobal('list_'.$this->name,$this->getContext()); |
---|
135 | foreach ($this->entries as $e) { |
---|
136 | $this->selection->addValue($e->post_id); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | public function getContext() { |
---|
141 | $ccontext = new ArrayObject(); |
---|
142 | foreach ($this->columns as $c) { |
---|
143 | if ($c->isEnabled()) { |
---|
144 | $c->appendEditLines($ccontext); |
---|
145 | } |
---|
146 | } |
---|
147 | $page = $this->page; |
---|
148 | $nb_pages = $this->nb_pages; |
---|
149 | $nb_pages_around = 2; |
---|
150 | $pages = array(1); |
---|
151 | if ($page>$nb_pages_around+2) { |
---|
152 | $pages[]='...'; |
---|
153 | } |
---|
154 | for ($p=max(2,$page-$nb_pages_around); |
---|
155 | $p<=min($page+$nb_pages_around,$nb_pages-1); $p++) { |
---|
156 | $pages[]=$p; |
---|
157 | } |
---|
158 | if ($page<$nb_pages-$nb_pages_around-1) { |
---|
159 | $pages[]='...'; |
---|
160 | } |
---|
161 | $pages[] = $nb_pages; |
---|
162 | |
---|
163 | |
---|
164 | return array( |
---|
165 | 'url' => array('',$this->filterset->getURLParams()), |
---|
166 | 'cols' => $ccontext, |
---|
167 | 'entries' => $this->entries, |
---|
168 | 'nb_entries' => $this->nb_items, |
---|
169 | 'page' => $page, |
---|
170 | 'pages_links' => $pages); |
---|
171 | } |
---|
172 | |
---|
173 | public function addColumn(dcColumn $c) { |
---|
174 | $this->columns[$c->getID()] = $c; |
---|
175 | $c->setForm($this); |
---|
176 | return $this; |
---|
177 | } |
---|
178 | |
---|
179 | public function getOrder() { |
---|
180 | $id = $this->sortby->getFields()->getValue(); |
---|
181 | return $this->columns[$id]->getColID().' '.$this->order->getFields()->getValue(); |
---|
182 | } |
---|
183 | |
---|
184 | public function setFilterSet($fs) { |
---|
185 | $this->filterset = $fs; |
---|
186 | } |
---|
187 | |
---|
188 | } |
---|
189 | |
---|
190 | class dcColumn { |
---|
191 | protected $form; |
---|
192 | protected $id; |
---|
193 | protected $name; |
---|
194 | protected $sortable; |
---|
195 | protected $col_id; |
---|
196 | protected $filter; |
---|
197 | protected $locked; |
---|
198 | |
---|
199 | public function __construct($id, $name, $col_id,$attributes=array()) { |
---|
200 | $this->id = $id; |
---|
201 | $this->name = $name; |
---|
202 | $this->col_id = $col_id; |
---|
203 | $this->locked = isset($attributes['locked']) && $attributes['locked']; |
---|
204 | $this->filter = new dcFilterCheckbox('col'.$id,$name,$name,'',array('static'=>true,'locked'=>$this->locked)); |
---|
205 | } |
---|
206 | |
---|
207 | public function getFilter () { |
---|
208 | return $this->filter; |
---|
209 | } |
---|
210 | public function getName() { |
---|
211 | return $this->name; |
---|
212 | } |
---|
213 | |
---|
214 | public function getID() { |
---|
215 | return $this->id; |
---|
216 | } |
---|
217 | |
---|
218 | public function getColID(){ |
---|
219 | return $this->col_id; |
---|
220 | } |
---|
221 | |
---|
222 | public function setForm($f){ |
---|
223 | $this->form = $f; |
---|
224 | } |
---|
225 | |
---|
226 | public function isEnabled() { |
---|
227 | $v = $this->filter->getAppliedValues(); |
---|
228 | return $v['values']['1']==1; |
---|
229 | } |
---|
230 | public function appendEditLines($line) { |
---|
231 | $line[] = array ( |
---|
232 | 'name' => $this->name, |
---|
233 | 'col_id' => $this->col_id, |
---|
234 | 'widget' => 'col_'.$this->id); |
---|
235 | } |
---|
236 | |
---|
237 | } |
---|
238 | |
---|
239 | abstract class dcListFetcher { |
---|
240 | protected $core; |
---|
241 | public function __construct($core) { |
---|
242 | $this->core = $core; |
---|
243 | } |
---|
244 | |
---|
245 | abstract function getEntries($params,$offset,$limit); |
---|
246 | abstract function getEntriesCount($params); |
---|
247 | } |
---|
248 | |
---|
249 | /** |
---|
250 | * dcFilterText - basic single field text filter |
---|
251 | * |
---|
252 | * @uses dcFilter |
---|
253 | * |
---|
254 | */ |
---|
255 | class dcFilterCheckbox extends dcFilter { |
---|
256 | |
---|
257 | /** |
---|
258 | * @see dcFilter::init() |
---|
259 | */ |
---|
260 | public function init() { |
---|
261 | $foptions = array('label' => $this->name); |
---|
262 | if (isset($this->options['locked']) && $this->options['locked']){ |
---|
263 | $foptions['read_only']= true; |
---|
264 | } |
---|
265 | $this->field = new dcFieldCheckbox($this->filter_id,false,$foptions); |
---|
266 | $this->filterset->addField($this->field); |
---|
267 | $this->multiple = false; |
---|
268 | } |
---|
269 | |
---|
270 | protected function parseData($data) { |
---|
271 | return parent::parseData($data); |
---|
272 | } |
---|
273 | /** |
---|
274 | * @see dcFilter::appendContextLine() |
---|
275 | */ |
---|
276 | public function appendContextLine($line,$pos) { |
---|
277 | /* |
---|
278 | Extra data provided by this filter : |
---|
279 | * ffield : field name |
---|
280 | * display_inline : true if the field is static |
---|
281 | * fwidget : name of the widget (filter_text) |
---|
282 | * desc : filter description |
---|
283 | */ |
---|
284 | $line['ffield'] = $this->field->getName(); |
---|
285 | if ($this->static) { |
---|
286 | $line['display_inline'] = true; |
---|
287 | } |
---|
288 | if ($pos == 0) { |
---|
289 | |
---|
290 | $line['fwidget']='filter_boolean'; |
---|
291 | $line['desc']=$this->desc; |
---|
292 | }; |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * @see dcFilter::addValue() |
---|
297 | */ |
---|
298 | protected function addValue($value=NULL) { |
---|
299 | $this->field->addValue($value,true); |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * @see dcFilter::applyFilter() |
---|
304 | */ |
---|
305 | public function applyFilter($params) { |
---|
306 | } |
---|
307 | |
---|
308 | public function getAppliedFilterText() { |
---|
309 | return ""; |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | dcItemList::__init__($GLOBALS['core']->tpl); |
---|
314 | |
---|
315 | ?> |
---|