Passed
Push — master ( 0f9140...c4489d )
by Alxarafe
22:27
created

AlCategories   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 137
dl 0
loc 215
rs 9.0399
c 0
b 0
f 0
wmc 42

4 Methods

Rating   Name   Duplication   Size   Complexity  
D main() 0 151 38
A index() 0 4 1
A __construct() 0 15 2
A getVars() 0 19 1

How to fix   Complexity   

Complex Class

Complex classes like AlCategories often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AlCategories, and based on these observations, apply Extract Interface, too.

1
<?php
2
/* Copyright (C) 2005		Matthieu Valleton	<[email protected]>
3
 * Copyright (C) 2006-2017	Laurent Destailleur	<[email protected]>
4
 * Copyright (C) 2005-2014	Regis Houssin		<[email protected]>
5
 * Copyright (C) 2007		Patrick Raguin		<[email protected]>
6
 * Copyright (C) 2013		Florian Henry		<[email protected]>
7
 * Copyright (C) 2015       Raphaël Doursenaud  <[email protected]>
8
 * Copyright (C) 2019       Alxarafe            <[email protected]>
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22
 */
23
namespace Alixar\Controllers;
24
25
use Alxarafe\Helpers\Skin;
26
use Alixar\Base\AlixarController;
27
use Alixar\Views\CategoriesView;
28
use Alixar\Views\CategoriesIndexView;
29
use Alixar\Helpers\Globals;
30
use Alixar\Helpers\AlDolUtils;
31
use Alixar\Base\AlCategorie;
32
use Alixar\Base\AlExtraFields;
33
34
// require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
35
// require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
36
// require_once DOL_DOCUMENT_ROOT . '/core/class/html.formother.class.php';
37
38
class AlCategories extends AlixarController
39
{
40
41
    public $action;
42
    public $cancel;
43
    public $origin;
44
    public $catorigin;
45
    public $type;
46
    public $urlfrom;
47
    public $backtopage;
48
    public $id;
49
    public $socid;
50
    public $label;
51
    public $description;
52
    public $color;
53
    public $visible;
54
    public $parent;
55
    public $object;
56
    public $catname;
57
58
    public function __construct()
59
    {
60
        die('En constructor');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
61
        parent::__construct();
0 ignored issues
show
Unused Code introduced by
parent::__construct() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
62
63
        // Load translation files required by the page
64
        Globals::$langs->load("categories");
65
66
        // Security check
67
        $this->socid = AlDolUtils::GETPOST('socid', 'int');
68
        if (!Globals::$user->rights->categorie->lire) {
69
            accessforbidden();
70
        }
71
72
        $this->getVars();
73
    }
74
75
    function getVars()
76
    {
77
78
        $this->action = AlDolUtils::GETPOST('action', 'alpha');
79
        $this->cancel = AlDolUtils::GETPOST('cancel', 'alpha');
80
        $this->origin = AlDolUtils::GETPOST('origin', 'alpha');
81
        $this->catorigin = AlDolUtils::GETPOST('catorigin', 'int');
82
        $this->type = AlDolUtils::GETPOST('type', 'alpha');
83
        $this->urlfrom = AlDolUtils::GETPOST('urlfrom', 'alpha');
84
        $this->backtopage = AlDolUtils::GETPOST('backtopage', 'alpha');
85
86
        $this->id = AlDolUtils::GETPOST('id', 'int');
87
        $this->socid = AlDolUtils::GETPOST('socid', 'int');
88
        $this->label = AlDolUtils::GETPOST('label');
89
        $this->description = AlDolUtils::GETPOST('description');
90
        $this->color = AlDolUtils::GETPOST('color');
91
        $this->visible = AlDolUtils::GETPOST('visible');
92
        $this->parent = AlDolUtils::GETPOST('parent');
93
        $this->catname = AlDolUtils::GETPOST('catname', 'alpha');
94
    }
95
96
    function index()
97
    {
98
        die('En index');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
99
        Skin::$view = new CategoriesIndexView($this);
0 ignored issues
show
Unused Code introduced by
Alxarafe\Helpers\Skin::v...egoriesIndexView($this) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
100
    }
101
102
    function main()
103
    {
104
        die('En main');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
105
        Skin::$view = new CategoriesView($this);
0 ignored issues
show
Unused Code introduced by
Alxarafe\Helpers\Skin::v...s\CategoriesView($this) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
106
        if ($this->origin) {
107
            if ($this->type == AlCategorie::TYPE_PRODUCT) {
108
                $idProdOrigin = $this->origin;
109
            }
110
            if ($this->type == AlCategorie::TYPE_SUPPLIER) {
111
                $idSupplierOrigin = $this->origin;
112
            }
113
            if ($this->type == AlCategorie::TYPE_CUSTOMER) {
114
                $idCompanyOrigin = $this->origin;
115
            }
116
            if ($this->type == AlCategorie::TYPE_MEMBER) {
117
                $idMemberOrigin = $this->origin;
118
            }
119
            if ($this->type == AlCategorie::TYPE_CONTACT) {
120
                $idContactOrigin = $this->origin;
121
            }
122
            if ($this->type == AlCategorie::TYPE_PROJECT) {
123
                $idProjectOrigin = $this->origin;
124
            }
125
        }
126
127
        if ($this->catorigin && $this->type == AlCategorie::TYPE_PRODUCT) {
128
            $idCatOrigin = $this->catorigin;
129
        }
130
131
        $this->object = new AlCategorie();
132
133
        $extrafields = new AlExtraFields();
134
        $extralabels = $extrafields->fetch_name_optionals_label($this->object->table_element);
135
136
        // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array array
137
        Globals::$hookManager->initHooks(array('categorycard'));
138
139
140
        /*
141
         * 	Actions
142
         */
143
144
        // Add action
145
        if ($this->action == 'add' && Globals::$user->rights->categorie->creer) {
146
            // Action ajout d'une categorie
147
            if ($this->cancel) {
148
                if ($this->urlfrom) {
149
                    header("Location: " . $urlfrom);
150
                    exit;
151
                }
152
                if ($idProdOrigin) {
153
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProdOrigin . '&type=' . $this->type);
154
                    exit;
155
                }
156
                if ($idCompanyOrigin) {
157
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idCompanyOrigin . '&type=' . $this->type);
158
                    exit;
159
                }
160
                if ($idSupplierOrigin) {
161
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idSupplierOrigin . '&type=' . $this->type);
162
                    exit;
163
                }
164
                if ($idMemberOrigin) {
165
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idMemberOrigin . '&type=' . $this->type);
166
                    exit;
167
                }
168
                if ($idContactOrigin) {
169
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idContactOrigin . '&type=' . $this->type);
170
                    exit;
171
                }
172
                if ($idProjectOrigin) {
173
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProjectOrigin . '&type=' . $this->type);
174
                    exit;
175
                }
176
                header("Location: " . DOL_URL_ROOT . '/categories/index.php?leftmenu=cat&type=' . $this->type);
177
                exit;
178
            }
179
180
            $object->label = $this->label;
181
            $object->color = $this->color;
182
            $object->description = AlDolUtils::dol_htmlcleanlastbr($this->description);
183
            $object->socid = ($this->socid ? $this->socid : 'null');
184
            $object->visible = $this->visible;
185
            $object->type = $this->type;
186
187
            if ($parent != "-1") {
188
                $object->fk_parent = $parent;
189
            }
190
191
            $ret = $extrafields->setOptionalsFromPost($extralabels, $object);
192
            if ($ret < 0) {
193
                $error++;
194
            }
195
196
            if (!$object->label) {
197
                $error++;
198
                setEventMessages(Globals::$langs->trans("ErrorFieldRequired", Globals::$langs->transnoentities("Ref")), null, 'errors');
199
                $this->action = 'create';
200
            }
201
202
            // Create category in database
203
            if (!$error) {
204
                $result = $object->create($user);
205
                if ($result > 0) {
206
                    $this->action = 'confirmed';
207
                    $_POST["addcat"] = '';
208
                } else {
209
                    setEventMessages($object->error, $object->errors, 'errors');
210
                }
211
            }
212
        }
213
214
        // Confirm action
215
        if (($this->action == 'add' || $this->action == 'confirmed') && Globals::$user->rights->categorie->creer) {
216
            // Action confirmation de creation categorie
217
            if ($this->action == 'confirmed') {
218
                if ($urlfrom) {
219
                    header("Location: " . $urlfrom);
220
                    exit;
221
                }
222
                if ($backtopage) {
223
                    header("Location: " . $backtopage);
224
                    exit;
225
                }
226
                if ($idProdOrigin) {
227
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProdOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
228
                    exit;
229
                }
230
                if ($idCompanyOrigin) {
231
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idCompanyOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
232
                    exit;
233
                }
234
                if ($idSupplierOrigin) {
235
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idSupplierOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
236
                    exit;
237
                }
238
                if ($idMemberOrigin) {
239
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idMemberOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
240
                    exit;
241
                }
242
                if ($idContactOrigin) {
243
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idContactOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
244
                    exit;
245
                }
246
                if ($idProjectOrigin) {
247
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProjectOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
248
                    exit;
249
                }
250
251
                header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $result . '&type=' . $this->type);
252
                exit;
253
            }
254
        }
255
    }
256
}
257