dolibarr 19.0.3
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 for action 'update'
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' && 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 (GETPOST($key, 'int') == '-1') {
46 $val_const = '';
47 } else {
48 $val_const = GETPOST($key, 'int');
49 }
50 } else {
51 $val_const = GETPOST($key, 'alpha');
52 }
53
54 $result = dolibarr_set_const($db, $key, $val_const, 'chaine', 0, '', $conf->entity);
55 if ($result < 0) {
56 $error++;
57 break;
58 }
59 }
60 }
61
62 if (!$error) {
63 $db->commit();
64 if (empty($nomessageinupdate)) {
65 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
66 }
67 } else {
68 $db->rollback();
69 if (empty($nomessageinupdate)) {
70 setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
71 }
72 }
73}
74
75if ($action == 'deletefile' && $modulepart == 'doctemplates' && !empty($user->admin)) {
76 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
77 $keyforuploaddir = GETPOST('keyforuploaddir', 'aZ09');
78 $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString($keyforuploaddir))));
79
80 foreach ($listofdir as $key => $tmpdir) {
81 $tmpdir = preg_replace('/DOL_DATA_ROOT\/*/', '', $tmpdir); // Clean string if we found a hardcoded DOL_DATA_ROOT
82 if (!$tmpdir) {
83 unset($listofdir[$key]);
84 continue;
85 }
86 $tmpdir = DOL_DATA_ROOT.'/'.$tmpdir; // Complete with DOL_DATA_ROOT. Only files into DOL_DATA_ROOT can be reach/set
87 if (!is_dir($tmpdir)) {
88 if (empty($nomessageinsetmoduleoptions)) {
89 setEventMessages($langs->trans("ErrorDirNotFound", $tmpdir), null, 'warnings');
90 }
91 } else {
92 $upload_dir = $tmpdir;
93 break; // So we take the first directory found into setup $conf->global->$keyforuploaddir
94 }
95 }
96
97 $filetodelete = $tmpdir.'/'.GETPOST('file');
98 $result = dol_delete_file($filetodelete);
99 if ($result > 0) {
100 setEventMessages($langs->trans("FileWasRemoved", GETPOST('file')), null, 'mesgs');
101 }
102}
103
104// Define constants for submodules that contains parameters (forms with param1, param2, ... and value1, value2, ...)
105if ($action == 'setModuleOptions' && !empty($user->admin)) {
106 $db->begin();
107
108 // Process common param fields
109 if (is_array($_POST)) {
110 foreach ($_POST as $key => $val) {
111 $reg = array();
112 if (preg_match('/^param(\d*)$/', $key, $reg)) { // Works for POST['param'], POST['param1'], POST['param2'], ...
113 $param = GETPOST("param".$reg[1], 'alpha');
114 $value = GETPOST("value".$reg[1], 'alpha');
115 if ($param) {
116 $res = dolibarr_set_const($db, $param, $value, 'chaine', 0, '', $conf->entity);
117 if (!($res > 0)) {
118 $error++;
119 }
120 }
121 }
122 }
123 }
124
125 // Process upload fields
126 if (GETPOST('upload', 'alpha') && GETPOST('keyforuploaddir', 'aZ09')) {
127 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
128 $keyforuploaddir = GETPOST('keyforuploaddir', 'aZ09');
129 $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString($keyforuploaddir))));
130
131 foreach ($listofdir as $key => $tmpdir) {
132 $tmpdir = trim($tmpdir);
133 $tmpdir = preg_replace('/DOL_DATA_ROOT\/*/', '', $tmpdir); // Clean string if we found a hardcoded DOL_DATA_ROOT
134 if (!$tmpdir) {
135 unset($listofdir[$key]);
136 continue;
137 }
138 $tmpdir = DOL_DATA_ROOT.'/'.$tmpdir; // Complete with DOL_DATA_ROOT. Only files into DOL_DATA_ROOT can be reach/set
139 if (!is_dir($tmpdir)) {
140 if (empty($nomessageinsetmoduleoptions)) {
141 setEventMessages($langs->trans("ErrorDirNotFound", $tmpdir), null, 'warnings');
142 }
143 } else {
144 $upload_dir = $tmpdir;
145 break; // So we take the first directory found into setup $conf->global->$keyforuploaddir
146 }
147 }
148
149 if ($upload_dir) {
150 $result = dol_add_file_process($upload_dir, 1, 1, 'uploadfile', '');
151 if ($result <= 0) {
152 $error++;
153 }
154 }
155 }
156
157 if (!$error) {
158 $db->commit();
159 if (empty($nomessageinsetmoduleoptions)) {
160 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
161 }
162 } else {
163 $db->rollback();
164 if (empty($nomessageinsetmoduleoptions)) {
165 setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
166 }
167 }
168}
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, $donotupdatesession=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).
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.