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

BomDocumentController::index()   F

Complexity

Conditions 12
Paths 384

Size

Total Lines 84
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 41
c 1
b 0
f 0
nc 384
nop 1
dl 0
loc 84
rs 3.8333

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) 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_document.php
33
 *    \ingroup    bom
34
 *    \brief      Tab for documents linked to BillOfMaterials
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 BASE_PATH . '/../Dolibarr/Lib/Company.php';
44
require_once BASE_PATH . '/../Dolibarr/Lib/Files.php';
45
require_once BASE_PATH . '/../Dolibarr/Lib/Images.php';
46
require_once DOL_DOCUMENT_ROOT . '/bom/class/bom.class.php';
47
require_once BASE_PATH . '/../Dolibarr/Modules/Bom/Lib/Bom.php';
48
49
class BomDocumentController 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
        global $mysoc;
61
62
// Load translation files required by the page
63
        $langs->loadLangs(["mrp", "companies", "other", "mails"]);
64
65
// Get parameters
66
        $action = GETPOST('action', 'aZ09');
67
        $confirm = GETPOST('confirm', 'alpha');
68
        $id = (GETPOSTINT('socid') ? GETPOSTINT('socid') : GETPOSTINT('id'));
69
        $ref = GETPOST('ref', 'alpha');
70
71
// Security check - Protection if external user
72
// if ($user->socid > 0) accessforbidden();
73
// if ($user->socid > 0) $socid = $user->socid;
74
// $result = restrictedArea($user, 'bom', $id);
75
76
// Load variables for pagination
77
        $limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
78
        $sortfield = GETPOST('sortfield', 'aZ09comma');
79
        $sortorder = GETPOST('sortorder', 'aZ09comma');
80
        $page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
81
        if (empty($page) || $page == -1) {
82
            $page = 0;
83
        }     // If $page is not defined, or '' or -1
84
        $offset = $limit * $page;
85
        $pageprev = $page - 1;
86
        $pagenext = $page + 1;
87
        if (!$sortorder) {
88
            $sortorder = "ASC";
89
        }
90
        if (!$sortfield) {
91
            $sortfield = "name";
92
        }
93
//if (! $sortfield) $sortfield="position_name";
94
95
// Initialize technical objects
96
        $object = new Bom($db);
97
        $extrafields = new ExtraFields($db);
98
        $diroutputmassaction = $conf->bom->dir_output . '/temp/massgeneration/' . $user->id;
99
        $hookmanager->initHooks(['bomdocument', 'globalcard']); // Note that conf->hooks_modules contains array
100
// Fetch optionals attributes and labels
101
        $extrafields->fetch_name_optionals_label($object->table_element);
102
103
// Load object
104
        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
105
106
        if ($id > 0 || !empty($ref)) {
107
            $upload_dir = $conf->bom->multidir_output[$object->entity ? $object->entity : 1] . "/" . get_exdir(0, 0, 0, 1, $object);
108
        }
109
110
// Security check - Protection if external user
111
//if ($user->socid > 0) accessforbidden();
112
//if ($user->socid > 0) $socid = $user->socid;
113
        $isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
114
        restrictedArea($user, 'bom', $object->id, $object->table_element, '', '', 'rowid', $isdraft);
115
116
        $permissiontoadd = $user->hasRight('bom', 'write'); // Used by the include of actions_addupdatedelete.inc.php and actions_linkedfiles.inc.php
117
118
119
        /*
120
         * Actions
121
         */
122
123
        include DOL_DOCUMENT_ROOT . '/core/actions_linkedfiles.inc.php';
124
125
126
        /*
127
         * View
128
         */
129
        require_once realpath(BASE_PATH . '/../Dolibarr/Modules/Bom/Views/bom_document.php');
130
131
132
        $db->close();
133
134
        return true;
135
    }
136
}
137