Completed
Push — master ( f63f4b...0cae82 )
by Laurent
01:38
created

Bbcvols::getPilot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2007-2012  Laurent Destailleur <[email protected]>
3
 * Copyright (C) 2014       Juanjo Menent       <[email protected]>
4
 * Copyright (C) 2015       Florian Henry       <[email protected]>
5
 * Copyright (C) 2015       Raphaël Doursenaud  <[email protected]>
6
 * Copyright (C) ---Put here your own copyright and developer email---
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
/**
23
 * \file    flightlog/bbcvols.class.php
24
 * \ingroup flightlog
25
 * \brief   This file is an example for a CRUD class file (Create/Read/Update/Delete)
26
 *          Put some comments here
27
 */
28
29
// Put here all includes required by your class file
30
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
31
require_once DOL_DOCUMENT_ROOT . '/flightballoon/class/bbc_ballons.class.php';
32
//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
34
35
/**
36
 * Class Bbcvols
37
 *
38
 * Put here description of your class
39
 * @see CommonObject
40
 */
41
class Bbcvols extends CommonObject
42
{
43
    /**
44
     * @var string Id to identify managed objects
45
     */
46
    public $element = 'flightlog_bbcvols';
47
    /**
48
     * @var string Name of table without prefix where object is stored
49
     */
50
    public $table_element = 'bbc_vols';
51
52
    /**
53
     * @var BbcvolsLine[] Lines
54
     */
55
    public $lines = array();
56
57
    public $idBBC_vols;
58
    public $date = '';
59
    public $lieuD;
60
    public $lieuA;
61
    public $heureD;
62
    public $heureA;
63
    public $BBC_ballons_idBBC_ballons;
64
    public $nbrPax;
65
    public $remarque;
66
    public $incidents;
67
    public $fk_type;
68
    public $fk_pilot;
69
    public $fk_organisateur;
70
    public $is_facture;
71
    public $kilometers;
72
    public $cost;
73
    public $fk_receiver;
74
    public $justif_kilometers;
75
    public $date_creation;
76
    public $date_update;
77
78
    /**
79
     * @var Bbc_ballons
80
     */
81
    private $balloon;
82
83
    /**
84
     * @var User
85
     */
86
    private $pilot;
87
88
    /**
89
     * @return int
90
     */
91
    public function getIdBBCVols()
92
    {
93
        return $this->idBBC_vols;
94
    }
95
96
    /**
97
     * @return int
98
     */
99
    public function getId()
100
    {
101
        return $this->getIdBBCVols();
102
    }
103
104
    /**
105
     * @param string|int $ref
106
     *
107
     * @return $this
108
     */
109
    public function setRef($ref)
110
    {
111
        $this->ref = $ref;
112
        return $this;
113
    }
114
115
    /**
116
     * Constructor
117
     *
118
     * @param DoliDb $db Database handler
119
     */
120
    public function __construct(DoliDB $db)
121
    {
122
        $this->db = $db;
123
        $this->cost = 0;
124
    }
125
126
    /**
127
     * Create object into database
128
     *
129
     * @param  User $user      User that creates
130
     * @param  bool $notrigger false=launch triggers after, true=disable triggers
131
     *
132
     * @return int <0 if KO, Id of created object if OK
133
     */
134
    public function create(User $user, $notrigger = false)
135
    {
136
        dol_syslog(__METHOD__, LOG_DEBUG);
137
138
        $error = 0;
139
140
        // Clean parameters
141
142
        if (isset($this->idBBC_vols)) {
143
            $this->idBBC_vols = trim($this->idBBC_vols);
144
        }
145
        if (isset($this->lieuD)) {
146
            $this->lieuD = trim($this->lieuD);
147
        }
148
        if (isset($this->lieuA)) {
149
            $this->lieuA = trim($this->lieuA);
150
        }
151
        if (isset($this->heureD)) {
152
            $this->heureD = trim($this->heureD);
153
        }
154
        if (isset($this->heureA)) {
155
            $this->heureA = trim($this->heureA);
156
        }
157
        if (isset($this->BBC_ballons_idBBC_ballons)) {
158
            $this->BBC_ballons_idBBC_ballons = trim($this->BBC_ballons_idBBC_ballons);
159
        }
160
        if (isset($this->nbrPax)) {
161
            $this->nbrPax = trim($this->nbrPax);
162
        }
163
        if (isset($this->remarque)) {
164
            $this->remarque = trim($this->remarque);
165
        }
166
        if (isset($this->incidents)) {
167
            $this->incidents = trim($this->incidents);
168
        }
169
        if (isset($this->fk_type)) {
170
            $this->fk_type = trim($this->fk_type);
171
        }
172
        if (isset($this->fk_pilot)) {
173
            $this->fk_pilot = trim($this->fk_pilot);
174
        }
175
        if (isset($this->fk_organisateur)) {
176
            $this->fk_organisateur = trim($this->fk_organisateur);
177
        }
178
        if (isset($this->is_facture)) {
179
            $this->is_facture = trim($this->is_facture);
180
        }
181
        if (isset($this->kilometers)) {
182
            $this->kilometers = trim($this->kilometers);
183
        }
184
        if (isset($this->cost)) {
185
            $this->cost = trim($this->cost);
186
        }
187
        if (isset($this->fk_receiver)) {
188
            $this->fk_receiver = trim($this->fk_receiver);
189
        }
190
        if (isset($this->justif_kilometers)) {
191
            $this->justif_kilometers = trim($this->justif_kilometers);
192
        }
193
194
195
        // Check parameters
196
        // Put here code to add control on parameters values
197
198
        // Insert request
199
        $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
200
201
        $sql .= 'date,';
202
        $sql .= 'lieuD,';
203
        $sql .= 'lieuA,';
204
        $sql .= 'heureD,';
205
        $sql .= 'heureA,';
206
        $sql .= 'BBC_ballons_idBBC_ballons,';
207
        $sql .= 'nbrPax,';
208
        $sql .= 'remarque,';
209
        $sql .= 'incidents,';
210
        $sql .= 'fk_type,';
211
        $sql .= 'fk_pilot,';
212
        $sql .= 'fk_organisateur,';
213
        $sql .= 'is_facture,';
214
        $sql .= 'kilometers,';
215
        $sql .= 'cost,';
216
        $sql .= 'fk_receiver,';
217
        $sql .= 'justif_kilometers,';
218
        $sql .= 'date_creation,';
219
        $sql .= 'date_update';
220
221
222
        $sql .= ') VALUES (';
223
224
        $sql .= ' ' . (!isset($this->date) || dol_strlen($this->date) == 0 ? 'NULL' : "'" . $this->db->idate($this->date) . "'") . ',';
225
        $sql .= ' ' . (!isset($this->lieuD) ? 'NULL' : "'" . $this->db->escape($this->lieuD) . "'") . ',';
226
        $sql .= ' ' . (!isset($this->lieuA) ? 'NULL' : "'" . $this->db->escape($this->lieuA) . "'") . ',';
227
        $sql .= ' ' . (!isset($this->heureD) ? 'NULL' : "'" . $this->heureD . "'") . ',';
228
        $sql .= ' ' . (!isset($this->heureA) ? 'NULL' : "'" . $this->heureA . "'") . ',';
229
        $sql .= ' ' . (!isset($this->BBC_ballons_idBBC_ballons) ? 'NULL' : $this->BBC_ballons_idBBC_ballons) . ',';
230
        $sql .= ' ' . (!isset($this->nbrPax) ? 'NULL' : "'" . $this->db->escape($this->nbrPax) . "'") . ',';
231
        $sql .= ' ' . (!isset($this->remarque) ? 'NULL' : "'" . $this->db->escape($this->remarque) . "'") . ',';
232
        $sql .= ' ' . (!isset($this->incidents) ? 'NULL' : "'" . $this->db->escape($this->incidents) . "'") . ',';
233
        $sql .= ' ' . (!isset($this->fk_type) ? 'NULL' : $this->fk_type) . ',';
234
        $sql .= ' ' . (!isset($this->fk_pilot) ? 'NULL' : $this->fk_pilot) . ',';
235
        $sql .= ' ' . (!isset($this->fk_organisateur) ? 'NULL' : $this->fk_organisateur) . ',';
236
        $sql .= ' ' . (!isset($this->is_facture) ? '0' : $this->is_facture) . ',';
237
        $sql .= ' ' . (!isset($this->kilometers) || empty($this->kilometers) ? '0' : $this->kilometers) . ',';
238
        $sql .= ' ' . (!isset($this->cost) ? 'NULL' : "'" . $this->db->escape($this->cost) . "'") . ',';
239
        $sql .= ' ' . (!isset($this->fk_receiver) ? 'NULL' : $this->fk_receiver) . ',';
240
        $sql .= ' ' . (!isset($this->justif_kilometers) ? 'NULL' : "'" . $this->db->escape($this->justif_kilometers) . "'") . ',';
241
        $sql .= ' ' . "'" . date('Y-m-d H:i:s') . "'" . ',';
242
        $sql .= ' ' . "'" . date('Y-m-d H:i:s') . "'" . '';
243
244
245
        $sql .= ')';
246
247
        $this->db->begin();
248
249
        $resql = $this->db->query($sql);
250
        if (!$resql) {
251
            $error++;
252
            $this->errors[] = 'Error ' . $this->db->lasterror();
253
            dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
254
        }
255
256 View Code Duplication
        if (!$error) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
257
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
258
259
            if (!$notrigger) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
260
                // Uncomment this and change MYOBJECT to your own tag if you
261
                // want this action to call a trigger.
262
263
                //// Call triggers
264
                //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
265
                //if ($result < 0) $error++;
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
266
                //// End call triggers
267
            }
268
        }
269
270
        // Commit or rollback
271
        if ($error) {
272
            $this->db->rollback();
273
274
            return -1 * $error;
275
        } else {
276
            $this->db->commit();
277
278
            return $this->id;
279
        }
280
    }
281
282
    /**
283
     * Load object in memory from the database
284
     *
285
     * @param int    $id  Id object
286
     * @param string $ref Ref
287
     *
288
     * @return int <0 if KO, 0 if not found, >0 if OK
289
     */
290
    public function fetch($id, $ref = null)
291
    {
292
        dol_syslog(__METHOD__, LOG_DEBUG);
293
294
        $sql = 'SELECT';
295
        $sql .= " t.idBBC_vols,";
296
        $sql .= " t.date,";
297
        $sql .= " t.lieuD,";
298
        $sql .= " t.lieuA,";
299
        $sql .= " t.heureD,";
300
        $sql .= " t.heureA,";
301
        $sql .= " t.BBC_ballons_idBBC_ballons,";
302
        $sql .= " t.nbrPax,";
303
        $sql .= " t.remarque,";
304
        $sql .= " t.incidents,";
305
        $sql .= " t.fk_type,";
306
        $sql .= " t.fk_pilot,";
307
        $sql .= " t.fk_organisateur,";
308
        $sql .= " t.is_facture,";
309
        $sql .= " t.kilometers,";
310
        $sql .= " t.cost,";
311
        $sql .= " t.fk_receiver,";
312
        $sql .= " t.justif_kilometers,";
313
        $sql .= " t.date_creation,";
314
        $sql .= " t.date_update";
315
316
317
        $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
318 View Code Duplication
        if (null !== $ref) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
319
            $sql .= ' WHERE t.ref = ' . '\'' . $ref . '\'';
320
        } else {
321
            $sql .= ' WHERE t.idBBC_vols = ' . $id;
322
        }
323
324
        $resql = $this->db->query($sql);
325
        if ($resql) {
326
            $numrows = $this->db->num_rows($resql);
327
            if ($numrows) {
328
                $obj = $this->db->fetch_object($resql);
329
330
                $this->id = $obj->idBBC_vols;
331
332
                $this->idBBC_vols = $obj->idBBC_vols;
333
                $this->date = $this->db->jdate($obj->date);
334
                $this->lieuD = $obj->lieuD;
335
                $this->lieuA = $obj->lieuA;
336
                $this->heureD = $obj->heureD;
337
                $this->heureA = $obj->heureA;
338
                $this->BBC_ballons_idBBC_ballons = $obj->BBC_ballons_idBBC_ballons;
339
                $this->nbrPax = $obj->nbrPax;
340
                $this->remarque = $obj->remarque;
341
                $this->incidents = $obj->incidents;
342
                $this->fk_type = $obj->fk_type;
343
                $this->fk_pilot = $obj->fk_pilot;
344
                $this->fk_organisateur = $obj->fk_organisateur;
345
                $this->is_facture = $obj->is_facture;
346
                $this->kilometers = $obj->kilometers;
347
                $this->cost = $obj->cost;
348
                $this->fk_receiver = $obj->fk_receiver;
349
                $this->justif_kilometers = $obj->justif_kilometers;
350
                $this->date_creation = $obj->date_creation;
351
                $this->date_update = $obj->date_update;
352
                
353
                $this->balloon = $this->fetchBalloon();
354
                $this->pilot = $this->fetchUser($this->fk_pilot);
355
            }
356
            $this->db->free($resql);
357
358
            if ($numrows) {
359
                return 1;
360
            } else {
361
                return 0;
362
            }
363 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
364
            $this->errors[] = 'Error ' . $this->db->lasterror();
365
            dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
366
367
            return -1;
368
        }
369
    }
370
371
    /**
372
     * Load object in memory from the database
373
     *
374
     * @param string $sortorder  Sort Order
375
     * @param string $sortfield  Sort field
376
     * @param int    $limit      offset limit
377
     * @param int    $offset     offset limit
378
     * @param array  $filter     filter array
379
     * @param string $filtermode filter mode (AND or OR)
380
     *
381
     * @return int <0 if KO, >0 if OK
382
     */
383
    public function fetchAll(
384
        $sortorder = '',
385
        $sortfield = '',
386
        $limit = 0,
387
        $offset = 0,
388
        array $filter = array(),
389
        $filtermode = 'AND'
390
    ) {
391
        dol_syslog(__METHOD__, LOG_DEBUG);
392
393
        $sql = 'SELECT';
394
        $sql .= " t.idBBC_vols,";
395
        $sql .= " t.date,";
396
        $sql .= " t.lieuD,";
397
        $sql .= " t.lieuA,";
398
        $sql .= " t.heureD,";
399
        $sql .= " t.heureA,";
400
        $sql .= " t.BBC_ballons_idBBC_ballons,";
401
        $sql .= " t.nbrPax,";
402
        $sql .= " t.remarque,";
403
        $sql .= " t.incidents,";
404
        $sql .= " t.fk_type,";
405
        $sql .= " t.fk_pilot,";
406
        $sql .= " t.fk_organisateur,";
407
        $sql .= " t.is_facture,";
408
        $sql .= " t.kilometers,";
409
        $sql .= " t.cost,";
410
        $sql .= " t.fk_receiver,";
411
        $sql .= " t.justif_kilometers,";
412
        $sql .= " t.date_creation,";
413
        $sql .= " t.date_update";
414
415
416
        $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
417
418
        // Manage filter
419
        $sqlwhere = array();
420
        if (count($filter) > 0) {
421
            foreach ($filter as $key => $value) {
422
                $sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
423
            }
424
        }
425 View Code Duplication
        if (count($sqlwhere) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
426
            $sql .= ' WHERE ' . implode(' ' . $filtermode . ' ', $sqlwhere);
427
        }
428
429
        if (!empty($sortfield)) {
430
            $sql .= $this->db->order($sortfield, $sortorder);
431
        }
432
        if (!empty($limit)) {
433
            $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
434
        }
435
        $this->lines = array();
436
437
        $resql = $this->db->query($sql);
438
        if ($resql) {
439
            $num = $this->db->num_rows($resql);
440
441
            while ($obj = $this->db->fetch_object($resql)) {
442
                $line = new BbcvolsLine();
443
444
                $line->id = $obj->idBBC_vols;
445
446
                $line->idBBC_vols = $obj->idBBC_vols;
447
                $line->date = $this->db->jdate($obj->date);
448
                $line->lieuD = $obj->lieuD;
449
                $line->lieuA = $obj->lieuA;
450
                $line->heureD = $obj->heureD;
451
                $line->heureA = $obj->heureA;
452
                $line->BBC_ballons_idBBC_ballons = $obj->BBC_ballons_idBBC_ballons;
453
                $line->nbrPax = $obj->nbrPax;
454
                $line->remarque = $obj->remarque;
455
                $line->incidents = $obj->incidents;
456
                $line->fk_type = $obj->fk_type;
457
                $line->fk_pilot = $obj->fk_pilot;
458
                $line->fk_organisateur = $obj->fk_organisateur;
459
                $line->is_facture = $obj->is_facture;
460
                $line->kilometers = $obj->kilometers;
461
                $line->cost = $obj->cost;
462
                $line->fk_receiver = $obj->fk_receiver;
463
                $line->justif_kilometers = $obj->justif_kilometers;
464
                $line->date_creation = $obj->date_creation;
465
                $line->date_update = $obj->date_update;
466
467
468
                $this->lines[$line->id] = $line;
469
            }
470
            $this->db->free($resql);
471
472
            return $num;
473 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
474
            $this->errors[] = 'Error ' . $this->db->lasterror();
475
            dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
476
477
            return -1;
478
        }
479
    }
480
481
    /**
482
     * Update object into database
483
     *
484
     * @param  User $user      User that modifies
485
     * @param  bool $notrigger false=launch triggers after, true=disable triggers
486
     *
487
     * @return int <0 if KO, >0 if OK
488
     */
489
    public function update(User $user, $notrigger = false)
490
    {
491
        $error = 0;
492
493
        dol_syslog(__METHOD__, LOG_DEBUG);
494
495
        // Clean parameters
496
497
        if (isset($this->idBBC_vols)) {
498
            $this->idBBC_vols = trim($this->idBBC_vols);
499
        }
500
        if (isset($this->lieuD)) {
501
            $this->lieuD = trim($this->lieuD);
502
        }
503
        if (isset($this->lieuA)) {
504
            $this->lieuA = trim($this->lieuA);
505
        }
506
        if (isset($this->heureD)) {
507
            $this->heureD = trim($this->heureD);
508
        }
509
        if (isset($this->heureA)) {
510
            $this->heureA = trim($this->heureA);
511
        }
512
        if (isset($this->BBC_ballons_idBBC_ballons)) {
513
            $this->BBC_ballons_idBBC_ballons = trim($this->BBC_ballons_idBBC_ballons);
514
        }
515
        if (isset($this->nbrPax)) {
516
            $this->nbrPax = trim($this->nbrPax);
517
        }
518
        if (isset($this->remarque)) {
519
            $this->remarque = trim($this->remarque);
520
        }
521
        if (isset($this->incidents)) {
522
            $this->incidents = trim($this->incidents);
523
        }
524
        if (isset($this->fk_type)) {
525
            $this->fk_type = trim($this->fk_type);
526
        }
527
        if (isset($this->fk_pilot)) {
528
            $this->fk_pilot = trim($this->fk_pilot);
529
        }
530
        if (isset($this->fk_organisateur)) {
531
            $this->fk_organisateur = trim($this->fk_organisateur);
532
        }
533
        if (isset($this->is_facture)) {
534
            $this->is_facture = trim($this->is_facture);
535
        }
536
        if (isset($this->kilometers)) {
537
            $this->kilometers = trim($this->kilometers);
538
        }
539
        if (isset($this->cost)) {
540
            $this->cost = trim($this->cost);
541
        }
542
        if (isset($this->fk_receiver)) {
543
            $this->fk_receiver = trim($this->fk_receiver);
544
        }
545
        if (isset($this->justif_kilometers)) {
546
            $this->justif_kilometers = trim($this->justif_kilometers);
547
        }
548
549
550
        // Check parameters
551
        // Put here code to add a control on parameters values
552
553
        // Update request
554
        $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
555
556
        $sql .= ' date = ' . (!isset($this->date) || dol_strlen($this->date) != 0 ? "'" . $this->db->idate($this->date) . "'" : 'null') . ',';
557
        $sql .= ' lieuD = ' . (isset($this->lieuD) ? "'" . $this->db->escape($this->lieuD) . "'" : "null") . ',';
558
        $sql .= ' lieuA = ' . (isset($this->lieuA) ? "'" . $this->db->escape($this->lieuA) . "'" : "null") . ',';
559
        $sql .= ' heureD = ' . (isset($this->heureD) ? "'" . $this->heureD . "'" : "null") . ',';
560
        $sql .= ' heureA = ' . (isset($this->heureA) ? "'" . $this->heureA . "'" : "null") . ',';
561
        $sql .= ' BBC_ballons_idBBC_ballons = ' . (isset($this->BBC_ballons_idBBC_ballons) ? $this->BBC_ballons_idBBC_ballons : "null") . ',';
562
        $sql .= ' nbrPax = ' . (isset($this->nbrPax) ? "'" . $this->db->escape($this->nbrPax) . "'" : "null") . ',';
563
        $sql .= ' remarque = ' . (isset($this->remarque) ? "'" . $this->db->escape($this->remarque) . "'" : "null") . ',';
564
        $sql .= ' incidents = ' . (isset($this->incidents) ? "'" . $this->db->escape($this->incidents) . "'" : "null") . ',';
565
        $sql .= ' fk_type = ' . (isset($this->fk_type) ? $this->fk_type : "null") . ',';
566
        $sql .= ' fk_pilot = ' . (isset($this->fk_pilot) ? $this->fk_pilot : "null") . ',';
567
        $sql .= ' fk_organisateur = ' . (isset($this->fk_organisateur) ? $this->fk_organisateur : "null") . ',';
568
        $sql .= ' is_facture = ' . (isset($this->is_facture) ? $this->is_facture : "0") . ',';
569
        $sql .= ' kilometers = ' . (!empty($this->kilometers) ? $this->kilometers : "0") . ',';
570
        $sql .= ' cost = ' . (isset($this->cost) ? "'" . $this->db->escape($this->cost) . "'" : "''") . ',';
571
        $sql .= ' fk_receiver = ' . (isset($this->fk_receiver) ? $this->fk_receiver : "null") . ',';
572
        $sql .= ' justif_kilometers = ' . (isset($this->justif_kilometers) ? "'" . $this->db->escape($this->justif_kilometers) . "'," : "'',");
573
        $sql .= ' date_update = ' . "'" . date('Y-m-d H:i:s') . "'";
574
575
        $sql .= ' WHERE idBBC_vols=' . $this->idBBC_vols;
576
577
        $this->db->begin();
578
579
        $resql = $this->db->query($sql);
580
        if (!$resql) {
581
            $error++;
582
            $this->errors[] = 'Error ' . $this->db->lasterror();
583
            dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
584
        }
585
586
        if (!$error && !$notrigger) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
587
            // Uncomment this and change MYOBJECT to your own tag if you
588
            // want this action calls a trigger.
589
590
            //// Call triggers
591
            //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
592
            //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
593
            //// End call triggers
594
        }
595
596
        // Commit or rollback
597
        if ($error) {
598
            $this->db->rollback();
599
600
            return -1 * $error;
601
        } else {
602
            $this->db->commit();
603
604
            return 1;
605
        }
606
    }
607
608
    /**
609
     * Delete object in database
610
     *
611
     * @param User $user      User that deletes
612
     * @param bool $notrigger false=launch triggers after, true=disable triggers
613
     *
614
     * @return int <0 if KO, >0 if OK
615
     */
616 View Code Duplication
    public function delete(User $user, $notrigger = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
617
    {
618
        dol_syslog(__METHOD__, LOG_DEBUG);
619
620
        $error = 0;
621
622
        $this->db->begin();
623
624
        if (!$error) {
625
            if (!$notrigger) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
626
                // Uncomment this and change MYOBJECT to your own tag if you
627
                // want this action calls a trigger.
628
629
                //// Call triggers
630
                //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
631
                //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
632
                //// End call triggers
633
            }
634
        }
635
636
        if (!$error) {
637
            $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
638
            $sql .= ' WHERE idBBC_vols=' . $this->idBBC_vols;
639
640
            $resql = $this->db->query($sql);
641
            if (!$resql) {
642
                $error++;
643
                $this->errors[] = 'Error ' . $this->db->lasterror();
644
                dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
645
            }
646
        }
647
648
        // Commit or rollback
649
        if ($error) {
650
            $this->db->rollback();
651
652
            return -1 * $error;
653
        } else {
654
            $this->db->commit();
655
656
            return 1;
657
        }
658
    }
659
660
    /**
661
     * Load an object from its id and create a new one in database
662
     *
663
     * @param int $fromid Id of object to clone
664
     *
665
     * @return int New id of clone
666
     */
667 View Code Duplication
    public function createFromClone($fromid)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
668
    {
669
        dol_syslog(__METHOD__, LOG_DEBUG);
670
671
        global $user;
672
        $error = 0;
673
        $object = new Bbcvols($this->db);
674
675
        $this->db->begin();
676
677
        // Load source object
678
        $object->fetch($fromid);
679
        // Reset object
680
        $object->id = 0;
681
682
        // Clear fields
683
        // ...
684
685
        // Create clone
686
        $result = $object->create($user);
687
688
        // Other options
689
        if ($result < 0) {
690
            $error++;
691
            $this->errors = $object->errors;
692
            dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
693
        }
694
695
        // End
696
        if (!$error) {
697
            $this->db->commit();
698
699
            return $object->id;
700
        } else {
701
            $this->db->rollback();
702
703
            return -1;
704
        }
705
    }
706
707
    /**
708
     *  Return a link to the user card (with optionaly the picto)
709
     *    Use this->id,this->lastname, this->firstname
710
     *
711
     * @param    int     $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
712
     * @param    string  $option    On what the link point to
713
     * @param    integer $notooltip 1=Disable tooltip
714
     * @param    int     $maxlen    Max length of visible user name
715
     * @param  string    $morecss   Add more css on link
716
     *
717
     * @return    string                        String with URL
718
     */
719
    function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
720
    {
721
        global $langs, $conf, $db;
722
        global $dolibarr_main_authentication, $dolibarr_main_demo;
723
        global $menumanager;
724
725
        $result = '';
726
        $companylink = '';
0 ignored issues
show
Unused Code introduced by
$companylink is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
727
728
        $label = '<u>' . $langs->trans("MyModule") . '</u>';
729
        $label .= '<div width="100%">';
730
        $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->idBBC_vols . '<br>';
731
        $label .= '<b>' . $langs->trans('Date') . ':</b> ' . dol_print_date($this->date, '%d-%m-%Y');
732
        $label .= '</div>';
733
734
        $link = '<a href="' . DOL_URL_ROOT . '/flightlog/card.php?id=' . $this->idBBC_vols . '"';
735
        $link .= ($notooltip ? '' : ' title="' . dol_escape_htmltag($label,
736
                1) . '" class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"');
737
        $link .= '>';
738
        $linkend = '</a>';
739
740 View Code Duplication
        if ($withpicto) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
741
            $result .= ($link . img_object(($notooltip ? '' : $label), 'label',
742
                    ($notooltip ? '' : 'class="classfortooltip"')) . $linkend);
743
            if ($withpicto != 2) {
744
                $result .= ' ';
745
            }
746
        }
747
        $result .= $link . $this->idBBC_vols . $linkend;
748
        return $result;
749
    }
750
751
    /**
752
     *  Retourne le libelle du status d'un user (actif, inactif)
753
     *
754
     * @param    int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
755
     *
756
     * @return    string                   Label of status
757
     */
758
    function getLibStatut($mode = 0)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
759
    {
760
        return $this->LibStatut($this->is_facture, $mode);
761
    }
762
763
    /**
764
     *  Renvoi le libelle d'un status donne
765
     *
766
     * @param    int $status Id status
767
     * @param  int   $mode   0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
768
     *
769
     * @return string                    Label of status
770
     */
771 View Code Duplication
    private function LibStatut($status, $mode = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
772
    {
773
        global $langs;
774
775
        if ($mode == 0) {
776
            $prefix = '';
0 ignored issues
show
Unused Code introduced by
$prefix is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
777
            if ($status == 1) {
778
                return $langs->trans('Enabled');
779
            }
780
            if ($status == 0) {
781
                return $langs->trans('Disabled');
782
            }
783
        }
784
        if ($mode == 1) {
785
            if ($status == 1) {
786
                return $langs->trans('Enabled');
787
            }
788
            if ($status == 0) {
789
                return $langs->trans('Disabled');
790
            }
791
        }
792
        if ($mode == 2) {
793
            if ($status == 1) {
794
                return img_picto($langs->trans('Enabled'), 'statut4') . ' ' . $langs->trans('Enabled');
795
            }
796
            if ($status == 0) {
797
                return img_picto($langs->trans('Disabled'), 'statut5') . ' ' . $langs->trans('Disabled');
798
            }
799
        }
800
        if ($mode == 3) {
801
            if ($status == 1) {
802
                return img_picto($langs->trans('Enabled'), 'statut4');
803
            }
804
            if ($status == 0) {
805
                return img_picto($langs->trans('Disabled'), 'statut5');
806
            }
807
        }
808
        if ($mode == 4) {
809
            if ($status == 1) {
810
                return img_picto($langs->trans('Enabled'), 'statut4') . ' ' . $langs->trans('Enabled');
811
            }
812
            if ($status == 0) {
813
                return img_picto($langs->trans('Disabled'), 'statut5') . ' ' . $langs->trans('Disabled');
814
            }
815
        }
816
        if ($mode == 5) {
817
            if ($status == 1) {
818
                return $langs->trans('Enabled') . ' ' . img_picto($langs->trans('Enabled'), 'statut4');
819
            }
820
            if ($status == 0) {
821
                return $langs->trans('Disabled') . ' ' . img_picto($langs->trans('Disabled'), 'statut5');
822
            }
823
        }
824
825
        return "";
826
    }
827
828
829
    /**
830
     * Initialise object with example values
831
     * Id must be 0 if object instance is a specimen
832
     *
833
     * @return void
834
     */
835
    public function initAsSpecimen()
836
    {
837
        $this->id = 0;
838
839
        $this->idBBC_vols = '';
840
        $this->date = '';
841
        $this->lieuD = '';
842
        $this->lieuA = '';
843
        $this->heureD = '';
844
        $this->heureA = '';
845
        $this->BBC_ballons_idBBC_ballons = '';
846
        $this->nbrPax = '';
847
        $this->remarque = '';
848
        $this->incidents = '';
849
        $this->fk_type = '';
850
        $this->fk_pilot = '';
851
        $this->fk_organisateur = '';
852
        $this->is_facture = '';
853
        $this->kilometers = '';
854
        $this->cost = '';
855
        $this->fk_receiver = '';
856
        $this->justif_kilometers = '';
857
858
859
    }
860
861
    /**
862
     * @return string
863
     */
864
    public function __toString()
865
    {
866
        return $this->idBBC_vols . " " . $this->lieuD . " " . $this->lieuA;
867
    }
868
869
    /**
870
     * @return string
871
     */
872
    public function toString()
873
    {
874
        return "" . $this;
875
    }
876
877
    /**
878
     * @return string
879
     */
880
    public function getStatus()
881
    {
882
        return $this->statut;
883
    }
884
885
    /**
886
     * @return boolean 
887
     */
888
    public function hasFacture(){
889
        return count($this->linkedObjectsIds) > 0;
890
    }
891
892
    /**
893
     * @param int $userId
894
     * 
895
     * @return User
896
     */
897
    private function fetchUser($userId)
898
    {
899
        $user = new User($this->db);
900
        $user->fetch($userId);
901
        
902
        return $user;
903
    }
904
905
    /**
906
     * @return Bbc_ballons
907
     */
908
    private function fetchBalloon()
909
    {
910
        $balloon = new Bbc_ballons($this->db);
911
        $balloon->fetch($this->BBC_ballons_idBBC_ballons);
912
913
        return $balloon;
914
    }
915
916
    /**
917
     * @return Bbc_ballons
918
     */
919
    public function getBalloon()
920
    {
921
        if(!$this->balloon){
922
            $this->balloon = $this->fetchBalloon();
923
        }
924
925
        return $this->balloon;
926
    }
927
928
    /**
929
     * @return User
930
     */
931
    public function getPilot()
932
    {
933
        if(!$this->pilot){
934
            $this->pilot = $this->fetchUser($this->fk_pilot);
935
        }
936
937
        return $this->pilot;
938
    }
939
940
    /**
941
     * @return string
942
     */
943
    public function getComment()
944
    {
945
        return $this->remarque;
946
    }
947
948
    /**
949
     * @return string
950
     */
951
    public function getIncident()
952
    {
953
        return $this->incidents;
954
    }
955
956
957
}
958
959
/**
960
 * Class BbcvolsLine
961
 */
962
class BbcvolsLine
963
{
964
    /**
965
     * @var int ID
966
     */
967
    public $id;
968
    /**
969
     * @var mixed Sample line property 1
970
     */
971
972
    public $idBBC_vols;
973
    public $date = '';
974
    public $lieuD;
975
    public $lieuA;
976
    public $heureD;
977
    public $heureA;
978
    public $BBC_ballons_idBBC_ballons;
979
    public $nbrPax;
980
    public $remarque;
981
    public $incidents;
982
    public $fk_type;
983
    public $fk_pilot;
984
    public $fk_organisateur;
985
    public $is_facture;
986
    public $kilometers;
987
    public $cost;
988
    public $fk_receiver;
989
    public $justif_kilometers;
990
    public $date_creation;
991
    public $date_update;
992
993
    /**
994
     * @var mixed Sample line property 2
995
     */
996
997
}
998