Passed
Push — main ( 875825...95c1dc )
by Rafael
48:39
created

BookMarksListController::index()   F

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 127
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 28
eloc 76
c 1
b 0
f 0
nc 21504
nop 1
dl 0
loc 127
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) 2005-2022  Laurent Destailleur     <[email protected]>
4
 * Copyright (C) 2024       Rafael San José         <[email protected]>
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace DoliModules\BookMarks\Controller;
21
22
global $conf;
23
global $db;
24
global $user;
25
global $hookmanager;
26
global $user;
27
global $menumanager;
28
global $langs;
29
global $mysoc;
30
31
use DoliCore\Base\DolibarrController;
32
33
34
/**
35
 *    \file       htdocs/bookmarks/list.php
36
 *    \ingroup    bookmark
37
 *    \brief      Page to display list of bookmarks
38
 */
39
40
// Load Dolibarr environment
41
use DoliCore\Form\Form;
42
use DoliCore\Lib\ExtraFields;
43
use DoliCore\Model\Bookmark;
44
45
require BASE_PATH . '/main.inc.php';
46
47
class BookMarksListController extends DolibarrController
48
{
49
50
    public function index(bool $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
60
61
// Load translation files required by the page
62
        $langs->loadLangs(['bookmarks', 'admin']);
63
64
// Get Parameters
65
        $action = GETPOST('action', 'aZ09');
66
        $massaction = GETPOST('massaction', 'alpha');
67
        $show_files = GETPOSTINT('show_files');
68
        $confirm = GETPOST('confirm', 'alpha');
69
        $cancel = GETPOST('cancel', 'alpha');
70
        $toselect = GETPOST('toselect', 'array');
71
        $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'bookmarklist'; // To manage different context of search
72
        $backtopage = GETPOST('backtopage', 'alpha');
73
        $optioncss = GETPOST('optioncss', 'alpha');
74
        $mode = GETPOST('mode', 'aZ');
75
76
        $id = GETPOSTINT("id");
77
        $search_title = GETPOST('search_title', 'alpha');
78
79
// Load variable for pagination
80
        $limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
81
        $sortfield = GETPOST('sortfield', 'aZ09comma');
82
        $sortorder = GETPOST('sortorder', 'aZ09comma');
83
        $page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
84
        if (empty($page) || $page < 0 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
85
            // If $page is not defined, or '' or -1 or if we click on clear filters
86
            $page = 0;
87
        }
88
        $offset = $limit * $page;
89
        $pageprev = $page - 1;
90
        $pagenext = $page + 1;
91
        if (!$sortfield) {
92
            $sortfield = 'b.position';
93
        }
94
        if (!$sortorder) {
95
            $sortorder = 'ASC';
96
        }
97
98
// Initialize Objects
99
        $object = new Bookmark($db);
100
        $extrafields = new ExtraFields($db);
101
        $arrayfields = [];
102
        $hookmanager->initHooks(['bookmarklist']); // Note that conf->hooks_modules contains array
103
104
        if ($id > 0) {
105
            $object->fetch($id);
106
        }
107
108
        $object->fields = dol_sort_array($object->fields, 'position');
109
        $arrayfields = dol_sort_array($arrayfields, 'position');
110
111
// Security check
112
        restrictedArea($user, 'bookmark', $object);
113
114
// Permissions
115
        $permissiontoread = $user->hasRight('bookmark', 'lire');
116
        $permissiontoadd = $user->hasRight('bookmark', 'creer');
117
        $permissiontodelete = ($user->hasRight('bookmark', 'supprimer') || ($permissiontoadd && $object->fk_user == $user->id));
118
119
120
        /*
121
         * Actions
122
         */
123
124
        if (GETPOST('cancel', 'alpha')) {
125
            $action = 'list';
126
            $massaction = '';
127
        }
128
        if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
129
            $massaction = '';
130
        }
131
132
        $parameters = [];
133
        $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
134
        if ($reshook < 0) {
135
            setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
136
        }
137
138
        if (empty($reshook)) {
139
            // Selection of new fields
140
            include DOL_DOCUMENT_ROOT . '/core/actions_changeselectedfields.inc.php';
141
142
            if (
143
                GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')
144
                || GETPOST('button_search_x', 'alpha') || GETPOST('button_search.x', 'alpha') || GETPOST('button_search', 'alpha')
145
            ) {
146
                $massaction = ''; // Protection to avoid mass action if we force a new search during a mass action confirmation
147
            }
148
149
            // Mass actions
150
            $objectclass = 'Bookmark';
151
            $objectlabel = 'Bookmark';
152
            $uploaddir = $conf->bookmark->dir_output;
153
            include DOL_DOCUMENT_ROOT . '/core/actions_massactions.inc.php';
154
155
            if ($action == 'delete' && $permissiontodelete) {
156
                $object->fetch($id);
157
                $res = $object->delete($user);
158
                if ($res > 0) {
159
                    header("Location: " . $_SERVER['PHP_SELF']);
160
                    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...
161
                } else {
162
                    setEventMessages($object->error, $object->errors, 'errors');
163
                    $action = '';
164
                }
165
            }
166
        }
167
168
169
        /*
170
         * View
171
         */
172
        require_once realpath(BASE_PATH . '/../Dolibarr/Modules/BookMarks/Views/list.php');
173
174
        $db->close();
175
176
        return true;
177
    }
178
}
179