Dotclear

source: inc/admin/lib.moduleslist.php @ 2148:621d4c6f4e8c

Revision 2148:621d4c6f4e8c, 9.9 KB checked in by Denis Jean-Chirstian <contact@…>, 12 years ago (diff)

Better repository search engine (and other stuff)

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) || $this->getSearchQuery() !== null) {
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 isDistributedModule($module)
225     {
226          return in_array($module, array(
227               'aboutConfig',
228               'akismet',
229               'antispam',
230               'attachments',
231               'blogroll',
232               'blowupConfig',
233               'daInstaller',
234               'fairTrackbacks',
235               'importExport',
236               'maintenance',
237               'pages',
238               'pings',
239               'simpleMenu',
240               'tags',
241               'themeEditor',
242               'userPref',
243               'widgets'
244          ));
245     }
246
247     public static function sortModules($modules, $field, $asc=true)
248     {
249          $sorter = array();
250          foreach($modules as $id => $module) {
251               $sorter[$id] = isset($module[$field]) ? $module[$field] : $field;
252          }
253          array_multisort($sorter, $asc ? SORT_ASC : SORT_DESC, $modules);
254
255          return $modules;
256     }
257
258     public function displayModulesList($cols=array('name', 'config', 'version', 'desc'), $actions=array(), $nav_limit=false)
259     {
260          echo 
261          '<div class="table-outer">'.
262          '<table class="modules"><caption class="hidden">'.html::escapeHTML(__('Modules list')).'</caption><tr>';
263
264          if (in_array('name', $cols)) {
265               echo 
266               '<th class="first nowrap">'.__('Name').'</th>';
267          }
268
269          if (in_array('version', $cols)) {
270               echo 
271               '<th class="nowrap count" scope="col">'.__('Version').'</th>';
272          }
273
274          if (in_array('current_version', $cols)) {
275               echo 
276               '<th class="nowrap count" scope="col">'.__('Current version').'</th>';
277          }
278
279          if (in_array('desc', $cols)) {
280               echo 
281               '<th class="nowrap" scope="col">'.__('Details').'</th>';
282          }
283
284          if (!empty($actions) && $this->core->auth->isSuperAdmin()) {
285               echo 
286               '<th class="minimal nowrap">'.__('Action').'</th>';
287          }
288
289          echo 
290          '</tr>';
291
292          $sort_field = $this->getSortQuery();
293
294          # Sort modules by id
295          $modules = $this->getSearchQuery() === null ?
296               self::sortModules($this->modules, $sort_field, $this->sort_asc) :
297               $this->modules;
298
299          $count = 0;
300          foreach ($modules as $id => $module)
301          {
302               # Show only requested modules
303               if ($nav_limit && $this->getSearchQuery() === null) {
304                    $char = substr($module[$sort_field], 0, 1);
305                    if (!in_array($char, $this->nav_list)) {
306                         $char = $this->nav_special;
307                    }
308                    if ($this->getNavQuery() != $char) {
309                         continue;
310                    }
311               }
312
313               echo 
314               '<tr class="line" id="'.$this->getPageTab().'_p_'.html::escapeHTML($id).'">';
315
316               # Link to config file
317               $config = in_array('config', $cols) && !empty($module['root']) && file_exists(path::real($module['root'].'/_config.php'));
318
319               echo 
320               '<td class="nowrap" scope="row">'.($config ? 
321                    '<a href="'.$this->getPageURL('module='.$id.'&conf=1').'">'.html::escapeHTML(__($module['name'])).'</a>' : 
322                    html::escapeHTML(__($module['name']))
323               ).'</td>';
324
325               if (in_array('version', $cols)) {
326                    echo 
327                    '<td class="nowrap count">'.html::escapeHTML($module['version']).'</td>';
328               }
329
330               if (in_array('current_version', $cols)) {
331                    echo 
332                    '<td class="nowrap count">'.html::escapeHTML($module['current_version']).'</td>';
333               }
334
335               if (in_array('desc', $cols)) {
336                    echo 
337                    '<td class="maximal">'.html::escapeHTML(__($module['desc'])).'</td>';
338               }
339
340               if (!empty($actions) && $this->core->auth->isSuperAdmin()) {
341                    echo 
342                    '<td class="nowrap">';
343
344                    $this->displayInlineActions($id, $module, $actions);
345
346                    echo
347                    '</td>';
348               }
349
350               echo 
351               '</tr>';
352
353               $count++;
354          }
355          echo 
356          '</table></div>';
357
358          if(!$count) {
359               echo 
360               '<p>'.__('There is no module.').'</p>';
361          }
362     }
363
364     protected function displayInlineActions($id, $module, $actions)
365     {
366          $submits = array();
367
368          # Activate
369          if (in_array('deactivate', $actions) && $module['root_writable']) {
370               $submits[] = '<input type="submit" name="deactivate" value="'.__('Deactivate').'" />';
371          }
372
373          # Deactivate
374          if (in_array('activate', $actions) && $module['root_writable']) {
375               $submits[] = '<input type="submit" name="activate" value="'.__('Activate').'" />';
376          }
377/*
378          # Delete
379          if (in_array('delete', $actions) && $this->isPathWritable() && preg_match('!^'.$this->path_pattern.'!', $module['root'])) {
380               $submits[] = '<input type="submit" class="delete" name="delete" value="'.__('Delete').'" />';
381          }
382
383          # Install (form repository)
384          if (in_array('install', $actions) && $this->isPathWritable()) {
385               $submits[] = '<input type="submit" name="install" value="'.__('Install').'" />';
386          }
387
388          # Update (from repository)
389          if (in_array('update', $actions) && $this->isPathWritable()) {
390               $submits[] = '<input type="submit" name="update" value="'.__('Update').'" />';
391          }
392*/
393          # Parse form
394          if (!empty($submits)) {
395               echo 
396               '<form action="'.$this->getPageURL().'" method="post">'.
397               '<div>'.
398               $this->core->formNonce().
399               form::hidden(array('module'), html::escapeHTML($id)).
400               form::hidden(array('tab'), $this->getPageTab()).
401               implode(' ', $submits).
402               '</div>'.
403               '</form>';
404          }
405     }
406
407     public static function sanitizeString($str)
408     {
409          return preg_replace('/[^A-Za-z0-9\@\#+_-]/', '', strtolower($str));
410     }
411}
412
413class adminThemesList extends adminModulesList
414{
415
416}
Note: See TracBrowser for help on using the repository browser.

Sites map