Passed
Push — main ( 7eb3b3...9a81fe )
by Rafael
42:16
created

BomNetNeedsController::index()   F

Complexity

Conditions 27
Paths 8448

Size

Total Lines 113
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 27
eloc 60
c 1
b 0
f 0
nc 8448
nop 1
dl 0
loc 113
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) 2017-2020  Laurent Destailleur     <[email protected]>
4
 * Copyright (C) 2019       Frédéric France         <[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\Bom\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
/**
32
 *      \file       htdocs/bom/bom_net_needs.php
33
 *      \ingroup    bom
34
 *      \brief      Page to create/edit/view bom
35
 */
36
37
// Load Dolibarr environment
38
use DoliCore\Base\DolibarrController;
39
use DoliCore\Lib\ExtraFields;
40
use DoliModules\Bom\Model\Bom;
41
42
require BASE_PATH . '/main.inc.php';
43
require_once DOL_DOCUMENT_ROOT . '/bom/class/bom.class.php';
44
require_once BASE_PATH . '/../Dolibarr/Modules/Bom/Lib/Bom.php';
45
46
class BomNetNeedsController extends DolibarrController
47
{
48
    public function index(bool $executeActions = true): bool
49
    {
50
        global $conf;
51
        global $db;
52
        global $user;
53
        global $hookmanager;
54
        global $user;
55
        global $menumanager;
56
        global $langs;
57
        global $mysoc;
58
59
// Load translation files required by the page
60
        $langs->loadLangs(["mrp", "other", "stocks"]);
61
62
// Get parameters
63
        $id = GETPOSTINT('id');
64
        $lineid = GETPOSTINT('lineid');
65
        $ref = GETPOST('ref', 'alpha');
66
        $action = GETPOST('action', 'aZ09');
67
        $confirm = GETPOST('confirm', 'alpha');
68
        $cancel = GETPOST('cancel', 'aZ09');
69
        $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'bomnet_needs'; // To manage different context of search
70
        $backtopage = GETPOST('backtopage', 'alpha');
71
72
73
// Initialize technical objects
74
        $object = new Bom($db);
75
        $extrafields = new ExtraFields($db);
76
77
// Initialize technical objects for hooks
78
        $hookmanager->initHooks(['bomnetneeds']); // Note that conf->hooks_modules contains array
79
80
// Massaction
81
        $diroutputmassaction = $conf->bom->dir_output . '/temp/massgeneration/' . $user->id;
82
83
// Fetch optionals attributes and labels
84
        $extrafields->fetch_name_optionals_label($object->table_element);
85
        $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
86
87
// Initialize array of search criteria
88
        $search_all = GETPOST("search_all", 'alpha');
89
        $search = [];
90
        foreach ($object->fields as $key => $val) {
91
            if (GETPOST('search_' . $key, 'alpha')) {
92
                $search[$key] = GETPOST('search_' . $key, 'alpha');
93
            }
94
        }
95
96
        if (empty($action) && empty($id) && empty($ref)) {
97
            $action = 'view';
98
        }
99
100
// Load object
101
        include DOL_DOCUMENT_ROOT . '/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
102
        if ($object->id > 0) {
103
            $object->calculateCosts();
104
        }
105
106
107
// Security check - Protection if external user
108
//if ($user->socid > 0) accessforbidden();
109
//if ($user->socid > 0) $socid = $user->socid;
110
        $isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
111
        $result = restrictedArea($user, 'bom', $object->id, $object->table_element, '', '', 'rowid', $isdraft);
112
113
// Permissions
114
        $permissionnote = $user->hasRight('bom', 'write'); // Used by the include of actions_setnotes.inc.php
115
        $permissiondellink = $user->hasRight('bom', 'write'); // Used by the include of actions_dellink.inc.php
116
        $permissiontoadd = $user->hasRight('bom', 'write'); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
117
        $permissiontodelete = $user->hasRight('bom', 'delete') || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT);
118
        $upload_dir = $conf->bom->multidir_output[isset($object->entity) ? $object->entity : 1];
119
120
121
        /*
122
         * Actions
123
         */
124
125
        $parameters = [];
126
        $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
127
        if ($reshook < 0) {
128
            setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
129
        }
130
131
        if (empty($reshook)) {
132
            $error = 0;
133
134
            $backurlforlist = '/bom/bom_list.php';
135
136
            if (empty($backtopage) || ($cancel && empty($id))) {
137
                if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
138
                    if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
139
                        $backtopage = $backurlforlist;
140
                    } else {
141
                        $backtopage = '/bom/bom_net_needs.php?id=' . ($id > 0 ? $id : '__ID__');
142
                    }
143
                }
144
            }
145
            if ($action == 'treeview') {
146
                $object->getNetNeedsTree($TChildBom, 1);
147
            } else {
148
                $object->getNetNeeds($TChildBom, 1);
149
            }
150
        }
151
152
153
        /*
154
         * View
155
         */
156
        require_once realpath(BASE_PATH . '/../Dolibarr/Modules/Bom/Views/bom_net_needs.php');
157
158
159
        $db->close();
160
        return true;
161
    }
162
}
163