| 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 | 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 | |
|---|
| 167 | return $this->columns[$id]->getColID().' '.$this->order->getFields()->getValue(); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | public function setFilterSet($fs) { |
|---|
| 171 | $this->filterset = $fs; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | class dcColumn { |
|---|
| 177 | protected $form; |
|---|
| 178 | protected $id; |
|---|
| 179 | protected $name; |
|---|
| 180 | protected $sortable; |
|---|
| 181 | protected $col_id; |
|---|
| 182 | |
|---|
| 183 | public function __construct($id, $name, $col_id,$attributes=array()) { |
|---|
| 184 | $this->id = $id; |
|---|
| 185 | $this->name = $name; |
|---|
| 186 | $this->col_id = $col_id; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | public function getName() { |
|---|
| 190 | return $this->name; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | public function getID() { |
|---|
| 194 | return $this->id; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | public function getColID(){ |
|---|
| 198 | return $this->col_id; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | public function setForm($f){ |
|---|
| 202 | $this->form = $f; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | public function appendEditLines($line) { |
|---|
| 206 | $line[] = array ( |
|---|
| 207 | 'name' => $this->name, |
|---|
| 208 | 'col_id' => $this->col_id, |
|---|
| 209 | 'widget' => 'col_'.$this->id); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | abstract class dcListFetcher { |
|---|
| 215 | protected $core; |
|---|
| 216 | public function __construct($core) { |
|---|
| 217 | $this->core = $core; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | abstract function getEntries($params,$offset,$limit); |
|---|
| 221 | abstract function getEntriesCount($params); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | dcItemList::__init__($GLOBALS['core']->tpl); |
|---|
| 225 | |
|---|
| 226 | ?> |
|---|