Passed
Push — main ( e2b3b9...324b47 )
by Rafael
46:15
created

FormAdmin::select_theme()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 9.8333
1
<?php
2
3
/* Copyright (C) 2004-2014 Laurent Destailleur  <[email protected]>
4
 * Copyright (C) 2005-2011 Regis Houssin        <[email protected]>
5
 * Copyright (C) 2007      Patrick Raguin 		<[email protected]>
6
 * Copyright (C) 2024       Frédéric France             <[email protected]>
7
 * Copyright (C) 2024       Rafael San José         <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
namespace DoliCore\Form;
24
25
use DoliCore\Base\Config;
26
27
/**
28
 *      \file       htdocs/core/class/html.formadmin.class.php
29
 *      \ingroup    core
30
 *      \brief      File of class for html functions for admin pages
31
 */
32
33
34
/**
35
 *      Class to generate html code for admin pages
36
 */
37
class FormAdmin
38
{
39
    /**
40
     * @var DoliDB Database handler.
0 ignored issues
show
Bug introduced by
The type DoliCore\Form\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
41
     */
42
    public $db;
43
44
    /**
45
     * @var string error message
46
     */
47
    public $error;
48
49
50
    /**
51
     *  Constructor
52
     *
53
     * @param DoliDB|null $db Database handler
54
     */
55
    public function __construct($db)
56
    {
57
        $this->db = $db;
58
    }
59
60
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
61
62
    /**
63
     *  Return html select list with available languages (key='en_US', value='United States' for example)
64
     *
65
     * @param string|array $selected Language pre-selected. Can be an array if $multiselect is 1.
66
     * @param string $htmlname Name of HTML select
67
     * @param int $showauto Show 'auto' choice
68
     * @param array $filter Array of keys to exclude in list (opposite of $onlykeys)
69
     * @param int|string $showempty '1'=Add empty value or 'string to show'
70
     * @param int $showwarning Show a warning if language is not complete
71
     * @param int $disabled Disable edit of select
72
     * @param string $morecss Add more css styles
73
     * @param int $showcode 1=Add language code into label at beginning, 2=Add language code into label at
74
     *                                   end
75
     * @param int $forcecombo Force to use combo box (so no ajax beautify effect)
76
     * @param int $multiselect Make the combo a multiselect
77
     * @param array $onlykeys Array of language keys to restrict list with the following keys (opposite of
78
     *                                   $filter). Example array('fr', 'es', ...)
79
     * @param int $mainlangonly 1=Show only main languages ('fr_FR' no' fr_BE', 'es_ES' not 'es_MX', ...)
80
     *
81
     * @return     string                          Return HTML select string with list of languages
82
     */
83
    public function select_language($selected = 'auto', $htmlname = 'lang_id', $showauto = 0, $filter = [], $showempty = '', $showwarning = 0, $disabled = 0, $morecss = '', $showcode = 0, $forcecombo = 0, $multiselect = 0, $onlykeys = [], $mainlangonly = 0)
84
    {
85
        // phpcs:enable
86
        global $conf, $langs;
87
88
        $langs = Config::getLangs();
89
        $langs->setDefaultLang($selected);
90
        $langs->loadLangs(['main', 'admin']);
91
92
        if (getDolGlobalString('MAIN_DEFAULT_LANGUAGE_FILTER')) {
93
            if (!is_array($filter)) {
94
                $filter = [];
95
            }
96
            $filter[getDolGlobalString('MAIN_DEFAULT_LANGUAGE_FILTER')] = 1;
97
        }
98
99
        $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12, 0, $mainlangonly);
100
101
        // If empty value is not allowed and the language to select is not inside the list of available language and we must find
102
        // an alternative of the language code to pre-select (to avoid to have first element in list pre-selected).
103
        if ($selected && empty($showempty)) {
104
            if (!is_array($selected) && !array_key_exists($selected, $langs_available)) {
105
                $tmparray = explode('_', $selected);
106
                if (!empty($tmparray[1])) {
107
                    $selected = getLanguageCodeFromCountryCode($tmparray[1]);
108
                }
109
                if (empty($selected)) {
110
                    $selected = $langs->defaultlang;
111
                }
112
            } else {
113
                // If the preselected value is an array, we do not try to find alternative to preselect
114
            }
115
        }
116
117
        $out = '';
118
119
        $out .= '<select ' . ($multiselect ? 'multiple="multiple" ' : '') . 'class="flat' . ($morecss ? ' ' . $morecss : '') . '" id="' . $htmlname . '" name="' . $htmlname . ($multiselect ? '[]' : '') . '"' . ($disabled ? ' disabled' : '') . '>';
120
        if ($showempty && !$multiselect) {
121
            if (is_numeric($showempty)) {
122
                $out .= '<option value="0"';
123
            } else {
124
                $out .= '<option value="-1"';
125
            }
126
            if ($selected === '') {
127
                $out .= ' selected';
128
            }
129
            $out .= '>';
130
            if ($showempty != '1') {
131
                $out .= $showempty;
132
            } else {
133
                $out .= '&nbsp;';
134
            }
135
            $out .= '</option>';
136
        }
137
        if ($showauto) {
138
            $out .= '<option value="auto"';
139
            if ($selected === 'auto') {
140
                $out .= ' selected';
141
            }
142
            $out .= '>' . $langs->trans("AutoDetectLang") . '</option>';
143
        }
144
145
        asort($langs_available);    // array('XX' => 'Language (Country)', ...)
146
147
        foreach ($langs_available as $key => $value) {
148
            $valuetoshow = $value;
149
            if ($showcode == 1) {
150
                if ($mainlangonly) {
151
                    $valuetoshow = '<span class="opacitymedium">' . preg_replace('/[_-].*$/', '', $key) . '</span> - ' . $value;
152
                } else {
153
                    $valuetoshow = '<span class="opacitymedium">' . $key . '</span> - ' . $value;
154
                }
155
            }
156
            if ($showcode == 2) {
157
                if ($mainlangonly) {
158
                    $valuetoshow = $value . ' <span class="opacitymedium">(' . preg_replace('/[_-].*$/', '', $key) . ')</span>';
159
                } else {
160
                    $valuetoshow = $value . ' <span class="opacitymedium">(' . $key . ')</span>';
161
                }
162
            }
163
164
            $keytouse = $key;
165
            if ($mainlangonly) {
166
                $keytouse = preg_replace('/[_-].*$/', '', $key);
167
            }
168
169
            if ($filter && is_array($filter) && array_key_exists($keytouse, $filter)) {
170
                continue;
171
            }
172
            if ($onlykeys && is_array($onlykeys) && !array_key_exists($keytouse, $onlykeys)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $onlykeys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
173
                continue;
174
            }
175
176
            $valuetoshow = picto_from_langcode($key, 'class="saturatemedium"') . ' ' . $valuetoshow;
177
            if ((is_string($selected) && (string)$selected == (string)$keytouse) || (is_array($selected) && in_array($keytouse, $selected))) {
178
                $out .= '<option value="' . $keytouse . '" selected data-html="' . dol_escape_htmltag($valuetoshow) . '">' . $valuetoshow . '</option>';
179
            } else {
180
                $out .= '<option value="' . $keytouse . '" data-html="' . dol_escape_htmltag($valuetoshow) . '">' . $valuetoshow . '</option>';
181
            }
182
        }
183
        $out .= '</select>';
184
185
        // Make select dynamic
186
        if (!$forcecombo) {
187
            include_once BASE_PATH . '/../Dolibarr/Lib/Ajax.php';
188
            $out .= ajax_combobox($htmlname);
189
        }
190
191
        return $out;
192
    }
193
194
    public function select_theme($selected = 'adminlte')
195
    {
196
        $options = [
197
            'adminlte' => 'AdminLTE',
198
            'eldy' => 'Dolibarr Eldy (deprecated)',
199
            'md' => 'Dolibarr MD (deprecated)',
200
        ];
201
202
        $out = '';
203
        $out .= '<select class="flat" id="theme" name="theme">';
204
        foreach ($options as $key => $option) {
205
            $isSelected = '';
206
            if ($selected === $key) {
207
                $isSelected = ' selected';
208
            }
209
            $out .= '<option value = "' . $key . '"' . $isSelected . '>' . $option . '</option>';
210
        }
211
        $out .= '</select>';
212
        return $out;
213
    }
214
215
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
216
217
    /**
218
     *    Return list of available menus (eldy_backoffice, ...)
219
     *
220
     * @param string $selected Preselected menu value
221
     * @param string $htmlname Name of html select
222
     * @param array $dirmenuarray Array of directories to scan
223
     * @param string $moreattrib More attributes on html select tag
224
     *
225
     * @return   integer|void
226
     */
227
    public function select_menu($selected, $htmlname, $dirmenuarray, $moreattrib = '')
228
    {
229
        // phpcs:enable
230
        global $langs, $conf;
231
232
        // Clean parameters
233
234
235
        // Check parameters
236
        if (!is_array($dirmenuarray)) {
237
            return -1;
238
        }
239
240
        $menuarray = [];
241
        foreach ($conf->file->dol_document_root as $dirroot) {
242
            foreach ($dirmenuarray as $dirtoscan) {
243
                $dir = $dirroot . $dirtoscan;
244
                //print $dir.'<br>';
245
                if (is_dir($dir)) {
246
                    $handle = opendir($dir);
247
                    if (is_resource($handle)) {
248
                        while (($file = readdir($handle)) !== false) {
249
                            if (is_file($dir . "/" . $file) && substr($file, 0, 1) != '.' && substr($file, 0, 3) != 'CVS' && substr($file, 0, 5) != 'index') {
250
                                if (preg_match('/lib\.php$/i', $file)) {
251
                                    continue; // We exclude library files
252
                                }
253
                                if (preg_match('/eldy_(backoffice|frontoffice)\.php$/i', $file)) {
254
                                    continue; // We exclude all menu manager files
255
                                }
256
                                if (preg_match('/auguria_(backoffice|frontoffice)\.php$/i', $file)) {
257
                                    continue; // We exclude all menu manager files
258
                                }
259
                                if (preg_match('/smartphone_(backoffice|frontoffice)\.php$/i', $file)) {
260
                                    continue; // We exclude all menu manager files
261
                                }
262
263
                                $filelib = preg_replace('/\.php$/i', '', $file);
264
                                $prefix = '';
265
                                // 0=Recommended, 1=Experimental, 2=Development, 3=Other
266
                                if (preg_match('/^eldy/i', $file)) {
267
                                    $prefix = '0';
268
                                } elseif (preg_match('/^smartphone/i', $file)) {
269
                                    $prefix = '2';
270
                                } else {
271
                                    $prefix = '3';
272
                                }
273
274
                                if ($file == $selected) {
275
                                    $menuarray[$prefix . '_' . $file] = '<option value="' . $file . '" selected>' . $filelib . '</option>';
276
                                } else {
277
                                    $menuarray[$prefix . '_' . $file] = '<option value="' . $file . '">' . $filelib . '</option>';
278
                                }
279
                            }
280
                        }
281
                        closedir($handle);
282
                    }
283
                }
284
            }
285
        }
286
        ksort($menuarray);
287
288
        // Output combo list of menus
289
        print '<select class="flat" id="' . $htmlname . '" name="' . $htmlname . '"' . ($moreattrib ? ' ' . $moreattrib : '') . '>';
290
        $oldprefix = '';
291
        foreach ($menuarray as $key => $val) {
292
            $tab = explode('_', $key);
293
            $newprefix = $tab[0];
294
            if ($newprefix == '1' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1)) {
295
                continue;
296
            }
297
            if ($newprefix == '2' && (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2)) {
298
                continue;
299
            }
300
            if ($newprefix != $oldprefix) { // Add separators
301
                // Affiche titre
302
                print '<option value="-1" disabled>';
303
                if ($newprefix == '0') {
304
                    print '-- ' . $langs->trans("VersionRecommanded") . ' --';
305
                }
306
                if ($newprefix == '1') {
307
                    print '-- ' . $langs->trans("VersionExperimental") . ' --';
308
                }
309
                if ($newprefix == '2') {
310
                    print '-- ' . $langs->trans("VersionDevelopment") . ' --';
311
                }
312
                if ($newprefix == '3') {
313
                    print '-- ' . $langs->trans("Other") . ' --';
314
                }
315
                print '</option>';
316
                $oldprefix = $newprefix;
317
            }
318
            print $val . "\n"; // Show menu entry
319
        }
320
        print '</select>';
321
322
        return;
323
    }
324
325
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
326
327
    /**
328
     *  Return combo list of available menu families
329
     *
330
     * @param string $selected Menu pre-selected
331
     * @param string $htmlname Name of html select
332
     * @param string[] $dirmenuarray Directories to scan
333
     *
334
     * @return void
335
     */
336
    public function select_menu_families($selected, $htmlname, $dirmenuarray)
337
    {
338
        // phpcs:enable
339
        global $langs, $conf;
340
341
        //$expdevmenu=array('smartphone_backoffice.php','smartphone_frontoffice.php');  // Menu to disable if $conf->global->MAIN_FEATURES_LEVEL is not set
342
        $expdevmenu = [];
343
344
        $menuarray = [];
345
346
        foreach ($dirmenuarray as $dirmenu) {
347
            foreach ($conf->file->dol_document_root as $dirroot) {
348
                $dir = $dirroot . $dirmenu;
349
                if (is_dir($dir)) {
350
                    $handle = opendir($dir);
351
                    if (is_resource($handle)) {
352
                        while (($file = readdir($handle)) !== false) {
353
                            if (is_file($dir . "/" . $file) && substr($file, 0, 1) != '.' && substr($file, 0, 3) != 'CVS') {
354
                                $filelib = preg_replace('/(_backoffice|_frontoffice)?\.php$/i', '', $file);
355
                                if (preg_match('/^index/i', $filelib)) {
356
                                    continue;
357
                                }
358
                                if (preg_match('/^default/i', $filelib)) {
359
                                    continue;
360
                                }
361
                                if (preg_match('/^empty/i', $filelib)) {
362
                                    continue;
363
                                }
364
                                if (preg_match('/\.lib/i', $filelib)) {
365
                                    continue;
366
                                }
367
                                if (getDolGlobalInt('MAIN_FEATURES_LEVEL') == 0 && in_array($file, $expdevmenu)) {
368
                                    continue;
369
                                }
370
371
                                $menuarray[$filelib] = 1;
372
                            }
373
                            $menuarray['all'] = 1;
374
                        }
375
                        closedir($handle);
376
                    }
377
                }
378
            }
379
        }
380
381
        ksort($menuarray);
382
383
        // Affichage liste deroulante des menus
384
        print '<select class="flat maxwidth100" id="' . $htmlname . '" name="' . $htmlname . '">';
385
        $oldprefix = '';
386
        foreach ($menuarray as $key => $val) {
387
            $tab = explode('_', $key);
388
            $newprefix = $tab[0];
389
            print '<option value="' . $key . '"';
390
            if ($key == $selected) {
391
                print '	selected';
392
            }
393
            print '>';
394
            if ($key == 'all') {
395
                print $langs->trans("AllMenus");
396
            } else {
397
                print $key;
398
            }
399
            print '</option>' . "\n";
400
        }
401
        print '</select>';
402
403
        print ajax_combobox($htmlname);
404
    }
405
406
407
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
408
409
    /**
410
     *  Return a HTML select list of timezones
411
     *
412
     * @param string $selected Menu pre-selectionnee
413
     * @param string $htmlname Nom de la zone select
414
     *
415
     * @return void
416
     */
417
    public function select_timezone($selected, $htmlname)
418
    {
419
        // phpcs:enable
420
        print '<select class="flat" id="' . $htmlname . '" name="' . $htmlname . '">';
421
        print '<option value="-1">&nbsp;</option>';
422
423
        $arraytz = [
424
            "Pacific/Midway" => "GMT-11:00",
425
            "Pacific/Fakaofo" => "GMT-10:00",
426
            "America/Anchorage" => "GMT-09:00",
427
            "America/Los_Angeles" => "GMT-08:00",
428
            "America/Dawson_Creek" => "GMT-07:00",
429
            "America/Chicago" => "GMT-06:00",
430
            "America/Bogota" => "GMT-05:00",
431
            "America/Anguilla" => "GMT-04:00",
432
            "America/Araguaina" => "GMT-03:00",
433
            "America/Noronha" => "GMT-02:00",
434
            "Atlantic/Azores" => "GMT-01:00",
435
            "Africa/Abidjan" => "GMT+00:00",
436
            "Europe/Paris" => "GMT+01:00",
437
            "Europe/Helsinki" => "GMT+02:00",
438
            "Europe/Moscow" => "GMT+03:00",
439
            "Asia/Dubai" => "GMT+04:00",
440
            "Asia/Karachi" => "GMT+05:00",
441
            "Indian/Chagos" => "GMT+06:00",
442
            "Asia/Jakarta" => "GMT+07:00",
443
            "Asia/Hong_Kong" => "GMT+08:00",
444
            "Asia/Tokyo" => "GMT+09:00",
445
            "Australia/Sydney" => "GMT+10:00",
446
            "Pacific/Noumea" => "GMT+11:00",
447
            "Pacific/Auckland" => "GMT+12:00",
448
            "Pacific/Enderbury" => "GMT+13:00",
449
        ];
450
        foreach ($arraytz as $lib => $gmt) {
451
            print '<option value="' . $lib . '"';
452
            if ($selected == $lib || $selected == $gmt) {
453
                print ' selected';
454
            }
455
            print '>' . $gmt . '</option>' . "\n";
456
        }
457
        print '</select>';
458
    }
459
460
461
462
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
463
464
    /**
465
     *  Return html select list with available languages (key='en_US', value='United States' for example)
466
     *
467
     * @param string $selected Paper format pre-selected
468
     * @param string $htmlname Name of HTML select field
469
     * @param string $filter Value to filter on code
470
     * @param int $showempty Add empty value
471
     * @param int $forcecombo Force to load all values and output a standard combobox (with no beautification)
472
     *
473
     * @return     string                  Return HTML output
474
     */
475
    public function select_paper_format($selected = '', $htmlname = 'paperformat_id', $filter = '', $showempty = 0, $forcecombo = 0)
476
    {
477
        // phpcs:enable
478
        global $langs;
479
480
        $langs->load("dict");
481
482
        $sql = "SELECT code, label, width, height, unit";
483
        $sql .= " FROM " . $this->db->prefix() . "c_paper_format";
484
        $sql .= " WHERE active=1";
485
        if ($filter) {
486
            $sql .= " AND code LIKE '%" . $this->db->escape($filter) . "%'";
487
        }
488
489
        $paperformat = [];
490
491
        $resql = $this->db->query($sql);
492
        if ($resql) {
493
            $num = $this->db->num_rows($resql);
494
            $i = 0;
495
            while ($i < $num) {
496
                $obj = $this->db->fetch_object($resql);
497
                $unitKey = $langs->trans('SizeUnit' . $obj->unit);
498
499
                $paperformat[$obj->code] = $langs->trans('PaperFormat' . strtoupper($obj->code)) . ' - ' . round($obj->width) . 'x' . round($obj->height) . ' ' . ($unitKey == 'SizeUnit' . $obj->unit ? $obj->unit : $unitKey);
500
501
                $i++;
502
            }
503
        } else {
504
            dol_print_error($this->db);
505
            return '';
506
        }
507
        $out = '';
508
509
        $out .= '<select class="flat" id="' . $htmlname . '" name="' . $htmlname . '">';
510
        if ($showempty) {
511
            $out .= '<option value=""';
512
            if ($selected == '') {
513
                $out .= ' selected';
514
            }
515
            $out .= '>&nbsp;</option>';
516
        }
517
        foreach ($paperformat as $key => $value) {
518
            if ($selected == $key) {
519
                $out .= '<option value="' . $key . '" selected>' . $value . '</option>';
520
            } else {
521
                $out .= '<option value="' . $key . '">' . $value . '</option>';
522
            }
523
        }
524
        $out .= '</select>';
525
526
        if (!$forcecombo) {
527
            include_once BASE_PATH . '/../Dolibarr/Lib/Ajax.php';
528
            $out .= ajax_combobox($htmlname);
529
        }
530
531
        return $out;
532
    }
533
534
535
    /**
536
     * Function to show the combo select to chose a type of field (varchar, int, email, ...)
537
     *
538
     * @param string $htmlname Name of HTML select component
539
     * @param string $type Type preselected
540
     * @param array $typewecanchangeinto Array of possible switch combination from 1 type to another one. This will
541
     *                                    grey not possible combinations.
542
     *
543
     * @return  string                          The combo HTML select component
544
     */
545
    public function selectTypeOfFields($htmlname, $type, $typewecanchangeinto = [])
546
    {
547
        global $type2label; // TODO Remove this
548
549
        $out = '';
550
551
        $out .= '<select class="flat type" id="' . $htmlname . '" name="' . $htmlname . '">';
552
        foreach ($type2label as $key => $val) {
553
            $selected = '';
554
            if ($key == $type) {
555
                $selected = ' selected="selected"';
556
            }
557
558
            // Set $valhtml with the picto for the type
559
            $valhtml = ($key ? getPictoForType($key) : '') . $val;
560
561
            if (empty($typewecanchangeinto) || in_array($key, $typewecanchangeinto[$type])) {
562
                $out .= '<option value="' . $key . '"' . $selected . ' data-html="' . dol_escape_htmltag($valhtml) . '">' . ($val ? $val : '&nbsp;') . '</option>';
563
            } else {
564
                $out .= '<option value="' . $key . '" disabled="disabled"' . $selected . ' data-html="' . dol_escape_htmltag($valhtml) . '">' . ($val ? $val : '&nbsp;') . '</option>';
565
            }
566
        }
567
        $out .= '</select>';
568
        $out .= ajax_combobox('type');
569
570
        return $out;
571
    }
572
}
573