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

BookCalAvailabilitiesContactController::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
nc 256
nop 1
dl 0
loc 99
rs 4.0333
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) 2007-2017  Laurent Destailleur     <[email protected]>
4
 * Copyright (C) 2022       Alice Adminson          <[email protected]>
5
 * Copyright (C) 2024       Rafael San José         <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace DoliModules\BookCal\Controller;
22
23
global $conf;
24
global $db;
25
global $user;
26
global $hookmanager;
27
global $user;
28
global $menumanager;
29
global $langs;
30
global $mysoc;
31
32
use DoliCore\Base\DolibarrController;
33
use DoliCore\Lib\ExtraFields;
34
use DoliModules\BookCal\Model\Availabilities;
35
36
/**
37
 *   \file       htdocs/bookcal/availabilities_contact.php
38
 *   \ingroup    bookcal
39
 *   \brief      Tab for contacts linked to Availabilities
40
 */
41
42
// Load Dolibarr environment
43
require BASE_PATH . '/main.inc.php';
44
require_once BASE_PATH . '/../Dolibarr/Modules/BookCal/Lib/BookCalAvailabilities.php';
45
46
class BookCalAvailabilitiesContactController extends DolibarrController
47
{
48
49
    public function index(bool $executeActions = true): bool
50
    {
51
        global $conf;
52
        global $db;
53
        global $user;
54
        global $hookmanager;
55
        global $user;
56
        global $menumanager;
57
        global $langs;
58
59
60
// Load translation files required by the page
61
        $langs->loadLangs(["agenda", "companies", "other", "mails"]);
62
63
        $id = (GETPOST('id') ? GETPOSTINT('id') : GETPOSTINT('facid')); // For backward compatibility
64
        $ref = GETPOST('ref', 'alpha');
65
        $lineid = GETPOSTINT('lineid');
66
        $socid = GETPOSTINT('socid');
67
        $action = GETPOST('action', 'aZ09');
68
69
// Initialize technical objects
70
        $object = new Availabilities($db);
71
        $extrafields = new ExtraFields($db);
72
        $diroutputmassaction = $conf->bookcal->dir_output . '/temp/massgeneration/' . $user->id;
73
        $hookmanager->initHooks(['availabilitiescontact', 'globalcard']); // Note that conf->hooks_modules contains array
74
// Fetch optionals attributes and labels
75
        $extrafields->fetch_name_optionals_label($object->table_element);
76
77
// Load object
78
        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
79
80
// There is several ways to check permission.
81
// Set $enablepermissioncheck to 1 to enable a minimum low level of checks
82
        $enablepermissioncheck = 0;
83
        if ($enablepermissioncheck) {
84
            $permissiontoread = $user->hasRight('bookcal', 'availabilities', 'read');
85
            $permission = $user->hasRight('bookcal', 'availabilities', 'write');
86
        } else {
87
            $permissiontoread = 1;
88
            $permission = 1;
89
        }
90
91
// Security check (enable the most restrictive one)
92
//if ($user->socid > 0) accessforbidden();
93
//if ($user->socid > 0) $socid = $user->socid;
94
//$isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
95
//restrictedArea($user, $object->element, $object->id, $object->table_element, '', 'fk_soc', 'rowid', $isdraft);
96
        if (!isModEnabled('bookcal')) {
97
            accessforbidden();
98
        }
99
        if (!$permissiontoread) {
100
            accessforbidden();
101
        }
102
103
104
        /*
105
         * Add a new contact
106
         */
107
108
        if ($action == 'addcontact' && $permission) {
109
            $contactid = (GETPOST('userid') ? GETPOSTINT('userid') : GETPOSTINT('contactid'));
110
            $typeid = (GETPOST('typecontact') ? GETPOST('typecontact') : GETPOST('type'));
111
            $result = $object->add_contact($contactid, $typeid, GETPOST("source", 'aZ09'));
112
113
            if ($result >= 0) {
114
                header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $object->id);
115
                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...
116
            } else {
117
                if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
118
                    $langs->load("errors");
119
                    setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
120
                } else {
121
                    setEventMessages($object->error, $object->errors, 'errors');
122
                }
123
            }
124
        } elseif ($action == 'swapstatut' && $permission) {
125
            // Toggle the status of a contact
126
            $result = $object->swapContactStatus(GETPOSTINT('ligne'));
127
        } elseif ($action == 'deletecontact' && $permission) {
128
            // Deletes a contact
129
            $result = $object->delete_contact($lineid);
130
131
            if ($result >= 0) {
132
                header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $object->id);
133
                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...
134
            } else {
135
                dol_print_error($db);
136
            }
137
        }
138
139
140
        /*
141
         * View
142
         */
143
        require_once realpath(BASE_PATH . '/../Dolibarr/Modules/BookCal/Views/availabilities_contact.php');
144
145
        $db->close();
146
147
        return true;
148
    }
149
}
150