Issues (2811)

public/htdocs/admin/defaultvalues.php (3 issues)

1
<?php
2
3
/* Copyright (C) 2017-2020  Laurent Destailleur         <[email protected]>
4
 * Copyright (C) 2017-2018	Regis Houssin		        <[email protected]>
5
 * Copyright (C) 2024       Rafael San José             <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
use Dolibarr\Code\Core\Classes\DefaultValues;
22
use Dolibarr\Code\Core\Classes\Form;
23
use Dolibarr\Code\Core\Classes\FormAdmin;
24
use Dolibarr\Lib\ViewMain;
25
26
/**
27
 *       \file      htdocs/admin/defaultvalues.php
28
 *       \brief     Page to set default values used used in a create form
29
 *                  Default values are stored into $user->default_values[url]['createform']['querystring'|'_noquery_'][paramkey]=paramvalue
30
 *                  Default filters are stored into $user->default_values[url]['filters']['querystring'|'_noquery_'][paramkey]=paramvalue
31
 *                  Default sort order are stored into $user->default_values[url]['sortorder']['querystring'|'_noquery_'][paramkey]=paramvalue
32
 *                  Default focus are stored into $user->default_values[url]['focus']['querystring'|'_noquery_'][paramkey]=paramvalue
33
 *                  Mandatory fields are stored into $user->default_values[url]['mandatory']['querystring'|'_noquery_'][paramkey]=paramvalue
34
 */
35
36
// Load Dolibarr environment
37
require constant('DOL_DOCUMENT_ROOT') . '/main.inc.php';
38
require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/admin.lib.php';
39
require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/files.lib.php';
40
41
// Load translation files required by the page
42
$langs->loadLangs(array('companies', 'products', 'admin', 'sms', 'other', 'errors'));
43
44
if (!$user->admin) {
45
    accessforbidden();
46
}
47
48
$id = GETPOSTINT('rowid');
49
$action = GETPOST('action', 'aZ09');
50
$optioncss = GETPOST('optionscss', 'alphanohtml');
51
52
$mode = GETPOST('mode', 'aZ09') ? GETPOST('mode', 'aZ09') : 'createform'; // 'createform', 'filters', 'sortorder', 'focus'
53
54
$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
55
$sortfield = GETPOST('sortfield', 'aZ09comma');
56
$sortorder = GETPOST('sortorder', 'aZ09comma');
57
$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
58
if (empty($page) || $page == -1) {
59
    $page = 0;
60
}     // If $page is not defined, or '' or -1
61
$offset = $limit * $page;
62
$pageprev = $page - 1;
63
$pagenext = $page + 1;
64
if (!$sortfield) {
65
    $sortfield = 'page,param';
66
}
67
if (!$sortorder) {
68
    $sortorder = 'ASC';
69
}
70
71
$defaulturl = GETPOST('defaulturl', 'alphanohtml');
72
$defaultkey = GETPOST('defaultkey', 'alphanohtml');
73
$defaultvalue = GETPOST('defaultvalue', 'restricthtml');
74
75
$defaulturl = preg_replace('/^\//', '', $defaulturl);
76
77
$urlpage = GETPOST('urlpage', 'alphanohtml');
78
$key = GETPOST('key', 'alphanohtml');
79
$value = GETPOST('value', 'restricthtml');
80
81
// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
82
$hookmanager->initHooks(array('admindefaultvalues', 'globaladmin'));
83
84
85
$object = new DefaultValues($db);
86
/*
87
 * Actions
88
 */
89
90
if (GETPOST('cancel', 'alpha')) {
91
    $action = 'list';
92
    $massaction = '';
93
}
94
if (!GETPOST('confirmmassaction', 'alpha') && !empty($massaction) && $massaction != 'presend' && $massaction != 'confirm_presend') {
95
    $massaction = '';
96
}
97
98
$parameters = array();
99
$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
100
if ($reshook < 0) {
101
    setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
102
}
103
104
include DOL_DOCUMENT_ROOT . '/core/actions_changeselectedfields.inc.php';
105
106
// Purge search criteria
107
if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
108
    $defaulturl = '';
109
    $defaultkey = '';
110
    $defaultvalue = '';
111
    $toselect = array();
112
    $search_array_options = array();
113
}
114
115
if ($action == 'setMAIN_ENABLE_DEFAULT_VALUES') {
116
    if (GETPOST('value')) {
117
        dolibarr_set_const($db, 'MAIN_ENABLE_DEFAULT_VALUES', 1, 'chaine', 0, '', $conf->entity);
118
    } else {
119
        dolibarr_set_const($db, 'MAIN_ENABLE_DEFAULT_VALUES', 0, 'chaine', 0, '', $conf->entity);
120
    }
121
}
122
123
if (($action == 'add' || (GETPOST('add') && $action != 'update')) || GETPOST('actionmodify')) {
124
    $error = 0;
125
126
    if (($action == 'add' || (GETPOST('add') && $action != 'update'))) {
127
        if (empty($defaulturl)) {
128
            setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors');
129
            $error++;
130
        }
131
        if (empty($defaultkey)) {
132
            setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors');
133
            $error++;
134
        }
135
    }
136
    if (GETPOST('actionmodify')) {
137
        if (empty($urlpage)) {
138
            setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors');
139
            $error++;
140
        }
141
        if (empty($key)) {
142
            setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors');
143
            $error++;
144
        }
145
    }
146
147
    if (!$error) {
148
        if ($action == 'add' || (GETPOST('add') && $action != 'update')) {
149
            $object->type = $mode;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mode can also be of type array or array or array. However, the property $type is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
150
            $object->user_id = 0;
151
            $object->page = $defaulturl;
152
            $object->param = $defaultkey;
153
            $object->value = $defaultvalue;
0 ignored issues
show
Documentation Bug introduced by
It seems like $defaultvalue can also be of type array or array or array. However, the property $value is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
154
            $object->entity = $conf->entity;
155
            $result = $object->create($user);
156
            if ($result < 0) {
157
                $action = '';
158
                setEventMessages($object->error, $object->errors, 'errors');
159
            } else {
160
                setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
161
                $action = '';
162
                $defaulturl = '';
163
                $defaultkey = '';
164
                $defaultvalue = '';
165
            }
166
        }
167
        if (GETPOST('actionmodify')) {
168
            $object->id = $id;
169
            $object->type = $mode;
170
            $object->page = $urlpage;
0 ignored issues
show
Documentation Bug introduced by
It seems like $urlpage can also be of type array or array or array. However, the property $page is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
171
            $object->param = $key;
172
            $object->value = $value;
173
            $object->entity = $conf->entity;
174
            $result = $object->update($user);
175
            if ($result < 0) {
176
                $action = '';
177
                setEventMessages($object->error, $object->errors, 'errors');
178
            } else {
179
                setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
180
                $action = "";
181
                $defaulturl = '';
182
                $defaultkey = '';
183
                $defaultvalue = '';
184
            }
185
        }
186
    }
187
}
188
189
// Delete line from delete picto
190
if ($action == 'delete') {
191
    $object->id = $id;
192
    $result = $object->delete($user);
193
    if ($result < 0) {
194
        $action = '';
195
        setEventMessages($object->error, $object->errors, 'errors');
196
    }
197
}
198
199
200
/*
201
 * View
202
 */
203
204
$form = new Form($db);
205
$formadmin = new FormAdmin($db);
206
207
$wikihelp = 'EN:First_setup|FR:Premiers_paramétrages|ES:Primeras_configuraciones';
208
ViewMain::llxHeader('', $langs->trans("Setup"), $wikihelp, '', 0, 0, '', '', '', 'mod-admin page-defaultvalues');
209
210
$param = '&mode=' . $mode;
211
212
$enabledisablehtml = $langs->trans("EnableDefaultValues") . ' ';
213
if (!getDolGlobalString('MAIN_ENABLE_DEFAULT_VALUES')) {
214
    // Button off, click to enable
215
    $enabledisablehtml .= '<a class="reposition valignmiddle" href="' . $_SERVER["PHP_SELF"] . '?action=setMAIN_ENABLE_DEFAULT_VALUES&token=' . newToken() . '&value=1' . $param . '">';
216
    $enabledisablehtml .= img_picto($langs->trans("Disabled"), 'switch_off');
217
    $enabledisablehtml .= '</a>';
218
} else {
219
    // Button on, click to disable
220
    $enabledisablehtml .= '<a class="reposition valignmiddle" href="' . $_SERVER["PHP_SELF"] . '?action=setMAIN_ENABLE_DEFAULT_VALUES&token=' . newToken() . '&value=0' . $param . '">';
221
    $enabledisablehtml .= img_picto($langs->trans("Activated"), 'switch_on');
222
    $enabledisablehtml .= '</a>';
223
}
224
225
print load_fiche_titre($langs->trans("DefaultValues"), $enabledisablehtml, 'title_setup');
226
227
print '<span class="opacitymedium">' . $langs->trans("DefaultValuesDesc") . "</span><br>\n";
228
print "<br>\n";
229
230
if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
231
    $param .= '&contextpage=' . urlencode($contextpage);
232
}
233
if ($limit > 0 && $limit != $conf->liste_limit) {
234
    $param .= '&limit=' . ((int)$limit);
235
}
236
if ($optioncss != '') {
237
    $param .= '&optioncss=' . urlencode($optioncss);
238
}
239
if ($defaulturl) {
240
    $param .= '&defaulturl=' . urlencode($defaulturl);
241
}
242
if ($defaultkey) {
243
    $param .= '&defaultkey=' . urlencode($defaultkey);
244
}
245
if ($defaultvalue) {
246
    $param .= '&defaultvalue=' . urlencode($defaultvalue);
247
}
248
249
250
print '<form action="' . $_SERVER["PHP_SELF"] . '" method="POST">';
251
if ($optioncss != '') {
252
    print '<input type="hidden" name="optioncss" value="' . $optioncss . '">';
253
}
254
print '<input type="hidden" name="token" value="' . newToken() . '">';
255
print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
256
print '<input type="hidden" name="action" value="list">';
257
print '<input type="hidden" name="sortfield" value="' . $sortfield . '">';
258
print '<input type="hidden" name="sortorder" value="' . $sortorder . '">';
259
print '<input type="hidden" name="page" value="' . $page . '">';
260
261
$head = defaultvalues_prepare_head();
262
263
print dol_get_fiche_head($head, $mode, '', -1, '');
264
265
if ($mode == 'sortorder') {
266
    print info_admin($langs->trans("WarningSettingSortOrder")) . '<br>';
267
}
268
if ($mode == 'mandatory') {
269
    print info_admin($langs->trans("FeatureSupportedOnTextFieldsOnly")) . '<br>';
270
}
271
272
print '<input type="hidden" name="token" value="' . newToken() . '">';
273
print '<input type="hidden" id="action" name="action" value="">';
274
print '<input type="hidden" id="mode" name="mode" value="' . dol_escape_htmltag($mode) . '">';
275
276
print '<div class="div-table-responsive-no-min">';
277
print '<table class="noborder centpercent">';
278
print '<tr class="liste_titre">';
279
// Page
280
$texthelp = $langs->trans("PageUrlForDefaultValues");
281
if ($mode == 'createform') {
282
    $texthelp .= $langs->trans("PageUrlForDefaultValuesCreate", 'societe/card.php', 'societe/card.php?abc=val1&def=val2');
283
} else {
284
    $texthelp .= $langs->trans("PageUrlForDefaultValuesList", 'societe/list.php', 'societe/list.php?abc=val1&def=val2');
285
}
286
$texthelp .= '<br><br>' . $langs->trans("AlsoDefaultValuesAreEffectiveForActionCreate");
287
$texturl = $form->textwithpicto($langs->trans("RelativeURL"), $texthelp);
288
print_liste_field_titre($texturl, $_SERVER["PHP_SELF"], 'page,param', '', $param, '', $sortfield, $sortorder);
289
// Field
290
$texthelp = $langs->trans("TheKeyIsTheNameOfHtmlField");
291
if ($mode != 'sortorder') {
292
    $textkey = $form->textwithpicto($langs->trans("Field"), $texthelp);
293
} else {
294
    $texthelp = 'field or alias.field';
295
    $textkey = $form->textwithpicto($langs->trans("Field"), $texthelp);
296
}
297
print_liste_field_titre($textkey, $_SERVER["PHP_SELF"], 'param', '', $param, '', $sortfield, $sortorder);
298
// Value
299
if ($mode != 'focus' && $mode != 'mandatory') {
300
    if ($mode != 'sortorder') {
301
        $substitutionarray = getCommonSubstitutionArray($langs, 2, array('object', 'objectamount')); // Must match list into GETPOST
302
        unset($substitutionarray['__USER_SIGNATURE__']);
303
        unset($substitutionarray['__SENDEREMAIL_SIGNATURE__']);
304
        $texthelp = $langs->trans("FollowingConstantsWillBeSubstituted") . '<br>';
305
        foreach ($substitutionarray as $key => $val) {
306
            $texthelp .= $key . ' -> ' . $val . '<br>';
307
        }
308
        $textvalue = $form->textwithpicto($langs->trans("Value"), $texthelp, 1, 'help', '', 0, 2, 'subsitutiontooltip');
309
    } else {
310
        $texthelp = 'ASC or DESC';
311
        $textvalue = $form->textwithpicto($langs->trans("SortOrder"), $texthelp);
312
    }
313
    print_liste_field_titre($textvalue, $_SERVER["PHP_SELF"], 'value', '', $param, '', $sortfield, $sortorder);
314
}
315
// Entity
316
if (isModEnabled('multicompany') && !$user->entity) {
317
    print_liste_field_titre("Entity", $_SERVER["PHP_SELF"], 'entity,page', '', $param, '', $sortfield, $sortorder);
318
} else {
319
    print_liste_field_titre("", $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
320
}
321
// Actions
322
print_liste_field_titre("", $_SERVER["PHP_SELF"], '', '', $param, '', $sortfield, $sortorder);
323
print "</tr>\n";
324
325
326
// Line to add new record
327
print "\n";
328
329
print '<tr class="oddeven">';
330
// Page
331
print '<td>';
332
print '<input type="text" class="flat minwidth200 maxwidthonsmartphone" name="defaulturl" value="' . dol_escape_htmltag($defaulturl) . '">';
333
print '</td>' . "\n";
334
// Field
335
print '<td>';
336
print '<input type="text" class="flat maxwidth100onsmartphone" name="defaultkey" value="' . dol_escape_htmltag($defaultkey) . '">';
337
print '</td>';
338
// Value
339
if ($mode != 'focus' && $mode != 'mandatory') {
340
    print '<td>';
341
    print '<input type="text" class="flat maxwidth100onsmartphone" name="defaultvalue" value="' . dol_escape_htmltag($defaultvalue) . '">';
342
    print '</td>';
343
}
344
// Limit to superadmin
345
if (isModEnabled('multicompany') && !$user->entity) {
346
    print '<td>';
347
    print '<input type="text" class="flat" size="1" disabled name="entity" value="' . $conf->entity . '">'; // We see environment, but to change it we must switch on other entity
348
    print '</td>';
349
} else {
350
    print '<td class="center">';
351
    print '<input type="hidden" name="entity" value="' . $conf->entity . '">';
352
    print '</td>';
353
}
354
print '<td class="center">';
355
$disabled = '';
356
if (!getDolGlobalString('MAIN_ENABLE_DEFAULT_VALUES')) {
357
    $disabled = ' disabled="disabled"';
358
}
359
print '<input type="submit" class="button"' . $disabled . ' value="' . $langs->trans("Add") . '" name="add">';
360
print '</td>' . "\n";
361
print '</tr>' . "\n";
362
363
$result = $object->fetchAll($sortorder, $sortfield, 0, 0, array('t.type' => $mode, 't.entity' => array($user->entity, $conf->entity)));
364
365
if (!is_array($result) && $result < 0) {
366
    setEventMessages($object->error, $object->errors, 'errors');
367
} elseif (is_array($result) && count($result) > 0) {
368
    foreach ($result as $key => $defaultvalue) {
369
        print '<tr class="oddeven">';
370
371
        // Page
372
        print '<td>';
373
        if ($action != 'edit' || GETPOSTINT('rowid') != $defaultvalue->id) {
374
            print $defaultvalue->page;
375
        } else {
376
            print '<input type="text" name="urlpage" value="' . dol_escape_htmltag($defaultvalue->page) . '">';
377
        }
378
        print '</td>' . "\n";
379
380
        // Field
381
        print '<td>';
382
        if ($action != 'edit' || GETPOST('rowid') != $defaultvalue->id) {
383
            print $defaultvalue->param;
384
        } else {
385
            print '<input type="text" name="key" value="' . dol_escape_htmltag($defaultvalue->param) . '">';
386
        }
387
        print '</td>' . "\n";
388
389
        // Value
390
        if ($mode != 'focus' && $mode != 'mandatory') {
391
            print '<td>';
392
            if ($action != 'edit' || GETPOST('rowid') != $defaultvalue->id) {
393
                print dol_escape_htmltag($defaultvalue->value);
394
            } else {
395
                print '<input type="text" name="value" value="' . dol_escape_htmltag($defaultvalue->value) . '">';
396
            }
397
            print '</td>';
398
        }
399
400
        // Multicompany
401
        print '<td>';
402
        if (isModEnabled('multicompany')) {
403
            print dol_escape_htmltag($defaultvalue->entity);
404
        }
405
        print '</td>';
406
407
        // Actions
408
        print '<td class="center">';
409
        if ($action != 'edit' || GETPOST('rowid') != $defaultvalue->id) {
410
            print '<a class="editfielda marginleftonly marginrightonly" href="' . $_SERVER['PHP_SELF'] . '?rowid=' . $defaultvalue->id . '&entity=' . $defaultvalue->entity . '&mode=' . $mode . '&action=edit&token=' . newToken() . '">' . img_edit() . '</a>';
411
            print '<a class="marginleftonly marginrightonly" href="' . $_SERVER['PHP_SELF'] . '?rowid=' . $defaultvalue->id . '&entity=' . $defaultvalue->entity . '&mode=' . $mode . '&action=delete&token=' . newToken() . '">' . img_delete() . '</a>';
412
        } else {
413
            print '<input type="hidden" name="page" value="' . $page . '">';
414
            print '<input type="hidden" name="rowid" value="' . $id . '">';
415
            print '<div name="' . (!empty($defaultvalue->id) ? $defaultvalue->id : 'none') . '"></div>';
416
            print '<input type="submit" class="button button-edit" name="actionmodify" value="' . $langs->trans("Modify") . '">';
417
            print '<input type="submit" class="button button-cancel" name="actioncancel" value="' . $langs->trans("Cancel") . '">';
418
        }
419
        print '</td>';
420
421
        print "</tr>\n";
422
    }
423
}
424
425
print '</table>';
426
print '</div>';
427
428
print dol_get_fiche_end();
429
430
print "</form>\n";
431
432
// End of page
433
ViewMain::llxFooter();
434
$db->close();
435