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

Controllers/AlCategories.php (8 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 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
        parent::__construct();
61
62
        // Load translation files required by the page
63
        Globals::$langs->load("categories");
64
65
        // Security check
66
        $this->socid = AlDolUtils::GETPOST('socid', 'int');
67
        if (!Globals::$user->rights->categorie->lire) {
68
            accessforbidden();
69
        }
70
71
        $this->getVars();
72
    }
73
74
    function getVars()
75
    {
76
77
        $this->action = AlDolUtils::GETPOST('action', 'alpha');
78
        $this->cancel = AlDolUtils::GETPOST('cancel', 'alpha');
79
        $this->origin = AlDolUtils::GETPOST('origin', 'alpha');
80
        $this->catorigin = AlDolUtils::GETPOST('catorigin', 'int');
81
        $this->type = AlDolUtils::GETPOST('type', 'alpha');
82
        $this->urlfrom = AlDolUtils::GETPOST('urlfrom', 'alpha');
83
        $this->backtopage = AlDolUtils::GETPOST('backtopage', 'alpha');
84
85
        $this->id = AlDolUtils::GETPOST('id', 'int');
86
        $this->socid = AlDolUtils::GETPOST('socid', 'int');
87
        $this->label = AlDolUtils::GETPOST('label');
88
        $this->description = AlDolUtils::GETPOST('description');
89
        $this->color = AlDolUtils::GETPOST('color');
90
        $this->visible = AlDolUtils::GETPOST('visible');
91
        $this->parent = AlDolUtils::GETPOST('parent');
92
        $this->catname = AlDolUtils::GETPOST('catname', 'alpha');
93
    }
94
95
    function index()
96
    {
97
        Skin::$view = new CategoriesIndexView($this);
98
    }
99
100
    function main()
101
    {
102
        Skin::$view = new CategoriesView($this);
103
        if ($this->origin) {
104
            if ($this->type == Categorie::TYPE_PRODUCT) {
0 ignored issues
show
The type Alixar\Controllers\Categorie was not found. Did you mean Categorie? If so, make sure to prefix the type with \.
Loading history...
105
                $idProdOrigin = $this->origin;
106
            }
107
            if ($this->type == Categorie::TYPE_SUPPLIER) {
108
                $idSupplierOrigin = $this->origin;
109
            }
110
            if ($this->type == Categorie::TYPE_CUSTOMER) {
111
                $idCompanyOrigin = $this->origin;
112
            }
113
            if ($this->type == Categorie::TYPE_MEMBER) {
114
                $idMemberOrigin = $this->origin;
115
            }
116
            if ($this->type == Categorie::TYPE_CONTACT) {
117
                $idContactOrigin = $this->origin;
118
            }
119
            if ($this->type == Categorie::TYPE_PROJECT) {
120
                $idProjectOrigin = $this->origin;
121
            }
122
        }
123
124
        if ($this->catorigin && $this->type == Categorie::TYPE_PRODUCT) {
125
            $idCatOrigin = $this->catorigin;
126
        }
127
128
        $this->object = new AlCategorie();
129
130
        $extrafields = new AlExtraFields();
131
        $extralabels = $extrafields->fetch_name_optionals_label($this->object->table_element);
132
133
        // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array array
134
        Globals::$hookManager->initHooks(array('categorycard'));
135
136
137
        /*
138
         * 	Actions
139
         */
140
141
        // Add action
142
        if ($this->action == 'add' && Globals::$user->rights->categorie->creer) {
143
            // Action ajout d'une categorie
144
            if ($this->cancel) {
145
                if ($this->urlfrom) {
146
                    header("Location: " . $urlfrom);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $urlfrom seems to be never defined.
Loading history...
147
                    exit;
148
                }
149
                if ($idProdOrigin) {
150
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProdOrigin . '&type=' . $this->type);
151
                    exit;
152
                }
153
                if ($idCompanyOrigin) {
154
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idCompanyOrigin . '&type=' . $this->type);
155
                    exit;
156
                }
157
                if ($idSupplierOrigin) {
158
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idSupplierOrigin . '&type=' . $this->type);
159
                    exit;
160
                }
161
                if ($idMemberOrigin) {
162
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idMemberOrigin . '&type=' . $this->type);
163
                    exit;
164
                }
165
                if ($idContactOrigin) {
166
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idContactOrigin . '&type=' . $this->type);
167
                    exit;
168
                }
169
                if ($idProjectOrigin) {
170
                    header("Location: " . DOL_URL_ROOT . '/categories/viewcat.php?id=' . $idProjectOrigin . '&type=' . $this->type);
171
                    exit;
172
                }
173
                header("Location: " . DOL_URL_ROOT . '/categories/index.php?leftmenu=cat&type=' . $this->type);
174
                exit;
175
            }
176
177
            $object->label = $this->label;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $object seems to be never defined.
Loading history...
178
            $object->color = $this->color;
179
            $object->description = AlDolUtils::dol_htmlcleanlastbr($this->description);
180
            $object->socid = ($this->socid ? $this->socid : 'null');
181
            $object->visible = $this->visible;
182
            $object->type = $this->type;
183
184
            if ($parent != "-1") {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parent seems to be never defined.
Loading history...
185
                $object->fk_parent = $parent;
186
            }
187
188
            $ret = $extrafields->setOptionalsFromPost($extralabels, $object);
0 ignored issues
show
Deprecated Code introduced by
The function Alixar\Base\AlExtraFields::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

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