dolibarr  13.0.2
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 
30 if ($action == 'update' && is_array($arrayofparameters))
31 {
32  $db->begin();
33 
34  foreach ($arrayofparameters as $key => $val)
35  {
36  // Modify constant only if key was posted (avoid resetting key to the null value)
37  if (GETPOSTISSET($key))
38  {
39  $result = dolibarr_set_const($db, $key, GETPOST($key, 'alpha'), 'chaine', 0, '', $conf->entity);
40  if ($result < 0)
41  {
42  $error++;
43  break;
44  }
45  }
46  }
47 
48  if (!$error)
49  {
50  $db->commit();
51  if (empty($nomessageinupdate)) setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
52  } else {
53  $db->rollback();
54  if (empty($nomessageinupdate)) setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
55  }
56 }
57 
58 // Define constants for submodules that contains parameters (forms with param1, param2, ... and value1, value2, ...)
59 if ($action == 'setModuleOptions')
60 {
61  $db->begin();
62 
63  // Process common param fields
64  if (is_array($_POST))
65  {
66  foreach ($_POST as $key => $val)
67  {
68  $reg = array();
69  if (preg_match('/^param(\d*)$/', $key, $reg)) // Works for POST['param'], POST['param1'], POST['param2'], ...
70  {
71  $param = GETPOST("param".$reg[1], 'alpha');
72  $value = GETPOST("value".$reg[1], 'alpha');
73  if ($param)
74  {
75  $res = dolibarr_set_const($db, $param, $value, 'chaine', 0, '', $conf->entity);
76  if (!($res > 0)) $error++;
77  }
78  }
79  }
80  }
81 
82  // Process upload fields
83  if (GETPOST('upload', 'alpha') && GETPOST('keyforuploaddir', 'aZ09'))
84  {
85  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
86  $keyforuploaddir = GETPOST('keyforuploaddir', 'aZ09');
87  $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim($conf->global->$keyforuploaddir)));
88  foreach ($listofdir as $key=>$tmpdir)
89  {
90  $tmpdir = trim($tmpdir);
91  $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
92  if (!$tmpdir) {
93  unset($listofdir[$key]); continue;
94  }
95  if (!is_dir($tmpdir)) {
96  if (empty($nomessageinsetmoduleoptions)) {
97  setEventMessages($langs->trans("ErrorDirNotFound", $tmpdir), null, 'warnings');
98  }
99  }
100  else {
101  $upload_dir = $tmpdir;
102  }
103  }
104  if ($upload_dir)
105  {
106  $result = dol_add_file_process($upload_dir, 1, 1, 'uploadfile', '');
107  if ($result <= 0) $error++;
108  }
109  }
110 
111  if (!$error)
112  {
113  $db->commit();
114  if (empty($nomessageinsetmoduleoptions)) setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
115  } else {
116  $db->rollback();
117  if (empty($nomessageinsetmoduleoptions)) setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
118  }
119 }
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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:575
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
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:1528