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

BookCalCalendarCardController::index()   F

Complexity

Conditions 33
Paths > 20000

Size

Total Lines 157
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 33
eloc 83
nc 55680
nop 1
dl 0
loc 157
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_card.php
39
 *      \ingroup    bookcal
40
 *      \brief      Page to create/edit/view calendar
41
 */
42
43
// Load Dolibarr environment
44
require BASE_PATH . '/main.inc.php';
45
require_once BASE_PATH . '/../Dolibarr/Modules/BookCal/Lib/BookCalCalendar.php';
46
47
class BookCalCalendarCardController 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(["agenda", "other"]);
63
64
// Get parameters
65
        $id = GETPOSTINT('id');
66
        $ref = GETPOST('ref', 'alpha');
67
        $lineid = GETPOSTINT('lineid');
68
69
        $action = GETPOST('action', 'aZ09');
70
        $confirm = GETPOST('confirm', 'alpha');
71
        $cancel = GETPOST('cancel', 'aZ09');
72
        $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : str_replace('_', '', basename(dirname(__FILE__)) . basename(__FILE__, '.php')); // To manage different context of search
73
        $backtopage = GETPOST('backtopage', 'alpha');                   // if not set, a default page will be used
74
        $backtopageforcancel = GETPOST('backtopageforcancel', 'alpha'); // if not set, $backtopage will be used
75
        $backtopagejsfields = GETPOST('backtopagejsfields', 'alpha');
76
        $dol_openinpopup = GETPOST('dol_openinpopup', 'aZ09');
77
78
        if (!empty($backtopagejsfields)) {
79
            $tmpbacktopagejsfields = explode(':', $backtopagejsfields);
80
            $dol_openinpopup = $tmpbacktopagejsfields[0];
81
        }
82
83
// Initialize technical objects
84
        $object = new Calendar($db);
85
        $extrafields = new ExtraFields($db);
86
        $diroutputmassaction = $conf->bookcal->dir_output . '/temp/massgeneration/' . $user->id;
87
        $hookmanager->initHooks(['calendarcard', 'globalcard']); // Note that conf->hooks_modules contains array
88
89
// Fetch optionals attributes and labels
90
        $extrafields->fetch_name_optionals_label($object->table_element);
91
92
        $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
93
94
// Initialize array of search criteria
95
        $search_all = GETPOST("search_all", 'alpha');
96
        $search = [];
97
        foreach ($object->fields as $key => $val) {
98
            if (GETPOST('search_' . $key, 'alpha')) {
99
                $search[$key] = GETPOST('search_' . $key, 'alpha');
100
            }
101
        }
102
103
        if (empty($action) && empty($id) && empty($ref)) {
104
            $action = 'view';
105
        }
106
107
// Load object
108
        include DOL_DOCUMENT_ROOT . '/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
109
110
// There is several ways to check permission.
111
// Set $enablepermissioncheck to 1 to enable a minimum low level of checks
112
        $enablepermissioncheck = 0;
113
        if ($enablepermissioncheck) {
114
            $permissiontoread = $user->hasRight('bookcal', 'calendar', 'read');
115
            $permissiontoadd = $user->hasRight('bookcal', 'calendar', 'write'); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
116
            $permissiontodelete = $user->hasRight('bookcal', 'calendar', 'delete') || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT);
117
            $permissionnote = $user->hasRight('bookcal', 'calendar', 'write'); // Used by the include of actions_setnotes.inc.php
118
            $permissiondellink = $user->hasRight('bookcal', 'calendar', 'write'); // Used by the include of actions_dellink.inc.php
119
        } else {
120
            $permissiontoread = 1;
121
            $permissiontoadd = 1; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
122
            $permissiontodelete = 1;
123
            $permissionnote = 1;
124
            $permissiondellink = 1;
125
        }
126
127
        $upload_dir = $conf->bookcal->multidir_output[isset($object->entity) ? $object->entity : 1] . '/calendar';
128
129
// Security check (enable the most restrictive one)
130
//if ($user->socid > 0) accessforbidden();
131
//if ($user->socid > 0) $socid = $user->socid;
132
//$isdraft = (isset($object->status) && ($object->status == $object::STATUS_DRAFT) ? 1 : 0);
133
//restrictedArea($user, $object->module, $object, $object->table_element, $object->element, 'fk_soc', 'rowid', $isdraft);
134
        if (!isModEnabled("bookcal")) {
135
            accessforbidden();
136
        }
137
        if (!$permissiontoread) {
138
            accessforbidden();
139
        }
140
141
142
        /*
143
         * Actions
144
         */
145
146
        $parameters = [];
147
        $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
148
        if ($reshook < 0) {
149
            setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
150
        }
151
152
        if (empty($reshook)) {
153
            $error = 0;
154
155
            $backurlforlist = dol_buildpath('/bookcal/calendar_list.php', 1);
156
157
            if (empty($backtopage) || ($cancel && empty($id))) {
158
                if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
159
                    if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
160
                        $backtopage = $backurlforlist;
161
                    } else {
162
                        $backtopage = dol_buildpath('/bookcal/calendar_card.php', 1) . '?id=' . ((!empty($id) && $id > 0) ? $id : '__ID__');
163
                    }
164
                }
165
            }
166
167
            $triggermodname = 'BOOKCAL_MYOBJECT_MODIFY'; // Name of trigger action code to execute when we modify record
168
169
            // Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
170
            include DOL_DOCUMENT_ROOT . '/core/actions_addupdatedelete.inc.php';
171
172
            // Actions when linking object each other
173
            include DOL_DOCUMENT_ROOT . '/core/actions_dellink.inc.php';
174
175
            // Actions when printing a doc from card
176
            include DOL_DOCUMENT_ROOT . '/core/actions_printing.inc.php';
177
178
            // Action to move up and down lines of object
179
            //include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php';
180
181
            // Action to build doc
182
            include DOL_DOCUMENT_ROOT . '/core/actions_builddoc.inc.php';
183
184
            if ($action == 'set_thirdparty' && $permissiontoadd) {
185
                $object->setValueFrom('fk_soc', GETPOSTINT('fk_soc'), '', '', 'date', '', $user, $triggermodname);
186
            }
187
            if ($action == 'classin' && $permissiontoadd) {
188
                $object->setProject(GETPOSTINT('projectid'));
189
            }
190
191
            // Actions to send emails
192
            $triggersendname = 'BOOKCAL_MYOBJECT_SENTBYMAIL';
193
            $autocopy = 'MAIN_MAIL_AUTOCOPY_MYOBJECT_TO';
194
            $trackid = 'calendar' . $object->id;
195
            include DOL_DOCUMENT_ROOT . '/core/actions_sendmails.inc.php';
196
        }
197
198
199
        /*
200
         * View
201
         */
202
        require_once realpath(BASE_PATH . '/../Dolibarr/Modules/BookCal/Views/calendar_card.php');
203
204
        $db->close();
205
206
        return true;
207
    }
208
}
209