Passed
Branch develop (803efa)
by
unknown
32:29
created

comparePermissionIdsDesc()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 5
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 10
rs 8.8333
1
<?php
2
/* Copyright (C) 2005-2009	Laurent Destailleur		<[email protected]>
3
 * Copyright (C) 2007		Rodolphe Quiedeville	<[email protected]>
4
 * Copyright (C) 2010-2012	Regis Houssin			<[email protected]>
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
20
/**
21
 *  \file       htdocs/admin/system/modules.php
22
 *  \brief      File to list all Dolibarr modules
23
 */
24
25
require '../../main.inc.php';
26
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
27
28
if (!$user->admin) {
29
	accessforbidden();
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", 'alpha');
44
$sortorder			= GETPOST("sortorder", 'alpha');
45
46
if (!$sortfield) $sortfield = "id";
47
if (!$sortorder) $sortorder = "asc";
48
49
// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks
50
$hookmanager->initHooks(array('moduleoverview'));
51
$form = new Form($db);
52
$object = new stdClass();
53
54
// Definition of fields for lists
55
$arrayfields = array(
56
	'name'=>array('label'=>$langs->trans("Modules"), 'checked'=>1, 'position'=>10),
57
	'version'=>array('label'=>$langs->trans("Version"), 'checked'=>1, 'position'=>20),
58
	'id'=>array('label'=>$langs->trans("IdModule"), 'checked'=>1, 'position'=>30),
59
	'permission'=>array('label'=>$langs->trans("IdPermissions"), 'checked'=>1, 'position'=>40)
60
);
61
62
$arrayfields = dol_sort_array($arrayfields, 'position');
63
64
/*
65
 * Actions
66
 */
67
68
$parameters = array();
69
$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
70
if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
71
72
if (empty($reshook)) {
73
	// Selection of new fields
74
	include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
75
}
76
77
78
// Load list of modules
79
$moduleList = array();
80
$modules = array();
81
$modules_files = array();
82
$modules_fullpath = array();
83
$modulesdir = dolGetModulesDirs();
84
$rights_ids = array();
85
86
foreach ($modulesdir as $dir) {
87
	$handle = @opendir(dol_osencode($dir));
88
	if (is_resource($handle)) {
89
		while (($file = readdir($handle)) !== false) {
90
			if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') {
91
				$modName = substr($file, 0, dol_strlen($file) - 10);
92
93
				if ($modName) {
94
					//print 'xx'.$dir.$file.'<br>';
95
					if (in_array($file, $modules_files)) {
96
						// File duplicate
97
						print "Warning duplicate file found : ".$file." (Found ".$dir.$file.", already found ".$modules_fullpath[$file].")<br>";
98
					}
99
					else {
100
						// File to load
101
						$res = include_once $dir.$file;
102
						if (class_exists($modName)) {
103
							try {
104
								$objMod = new $modName($db);
105
106
								$modules[$objMod->numero] = $objMod;
107
								$modules_files[$objMod->numero] = $file;
108
								$modules_fullpath[$file] = $dir.$file;
109
							}
110
							catch (Exception $e) {
111
								dol_syslog("Failed to load ".$dir.$file." ".$e->getMessage(), LOG_ERR);
112
							}
113
						}
114
						else {
115
							print "Warning bad descriptor file : ".$dir.$file." (Class ".$modName." not found into file)<br>";
116
						}
117
					}
118
				}
119
			}
120
		}
121
		closedir($handle);
122
	}
123
}
124
125
// create pre-filtered list for modules
126
foreach ($modules as $key=>$module) {
127
	$newModule = new stdClass();
128
129
	$newModule->name	= $module->getName();
130
	$newModule->version	= $module->getVersion();
131
	$newModule->id		= $key;
132
133
	$alt = $module->name.' - '.$modules_files[$key];
134
135
	if (!empty($module->picto)) {
136
		if (preg_match('/^\//', $module->picto)) $newModule->picto = img_picto($alt, $module->picto, 'width="14px"', 1);
137
		else $newModule->picto = img_object($alt, $module->picto, 'width="14px"');
138
	}
139
	else {
140
		$newModule->picto = img_object($alt, 'generic', 'width="14px"');
141
	}
142
143
	$permission = array();
144
	if ($module->rights) {
145
		foreach ($module->rights as $rights) {
146
			if (empty($rights[0])) {
147
				continue;
148
			}
149
150
			$permission[] = $rights[0];
151
152
			array_push($rights_ids, $rights[0]);
153
		}
154
	}
155
156
	$newModule->permission = $permission;
157
158
	// pre-filter list
159
	if ($search_name && !stristr($newModule->name, $search_name))			continue;
160
	if ($search_version && !stristr($newModule->version, $search_version))	continue;
161
	if ($search_id && !stristr($newModule->id, $search_id))					continue;
162
163
	if ($search_permission) {
164
		$found = false;
165
166
		foreach ($newModule->permission as $permission) {
167
			if (stristr($permission, $search_permission)) {
168
				$found = true;
169
				break;
170
			}
171
		}
172
173
		if (!$found) continue;
174
	}
175
176
	$moduleList[] = $newModule;
177
}
178
179
180
181
/*
182
 * View
183
 */
184
185
llxHeader();
186
187
print '<form action="'.$_SERVER["PHP_SELF"].'" method="post" name="formulaire">';
188
if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
189
print '<input type="hidden" name="token" value="'.newToken().'">';
190
print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
191
print '<input type="hidden" name="action" value="list">';
192
print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
193
print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
194
print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
195
196
print_barre_liste($langs->trans("AvailableModules"), $page, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, $massactionbutton, -1, '', 'title_setup', 0, '', '', 0, 1, 1);
197
198
print '<span class="opacitymedium">'.$langs->trans("ToActivateModule").'</span>';
199
print '<br>';
200
print '<br>';
201
202
$varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
203
$selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
204
205
print '<div class="div-table-responsive-no-min">';
206
print '<table class="noborder centpercent">';
207
208
// Lines with input filters
209
print '<tr class="liste_titre_filter">';
210
211
if ($arrayfields['name']['checked']) {
212
	print '<td class="liste_titre left">';
213
	print '<input class="flat" type="text" name="search_name" size="8" value="'.$search_name.'">';
214
	print '</td>';
215
}
216
if ($arrayfields['version']['checked']) {
217
	print '<td class="liste_titre left">';
218
	print '<input class="flat" type="text" name="search_version" size="8" value="'.$search_version.'">';
219
	print '</td>';
220
}
221
if ($arrayfields['id']['checked']) {
222
	print '<td class="liste_titre left">';
223
	print '<input class="flat" type="text" name="search_id" size="8" value="'.$search_id.'">';
224
	print '</td>';
225
}
226
if ($arrayfields['permission']['checked']) {
227
	print '<td class="liste_titre left">';
228
	print '<input class="flat" type="text" name="search_permission" size="8" value="'.$search_permission.'">';
229
	print '</td>';
230
}
231
232
print '<td class="liste_titre center maxwidthsearch">';
233
$searchpicto = $form->showFilterButtons();
234
print $searchpicto;
235
print '</td>';
236
237
print '</tr>';
238
239
print '<tr class="liste_titre">';
240
241
if ($arrayfields['name']['checked']) {
242
	print_liste_field_titre($arrayfields['name']['label'], $_SERVER["PHP_SELF"], "name", "", "", "", $sortfield, $sortorder);
243
}
244
if ($arrayfields['version']['checked']) {
245
	print_liste_field_titre($arrayfields['version']['label'], $_SERVER["PHP_SELF"], "version", "", "", "", $sortfield, $sortorder);
246
}
247
if ($arrayfields['id']['checked']) {
248
	print_liste_field_titre($arrayfields['id']['label'], $_SERVER["PHP_SELF"], "id", "", "", "", $sortfield, $sortorder);
249
}
250
if ($arrayfields['permission']['checked']) {
251
	print_liste_field_titre($arrayfields['permission']['label'], $_SERVER["PHP_SELF"], "permission", "", "", "", $sortfield, $sortorder);
252
}
253
254
// Fields from hook
255
$parameters = array('arrayfields'=>$arrayfields, 'param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
256
$reshook = $hookmanager->executeHooks('printFieldListOption', $parameters); // Note that $action and $object may have been modified by hook
257
print $hookmanager->resPrint;
258
259
print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ');
260
print '</tr>';
261
262
// sort list
263
if ($sortfield == "name" && $sortorder == "asc") usort($moduleList, function (stdClass $a, stdClass $b) {
264
	return strcasecmp($a->name, $b->name); });
265
if ($sortfield == "name" && $sortorder == "desc") usort($moduleList, function (stdClass $a, stdClass $b) {
266
	return strcasecmp($b->name, $a->name); });
267
if ($sortfield == "version" && $sortorder == "asc") usort($moduleList, function (stdClass $a, stdClass $b) {
268
	return strcasecmp($a->version, $b->version); });
269
if ($sortfield == "version" && $sortorder == "desc") usort($moduleList, function (stdClass $a, stdClass $b) {
270
	return strcasecmp($b->version, $a->version); });
271
if ($sortfield == "id" && $sortorder == "asc") usort($moduleList, "compareIdAsc");
272
if ($sortfield == "id" && $sortorder == "desc") usort($moduleList, "compareIdDesc");
273
if ($sortfield == "permission" && $sortorder == "asc") usort($moduleList, "comparePermissionIdsAsc");
274
if ($sortfield == "permission" && $sortorder == "desc") usort($moduleList, "comparePermissionIdsDesc");
275
276
foreach ($moduleList as $module) {
277
	print '<tr class="oddeven">';
278
279
	if ($arrayfields['name']['checked']) {
280
		print '<td width="300" class="nowrap">';
281
		print $module->picto;
282
		print ' '.$module->name;
283
		print "</td>";
284
	}
285
286
	if ($arrayfields['version']['checked']) {
287
		print '<td>'.$module->version.'</td>';
288
	}
289
290
	if ($arrayfields['id']['checked']) {
291
		print '<td class="center">'.$module->id.'</td>';
292
	}
293
294
	if ($arrayfields['permission']['checked']) {
295
		$idperms = '';
296
297
		foreach ($module->permission as $permission) {
298
			$idperms .= ($idperms ? ", " : "").$permission;
299
			$translationKey = "Permission".$permission;
300
301
			if (! empty($conf->global->MAIN_SHOW_PERMISSION)) {
302
				if (empty($langs->tab_translate[$translationKey])) {
303
					$tooltip = 'Missing translation (key '.$translationkey.' not found in admin.lang)';
304
					$idperms .= ' <img src="../../theme/eldy/img/warning.png" alt="Warning" title="'.$tooltip.'">';
305
				}
306
			}
307
		}
308
309
		print '<td>'.($idperms ? $idperms : "&nbsp;").'</td>';
310
	}
311
312
	print '<td></td>';
313
	print '</tr>';
314
}
315
316
print '</table>';
317
print '</div>';
318
print '</form>';
319
print '<br>';
320
321
sort($rights_ids);
322
$old = '';
323
324
foreach ($rights_ids as $right_id) {
325
	if ($old == $right_id) {
326
		print "Warning duplicate id on permission : ".$right_id."<br>";
327
	}
328
329
	$old = $right_id;
330
}
331
332
// End of page
333
llxFooter();
334
$db->close();
335
336
337
 /**
338
  * Compare two modules by their ID for a ascending order
339
  *
340
  * @param	stdClass 	$a		First module
341
  * @param	stdClass 	$b		Second module
342
  * @return	int					Compare result (-1, 0, 1)
343
  */
344
function compareIdAsc(stdClass $a, stdClass $b)
345
{
346
	if ($a->id == $b->id) return 0;
347
348
	return $a->id > $b->id ? -1 : 1;
349
}
350
351
 /**
352
  * Compare two modules by their ID for a descending order
353
  *
354
  * @param	stdClass 	$a		First module
355
  * @param	stdClass 	$b		Second module
356
  * @return	int					Compare result (-1, 0, 1)
357
  */
358
function compareIdDesc(stdClass $a, stdClass $b)
359
{
360
	if ($a->id == $b->id) return 0;
361
362
	return $b->id > $a->id ? -1 : 1;
363
}
364
365
 /**
366
  * Compare two modules by their ID for a ascending order
367
  *
368
  * @param	stdClass 	$a		First module
369
  * @param	stdClass 	$b		Second module
370
  * @return	int					Compare result (-1, 0, 1)
371
  */
372
function comparePermissionIdsAsc(stdClass $a, stdClass $b)
373
{
374
	if (empty($a->permission) && empty($b->permission)) return compareIdAsc($a, $b);
375
376
	if (empty($a->permission)) return 1;
377
	if (empty($b->permission)) return -1;
378
379
	if ($a->permission[0] == $b->permission[0]) return 0;
380
381
	return $a->permission[0] > $b->permission[0] ? -1 : 1;
382
}
383
384
 /**
385
  * Compare two modules by their permissions for a descending order
386
  *
387
  * @param	stdClass 	$a		First module
388
  * @param	stdClass 	$b		Second module
389
  * @return	int					Compare result (-1, 0, 1)
390
  */
391
function comparePermissionIdsDesc(stdClass $a, stdClass $b)
392
{
393
	if (empty($a->permission) && empty($b->permission)) return compareIdDesc($a, $b);
394
395
	if (empty($a->permission)) return -1;
396
	if (empty($b->permission)) return 1;
397
398
	if ($a->permission[0] == $b->permission[0]) return 0;
399
400
	return $a->permission[0] > $b->permission[0] ? 1 : -1;
401
}
402