Test Failed
Push — main ( ac1aa2...91ffd3 )
by Rafael
51:29
created

AdminMenuEditController::index()   F

Complexity

Conditions 44
Paths > 20000

Size

Total Lines 227
Code Lines 158

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 44
eloc 158
c 1
b 0
f 0
nc 285232
nop 1
dl 0
loc 227
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* Copyright (C) 2007       Patrick Raguin          <[email protected]>
4
 * Copyright (C) 2007-2012  Laurent Destailleur     <[email protected]>
5
 * Copyright (C) 2009-2011  Regis Houssin           <[email protected]>
6
 * Copyright (C) 2016       Meziane Sof             <[email protected]>
7
 * Copyright (C) 2024       Rafael San José         <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
namespace DoliModules\Admin\Controller;
24
25
global $conf;
26
global $db;
27
global $user;
28
global $hookmanager;
29
global $user;
30
global $menumanager;
31
global $langs;
32
global $mysoc;
33
34
35
// Load Dolibarr environment
36
require BASE_PATH . '/main.inc.php';
37
38
use DoliCore\Base\DolibarrController;
39
use DoliCore\Lib\Menu;
40
use DoliModules\Adherent\Model\AdherentType;
41
use ExtraFields;
42
43
class AdminMenuEditController extends DolibarrController
44
{
45
    /**
46
     *      \file       htdocs/adherents/type.php
47
     *      \ingroup    member
48
     *      \brief      Member's type setup
49
     */
50
    public function index($executeActions = true): bool
51
    {
52
        global $conf;
53
        global $db;
54
        global $user;
55
        global $hookmanager;
56
        global $user;
57
        global $menumanager;
58
        global $langs;
59
        global $mysoc;
60
61
// Load translation files required by the page
62
        $langs->loadLangs(["other", "admin"]);
63
64
        $cancel = GETPOST('cancel', 'alphanohtml'); // We click on a Cancel button
65
        $confirm = GETPOST('confirm');
66
67
        if (!$user->admin) {
68
            accessforbidden();
69
        }
70
71
        $dirstandard = [];
72
        $dirsmartphone = [];
73
        $dirmenus = array_merge(["/core/menus/"], (array) $conf->modules_parts['menus']);
74
        foreach ($dirmenus as $dirmenu) {
75
            $dirstandard[] = $dirmenu . 'standard';
76
            $dirsmartphone[] = $dirmenu . 'smartphone';
77
        }
78
79
        $action = GETPOST('action', 'aZ09');
80
81
        $menu_handler_top = getDolGlobalString('MAIN_MENU_STANDARD');
82
        $menu_handler_smartphone = getDolGlobalString('MAIN_MENU_SMARTPHONE');
83
        $menu_handler_top = preg_replace('/_backoffice.php/i', '', $menu_handler_top);
84
        $menu_handler_top = preg_replace('/_frontoffice.php/i', '', $menu_handler_top);
85
        $menu_handler_smartphone = preg_replace('/_backoffice.php/i', '', $menu_handler_smartphone);
86
        $menu_handler_smartphone = preg_replace('/_frontoffice.php/i', '', $menu_handler_smartphone);
87
88
        $menu_handler = $menu_handler_top;
89
90
        if (GETPOST("handler_origine")) {
91
            $menu_handler = GETPOST("handler_origine");
92
        }
93
        if (GETPOST("menu_handler")) {
94
            $menu_handler = GETPOST("menu_handler");
95
        }
96
97
98
        /*
99
         * Actions
100
         */
101
102
        if ($action == 'add') {
103
            if ($cancel) {
104
                header("Location: " . DOL_URL_ROOT . "/admin/menus/index.php?menu_handler=" . $menu_handler);
105
                exit;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
106
            }
107
108
            $leftmenu = '';
109
            $mainmenu = '';
110
            if (GETPOST('menuIdParent', 'alphanohtml') && !is_numeric(GETPOST('menuIdParent', 'alphanohtml'))) {
111
                $tmp = explode('&', GETPOST('menuIdParent', 'alphanohtml'));
112
                foreach ($tmp as $s) {
113
                    if (preg_match('/fk_mainmenu=/', $s)) {
114
                        $mainmenu = preg_replace('/fk_mainmenu=/', '', $s);
115
                    }
116
                    if (preg_match('/fk_leftmenu=/', $s)) {
117
                        $leftmenu = preg_replace('/fk_leftmenu=/', '', $s);
118
                    }
119
                }
120
            }
121
122
            $langs->load("errors");
123
124
            $error = 0;
125
            if (!$error && !GETPOST('menu_handler')) {
126
                setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("MenuHandler")), null, 'errors');
127
                $action = 'create';
128
                $error++;
129
            }
130
            if (!$error && !GETPOST('type')) {
131
                setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Position")), null, 'errors');
132
                $action = 'create';
133
                $error++;
134
            }
135
            if (!$error && !GETPOST('url')) {
136
                setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("URL")), null, 'errors');
137
                $action = 'create';
138
                $error++;
139
            }
140
            if (!$error && !GETPOST('titre')) {
141
                setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Title")), null, 'errors');
142
                $action = 'create';
143
                $error++;
144
            }
145
            if (!$error && GETPOST('menuIdParent', 'alphanohtml') && GETPOST('type') == 'top') {
146
                setEventMessages($langs->trans("ErrorTopMenuMustHaveAParentWithId0"), null, 'errors');
147
                $action = 'create';
148
                $error++;
149
            }
150
            if (!$error && !GETPOST('menuIdParent', 'alphanohtml') && GETPOST('type') == 'left') {
151
                setEventMessages($langs->trans("ErrorLeftMenuMustHaveAParentId"), null, 'errors');
152
                $action = 'create';
153
                $error++;
154
            }
155
156
            if (!$error) {
157
                $menu = new Menubase($db);
0 ignored issues
show
Bug introduced by
The type DoliModules\Admin\Controller\Menubase was not found. Did you mean Menubase? If so, make sure to prefix the type with \.
Loading history...
158
                $menu->menu_handler = preg_replace('/_menu$/', '', GETPOST('menu_handler', 'aZ09'));
159
                $menu->type = (string) GETPOST('type', 'alphanohtml');
160
                $menu->title = (string) GETPOST('titre', 'alphanohtml');
161
                $menu->prefix = (string) GETPOST('picto', 'restricthtmlallowclass');
162
                $menu->url = (string) GETPOST('url', 'alphanohtml');
163
                $menu->langs = (string) GETPOST('langs', 'alphanohtml');
164
                $menu->position = GETPOSTINT('position');
165
                $menu->enabled = (string) GETPOST('enabled', 'alphanohtml');
166
                $menu->perms = (string) GETPOST('perms', 'alphanohtml');
167
                $menu->target = (string) GETPOST('target', 'alphanohtml');
168
                $menu->user = (string) GETPOST('user', 'alphanohtml');
169
                $menu->mainmenu = (string) GETPOST('propertymainmenu', 'alphanohtml');
170
                if (is_numeric(GETPOST('menuIdParent', 'alphanohtml'))) {
171
                    $menu->fk_menu = (int) GETPOST('menuIdParent', 'alphanohtml');
172
                } else {
173
                    if (GETPOST('type', 'alphanohtml') == 'top') {
174
                        $menu->fk_menu = 0;
175
                    } else {
176
                        $menu->fk_menu = -1;
177
                    }
178
                    $menu->fk_mainmenu = $mainmenu;
179
                    $menu->fk_leftmenu = $leftmenu;
180
                }
181
182
                $result = $menu->create($user);
183
                if ($result > 0) {
184
                    header("Location: " . DOL_URL_ROOT . "/admin/menus/index.php?menu_handler=" . GETPOST('menu_handler', 'aZ09'));
185
                    exit;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
186
                } else {
187
                    $action = 'create';
188
                    setEventMessages($menu->error, $menu->errors, 'errors');
189
                }
190
            }
191
        }
192
193
        if ($action == 'update') {
194
            if (!$cancel) {
195
                $leftmenu = '';
196
                $mainmenu = '';
197
                if (GETPOST('menuIdParent', 'alphanohtml') && !is_numeric(GETPOST('menuIdParent', 'alphanohtml'))) {
198
                    $tmp = explode('&', GETPOST('menuIdParent', 'alphanohtml'));
199
                    foreach ($tmp as $s) {
200
                        if (preg_match('/fk_mainmenu=/', $s)) {
201
                            $mainmenu = preg_replace('/fk_mainmenu=/', '', $s);
202
                        }
203
                        if (preg_match('/fk_leftmenu=/', $s)) {
204
                            $leftmenu = preg_replace('/fk_leftmenu=/', '', $s);
205
                        }
206
                    }
207
                }
208
209
                $error = 0;
210
                if (!$error && !GETPOST('url')) {
211
                    setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("URL")), null, 'errors');
212
                    $action = 'create';
213
                    $error++;
214
                }
215
216
                if (!$error) {
217
                    $menu = new Menubase($db);
218
                    $result = $menu->fetch(GETPOSTINT('menuId'));
219
                    if ($result > 0) {
220
                        $menu->title = (string) GETPOST('titre', 'alphanohtml');
221
                        $menu->prefix = (string) GETPOST('picto', 'restricthtmlallowclass');
222
                        $menu->leftmenu = (string) GETPOST('leftmenu', 'aZ09');
223
                        $menu->url = (string) GETPOST('url', 'alphanohtml');
224
                        $menu->langs = (string) GETPOST('langs', 'alphanohtml');
225
                        $menu->position = GETPOSTINT('position');
226
                        $menu->enabled = (string) GETPOST('enabled', 'alphanohtml');
227
                        $menu->perms = (string) GETPOST('perms', 'alphanohtml');
228
                        $menu->target = (string) GETPOST('target', 'alphanohtml');
229
                        $menu->user = (string) GETPOST('user', 'alphanohtml');
230
                        $menu->mainmenu = (string) GETPOST('propertymainmenu', 'alphanohtml');
231
                        if (is_numeric(GETPOST('menuIdParent', 'alphanohtml'))) {
232
                            $menu->fk_menu = (int) GETPOST('menuIdParent', 'alphanohtml');
233
                        } else {
234
                            if (GETPOST('type', 'alphanohtml') == 'top') {
235
                                $menu->fk_menu = 0;
236
                            } else {
237
                                $menu->fk_menu = -1;
238
                            }
239
                            $menu->fk_mainmenu = $mainmenu;
240
                            $menu->fk_leftmenu = $leftmenu;
241
                        }
242
243
                        $result = $menu->update($user);
244
                        if ($result > 0) {
245
                            setEventMessages($langs->trans("RecordModifiedSuccessfully"), null, 'mesgs');
246
                        } else {
247
                            setEventMessages($menu->error, $menu->errors, 'errors');
248
                        }
249
                    } else {
250
                        setEventMessages($menu->error, $menu->errors, 'errors');
251
                    }
252
253
                    $action = "edit";
254
255
                    header("Location: " . DOL_URL_ROOT . "/admin/menus/index.php?menu_handler=" . $menu_handler);
256
                    exit;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
257
                } else {
258
                    $action = 'edit';
259
                }
260
            } else {
261
                header("Location: " . DOL_URL_ROOT . "/admin/menus/index.php?menu_handler=" . $menu_handler);
262
                exit;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
263
            }
264
        }
265
266
267
        /*
268
         * View
269
         */
270
        require_once realpath(BASE_PATH . '/../Dolibarr/Modules/Admin/Views/admin_menu_edit.php');
271
272
        $db->close();
273
274
//        $this->template = '/page/adherent/type_list';
275
//
276
        return true;
277
    }
278
}
279