Passed
Push — main ( 9a81fe...875825 )
by Rafael
41:55
created

BookCalAdminSetupController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 73
dl 0
loc 166
rs 10
c 0
b 0
f 0
wmc 23

1 Method

Rating   Name   Duplication   Size   Complexity  
D index() 0 163 23
1
<?php
2
3
/* Copyright (C) 2004-2017  Laurent Destailleur     <[email protected]>
4
 * Copyright (C) 2022       Alice Adminson          <[email protected]>
5
 * Copyright (C) 2024		MDW						<[email protected]>
6
 * Copyright (C) 2024       Rafael San José         <[email protected]>
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace DoliModules\BookCal\Controller;
23
24
global $conf;
25
global $db;
26
global $user;
27
global $hookmanager;
28
global $user;
29
global $menumanager;
30
global $langs;
31
global $mysoc;
32
33
use DoliCore\Base\DolibarrController;
34
use DoliCore\Form\FormSetup;
35
36
/**
37
 * \file    bookcal/admin/setup.php
38
 * \ingroup bookcal
39
 * \brief   BookCal setup page.
40
 */
41
42
// Load Dolibarr environment
43
require BASE_PATH . '/main.inc.php';
44
45
global $langs, $user;
46
47
// Libraries
48
require_once BASE_PATH . '/../Dolibarr/Lib/Admin.php';
49
require_once BASE_PATH . '/../Dolibarr/Modules/BookCal/Lib/BookCal.php';
50
require_once BASE_PATH . '/../Dolibarr/Modules/BookCal/Lib/BookCal.php';
51
52
//require_once "../class/myclass.class.php";
53
54
class BookCalAdminSetupController extends DolibarrController
55
{
56
57
    public function index(bool $executeActions = true): bool
58
    {
59
        global $conf;
60
        global $db;
61
        global $user;
62
        global $hookmanager;
63
        global $user;
64
        global $menumanager;
65
        global $langs;
66
67
68
// Translations
69
        $langs->loadLangs(["admin", "agenda"]);
70
71
// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
72
        $hookmanager->initHooks(['bookcalsetup', 'globalsetup']);
73
74
// Access control
75
        if (!$user->admin) {
76
            accessforbidden();
77
        }
78
79
// Parameters
80
        $action = GETPOST('action', 'aZ09');
81
        $backtopage = GETPOST('backtopage', 'alpha');
82
        $modulepart = GETPOST('modulepart', 'aZ09');    // Used by actions_setmoduleoptions.inc.php
83
84
        $value = GETPOST('value', 'alpha');
85
        $label = GETPOST('label', 'alpha');
86
        $scandir = GETPOST('scan_dir', 'alpha');
87
        $type = 'myobject';
88
89
90
        $error = 0;
91
        $setupnotempty = 0;
92
93
// Set this to 1 to use the factory to manage constants. Warning, the generated module will be compatible with version v15+ only
94
        $useFormSetup = 1;
95
96
        $formSetup = new FormSetup($db);
97
98
// Setup conf BOOKCAL_PUBLIC_INTERFACE_TOPIC
99
        $item = $formSetup->newItem('BOOKCAL_PUBLIC_INTERFACE_TOPIC');
100
        $item->defaultFieldValue = 'MyBigCompany public interface for Bookcal';
101
        /*// Setup conf BOOKCAL_MYPARAM8
102
        $item = $formSetup->newItem('BOOKCAL_MYPARAM8');
103
        $TField = array(
104
            'test01' => $langs->trans('test01'),
105
            'test02' => $langs->trans('test02'),
106
            'test03' => $langs->trans('test03'),
107
            'test04' => $langs->trans('test04'),
108
            'test05' => $langs->trans('test05'),
109
            'test06' => $langs->trans('test06'),
110
        );
111
        $item->setAsMultiSelect($TField);
112
        $item->helpText = $langs->transnoentities('BOOKCAL_MYPARAM8');
113
114
115
        // Setup conf BOOKCAL_MYPARAM9
116
        $formSetup->newItem('BOOKCAL_MYPARAM9')->setAsSelect($TField);
117
118
119
        // Setup conf BOOKCAL_MYPARAM10
120
        $item = $formSetup->newItem('BOOKCAL_MYPARAM10');
121
        $item->setAsColor();
122
        $item->defaultFieldValue = '#FF0000';
123
        $item->nameText = $item->getNameText().' more html text ';
124
        $item->fieldInputOverride = '';
125
        $item->helpText = $langs->transnoentities('AnHelpMessage');*/
126
//$item->fieldValue = '';
127
//$item->fieldAttr = array() ; // fields attribute only for compatible fields like input text
128
//$item->fieldOverride = false; // set this var to override field output will override $fieldInputOverride and $fieldOutputOverride too
129
//$item->fieldInputOverride = false; // set this var to override field input
130
//$item->fieldOutputOverride = false; // set this var to override field output
131
132
133
        $setupnotempty = +count($formSetup->items);
134
135
136
        $dirmodels = array_merge(['/'], (array) $conf->modules_parts['models']);
137
138
139
        /*
140
         * Actions
141
         */
142
143
// For retrocompatibility Dolibarr < 15.0
144
        if (versioncompare(explode('.', DOL_VERSION), [15]) < 0 && $action == 'update' && !empty($user->admin)) {
0 ignored issues
show
Bug introduced by
The constant DoliModules\BookCal\Controller\DOL_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
145
            $formSetup->saveConfFromPost();
146
        }
147
148
        include DOL_DOCUMENT_ROOT . '/core/actions_setmoduleoptions.inc.php';
149
150
        if ($action == 'updateMask') {
151
            $maskconst = GETPOST('maskconst', 'aZ09');
152
            $maskvalue = GETPOST('maskvalue', 'alpha');
153
154
            if ($maskconst && preg_match('/_MASK$/', $maskconst)) {
155
                $res = dolibarr_set_const($db, $maskconst, $maskvalue, 'chaine', 0, '', $conf->entity);
156
                if (!($res > 0)) {
157
                    $error++;
158
                }
159
            }
160
161
            if (!$error) {
162
                setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
163
            } else {
164
                setEventMessages($langs->trans("Error"), null, 'errors');
165
            }
166
        } elseif ($action == 'setmod') {
167
            // TODO Check if numbering module chosen can be activated by calling method canBeActivated
168
            $tmpobjectkey = GETPOST('object', 'aZ09');
169
            if (!empty($tmpobjectkey)) {
170
                $constforval = 'BOOKCAL_' . strtoupper($tmpobjectkey) . "_ADDON";
171
                dolibarr_set_const($db, $constforval, $value, 'chaine', 0, '', $conf->entity);
172
            }
173
        } elseif ($action == 'set') {
174
            // Activate a model
175
            $ret = addDocumentModel($value, $type, $label, $scandir);
176
        } elseif ($action == 'del') {
177
            $ret = delDocumentModel($value, $type);
178
            if ($ret > 0) {
179
                $tmpobjectkey = GETPOST('object', 'aZ09');
180
                if (!empty($tmpobjectkey)) {
181
                    $constforval = 'BOOKCAL_' . strtoupper($tmpobjectkey) . '_ADDON_PDF';
182
                    if (getDolGlobalString($constforval) == "$value") {
183
                        dolibarr_del_const($db, $constforval, $conf->entity);
184
                    }
185
                }
186
            }
187
        } elseif ($action == 'setdoc') {
188
            // Set or unset default model
189
            $tmpobjectkey = GETPOST('object', 'aZ09');
190
            if (!empty($tmpobjectkey)) {
191
                $constforval = 'BOOKCAL_' . strtoupper($tmpobjectkey) . '_ADDON_PDF';
192
                if (dolibarr_set_const($db, $constforval, $value, 'chaine', 0, '', $conf->entity)) {
193
                    // The constant that was read before the new set
194
                    // We therefore requires a variable to have a coherent view
195
                    $conf->global->$constforval = $value;
196
                }
197
198
                // We disable/enable the document template (into llx_document_model table)
199
                $ret = delDocumentModel($value, $type);
200
                if ($ret > 0) {
201
                    $ret = addDocumentModel($value, $type, $label, $scandir);
202
                }
203
            }
204
        } elseif ($action == 'unsetdoc') {
205
            $tmpobjectkey = GETPOST('object', 'aZ09');
206
            if (!empty($tmpobjectkey)) {
207
                $constforval = 'BOOKCAL_' . strtoupper($tmpobjectkey) . '_ADDON_PDF';
208
                dolibarr_del_const($db, $constforval, $conf->entity);
209
            }
210
        }
211
212
        /*
213
         * View
214
         */
215
        require_once realpath(BASE_PATH . '/../Dolibarr/Modules/BookCal/Views/admin_setup.php');
216
217
        $db->close();
218
219
        return true;
220
    }
221
}
222