Test Failed
Push — main ( d8c629...1dcfac )
by Rafael
43:23
created

Cstate::update()   F

Complexity

Conditions 13
Paths 1024

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 29
nc 1024
nop 2
dl 0
loc 47
rs 2.45
c 0
b 0
f 0

How to fix   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) 2016 Laurent Destailleur  <[email protected]>
4
 * Copyright (C) 2024       Rafael San José         <[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 DoliCore\Model;
21
22
use DoliCore\Base\CommonDict;
23
24
/**
25
 *      \file       htdocs/core/class/cstate.class.php
26
 *      \ingroup    core
27
 *      \brief      This file is a CRUD class file (Create/Read/Update/Delete) for c_departements dictionary
28
 */
29
30
// Put here all includes required by your class file
31
32
33
/**
34
 *  Class to manage dictionary States (used by imports)
35
 */
36
class Cstate extends CommonDict
0 ignored issues
show
Deprecated Code introduced by
The class DoliCore\Base\CommonDict has been deprecated: This class is only needed for compatibility with Dolibarr. ( Ignorable by Annotation )

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

36
class Cstate extends /** @scrutinizer ignore-deprecated */ CommonDict
Loading history...
37
{
38
    /**
39
     * @var int         The ID of the state
40
     */
41
    public $rowid;
42
43
    /**
44
     * @var string      The code of the state
45
     *                  (ex: LU0011, MA12, 07, 0801, etc.)
46
     */
47
    public $code_departement;
48
49
    /**
50
     * @var string      The name of the state
51
     */
52
    public $name = '';
53
54
    /**
55
     * @var string
56
     * @deprecated
57
     * @see $name
58
     */
59
    public $nom = '';
60
61
62
    /**
63
     *  Constructor
64
     *
65
     * @param DoliDB $db Database handler
0 ignored issues
show
Bug introduced by
The type DoliCore\Model\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
66
     */
67
    public function __construct($db)
68
    {
69
        $this->db = $db;
70
    }
71
72
73
    /**
74
     *  Create object into database
75
     *
76
     * @param User $user      User that create
77
     * @param int  $notrigger 0=launch triggers after, 1=disable triggers
78
     *
79
     * @return     int                  Return integer <0 if KO, Id of created object if OK
80
     */
81
    public function create($user, $notrigger = 0)
82
    {
83
        $error = 0;
84
85
        // Clean parameters
86
        if (isset($this->code_departement)) {
87
            $this->code_departement = trim($this->code_departement);
88
        }
89
        if (isset($this->nom)) {
90
            $this->nom = trim($this->nom);
91
        }
92
        if (isset($this->active)) {
93
            $this->active = (int) $this->active;
94
        }
95
96
        // Check parameters
97
        // Put here code to add control on parameters values
98
99
        // Insert request
100
        $sql = "INSERT INTO " . $this->db->prefix() . "c_departements(";
101
        $sql .= "rowid,";
102
        $sql .= "code_departement,";
103
        $sql .= "nom,";
104
        $sql .= "active";
105
        $sql .= ") VALUES (";
106
        $sql .= " " . (!isset($this->rowid) ? 'NULL' : "'" . $this->db->escape($this->rowid) . "'") . ",";
107
        $sql .= " " . (!isset($this->code_departement) ? 'NULL' : "'" . $this->db->escape($this->code_departement) . "'") . ",";
108
        $sql .= " " . (!isset($this->nom) ? 'NULL' : "'" . $this->db->escape($this->nom) . "'") . ",";
109
        $sql .= " " . (!isset($this->active) ? 'NULL' : "'" . $this->db->escape($this->active) . "'");
110
        $sql .= ")";
111
112
        $this->db->begin();
113
114
        dol_syslog(get_class($this) . "::create", LOG_DEBUG);
115
        $resql = $this->db->query($sql);
116
        if (!$resql) {
117
            $error++;
118
            $this->errors[] = "Error " . $this->db->lasterror();
119
        }
120
121
        if (!$error) {
122
            $this->id = $this->db->last_insert_id($this->db->prefix() . "c_departements");
123
        }
124
125
        // Commit or rollback
126
        if ($error) {
127
            foreach ($this->errors as $errmsg) {
128
                dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
129
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
130
            }
131
            $this->db->rollback();
132
            return -1 * $error;
133
        } else {
134
            $this->db->commit();
135
            return $this->id;
136
        }
137
    }
138
139
140
    /**
141
     *  Load object in memory from database
142
     *
143
     * @param int    $id   State ID
144
     * @param string $code State code
145
     *
146
     * @return     int             Return integer <0 if KO, >0 if OK
147
     */
148
    public function fetch($id, $code = '')
149
    {
150
        $sql = "SELECT";
151
        $sql .= " t.rowid,";
152
        $sql .= " t.code_departement,";
153
        $sql .= " t.nom,";
154
        $sql .= " t.active";
155
        $sql .= " FROM " . $this->db->prefix() . "c_departements as t";
156
        if ($id) {
157
            $sql .= " WHERE t.rowid = " . ((int) $id);
158
        } elseif ($code) {
159
            $sql .= " WHERE t.code_departement = '" . $this->db->escape($code) . "'";
160
        }
161
162
        dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
163
        $resql = $this->db->query($sql);
164
        if ($resql) {
165
            if ($this->db->num_rows($resql)) {
166
                $obj = $this->db->fetch_object($resql);
167
168
                $this->id = $obj->rowid;
169
                $this->code_departement = $obj->code_departement; //deprecated
170
                $this->code = $obj->code_departement;
171
                $this->nom = $obj->nom; //deprecated
172
                $this->name = $obj->nom;
173
                $this->active = $obj->active;
174
            }
175
            $this->db->free($resql);
176
177
            return 1;
178
        } else {
179
            $this->error = "Error " . $this->db->lasterror();
180
            return -1;
181
        }
182
    }
183
184
185
    /**
186
     *  Update object into database
187
     *
188
     * @param User $user      User who updates
189
     * @param int  $notrigger 0=launch triggers after, 1=disable triggers
190
     *
191
     * @return     int                  Return integer <0 if KO, >0 if OK
192
     */
193
    public function update($user = null, $notrigger = 0)
194
    {
195
        $error = 0;
196
197
        // Clean parameters
198
        if (isset($this->code_departement)) {
199
            $this->code_departement = trim($this->code_departement);
200
        }
201
        if (isset($this->name)) {
202
            $this->name = trim($this->name);
203
        }
204
        if (isset($this->active)) {
205
            $this->active = (int) $this->active;
206
        }
207
208
        // Check parameters
209
        if (empty($this->name) && !empty($this->nom)) {
210
            $this->name = $this->nom;
211
        }
212
213
        // Update request
214
        $sql = "UPDATE " . $this->db->prefix() . "c_departements SET";
215
        $sql .= " code_departement=" . (isset($this->code_departement) ? "'" . $this->db->escape($this->code_departement) . "'" : "null") . ",";
216
        $sql .= " nom=" . (isset($this->name) ? "'" . $this->db->escape($this->name) . "'" : "null") . ",";
217
        $sql .= " active=" . (isset($this->active) ? ((int) $this->active) : "null");
218
        $sql .= " WHERE rowid=" . ((int) $this->id);
219
220
        $this->db->begin();
221
222
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
223
        $resql = $this->db->query($sql);
224
        if (!$resql) {
225
            $error++;
226
            $this->errors[] = "Error " . $this->db->lasterror();
227
        }
228
229
        // Commit or rollback
230
        if ($error) {
231
            foreach ($this->errors as $errmsg) {
232
                dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
233
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
234
            }
235
            $this->db->rollback();
236
            return -1 * $error;
237
        } else {
238
            $this->db->commit();
239
            return 1;
240
        }
241
    }
242
243
    /**
244
     *  Delete object in database
245
     *
246
     * @param User $user      User that delete
247
     * @param int  $notrigger 0=launch triggers after, 1=disable triggers
248
     *
249
     * @return int                  Return integer <0 if KO, >0 if OK
250
     */
251
    public function delete($user, $notrigger = 0)
252
    {
253
        $error = 0;
254
255
        $sql = "DELETE FROM " . $this->db->prefix() . "c_departements";
256
        $sql .= " WHERE rowid=" . ((int) $this->id);
257
258
        $this->db->begin();
259
260
        dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
261
        $resql = $this->db->query($sql);
262
        if (!$resql) {
263
            $error++;
264
            $this->errors[] = "Error " . $this->db->lasterror();
265
        }
266
267
        // Commit or rollback
268
        if ($error) {
269
            foreach ($this->errors as $errmsg) {
270
                dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
271
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
272
            }
273
            $this->db->rollback();
274
            return -1 * $error;
275
        } else {
276
            $this->db->commit();
277
            return 1;
278
        }
279
    }
280
281
    /**
282
     *  Return a link to the object card (with optionally the picto)
283
     *
284
     * @param int    $withpicto             Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
285
     * @param string $option                On what the link point to ('nolink', ...)
286
     * @param int    $notooltip             1=Disable tooltip
287
     * @param string $morecss               Add more css on link
288
     * @param int    $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save
289
     *                                      lastsearch_values whenclicking
290
     *
291
     * @return string                              String with URL
292
     */
293
    public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
294
    {
295
        global $langs;
296
        return $langs->trans($this->name);
297
    }
298
}
299