dolibarr  16.0.5
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 
32 if ($action == 'update' && !empty($formSetup) && is_object($formSetup) && !empty($user->admin)) {
33  $formSetup->saveConfFromPost();
34  return;
35 }
36 
37 
38 if ($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 (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 
75 if ($action == 'deletefile' && $modulepart == 'doctemplates' && !empty($user->admin)) {
76  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
77  $keyforuploaddir = GETPOST('keyforuploaddir', 'aZ09');
78 
79  $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString($keyforuploaddir))));
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, ...)
105 if ($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  foreach ($listofdir as $key => $tmpdir) {
131  $tmpdir = trim($tmpdir);
132  $tmpdir = preg_replace('/DOL_DATA_ROOT\/*/', '', $tmpdir); // Clean string if we found a hardcoded DOL_DATA_ROOT
133  if (!$tmpdir) {
134  unset($listofdir[$key]);
135  continue;
136  }
137  $tmpdir = DOL_DATA_ROOT.'/'.$tmpdir; // Complete with DOL_DATA_ROOT. Only files into DOL_DATA_ROOT can be reach/set
138  if (!is_dir($tmpdir)) {
139  if (empty($nomessageinsetmoduleoptions)) {
140  setEventMessages($langs->trans("ErrorDirNotFound", $tmpdir), null, 'warnings');
141  }
142  } else {
143  $upload_dir = $tmpdir;
144  break; // So we take the first directory found into setup $conf->global->$keyforuploaddir
145  }
146  }
147  if ($upload_dir) {
148  $result = dol_add_file_process($upload_dir, 1, 1, 'uploadfile', '');
149  if ($result <= 0) {
150  $error++;
151  }
152  }
153  }
154 
155  if (!$error) {
156  $db->commit();
157  if (empty($nomessageinsetmoduleoptions)) {
158  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
159  }
160  } else {
161  $db->rollback();
162  if (empty($nomessageinsetmoduleoptions)) {
163  setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
164  }
165  }
166 }
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_add_file_process
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).
Definition: files.lib.php:1634
dol_delete_file
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.
Definition: files.lib.php:1231
getDolGlobalString
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:80
dolibarr_set_const
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).
Definition: admin.lib.php:627
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137