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

AdminMenuIndexController::index()   D

Complexity

Conditions 16
Paths 112

Size

Total Lines 182
Code Lines 133

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 133
c 1
b 0
f 0
nc 112
nop 1
dl 0
loc 182
rs 4.3733

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-2012  Regis Houssin           <[email protected]>
6
 * Copyright (C) 2019       Frédéric France         <[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
40
class AdminMenuIndexController extends DolibarrController
41
{
42
    /**
43
     *      \file       htdocs/adherents/type.php
44
     *      \ingroup    member
45
     *      \brief      Member's type setup
46
     */
47
    public function index($executeActions = true): bool
48
    {
49
        global $conf;
50
        global $db;
51
        global $user;
52
        global $hookmanager;
53
        global $user;
54
        global $menumanager;
55
        global $langs;
56
        global $mysoc;
57
58
// Load translation files required by the page
59
        $langs->loadLangs(["other", "admin"]);
60
61
        $dirstandard = [];
62
        $dirsmartphone = [];
63
        $dirmenus = array_merge(["/core/menus/"], (array) $conf->modules_parts['menus']);
64
        foreach ($dirmenus as $dirmenu) {
65
            $dirstandard[] = $dirmenu . 'standard';
66
            $dirsmartphone[] = $dirmenu . 'smartphone';
67
        }
68
69
        $action = GETPOST('action', 'aZ09');
70
        $confirm = GETPOST('confirm', 'alpha');
71
72
//$menu_handler_top = getDolGlobalString('MAIN_MENU_STANDARD');
73
        $menu_handler_top = 'all';
74
        $menu_handler_top = preg_replace('/(_backoffice\.php|_menu\.php)/i', '', $menu_handler_top);
75
        $menu_handler_top = preg_replace('/(_frontoffice\.php|_menu\.php)/i', '', $menu_handler_top);
76
77
        $menu_handler = $menu_handler_top;
78
79
        if (GETPOST("handler_origine")) {
80
            $menu_handler = GETPOST("handler_origine");
81
        }
82
        if (GETPOST("menu_handler")) {
83
            $menu_handler = GETPOST("menu_handler");
84
        }
85
86
        $menu_handler_to_search = preg_replace('/(_backoffice|_frontoffice|_menu)?(\.php)?/i', '', $menu_handler);
87
88
        if (empty($user->admin)) {
89
            accessforbidden();
90
        }
91
92
93
        /*
94
         * Actions
95
         */
96
97
        if ($action == 'up') {
98
            $current = [];
99
            $previous = [];
100
101
            // Get current position
102
            $sql = "SELECT m.rowid, m.position, m.type, m.fk_menu";
103
            $sql .= " FROM " . MAIN_DB_PREFIX . "menu as m";
104
            $sql .= " WHERE m.rowid = " . GETPOSTINT("menuId");
105
            dol_syslog("admin/menus/index.php " . $sql);
106
            $result = $db->query($sql);
107
            $num = $db->num_rows($result);
108
            $i = 0;
109
            while ($i < $num) {
110
                $obj = $db->fetch_object($result);
111
                $current['rowid'] = $obj->rowid;
112
                $current['order'] = $obj->position;
113
                $current['type'] = $obj->type;
114
                $current['fk_menu'] = $obj->fk_menu;
115
                $i++;
116
            }
117
118
            // Menu before
119
            $sql = "SELECT m.rowid, m.position";
120
            $sql .= " FROM " . MAIN_DB_PREFIX . "menu as m";
121
            $sql .= " WHERE (m.position < " . ($current['order']) . " OR (m.position = " . ($current['order']) . " AND rowid < " . GETPOSTINT("menuId") . "))";
122
            $sql .= " AND m.menu_handler='" . $db->escape($menu_handler_to_search) . "'";
123
            $sql .= " AND m.entity = " . $conf->entity;
124
            $sql .= " AND m.type = '" . $db->escape($current['type']) . "'";
125
            $sql .= " AND m.fk_menu = '" . $db->escape($current['fk_menu']) . "'";
126
            $sql .= " ORDER BY m.position, m.rowid";
127
            dol_syslog("admin/menus/index.php " . $sql);
128
            $result = $db->query($sql);
129
            $num = $db->num_rows($result);
130
            $i = 0;
131
            while ($i < $num) {
132
                $obj = $db->fetch_object($result);
133
                $previous['rowid'] = $obj->rowid;
134
                $previous['order'] = $obj->position;
135
                $i++;
136
            }
137
138
            $sql = "UPDATE " . MAIN_DB_PREFIX . "menu as m";
139
            $sql .= " SET m.position = " . ((int) $previous['order']);
140
            $sql .= " WHERE m.rowid = " . ((int) $current['rowid']); // Up the selected entry
141
            dol_syslog("admin/menus/index.php " . $sql);
142
            $db->query($sql);
143
            $sql = "UPDATE " . MAIN_DB_PREFIX . "menu as m";
144
            $sql .= " SET m.position = " . ((int) ($current['order'] != $previous['order'] ? $current['order'] : $current['order'] + 1));
145
            $sql .= " WHERE m.rowid = " . ((int) $previous['rowid']); // Descend celui du dessus
146
            dol_syslog("admin/menus/index.php " . $sql);
147
            $db->query($sql);
148
        } elseif ($action == 'down') {
149
            $current = [];
150
            $next = [];
151
152
            // Get current position
153
            $sql = "SELECT m.rowid, m.position, m.type, m.fk_menu";
154
            $sql .= " FROM " . MAIN_DB_PREFIX . "menu as m";
155
            $sql .= " WHERE m.rowid = " . GETPOSTINT("menuId");
156
            dol_syslog("admin/menus/index.php " . $sql);
157
            $result = $db->query($sql);
158
            $num = $db->num_rows($result);
159
            $i = 0;
160
            while ($i < $num) {
161
                $obj = $db->fetch_object($result);
162
                $current['rowid'] = $obj->rowid;
163
                $current['order'] = $obj->position;
164
                $current['type'] = $obj->type;
165
                $current['fk_menu'] = $obj->fk_menu;
166
                $i++;
167
            }
168
169
            // Menu after
170
            $sql = "SELECT m.rowid, m.position";
171
            $sql .= " FROM " . MAIN_DB_PREFIX . "menu as m";
172
            $sql .= " WHERE (m.position > " . ($current['order']) . " OR (m.position = " . ($current['order']) . " AND rowid > " . GETPOSTINT("menuId") . "))";
173
            $sql .= " AND m.menu_handler='" . $db->escape($menu_handler_to_search) . "'";
174
            $sql .= " AND m.entity = " . $conf->entity;
175
            $sql .= " AND m.type = '" . $db->escape($current['type']) . "'";
176
            $sql .= " AND m.fk_menu = '" . $db->escape($current['fk_menu']) . "'";
177
            $sql .= " ORDER BY m.position, m.rowid";
178
            dol_syslog("admin/menus/index.php " . $sql);
179
            $result = $db->query($sql);
180
            $num = $db->num_rows($result);
181
            $i = 0;
182
            while ($i < $num) {
183
                $obj = $db->fetch_object($result);
184
                $next['rowid'] = $obj->rowid;
185
                $next['order'] = $obj->position;
186
                $i++;
187
            }
188
189
            $sql = "UPDATE " . MAIN_DB_PREFIX . "menu as m";
190
            $sql .= " SET m.position = " . ((int) ($current['order'] != $next['order'] ? $next['order'] : $current['order'] + 1)); // Down the selected entry
191
            $sql .= " WHERE m.rowid = " . ((int) $current['rowid']);
192
            dol_syslog("admin/menus/index.php " . $sql);
193
            $db->query($sql);
194
            $sql = "UPDATE " . MAIN_DB_PREFIX . "menu as m"; // Up the next entry
195
            $sql .= " SET m.position = " . ((int) $current['order']);
196
            $sql .= " WHERE m.rowid = " . ((int) $next['rowid']);
197
            dol_syslog("admin/menus/index.php " . $sql);
198
            $db->query($sql);
199
        } elseif ($action == 'confirm_delete' && $confirm == 'yes') {
200
            $db->begin();
201
202
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "menu";
203
            $sql .= " WHERE rowid = " . GETPOSTINT('menuId');
204
            $resql = $db->query($sql);
205
            if ($resql) {
206
                $db->commit();
207
208
                setEventMessages($langs->trans("MenuDeleted"), null, 'mesgs');
209
210
                header("Location: " . DOL_URL_ROOT . '/admin/menus/index.php?menu_handler=' . $menu_handler);
211
                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...
212
            } else {
213
                $db->rollback();
214
215
                $reload = 0;
216
                $action = '';
217
            }
218
        }
219
220
        /*
221
         * View
222
         */
223
224
        require_once realpath(BASE_PATH . '/../Dolibarr/Modules/Admin/Views/admin_menu_index.php');
225
226
        $db->close();
227
228
        return true;
229
    }
230
}
231