Passed
Push — main ( e2b3b9...324b47 )
by Rafael
46:15
created

BookCalCalendarContactController::index()   D

Complexity

Conditions 16
Paths 256

Size

Total Lines 99
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 55
c 0
b 0
f 0
nc 256
nop 1
dl 0
loc 99
rs 4.0333

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-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_contact.php
39
 *  \ingroup    bookcal
40
 *  \brief      Tab for contacts linked to Calendar
41
 */
42
43
// Load Dolibarr environment
44
require BASE_PATH . '/main.inc.php';
45
46
dol_include_once('/bookcal/class/calendar.class.php');
47
dol_include_once('/bookcal/lib/bookcal_calendar.lib.php');
48
49
class BookCalCalendarContactController extends DolibarrController
50
{
51
    public function index(bool $executeActions = true): bool
52
    {
53
        global $conf;
54
        global $db;
55
        global $user;
56
        global $hookmanager;
57
        global $user;
58
        global $menumanager;
59
        global $langs;
60
61
62
// Load translation files required by the page
63
        $langs->loadLangs(["agenda", "companies", "other", "mails"]);
64
65
        $id = (GETPOST('id') ? GETPOSTINT('id') : GETPOSTINT('facid')); // For backward compatibility
66
        $ref = GETPOST('ref', 'alpha');
67
        $lineid = GETPOSTINT('lineid');
68
        $socid = GETPOSTINT('socid');
69
        $action = GETPOST('action', 'aZ09');
70
71
// Initialize technical objects
72
        $object = new Calendar($db);
73
        $extrafields = new ExtraFields($db);
74
        $diroutputmassaction = $conf->bookcal->dir_output . '/temp/massgeneration/' . $user->id;
75
        $hookmanager->initHooks(['calendarcontact', 'globalcard']); // Note that conf->hooks_modules contains array
76
// Fetch optionals attributes and labels
77
        $extrafields->fetch_name_optionals_label($object->table_element);
78
79
// Load object
80
        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
81
82
// There is several ways to check permission.
83
// Set $enablepermissioncheck to 1 to enable a minimum low level of checks
84
        $enablepermissioncheck = 0;
85
        if ($enablepermissioncheck) {
86
            $permissiontoread = $user->hasRight('bookcal', 'calendar', 'read');
87
            $permission = $user->hasRight('bookcal', 'calendar', 'write');
88
        } else {
89
            $permissiontoread = 1;
90
            $permission = 1;
91
        }
92
93
// Security check (enable the most restrictive one)
94
//if ($user->socid > 0) accessforbidden();
95
//if ($user->socid > 0) $socid = $user->socid;
96
//$isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
97
//restrictedArea($user, $object->module, $object->id, $object->table_element, $object->element, 'fk_soc', 'rowid', $isdraft);
98
        if (!isModEnabled("bookcal")) {
99
            accessforbidden();
100
        }
101
        if (!$permissiontoread) {
102
            accessforbidden();
103
        }
104
105
106
        /*
107
         * Add a new contact
108
         */
109
110
        if ($action == 'addcontact' && $permission) {
111
            $contactid = (GETPOST('userid') ? GETPOSTINT('userid') : GETPOSTINT('contactid'));
112
            $typeid = (GETPOST('typecontact') ? GETPOST('typecontact') : GETPOST('type'));
113
            $result = $object->add_contact($contactid, $typeid, GETPOST("source", 'aZ09'));
114
115
            if ($result >= 0) {
116
                header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $object->id);
117
                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...
118
            } else {
119
                if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
120
                    $langs->load("errors");
121
                    setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
122
                } else {
123
                    setEventMessages($object->error, $object->errors, 'errors');
124
                }
125
            }
126
        } elseif ($action == 'swapstatut' && $permission) {
127
            // Toggle the status of a contact
128
            $result = $object->swapContactStatus(GETPOSTINT('ligne'));
129
        } elseif ($action == 'deletecontact' && $permission) {
130
            // Deletes a contact
131
            $result = $object->delete_contact($lineid);
132
133
            if ($result >= 0) {
134
                header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $object->id);
135
                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...
136
            } else {
137
                dol_print_error($db);
138
            }
139
        }
140
141
142
        /*
143
         * View
144
         */
145
        require_once realpath(BASE_PATH . '/../Dolibarr/Modules/BookCal/Views/calendar_contact.php');
146
147
        $db->close();
148
149
        return true;
150
    }
151
}
152