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