Passed
Push — master ( 65bdac...4e88da )
by Alxarafe
32:38
created

Controllers/Categories.php (3 issues)

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 Alixar\Base\AlixarController;
26
use Alixar\Views\CategoriesView;
27
use Alixar\Helpers\Globals;
28
use Alixar\Helpers\DolUtils;
29
use Alixar\Base\Categorie;
30
use Alixar\Base\ExtraFields;
31
32
// require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
33
// require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
34
// require_once DOL_DOCUMENT_ROOT . '/core/class/html.formother.class.php';
35
36
class Categories extends AlixarController
37
{
38
39
    public function __construct()
40
    {
41
        parent::__construct();
42
        Skin::$view = new CategoriesView($this);
43
    }
44
45
    function main()
46
    {
47
48
// Load translation files required by the page
49
       Globals::$langs->load("categories");
50
51
// Security check
52
        $socid = DolUtils::GETPOST('socid', 'int');
53
        if (!Globals::$user->rights->categorie->lire)
54
            accessforbidden();
55
56
        $action = DolUtils::GETPOST('action', 'alpha');
57
        $cancel = DolUtils::GETPOST('cancel', 'alpha');
58
        $origin = DolUtils::GETPOST('origin', 'alpha');
59
        $catorigin = DolUtils::GETPOST('catorigin', 'int');
60
        $type = DolUtils::GETPOST('type', 'alpha');
61
        $urlfrom = DolUtils::GETPOST('urlfrom', 'alpha');
62
        $backtopage = DolUtils::GETPOST('backtopage', 'alpha');
63
64
        $socid = DolUtils::GETPOST('socid', 'int');
65
        $label = DolUtils::GETPOST('label');
66
        $description = DolUtils::GETPOST('description');
67
        $color = DolUtils::GETPOST('color');
68
        $visible = DolUtils::GETPOST('visible');
69
        $parent = DolUtils::GETPOST('parent');
70
71
        if ($origin) {
72
            if ($type == Categorie::TYPE_PRODUCT)
73
                $idProdOrigin = $origin;
74
            if ($type == Categorie::TYPE_SUPPLIER)
75
                $idSupplierOrigin = $origin;
76
            if ($type == Categorie::TYPE_CUSTOMER)
77
                $idCompanyOrigin = $origin;
78
            if ($type == Categorie::TYPE_MEMBER)
79
                $idMemberOrigin = $origin;
80
            if ($type == Categorie::TYPE_CONTACT)
81
                $idContactOrigin = $origin;
82
            if ($type == Categorie::TYPE_PROJECT)
83
                $idProjectOrigin = $origin;
84
        }
85
86
        if ($catorigin && $type == Categorie::TYPE_PRODUCT)
87
            $idCatOrigin = $catorigin;
88
89
        $object = new Categorie();
90
91
        $extrafields = new ExtraFields();
92
        $extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
93
94
// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array array
95
        Globals::$hookManager->initHooks(array('categorycard'));
96
97
98
        /*
99
         * 	Actions
100
         */
101
102
// Add action
103
        if ($action == 'add' && Globals::$user->rights->categorie->creer) {
104
            // Action ajout d'une categorie
105
            if ($cancel) {
106
                if ($urlfrom) {
107
                    header("Location: " . $urlfrom);
108
                    exit;
109
                } else if ($idProdOrigin) {
110
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProdOrigin . '&type=' . $type);
111
                    exit;
112
                } else if ($idCompanyOrigin) {
113
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idCompanyOrigin . '&type=' . $type);
114
                    exit;
115
                } else if ($idSupplierOrigin) {
116
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idSupplierOrigin . '&type=' . $type);
117
                    exit;
118
                } else if ($idMemberOrigin) {
119
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idMemberOrigin . '&type=' . $type);
120
                    exit;
121
                } else if ($idContactOrigin) {
122
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idContactOrigin . '&type=' . $type);
123
                    exit;
124
                } else if ($idProjectOrigin) {
125
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProjectOrigin . '&type=' . $type);
126
                    exit;
127
                } else {
128
                    header("Location: " . DOL_URL_ROOT . '/categories/index.php?leftmenu=cat&type=' . $type);
129
                    exit;
130
                }
131
            }
132
133
134
135
            $object->label = $label;
0 ignored issues
show
Documentation Bug introduced by
It seems like $label can also be of type string[]. However, the property $label is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
136
            $object->color = $color;
0 ignored issues
show
Documentation Bug introduced by
It seems like $color can also be of type string[]. However, the property $color is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
137
            $object->description = dol_htmlcleanlastbr($description);
138
            $object->socid = ($socid ? $socid : 'null');
139
            $object->visible = $visible;
140
            $object->type = $type;
141
142
            if ($parent != "-1")
143
                $object->fk_parent = $parent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parent of type string or string[] is incompatible with the declared type integer of property $fk_parent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
144
145
            $ret = $extrafields->setOptionalsFromPost($extralabels, $object);
146
            if ($ret < 0)
147
                $error++;
148
149
            if (!$object->label) {
150
                $error++;
151
                setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
152
                $action = 'create';
153
            }
154
155
            // Create category in database
156
            if (!$error) {
157
                $result = $object->create($user);
158
                if ($result > 0) {
159
                    $action = 'confirmed';
160
                    $_POST["addcat"] = '';
161
                } else {
162
                    setEventMessages($object->error, $object->errors, 'errors');
163
                }
164
            }
165
        }
166
167
// Confirm action
168
        if (($action == 'add' || $action == 'confirmed') && Globals::$user->rights->categorie->creer) {
169
            // Action confirmation de creation categorie
170
            if ($action == 'confirmed') {
171
                if ($urlfrom) {
172
                    header("Location: " . $urlfrom);
173
                    exit;
174
                } elseif ($backtopage) {
175
                    header("Location: " . $backtopage);
176
                    exit;
177
                } else if ($idProdOrigin) {
178
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProdOrigin . '&type=' . $type . '&mesg=' . urlencode($langs->trans("CatCreated")));
179
                    exit;
180
                } else if ($idCompanyOrigin) {
181
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idCompanyOrigin . '&type=' . $type . '&mesg=' . urlencode($langs->trans("CatCreated")));
182
                    exit;
183
                } else if ($idSupplierOrigin) {
184
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idSupplierOrigin . '&type=' . $type . '&mesg=' . urlencode($langs->trans("CatCreated")));
185
                    exit;
186
                } else if ($idMemberOrigin) {
187
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idMemberOrigin . '&type=' . $type . '&mesg=' . urlencode($langs->trans("CatCreated")));
188
                    exit;
189
                } else if ($idContactOrigin) {
190
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idContactOrigin . '&type=' . $type . '&mesg=' . urlencode($langs->trans("CatCreated")));
191
                    exit;
192
                } else if ($idProjectOrigin) {
193
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProjectOrigin . '&type=' . $type . '&mesg=' . urlencode($langs->trans("CatCreated")));
194
                    exit;
195
                }
196
197
                header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $result . '&type=' . $type);
198
                exit;
199
            }
200
        }
201
202
203
    }
204
}
205