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

BookCalCalendarAgendaController::index()   F

Complexity

Conditions 25
Paths > 20000

Size

Total Lines 121
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 68
nc 73728
nop 1
dl 0
loc 121
rs 0
c 0
b 0
f 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) 2017       Laurent Destailleur     <[email protected]>
4
 * Copyright (C) 2023       Alice Adminson          <[email protected]>
5
 * Copyright (C) 2024       Frédéric France         <[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\Lib\ExtraFields;
35
use DoliModules\BookCal\Model\Calendar;
36
37
/**
38
 *  \file       htdocs/bookcal/calendar_agenda.php
39
 *  \ingroup    bookcal
40
 *  \brief      Tab of events on Calendar
41
 */
42
43
// Load Dolibarr environment
44
require BASE_PATH . '/main.inc.php';
45
require_once BASE_PATH . '/../Dolibarr/Lib/Company.php';
46
require_once BASE_PATH . '/../Dolibarr/Lib/Functions2.php';
47
require_once BASE_PATH . '/../Dolibarr/Modules/BookCal/Lib/BookCalCalendar.php';
48
49
class BookCalCalendarAgendaController extends DolibarrController
50
{
51
52
    public function index(bool $executeActions = true): bool
53
    {
54
        global $conf;
55
        global $db;
56
        global $user;
57
        global $hookmanager;
58
        global $user;
59
        global $menumanager;
60
        global $langs;
61
62
63
// Load translation files required by the page
64
        $langs->loadLangs(["agenda", "other"]);
65
66
// Get parameters
67
        $id = GETPOSTINT('id');
68
        $ref = GETPOST('ref', 'alpha');
69
        $action = GETPOST('action', 'aZ09');
70
        $cancel = GETPOST('cancel', 'aZ09');
71
        $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : str_replace('_', '', basename(dirname(__FILE__)) . basename(__FILE__, '.php')); // To manage different context of search
72
        $backtopage = GETPOST('backtopage', 'alpha');
73
74
        if (GETPOST('actioncode', 'array')) {
75
            $actioncode = GETPOST('actioncode', 'array', 3);
76
            if (!count($actioncode)) {
77
                $actioncode = '0';
78
            }
79
        } else {
80
            $actioncode = GETPOST("actioncode", "alpha", 3) ? GETPOST("actioncode", "alpha", 3) : (GETPOST("actioncode") == '0' ? '0' : getDolGlobalString('AGENDA_DEFAULT_FILTER_TYPE_FOR_OBJECT'));
81
        }
82
        $search_rowid = GETPOST('search_rowid');
83
        $search_agenda_label = GETPOST('search_agenda_label');
84
85
        $limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
86
        $sortfield = GETPOST('sortfield', 'aZ09comma');
87
        $sortorder = GETPOST('sortorder', 'aZ09comma');
88
        $page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
89
        if (empty($page) || $page == -1) {
90
            $page = 0;
91
        }     // If $page is not defined, or '' or -1
92
        $offset = $limit * $page;
93
        $pageprev = $page - 1;
94
        $pagenext = $page + 1;
95
        if (!$sortfield) {
96
            $sortfield = 'a.datep,a.id';
97
        }
98
        if (!$sortorder) {
99
            $sortorder = 'DESC,DESC';
100
        }
101
102
// Initialize technical objects
103
        $object = new Calendar($db);
104
        $extrafields = new ExtraFields($db);
105
        $diroutputmassaction = $conf->bookcal->dir_output . '/temp/massgeneration/' . $user->id;
106
        $hookmanager->initHooks(['calendaragenda', 'globalcard']); // Note that conf->hooks_modules contains array
107
// Fetch optionals attributes and labels
108
        $extrafields->fetch_name_optionals_label($object->table_element);
109
110
// Load object
111
        include DOL_DOCUMENT_ROOT . '/core/actions_fetchobject.inc.php'; // Must be include, not include_once  // Must be include, not include_once. Include fetch and fetch_thirdparty but not fetch_optionals
112
        if ($id > 0 || !empty($ref)) {
113
            $upload_dir = $conf->bookcal->multidir_output[!empty($object->entity) ? $object->entity : $conf->entity] . "/" . $object->id;
114
        }
115
116
// There is several ways to check permission.
117
// Set $enablepermissioncheck to 1 to enable a minimum low level of checks
118
        $enablepermissioncheck = 0;
119
        if ($enablepermissioncheck) {
120
            $permissiontoread = $user->hasRight('bookcal', 'calendar', 'read');
121
            $permissiontoadd = $user->hasRight('bookcal', 'calendar', 'write');
122
        } else {
123
            $permissiontoread = 1;
124
            $permissiontoadd = 1;
125
        }
126
127
// Security check (enable the most restrictive one)
128
//if ($user->socid > 0) accessforbidden();
129
//if ($user->socid > 0) $socid = $user->socid;
130
//$isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
131
//restrictedArea($user, $object->module, $object->id, $object->table_element, $object->element, 'fk_soc', 'rowid', $isdraft);
132
        if (!isModEnabled("bookcal")) {
133
            accessforbidden();
134
        }
135
        if (!$permissiontoread) {
136
            accessforbidden();
137
        }
138
139
140
        /*
141
         *  Actions
142
         */
143
144
        $parameters = ['id' => $id];
145
        $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
146
        if ($reshook < 0) {
147
            setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
148
        }
149
150
        if (empty($reshook)) {
151
            // Cancel
152
            if (GETPOST('cancel', 'alpha') && !empty($backtopage)) {
153
                header("Location: " . $backtopage);
154
                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...
155
            }
156
157
            // Purge search criteria
158
            if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
159
                $actioncode = '';
160
                $search_agenda_label = '';
161
            }
162
        }
163
164
165
        /*
166
         *	View
167
         */
168
        require_once realpath(BASE_PATH . '/../Dolibarr/Modules/BookCal/Views/calendar_agenda.php');
169
170
        $db->close();
171
172
        return true;
173
    }
174
}
175