Dotclear

source: inc/admin/lib.moduleslist.php @ 2147:b65846ba40f1

Revision 2147:b65846ba40f1, 9.4 KB checked in by Denis Jean-Chirstian <contact@…>, 12 years ago (diff)

Add a generic class to display modules lists (and some fix)

Line 
1<?php
2
3class adminModulesList
4{
5     public $core;
6     public $modules;
7
8     protected $page_url = '';
9     protected $page_qs = '?';
10     protected $page_tab = '';
11
12     public static $nav_indexes = 'abcdefghijklmnopqrstuvwxyz0123456789';
13     protected $nav_list = array();
14     protected $nav_special = 'other';
15
16     protected $sort_field = 'sname';
17     protected $sort_asc = true;
18
19     public function __construct($core, $modules, $page_url='', $page_tab='')
20     {
21          $this->core = $core;
22          $this->setModules($modules);
23          $this->setPageURL($page_url);
24          $this->setPageTab($page_tab);
25          $this->setNavSpecial(__('other'));
26
27          $this->init();
28     }
29
30     protected function init()
31     {
32          return null;
33     }
34
35     public function setPageURL($url)
36     {
37          $this->page_qs = strpos('?', $url) ? '&' : '?';
38          $this->page_url = $url;
39
40          return $this;
41     }
42
43     public function getPageURL($queries='', $with_tab=true)
44     {
45          return $this->page_url.
46               ($with_tab && !empty($this->page_tab) || !empty($queries) ? $this->page_qs : '').
47               (is_array($queries) ? http_build_query($queries) : $queries).
48               ($with_tab && !empty($this->page_tab) && !empty($queries) ? '&' : '').
49               ($with_tab && !empty($this->page_tab) ? /*'tab='$this->page_tab.*/'#'.$this->page_tab : '');
50     }
51
52     public function setPageTab($tab)
53     {
54          $this->page_tab = $tab;
55
56          return $this;
57     }
58
59     public function getPageTab()
60     {
61          return $this->page_tab;
62     }
63
64     public function getSearchQuery()
65     {
66          $query = !empty($_REQUEST['m_search']) ? trim($_REQUEST['m_search']) : null;
67          return strlen($query) > 1 ? $query : null;
68     }
69
70     public function displaySearchForm()
71     {
72          if (empty($this->modules)) {
73               return $this;
74          }
75
76          $query = $this->getSearchQuery();
77
78          echo 
79          '<div class="box">'.
80          '<form action="'.$this->getPageURL().'" method="get">'.
81          '<p><label for="m_search" class="classic">'.__('Search:').'&nbsp;</label><br />'.
82          form::field(array('m_search'), 30, 255, html::escapeHTML($query)).
83          '<input type="submit" value="'.__('Search').'" /> '.
84          '</p>';
85
86          if ($query) {
87               echo 
88               '<p class="info">'.sprintf(
89                    __('Found %d result for search "%s".', 'Found %d results for search "%s".', count($this->modules)), 
90                    count($this->modules), html::escapeHTML($query)
91               ).' <a href="'.$this->getPageURL().'">'.__('Reset search').'</a>'.
92               '</p>';
93          }
94
95          echo 
96          '</form>'.
97          '</div>';
98
99          return $this;
100     }
101
102     public function setNavSpecial($str)
103     {
104          $this->nav_special = (string) $str;
105          $this->nav_list = array_merge(str_split(self::$nav_indexes), array($this->nav_special));
106
107          return $this;
108     }
109
110     public function getNavQuery()
111     {
112          return isset($_REQUEST['m_nav']) && in_array($_REQUEST['m_nav'], $this->nav_list) ? $_REQUEST['m_nav'] : $this->nav_list[0];
113     }
114
115     public function displayNavMenu()
116     {
117          if (empty($this->modules)) {
118               return $this;
119          }
120
121          # Fetch modules required field
122          $indexes = array();
123          foreach ($this->modules as $id => $module) {
124               if (!isset($module[$this->sort_field])) {
125                    continue;
126               }
127               $char = substr($module[$this->sort_field], 0, 1);
128               if (!in_array($char, $this->nav_list)) {
129                    $char = $this->nav_special;
130               }
131               if (!isset($indexes[$char])) {
132                    $indexes[$char] = 0;
133               }
134               $indexes[$char]++;
135          }
136
137          $buttons = array();
138          foreach($this->nav_list as $char) {
139               # Selected letter
140               if ($this->getNavQuery() == $char) {
141                    $buttons[] = '<li class="active" title="'.__('current selection').'"><strong> '.$char.' </strong></li>';
142               }
143               # Letter having modules
144               elseif (!empty($indexes[$char])) {
145                    $title = sprintf(__('%d module', '%d modules', $indexes[$char]), $indexes[$char]);
146                    $buttons[] = '<li class="btn" title="'.$title.'"><a href="'.$this->getPageURL('m_nav='.$char).'" title="'.$title.'"> '.$char.' </a></li>';
147               }
148               # Letter without modules
149               else {
150                    $buttons[] = '<li class="btn no-link" title="'.__('no module').'"> '.$char.' </li>';
151               }
152          }
153          # Parse navigation menu
154          echo '<div class="pager">'.__('Index:').' <ul>'.implode('',$buttons).'</ul></div>';
155
156          return $this;
157     }
158
159     public function setSortField($field, $asc=true)
160     {
161          $this->sort_field = $field;
162          $this->sort_asc = (boolean) $asc;
163
164          return $this;
165     }
166
167     public function getSortQuery()
168     {
169          return !empty($_REQUEST['m_sort']) ? $_REQUEST['m_sort'] : $this->sort_field;
170     }
171
172     public function displaySortForm()
173     {
174          //not yet implemented
175     }
176
177     public function setModules($modules)
178     {
179          $this->modules = array();
180          foreach($modules as $id => $module) {
181               $this->modules[$id] = $this->setModuleInfo($id, $module);
182          }
183
184          return $this;
185     }
186
187     public function getModules()
188     {
189          return $this->modules;
190     }
191
192     protected function setModuleInfo($id, $module)
193     {
194          $label = empty($module['label']) ? $id : $module['label'];
195          $name = __(empty($module['name']) ? $label : $module['name']);
196         
197          return array_merge(
198               # Default values
199               array(
200                    'desc'                   => '',
201                    'author'            => '',
202                    'version'                => 0,
203                    'current_version'   => 0,
204                    'root'                   => '',
205                    'root_writable'     => false,
206                    'permissions'       => null,
207                    'parent'            => null,
208                    'priority'               => 1000,
209                    'standalone_config' => false
210               ),
211               # Module's values
212               $module,
213               # Clean up values
214               array(
215                    'id'                     => $id,
216                    'sid'                    => self::sanitizeString($id),
217                    'label'             => $label,
218                    'name'                   => $name,
219                    'sname'             => self::sanitizeString($name)
220               )
221          );
222     }
223
224     public static function sortModules($modules, $field, $asc=true)
225     {
226          $sorter = array();
227          foreach($modules as $id => $module) {
228               $sorter[$id] = isset($module[$field]) ? $module[$field] : $field;
229          }
230          array_multisort($sorter, $asc ? SORT_ASC : SORT_DESC, $modules);
231
232          return $modules;
233     }
234
235     public function displayModulesList($cols=array('name', 'config', 'version', 'desc'), $actions=array(), $nav_limit=false)
236     {
237          echo 
238          '<div class="table-outer">'.
239          '<table class="modules"><caption class="hidden">'.html::escapeHTML(__('Modules list')).'</caption><tr>';
240
241          if (in_array('name', $cols)) {
242               echo 
243               '<th class="first nowrap">'.__('Name').'</th>';
244          }
245
246          if (in_array('version', $cols)) {
247               echo 
248               '<th class="nowrap count" scope="col">'.__('Version').'</th>';
249          }
250
251          if (in_array('current_version', $cols)) {
252               echo 
253               '<th class="nowrap count" scope="col">'.__('Current version').'</th>';
254          }
255
256          if (in_array('desc', $cols)) {
257               echo 
258               '<th class="nowrap" scope="col">'.__('Details').'</th>';
259          }
260
261          if (!empty($actions) && $this->core->auth->isSuperAdmin()) {
262               echo 
263               '<th class="minimal nowrap">'.__('Action').'</th>';
264          }
265
266          echo 
267          '</tr>';
268
269          $sort_field = $this->getSortQuery();
270
271          # Sort modules by id
272          $modules = self::sortModules($this->modules, $sort_field, $this->sort_asc);
273
274          $count = 0;
275          foreach ($modules as $id => $module)
276          {
277               # Show only requested modules
278               if ($nav_limit && $this->getSearchQuery() === null) {
279                    $char = substr($module[$sort_field], 0, 1);
280                    if (!in_array($char, $this->nav_list)) {
281                         $char = $this->nav_special;
282                    }
283                    if ($this->getNavQuery() != $char) {
284                         continue;
285                    }
286               }
287
288               echo 
289               '<tr class="line" id="'.$this->getPageTab().'_p_'.html::escapeHTML($id).'">';
290
291               # Link to config file
292               $config = in_array('config', $cols) && !empty($module['root']) && file_exists(path::real($module['root'].'/_config.php'));
293
294               echo 
295               '<td class="nowrap" scope="row">'.($config ? 
296                    '<a href="'.$this->getPageURL('module='.$id.'&conf=1').'">'.html::escapeHTML(__($module['name'])).'</a>' : 
297                    html::escapeHTML(__($module['name']))
298               ).'</td>';
299
300               if (in_array('version', $cols)) {
301                    echo 
302                    '<td class="nowrap count">'.html::escapeHTML($module['version']).'</td>';
303               }
304
305               if (in_array('old_version', $cols)) {
306                    echo 
307                    '<td class="nowrap count">'.html::escapeHTML($module['old_version']).'</td>';
308               }
309
310               if (in_array('desc', $cols)) {
311                    echo 
312                    '<td class="maximal">'.html::escapeHTML(__($module['desc'])).'</td>';
313               }
314
315               if (!empty($actions) && $this->core->auth->isSuperAdmin()) {
316                    echo 
317                    '<td class="nowrap">';
318
319                    $this->displayInlineActions($id, $module, $actions);
320
321                    echo
322                    '</td>';
323               }
324
325               echo 
326               '</tr>';
327
328               $count++;
329          }
330          echo 
331          '</table></div>';
332
333          if(!$count) {
334               echo 
335               '<p>'.__('There is no module.').'</p>';
336          }
337     }
338
339     protected function displayInlineActions($id, $module, $actions)
340     {
341          $submits = array();
342
343          # Activate
344          if (in_array('deactivate', $actions) && $module['root_writable']) {
345               $submits[] = '<input type="submit" name="deactivate" value="'.__('Deactivate').'" />';
346          }
347
348          # Deactivate
349          if (in_array('activate', $actions) && $module['root_writable']) {
350               $submits[] = '<input type="submit" name="activate" value="'.__('Activate').'" />';
351          }
352/*
353          # Delete
354          if (in_array('delete', $actions) && $this->isPathWritable() && preg_match('!^'.$this->path_pattern.'!', $module['root'])) {
355               $submits[] = '<input type="submit" class="delete" name="delete" value="'.__('Delete').'" />';
356          }
357
358          # Install (form repository)
359          if (in_array('install', $actions) && $this->isPathWritable()) {
360               $submits[] = '<input type="submit" name="install" value="'.__('Install').'" />';
361          }
362
363          # Update (from repository)
364          if (in_array('update', $actions) && $this->isPathWritable()) {
365               $submits[] = '<input type="submit" name="update" value="'.__('Update').'" />';
366          }
367*/
368          # Parse form
369          if (!empty($submits)) {
370               echo 
371               '<form action="'.$this->getPageURL().'" method="post">'.
372               '<div>'.
373               $this->core->formNonce().
374               form::hidden(array('module'), html::escapeHTML($id)).
375               form::hidden(array('tab'), $this->getPageTab()).
376               implode(' ', $submits).
377               '</div>'.
378               '</form>';
379          }
380     }
381
382     public static function sanitizeString($str)
383     {
384          return preg_replace('/[^A-Za-z0-9\@\#+_-]/', '', strtolower($str));
385     }
386}
387
388class adminThemesList extends adminModulesList
389{
390
391}
Note: See TracBrowser for help on using the repository browser.

Sites map