Passed
Push — master ( 1007d6...492b54 )
by Alxarafe
26:10
created

Categories::getVars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 19
rs 9.7666
c 0
b 0
f 0
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\DolUtils;
31
use Alixar\Base\Categorie;
32
use Alixar\Base\ExtraFields;
33
use Alixar\Base\Form;
34
35
// require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
36
// require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
37
// require_once DOL_DOCUMENT_ROOT . '/core/class/html.formother.class.php';
38
39
class Categories extends AlixarController
40
{
41
42
    public $action;
43
    public $cancel;
44
    public $origin;
45
    public $catorigin;
46
    public $type;
47
    public $urlfrom;
48
    public $backtopage;
49
    public $id;
50
    public $socid;
51
    public $label;
52
    public $description;
53
    public $color;
54
    public $visible;
55
    public $parent;
56
    public $object;
57
    public $catname;
58
59
    public function __construct()
60
    {
61
        parent::__construct();
62
63
        // Load translation files required by the page
64
        Globals::$langs->load("categories");
65
66
        // Security check
67
        $this->socid = DolUtils::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 = DolUtils::GETPOST('action', 'alpha');
79
        $this->cancel = DolUtils::GETPOST('cancel', 'alpha');
80
        $this->origin = DolUtils::GETPOST('origin', 'alpha');
81
        $this->catorigin = DolUtils::GETPOST('catorigin', 'int');
82
        $this->type = DolUtils::GETPOST('type', 'alpha');
83
        $this->urlfrom = DolUtils::GETPOST('urlfrom', 'alpha');
84
        $this->backtopage = DolUtils::GETPOST('backtopage', 'alpha');
85
86
        $this->id = DolUtils::GETPOST('id', 'int');
87
        $this->socid = DolUtils::GETPOST('socid', 'int');
88
        $this->label = DolUtils::GETPOST('label');
89
        $this->description = DolUtils::GETPOST('description');
90
        $this->color = DolUtils::GETPOST('color');
91
        $this->visible = DolUtils::GETPOST('visible');
92
        $this->parent = DolUtils::GETPOST('parent');
93
        $this->catname = DolUtils::GETPOST('catname', 'alpha');
94
    }
95
96
    function index()
97
    {
98
        Skin::$view = new CategoriesIndexView($this);
99
    }
100
101
    function main()
102
    {
103
        Skin::$view = new CategoriesView($this);
104
        if ($this->origin) {
105
            if ($this->type == Categorie::TYPE_PRODUCT) {
106
                $idProdOrigin = $this->origin;
107
            }
108
            if ($this->type == Categorie::TYPE_SUPPLIER) {
109
                $idSupplierOrigin = $this->origin;
110
            }
111
            if ($this->type == Categorie::TYPE_CUSTOMER) {
112
                $idCompanyOrigin = $this->origin;
113
            }
114
            if ($this->type == Categorie::TYPE_MEMBER) {
115
                $idMemberOrigin = $this->origin;
116
            }
117
            if ($this->type == Categorie::TYPE_CONTACT) {
118
                $idContactOrigin = $this->origin;
119
            }
120
            if ($this->type == Categorie::TYPE_PROJECT) {
121
                $idProjectOrigin = $this->origin;
122
            }
123
        }
124
125
        if ($this->catorigin && $this->type == Categorie::TYPE_PRODUCT) {
126
            $idCatOrigin = $this->catorigin;
127
        }
128
129
        $this->object = new Categorie();
130
131
        $extrafields = new ExtraFields();
132
        $extralabels = $extrafields->fetch_name_optionals_label($this->object->table_element);
133
134
        // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array array
135
        Globals::$hookManager->initHooks(array('categorycard'));
136
137
138
        /*
139
         * 	Actions
140
         */
141
142
        // Add action
143
        if ($this->action == 'add' && Globals::$user->rights->categorie->creer) {
144
            // Action ajout d'une categorie
145
            if ($this->cancel) {
146
                if ($this->urlfrom) {
147
                    header("Location: " . $urlfrom);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $urlfrom seems to be never defined.
Loading history...
148
                    exit;
149
                }
150
                if ($idProdOrigin) {
151
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProdOrigin . '&type=' . $this->type);
152
                    exit;
153
                }
154
                if ($idCompanyOrigin) {
155
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idCompanyOrigin . '&type=' . $this->type);
156
                    exit;
157
                }
158
                if ($idSupplierOrigin) {
159
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idSupplierOrigin . '&type=' . $this->type);
160
                    exit;
161
                }
162
                if ($idMemberOrigin) {
163
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idMemberOrigin . '&type=' . $this->type);
164
                    exit;
165
                }
166
                if ($idContactOrigin) {
167
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idContactOrigin . '&type=' . $this->type);
168
                    exit;
169
                }
170
                if ($idProjectOrigin) {
171
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProjectOrigin . '&type=' . $this->type);
172
                    exit;
173
                }
174
                header("Location: " . DOL_URL_ROOT . '/categories/index.php?leftmenu=cat&type=' . $this->type);
175
                exit;
176
            }
177
178
            $object->label = $this->label;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $object seems to be never defined.
Loading history...
179
            $object->color = $this->color;
180
            $object->description = DolUtils::dol_htmlcleanlastbr($this->description);
181
            $object->socid = ($this->socid ? $this->socid : 'null');
182
            $object->visible = $this->visible;
183
            $object->type = $this->type;
184
185
            if ($parent != "-1") {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parent seems to be never defined.
Loading history...
186
                $object->fk_parent = $parent;
187
            }
188
189
            $ret = $extrafields->setOptionalsFromPost($extralabels, $object);
0 ignored issues
show
Deprecated Code introduced by
The function Alixar\Base\ExtraFields::setOptionalsFromPost() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

189
            $ret = /** @scrutinizer ignore-deprecated */ $extrafields->setOptionalsFromPost($extralabels, $object);
Loading history...
190
            if ($ret < 0) {
191
                $error++;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $error seems to be never defined.
Loading history...
192
            }
193
194
            if (!$object->label) {
195
                $error++;
196
                setEventMessages(Globals::$langs->trans("ErrorFieldRequired", Globals::$langs->transnoentities("Ref")), null, 'errors');
197
                $this->action = 'create';
198
            }
199
200
            // Create category in database
201
            if (!$error) {
202
                $result = $object->create($user);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user seems to be never defined.
Loading history...
203
                if ($result > 0) {
204
                    $this->action = 'confirmed';
205
                    $_POST["addcat"] = '';
206
                } else {
207
                    setEventMessages($object->error, $object->errors, 'errors');
208
                }
209
            }
210
        }
211
212
        // Confirm action
213
        if (($this->action == 'add' || $this->action == 'confirmed') && Globals::$user->rights->categorie->creer) {
214
            // Action confirmation de creation categorie
215
            if ($this->action == 'confirmed') {
216
                if ($urlfrom) {
217
                    header("Location: " . $urlfrom);
218
                    exit;
219
                }
220
                if ($backtopage) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $backtopage seems to be never defined.
Loading history...
221
                    header("Location: " . $backtopage);
222
                    exit;
223
                }
224
                if ($idProdOrigin) {
225
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProdOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
226
                    exit;
227
                }
228
                if ($idCompanyOrigin) {
229
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idCompanyOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
230
                    exit;
231
                }
232
                if ($idSupplierOrigin) {
233
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idSupplierOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
234
                    exit;
235
                }
236
                if ($idMemberOrigin) {
237
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idMemberOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
238
                    exit;
239
                }
240
                if ($idContactOrigin) {
241
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idContactOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
242
                    exit;
243
                }
244
                if ($idProjectOrigin) {
245
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProjectOrigin . '&type=' . $this->type . '&mesg=' . urlencode(Globals::$langs->trans("CatCreated")));
246
                    exit;
247
                }
248
249
                header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $result . '&type=' . $this->type);
250
                exit;
251
            }
252
        }
253
    }
254
}
255