Conditions | 26 |
Paths | 6460 |
Total Lines | 131 |
Code Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
218 | function getTriggersList($forcedirtriggers = null) |
||
219 | { |
||
220 | // global $conf, $langs, $db; |
||
221 | $conf = Globals::$conf; |
||
222 | $langs = Globals::$langs; |
||
223 | $db = Config::$dbEngine; |
||
224 | |||
225 | $files = array(); |
||
226 | $fullpath = array(); |
||
227 | $relpath = array(); |
||
228 | $iscoreorexternal = array(); |
||
229 | $modules = array(); |
||
230 | $orders = array(); |
||
231 | $i = 0; |
||
232 | |||
233 | $dirtriggers = array_merge(array('/core/triggers/'), Globals::$conf->modules_parts['triggers']); |
||
234 | if (is_array($forcedirtriggers)) { |
||
235 | $dirtriggers = $forcedirtriggers; |
||
236 | } |
||
237 | |||
238 | foreach ($dirtriggers as $reldir) { |
||
239 | $dir = DolUtils::dol_buildpath($reldir, 0); |
||
240 | $newdir = DolUtils::dol_osencode($dir); |
||
241 | |||
242 | // Check if directory exists (we do not use dol_is_dir to avoid loading files.lib.php at each call) |
||
243 | if (!is_dir($newdir)) |
||
244 | continue; |
||
245 | |||
246 | $handle = opendir($newdir); |
||
247 | if (is_resource($handle)) { |
||
248 | while (($file = readdir($handle)) !== false) { |
||
249 | if (is_readable($newdir . '/' . $file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/', $file, $reg)) { |
||
250 | if (preg_match('/\.back$/', $file)) |
||
251 | continue; |
||
252 | |||
253 | $part1 = $reg[1]; |
||
254 | $part2 = $reg[2]; |
||
255 | $part3 = $reg[3]; |
||
256 | |||
257 | $modName = 'Interface' . ucfirst($reg[3]); |
||
258 | //print "file=$file"; print "modName=$modName"; exit; |
||
259 | if (in_array($modName, $modules)) { |
||
260 | $langs->load("errors"); |
||
261 | print '<div class="error">' . $langs->trans("Error") . ' : ' . $langs->trans("ErrorDuplicateTrigger", $modName, "/htdocs/core/triggers/") . '</div>'; |
||
262 | } else { |
||
263 | include_once $newdir . '/' . $file; |
||
264 | } |
||
265 | |||
266 | $files[$i] = $file; |
||
267 | $fullpath[$i] = $dir . '/' . $file; |
||
268 | $relpath[$i] = preg_replace('/^\//', '', $reldir) . '/' . $file; |
||
269 | $iscoreorexternal[$i] = ($reldir == '/core/triggers/' ? 'internal' : 'external'); |
||
270 | $modules[$i] = $modName; |
||
271 | $orders[$i] = $part1 . '_' . $part2 . '_' . $part3; // Set sort criteria value |
||
272 | |||
273 | $i++; |
||
274 | } |
||
275 | } |
||
276 | closedir($handle); |
||
277 | } |
||
278 | } |
||
279 | |||
280 | asort($orders); |
||
281 | |||
282 | $triggers = array(); |
||
283 | $j = 0; |
||
284 | |||
285 | // Loop on each trigger |
||
286 | foreach ($orders as $key => $value) { |
||
287 | $modName = $modules[$key]; |
||
288 | if (empty($modName)) |
||
289 | continue; |
||
290 | |||
291 | if (!class_exists($modName)) { |
||
292 | print 'Error: A trigger file was found but its class "' . $modName . '" was not found.' . "<br>\n"; |
||
293 | continue; |
||
294 | } |
||
295 | |||
296 | $objMod = new $modName($db); |
||
297 | |||
298 | // Define disabledbyname and disabledbymodule |
||
299 | $disabledbyname = 0; |
||
300 | $disabledbymodule = 1; |
||
301 | $module = ''; |
||
302 | |||
303 | // Check if trigger file is disabled by name |
||
304 | if (preg_match('/NORUN$/i', $files[$key])) |
||
305 | $disabledbyname = 1; |
||
306 | // Check if trigger file is for a particular module |
||
307 | if (preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php/i', $files[$key], $reg)) { |
||
308 | $module = preg_replace('/^mod/i', '', $reg[2]); |
||
309 | $constparam = 'MAIN_MODULE_' . strtoupper($module); |
||
310 | if (strtolower($module) == 'all') |
||
311 | $disabledbymodule = 0; |
||
312 | else if (empty(Globals::$conf->global->$constparam)) |
||
313 | $disabledbymodule = 2; |
||
314 | $triggers[$j]['module'] = strtolower($module); |
||
315 | } |
||
316 | |||
317 | // We set info of modules |
||
318 | $triggers[$j]['picto'] = $objMod->picto ? img_object('', $objMod->picto) : img_object('', 'generic'); |
||
319 | $triggers[$j]['file'] = $files[$key]; |
||
320 | $triggers[$j]['fullpath'] = $fullpath[$key]; |
||
321 | $triggers[$j]['relpath'] = $relpath[$key]; |
||
322 | $triggers[$j]['iscoreorexternal'] = $iscoreorexternal[$key]; |
||
323 | $triggers[$j]['version'] = $objMod->getVersion(); |
||
324 | $triggers[$j]['status'] = img_picto($langs->trans("Active"), 'tick'); |
||
325 | if ($disabledbyname > 0 || $disabledbymodule > 1) |
||
326 | $triggers[$j]['status'] = ''; |
||
327 | |||
328 | $text = '<b>' . $langs->trans("Description") . ':</b><br>'; |
||
329 | $text .= $objMod->getDesc() . '<br>'; |
||
330 | $text .= '<br><b>' . $langs->trans("Status") . ':</b><br>'; |
||
331 | if ($disabledbyname == 1) { |
||
332 | $text .= $langs->trans("TriggerDisabledByName") . '<br>'; |
||
333 | if ($disabledbymodule == 2) |
||
334 | $text .= $langs->trans("TriggerDisabledAsModuleDisabled", $module) . '<br>'; |
||
335 | } |
||
336 | else { |
||
337 | if ($disabledbymodule == 0) |
||
338 | $text .= $langs->trans("TriggerAlwaysActive") . '<br>'; |
||
339 | if ($disabledbymodule == 1) |
||
340 | $text .= $langs->trans("TriggerActiveAsModuleActive", $module) . '<br>'; |
||
341 | if ($disabledbymodule == 2) |
||
342 | $text .= $langs->trans("TriggerDisabledAsModuleDisabled", $module) . '<br>'; |
||
343 | } |
||
344 | |||
345 | $triggers[$j]['info'] = $text; |
||
346 | $j++; |
||
347 | } |
||
348 | return $triggers; |
||
349 | } |
||
351 |