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 $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 | |
---|
24 | |
---|
25 | public static function __init__($env) { |
---|
26 | $env->getExtension('dc_form')->addTemplate('@forms/lists_layout.html.twig'); |
---|
27 | $env->addFunction( |
---|
28 | new Twig_SimpleFunction( |
---|
29 | 'listitems', |
---|
30 | 'dcItemList::renderList', |
---|
31 | array( |
---|
32 | 'is_safe' => array('html'), |
---|
33 | 'needs_context' => true, |
---|
34 | 'needs_environment' => true |
---|
35 | ))); |
---|
36 | } |
---|
37 | |
---|
38 | public static function renderList($env,$context,$name,$attributes=array()) |
---|
39 | { |
---|
40 | $context['listname']=$name; |
---|
41 | echo $env->getExtension('dc_form')->renderWidget( |
---|
42 | 'entrieslist', |
---|
43 | $context |
---|
44 | ); |
---|
45 | } |
---|
46 | /** |
---|
47 | Inits dcItemList object |
---|
48 | |
---|
49 | @param core <b>dcCore</b> Dotclear core reference |
---|
50 | @param form_prefix <b>string</b> form prefix to use for parameters |
---|
51 | */ |
---|
52 | public function __construct($core,$name,$filterset,$fetcher,$action,$form_prefix="f_") { |
---|
53 | parent::__construct($core,$name,$action,'POST'); |
---|
54 | $this->entries = array(); |
---|
55 | $this->columns = array(); |
---|
56 | $this->selection = new dcFieldCheckbox('entries',NULL,array('multiple' => true)); |
---|
57 | $this->addField($this->selection); |
---|
58 | $this->filterset = $filterset; |
---|
59 | $this->fetcher = $fetcher; |
---|
60 | } |
---|
61 | |
---|
62 | public function setup() { |
---|
63 | $this |
---|
64 | ->addField(new dcFieldCombo('action',$this->actions_combo, '', array( |
---|
65 | 'label' => __('Selected entries action:')))) |
---|
66 | ->addField(new dcFieldSubmit('ok',__('ok'), array())) |
---|
67 | ->addField(new dcFieldHidden('page','1')); |
---|
68 | $columns_combo = array(); |
---|
69 | foreach ($this->columns as $c) { |
---|
70 | $columns_combo[$c->getID()] = $c->getName(); |
---|
71 | } |
---|
72 | $this->filterset->addFilter(new dcFilterCombo( |
---|
73 | 'sortby', |
---|
74 | __('Sort By'), |
---|
75 | __('Sort by'), 'sortby', $columns_combo,array('singleval'=> true,'static' => true))); |
---|
76 | $order_combo = array('asc' => __('Ascending'),'desc' => __('Descending')); |
---|
77 | $this->filterset->addFilter(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($limit); |
---|
85 | $this->filterset->setup(); |
---|
86 | parent::setup(); |
---|
87 | $this->nb_items_per_page = $limit->getFields()->getValue(); |
---|
88 | $this->fetchEntries(); |
---|
89 | |
---|
90 | } |
---|
91 | |
---|
92 | protected function fetchEntries() { |
---|
93 | $params = new ArrayObject(); |
---|
94 | $offset = $this->nb_items_per_page*($this->page->getValue()-1); |
---|
95 | $this->filterset->applyFilters($params); |
---|
96 | $this->nb_items = $this->fetcher->getEntriesCount($params); |
---|
97 | $entries = $this->fetcher->getEntries($params,$offset,$this->nb_items_per_page); |
---|
98 | $this->setEntries($entries); |
---|
99 | /*echo "LIMIT:".$this->nb_items_per_page; |
---|
100 | echo 'count :'.print_r($this->nb_items,true); |
---|
101 | echo 'page'.$this->page;*/ |
---|
102 | } |
---|
103 | |
---|
104 | public function setEntries($entries) { |
---|
105 | $this->entries = $entries; |
---|
106 | $this->core->tpl->addGlobal('list_'.$this->name,$this->getContext()); |
---|
107 | foreach ($this->entries as $e) { |
---|
108 | $this->selection->addValue($e->post_id); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | public function getContext() { |
---|
113 | $ccontext = new ArrayObject(); |
---|
114 | foreach ($this->columns as $c) { |
---|
115 | $c->appendEditLines($ccontext); |
---|
116 | } |
---|
117 | return array( |
---|
118 | 'cols' => $ccontext, |
---|
119 | 'entries' => $this->entries); |
---|
120 | } |
---|
121 | |
---|
122 | public function addColumn($c) { |
---|
123 | $this->columns[] = $c; |
---|
124 | $c->setForm($this); |
---|
125 | return $this; |
---|
126 | } |
---|
127 | |
---|
128 | public function setFilterSet($fs) { |
---|
129 | $this->filterset = $fs; |
---|
130 | } |
---|
131 | |
---|
132 | } |
---|
133 | |
---|
134 | class dcColumn { |
---|
135 | protected $form; |
---|
136 | protected $id; |
---|
137 | protected $name; |
---|
138 | protected $sortable; |
---|
139 | protected $col_id; |
---|
140 | |
---|
141 | public function __construct($id, $name, $col_id,$attributes=array()) { |
---|
142 | $this->id = $id; |
---|
143 | $this->name = $name; |
---|
144 | $this->col_id = $col_id; |
---|
145 | } |
---|
146 | |
---|
147 | public function getName() { |
---|
148 | return $this->name; |
---|
149 | } |
---|
150 | |
---|
151 | public function getID() { |
---|
152 | return $this->id; |
---|
153 | } |
---|
154 | |
---|
155 | public function getColID(){ |
---|
156 | return $this->col_id; |
---|
157 | } |
---|
158 | |
---|
159 | public function setForm($f){ |
---|
160 | $this->form = $f; |
---|
161 | } |
---|
162 | |
---|
163 | public function appendEditLines($line) { |
---|
164 | $line[] = array ( |
---|
165 | 'name' => $this->name, |
---|
166 | 'col_id' => $this->col_id, |
---|
167 | 'widget' => 'col_'.$this->id); |
---|
168 | } |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | abstract class dcListFetcher { |
---|
173 | protected $core; |
---|
174 | public function __construct($core) { |
---|
175 | $this->core = $core; |
---|
176 | } |
---|
177 | |
---|
178 | abstract function getEntries($params,$offset,$limit); |
---|
179 | abstract function getEntriesCount($params); |
---|
180 | } |
---|
181 | |
---|
182 | dcItemList::__init__($GLOBALS['core']->tpl); |
---|
183 | |
---|
184 | ?> |
---|