dolibarr 21.0.0-alpha
actions_setmoduleoptions.inc.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2014-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 * or see https://www.gnu.org/
18 */
19
25// $error must have been initialized to 0
26// $action must be defined
27// $arrayofparameters must be set to list of parameters to update for action 'update' on constants
28// $nomessageinupdate can be set to 1
29// $nomessageinsetmoduleoptions can be set to 1
30// $formSetup may be defined
31
32
33if ($action == 'update' && !empty($formSetup) && is_object($formSetup) && !empty($user->admin)) {
34 $formSetup->saveConfFromPost();
35 return;
36}
37
38'
39@phan-var-force FormSetup $formSetup
40';
41
42$upload_dir = null;
43
44if ($action == 'update' && !empty($arrayofparameters) && is_array($arrayofparameters) && !empty($user->admin)) {
45 $db->begin();
46
47 foreach ($arrayofparameters as $key => $val) {
48 // Modify constant only if key was posted (avoid resetting key to the null value)
49 if (GETPOSTISSET($key)) {
50 if (!empty($val['type']) && preg_match('/category:/', $val['type'])) {
51 if (GETPOSTINT($key) == '-1') {
52 $val_const = '';
53 } else {
54 $val_const = GETPOSTINT($key);
55 }
56 } elseif ($val['type'] == 'html') {
57 $val_const = GETPOST($key, 'restricthtml');
58 } else {
59 $val_const = GETPOST($key, 'alpha');
60 }
61
62 $result = dolibarr_set_const($db, $key, $val_const, 'chaine', 0, '', $conf->entity);
63 if ($result < 0) {
64 $error++;
65 break;
66 }
67 }
68 }
69
70 if (!$error) {
71 $db->commit();
72 if (empty($nomessageinupdate)) {
73 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
74 }
75 } else {
76 $db->rollback();
77 if (empty($nomessageinupdate)) {
78 setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
79 }
80 }
81}
82
83if ($action == 'deletefile' && $modulepart == 'doctemplates' && !empty($user->admin)) {
84 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
85 $keyforuploaddir = GETPOST('keyforuploaddir', 'aZ09');
86 $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString($keyforuploaddir))));
87
88 foreach ($listofdir as $key => $tmpdir) {
89 $tmpdir = preg_replace('/DOL_DATA_ROOT\/*/', '', $tmpdir); // Clean string if we found a hardcoded DOL_DATA_ROOT
90 if (!$tmpdir) {
91 unset($listofdir[$key]);
92 continue;
93 }
94 $tmpdir = DOL_DATA_ROOT.'/'.$tmpdir; // Complete with DOL_DATA_ROOT. Only files into DOL_DATA_ROOT can be reach/set
95 if (!is_dir($tmpdir)) {
96 if (empty($nomessageinsetmoduleoptions)) {
97 setEventMessages($langs->trans("ErrorDirNotFound", $tmpdir), null, 'warnings');
98 }
99 } else {
100 $upload_dir = $tmpdir;
101 break; // So we take the first directory found into setup $conf->global->$keyforuploaddir
102 }
103 }
104
105 $filetodelete = $tmpdir.'/'.GETPOST('file');
106 $result = dol_delete_file($filetodelete);
107 if ($result > 0) {
108 setEventMessages($langs->trans("FileWasRemoved", GETPOST('file')), null, 'mesgs');
109 }
110}
111
112// Define constants for submodules that contains parameters (forms with param1, param2, ... and value1, value2, ...)
113if ($action == 'setModuleOptions' && !empty($user->admin)) {
114 $db->begin();
115
116 // Process common param fields
117 if (is_array($_POST)) {
118 foreach ($_POST as $key => $val) {
119 $reg = array();
120 if (preg_match('/^param(\d*)$/', $key, $reg)) { // Works for POST['param'], POST['param1'], POST['param2'], ...
121 $param = GETPOST("param".$reg[1], 'alpha');
122 $value = GETPOST("value".$reg[1], 'alpha');
123 if ($param) {
124 $res = dolibarr_set_const($db, $param, $value, 'chaine', 0, '', $conf->entity);
125 if (!($res > 0)) {
126 $error++;
127 }
128 }
129 }
130 }
131 }
132
133 // Process upload fields
134 if (GETPOST('upload', 'alpha') && GETPOST('keyforuploaddir', 'aZ09')) {
135 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
136 $keyforuploaddir = GETPOST('keyforuploaddir', 'aZ09');
137 $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString($keyforuploaddir))));
138
139 foreach ($listofdir as $key => $tmpdir) {
140 $tmpdir = trim($tmpdir);
141 $tmpdir = preg_replace('/DOL_DATA_ROOT\/*/', '', $tmpdir); // Clean string if we found a hardcoded DOL_DATA_ROOT
142 if (!$tmpdir) {
143 unset($listofdir[$key]);
144 continue;
145 }
146 $tmpdir = DOL_DATA_ROOT.'/'.$tmpdir; // Complete with DOL_DATA_ROOT. Only files into DOL_DATA_ROOT can be reach/set
147 if (!is_dir($tmpdir)) {
148 if (empty($nomessageinsetmoduleoptions)) {
149 setEventMessages($langs->trans("ErrorDirNotFound", $tmpdir), null, 'warnings');
150 }
151 } else {
152 $upload_dir = $tmpdir;
153 break; // So we take the first directory found into setup $conf->global->$keyforuploaddir
154 }
155 }
156
157 if ($upload_dir) {
158 $result = dol_add_file_process($upload_dir, 1, 1, 'uploadfile', '');
159 if ($result <= 0) {
160 $error++;
161 }
162 }
163 }
164
165 if (!$error) {
166 $db->commit();
167 if (empty($nomessageinsetmoduleoptions)) {
168 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
169 }
170 } else {
171 $db->rollback();
172 if (empty($nomessageinsetmoduleoptions)) {
173 setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
174 }
175 }
176}
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dol_add_file_process($upload_dir, $allowoverwrite=0, $updatesessionordb=0, $varfiles='addedfile', $savingdocmask='', $link=null, $trackid='', $generatethumbs=1, $object=null)
Get and save an upload file (for example after submitting a new file a mail form).
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.