dolibarr  16.0.5
modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
4  * Copyright (C) 2010-2012 Regis Houssin <regis.houssin@inodbox.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
25 require '../../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
27 
28 if (empty($user->admin)) {
30 }
31 
32 // Load translation files required by the page
33 $langs->loadLangs(array("install", "other", "admin"));
34 
35 $optioncss = GETPOST('optioncss', 'alpha');
36 $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'moduleoverview';
37 
38 $search_name = GETPOST("search_name", 'alpha');
39 $search_id = GETPOST("search_id", 'alpha');
40 $search_version = GETPOST("search_version", 'alpha');
41 $search_permission = GETPOST("search_permission", 'alpha');
42 
43 $sortfield = GETPOST('sortfield', 'aZ09comma');
44 $sortorder = GETPOST('sortorder', 'aZ09comma');
45 
46 if (!$sortfield) {
47  $sortfield = "id";
48 }
49 if (!$sortorder) {
50  $sortorder = "asc";
51 }
52 
53 // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
54 $hookmanager->initHooks(array('moduleoverview'));
55 $form = new Form($db);
56 $object = new stdClass();
57 
58 // Definition of fields for lists
59 $arrayfields = array(
60  'name'=>array('label'=>$langs->trans("Modules"), 'checked'=>1, 'position'=>10),
61  'version'=>array('label'=>$langs->trans("Version"), 'checked'=>1, 'position'=>20),
62  'id'=>array('label'=>$langs->trans("IdModule"), 'checked'=>1, 'position'=>30),
63  'module_position'=>array('label'=>$langs->trans("Position"), 'checked'=>1, 'position'=>35),
64  'permission'=>array('label'=>$langs->trans("IdPermissions"), 'checked'=>1, 'position'=>40)
65 );
66 
67 $arrayfields = dol_sort_array($arrayfields, 'position');
68 $param = '';
69 
70 
71 /*
72  * Actions
73  */
74 
75 $parameters = array();
76 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
77 if ($reshook < 0) {
78  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
79 }
80 
81 if (empty($reshook)) {
82  // Selection of new fields
83  include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
84 }
85 
86 
87 // Load list of modules
88 $moduleList = array();
89 $modules = array();
90 $modules_files = array();
91 $modules_fullpath = array();
92 $modulesdir = dolGetModulesDirs();
93 $rights_ids = array();
94 $arrayofpermissions = array();
95 
96 foreach ($modulesdir as $dir) {
97  $handle = @opendir(dol_osencode($dir));
98  if (is_resource($handle)) {
99  while (($file = readdir($handle)) !== false) {
100  if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
101  $modName = substr($file, 0, dol_strlen($file) - 10);
102 
103  if ($modName) {
104  //print 'xx'.$dir.$file.'<br>';
105  if (in_array($file, $modules_files)) {
106  // File duplicate
107  print "Warning duplicate file found : ".$file." (Found ".$dir.$file.", already found ".$modules_fullpath[$file].")<br>";
108  } else {
109  // File to load
110  $res = include_once $dir.$file;
111  if (class_exists($modName)) {
112  try {
113  $objMod = new $modName($db);
114 
115  $modules[$objMod->numero] = $objMod;
116  $modules_files[$objMod->numero] = $file;
117  $modules_fullpath[$file] = $dir.$file;
118  } catch (Exception $e) {
119  dol_syslog("Failed to load ".$dir.$file." ".$e->getMessage(), LOG_ERR);
120  }
121  } else {
122  print "Warning bad descriptor file : ".$dir.$file." (Class ".$modName." not found into file)<br>";
123  }
124  }
125  }
126  }
127  }
128  closedir($handle);
129  }
130 }
131 
132 // create pre-filtered list for modules
133 foreach ($modules as $key => $module) {
134  $newModule = new stdClass();
135 
136  $newModule->name = $module->getName();
137  $newModule->version = $module->getVersion();
138  $newModule->id = $key;
139  $newModule->module_position = $module->getModulePosition();
140 
141  $alt = $module->name.' - '.$modules_files[$key];
142 
143  if (!empty($module->picto)) {
144  if (preg_match('/^\//', $module->picto)) {
145  $newModule->picto = img_picto($alt, $module->picto, 'width="14px"', 1);
146  } else {
147  $newModule->picto = img_object($alt, $module->picto, 'width="14px"');
148  }
149  } else {
150  $newModule->picto = img_object($alt, 'generic', 'width="14px"');
151  }
152 
153  $permission = array();
154  if ($module->rights) {
155  foreach ($module->rights as $rights) {
156  if (empty($rights[0])) {
157  continue;
158  }
159  $arrayofpermissions[$rights[0]] = array('label'=> 'user->rights->'.$module->rights_class.'->'.$rights[4].(empty($rights[5]) ? '' : '->'.$rights[5]));
160  $permission[] = $rights[0];
161 
162  array_push($rights_ids, $rights[0]);
163  }
164  }
165 
166  $newModule->permission = $permission;
167 
168  // pre-filter list
169  if (!empty($search_name) && !stristr($newModule->name, $search_name)) {
170  continue;
171  }
172  if (!empty($search_version) && !stristr($newModule->version, $search_version)) {
173  continue;
174  }
175  if (!empty($search_id) && !stristr($newModule->id, $search_id)) {
176  continue;
177  }
178 
179  if (!empty($search_permission)) {
180  $found = false;
181 
182  foreach ($newModule->permission as $permission) {
183  if (stristr($permission, $search_permission)) {
184  $found = true;
185  break;
186  }
187  }
188 
189  if (!$found) {
190  continue;
191  }
192  }
193 
194  $moduleList[] = $newModule;
195 }
196 
197 
198 
199 /*
200  * View
201  */
202 
203 llxHeader();
204 
205 print '<form action="'.$_SERVER["PHP_SELF"].'" method="post" name="formulaire">';
206 if ($optioncss != '') {
207  print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
208 }
209 print '<input type="hidden" name="token" value="'.newToken().'">';
210 print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
211 print '<input type="hidden" name="action" value="list">';
212 print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
213 print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
214 print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
215 
216 print_barre_liste($langs->trans("AvailableModules"), empty($page) ? 0 : $page, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, '', -1, '', 'title_setup', 0, '', '', 0, 1, 1);
217 
218 print '<span class="opacitymedium">'.$langs->trans("ToActivateModule").'</span>';
219 print '<br>';
220 print '<br>';
221 
222 $varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
223 $selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
224 
225 print '<div class="div-table-responsive-no-min">';
226 print '<table class="noborder centpercent">';
227 
228 // Lines with input filters
229 print '<tr class="liste_titre_filter">';
230 
231 if ($arrayfields['name']['checked']) {
232  print '<td class="liste_titre left">';
233  print '<input class="flat" type="text" name="search_name" size="8" value="'.dol_escape_htmltag($search_name).'">';
234  print '</td>';
235 }
236 if ($arrayfields['version']['checked']) {
237  print '<td class="liste_titre left">';
238  print '<input class="flat" type="text" name="search_version" size="6" value="'.dol_escape_htmltag($search_version).'">';
239  print '</td>';
240 }
241 if ($arrayfields['id']['checked']) {
242  print '<td class="liste_titre left">';
243  print '<input class="flat" type="text" name="search_id" size="6 value="'.dol_escape_htmltag($search_id).'">';
244  print '</td>';
245 }
246 if ($arrayfields['permission']['checked']) {
247  print '<td class="liste_titre left">';
248  print '<input class="flat" type="text" name="search_permission" size="8" value="'.dol_escape_htmltag($search_permission).'">';
249  print '</td>';
250 }
251 if ($arrayfields['module_position']['checked']) {
252  print '<td class="liste_titre left">';
253  print '</td>';
254 }
255 
256 print '<td class="liste_titre center maxwidthsearch">';
257 $searchpicto = $form->showFilterButtons();
258 print $searchpicto;
259 print '</td>';
260 
261 print '</tr>';
262 
263 print '<tr class="liste_titre">';
264 
265 if ($arrayfields['name']['checked']) {
266  print_liste_field_titre($arrayfields['name']['label'], $_SERVER["PHP_SELF"], "name", "", "", "", $sortfield, $sortorder);
267 }
268 if ($arrayfields['version']['checked']) {
269  print_liste_field_titre($arrayfields['version']['label'], $_SERVER["PHP_SELF"], "version", "", "", "", $sortfield, $sortorder);
270 }
271 if ($arrayfields['id']['checked']) {
272  print_liste_field_titre($arrayfields['id']['label'], $_SERVER["PHP_SELF"], "id", "", "", "", $sortfield, $sortorder, 'nowraponall ');
273 }
274 if ($arrayfields['permission']['checked']) {
275  print_liste_field_titre($arrayfields['permission']['label'], $_SERVER["PHP_SELF"], "permission", "", "", "", $sortfield, $sortorder);
276 }
277 if ($arrayfields['module_position']['checked']) {
278  print_liste_field_titre($arrayfields['module_position']['label'], $_SERVER["PHP_SELF"], "module_position", "", "", "", $sortfield, $sortorder);
279 }
280 
281 // Fields from hook
282 $parameters = array('arrayfields'=>$arrayfields, 'param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
283 $reshook = $hookmanager->executeHooks('printFieldListOption', $parameters); // Note that $action and $object may have been modified by hook
284 print $hookmanager->resPrint;
285 
286 print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ');
287 print '</tr>';
288 
289 // sort list
290 if ($sortfield == "name" && $sortorder == "asc") {
291  usort($moduleList, function (stdClass $a, stdClass $b) {
292  return strcasecmp($a->name, $b->name);
293  });
294 } elseif ($sortfield == "name" && $sortorder == "desc") {
295  usort($moduleList, function (stdClass $a, stdClass $b) {
296  return strcasecmp($b->name, $a->name);
297  });
298 } elseif ($sortfield == "version" && $sortorder == "asc") {
299  usort($moduleList, function (stdClass $a, stdClass $b) {
300  return strcasecmp($a->version, $b->version);
301  });
302 } elseif ($sortfield == "version" && $sortorder == "desc") {
303  usort($moduleList, function (stdClass $a, stdClass $b) {
304  return strcasecmp($b->version, $a->version);
305  });
306 } elseif ($sortfield == "id" && $sortorder == "asc") {
307  usort($moduleList, "compareIdAsc");
308 } elseif ($sortfield == "id" && $sortorder == "desc") {
309  usort($moduleList, "compareIdDesc");
310 } elseif ($sortfield == "permission" && $sortorder == "asc") {
311  usort($moduleList, "comparePermissionIdsAsc");
312 } elseif ($sortfield == "permission" && $sortorder == "desc") {
313  usort($moduleList, "comparePermissionIdsDesc");
314 } else {
315  $moduleList = dol_sort_array($moduleList, 'module_position');
316 }
317 
318 foreach ($moduleList as $module) {
319  print '<tr class="oddeven">';
320 
321  if ($arrayfields['name']['checked']) {
322  print '<td width="300" class="nowrap">';
323  print $module->picto;
324  print ' '.$module->name;
325  print "</td>";
326  }
327 
328  if ($arrayfields['version']['checked']) {
329  print '<td class="nowraponall">'.$module->version.'</td>';
330  }
331 
332  if ($arrayfields['id']['checked']) {
333  print '<td class="center">'.$module->id.'</td>';
334  }
335 
336  if ($arrayfields['permission']['checked']) {
337  $idperms = '';
338 
339  foreach ($module->permission as $permission) {
340  $translationKey = "Permission".$permission;
341  $labelpermission = $langs->trans($translationKey);
342  $labelpermission .= ' : '.$arrayofpermissions[$permission]['label'];
343  $idperms .= ($idperms ? ", " : "").'<span title="'.$labelpermission.'">'.$permission.'</a>';
344 
345  if (!empty($conf->global->MAIN_SHOW_PERMISSION)) {
346  if (empty($langs->tab_translate[$translationKey])) {
347  $tooltip = 'Missing translation (key '.$translationkey.' not found in admin.lang)';
348  $idperms .= ' <img src="../../theme/eldy/img/warning.png" alt="Warning" title="'.$tooltip.'">';
349  }
350  }
351  }
352 
353  print '<td><span class="opacitymedium">'.($idperms ? $idperms : "&nbsp;").'</span></td>';
354  }
355 
356  if ($arrayfields['module_position']['checked']) {
357  print '<td class="center">'.$module->module_position.'</td>';
358  }
359 
360  print '<td></td>';
361  print '</tr>';
362 }
363 
364 print '</table>';
365 print '</div>';
366 print '</form>';
367 print '<br>';
368 
369 sort($rights_ids);
370 $old = '';
371 
372 foreach ($rights_ids as $right_id) {
373  if ($old == $right_id) {
374  print "Warning duplicate id on permission : ".$right_id."<br>";
375  }
376 
377  $old = $right_id;
378 }
379 
380 // End of page
381 llxFooter();
382 $db->close();
383 
384 
392 function compareIdAsc(stdClass $a, stdClass $b)
393 {
394  if ((int) $a->id == (int) $b->id) {
395  return 0;
396  }
397 
398  return ((int) $a->id < (int) $b->id) ? -1 : 1;
399 }
400 
408 function compareIdDesc(stdClass $a, stdClass $b)
409 {
410  if ((int) $a->id == (int) $b->id) {
411  return 0;
412  }
413 
414  return ((int) $b->id < (int) $a->id) ? -1 : 1;
415 }
416 
424 function comparePermissionIdsAsc(stdClass $a, stdClass $b)
425 {
426  if (empty($a->permission) && empty($b->permission)) {
427  return compareIdAsc($a, $b);
428  }
429 
430  if (empty($a->permission)) {
431  return 1;
432  }
433  if (empty($b->permission)) {
434  return -1;
435  }
436 
437  if ($a->permission[0] == $b->permission[0]) {
438  return 0;
439  }
440 
441  return $a->permission[0] < $b->permission[0] ? -1 : 1;
442 }
443 
451 function comparePermissionIdsDesc(stdClass $a, stdClass $b)
452 {
453  if (empty($a->permission) && empty($b->permission)) {
454  return compareIdDesc($a, $b);
455  }
456 
457  if (empty($a->permission)) {
458  return -1;
459  }
460  if (empty($b->permission)) {
461  return 1;
462  }
463 
464  if ($a->permission[0] == $b->permission[0]) {
465  return 0;
466  }
467 
468  return $b->permission[0] < $a->permission[0] ? -1 : 1;
469 }
compareIdAsc
compareIdAsc(stdClass $a, stdClass $b)
Compare two modules by their ID for a ascending order.
Definition: modules.php:392
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
dol_osencode
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Definition: functions.lib.php:8499
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_sort_array
dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
Advanced sort array by second index function, which produces ascending (default) or descending output...
Definition: functions.lib.php:8385
comparePermissionIdsAsc
comparePermissionIdsAsc(stdClass $a, stdClass $b)
Compare two modules by their ID for a ascending order.
Definition: modules.php:424
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
compareIdDesc
compareIdDesc(stdClass $a, stdClass $b)
Compare two modules by their ID for a descending order.
Definition: modules.php:408
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
Exception
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
print_barre_liste
print_barre_liste($titre, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $hideselectlimit=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
Definition: functions.lib.php:5257
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
dolGetModulesDirs
dolGetModulesDirs($subdir='')
Return list of modules directories.
Definition: functions2.lib.php:80
print_liste_field_titre
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
Definition: functions.lib.php:5026
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
comparePermissionIdsDesc
comparePermissionIdsDesc(stdClass $a, stdClass $b)
Compare two modules by their permissions for a descending order.
Definition: modules.php:451
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
if
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
Definition: journals_list.php:25