Completed
Push — master ( a5a50f...1f5af2 )
by Laurent
01:49
created

Bbcvols::fetchUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
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 . '/flightlog/class/bbctypes.class.php';
33
34
/**
35
 * Class Bbcvols
36
 *
37
 * Put here description of your class
38
 *
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 (int) $this->idBBC_vols;
94
    }
95
96
    /**
97
     * @return int
98
     */
99
    public function getId()
100
    {
101
        return (int) $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) . '00';
153
        }
154
        if (isset($this->heureA)) {
155
            $this->heureA = trim($this->heureA) . '00';
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
        if (!$error) {
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
     *  Return a link to the user card (with optionaly the picto)
662
     *    Use this->id,this->lastname, this->firstname
663
     *
664
     * @param    int     $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
665
     * @param    string  $option    On what the link point to
666
     * @param    integer $notooltip 1=Disable tooltip
667
     * @param    int     $maxlen    Max length of visible user name
668
     * @param  string    $morecss   Add more css on link
669
     *
670
     * @return    string                        String with URL
671
     */
672
    public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
673
    {
674
        global $langs;
675
676
        $result = '';
677
678
        $label = '<u>' . $langs->trans("MyModule") . '</u>';
679
        $label .= '<div width="100%">';
680
        $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->idBBC_vols . '<br>';
681
        $label .= '<b>' . $langs->trans('Date') . ':</b> ' . dol_print_date($this->date, '%d-%m-%Y');
682
        $label .= '</div>';
683
684
        $link = '<a href="' . DOL_URL_ROOT . '/flightlog/card.php?id=' . $this->idBBC_vols . '"';
685
        $link .= ($notooltip ? '' : ' title="' . dol_escape_htmltag($label,
686
                1) . '" class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"');
687
        $link .= '>';
688
        $linkend = '</a>';
689
690 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...
691
            $result .= ($link . img_object(($notooltip ? '' : $label), 'label',
692
                    ($notooltip ? '' : 'class="classfortooltip"')) . $linkend);
693
            if ($withpicto != 2) {
694
                $result .= ' ';
695
            }
696
        }
697
        $result .= $link . $this->idBBC_vols . $linkend;
698
        return $result;
699
    }
700
701
    /**
702
     *  Retourne le libelle du status d'un user (actif, inactif)
703
     *
704
     * @param    int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
705
     *
706
     * @return    string                   Label of status
707
     */
708
    public function getLibStatut($mode = 0)
709
    {
710
        return $this->LibStatut($this->is_facture, $mode);
711
    }
712
713
    /**
714
     * Renvoi le libelle d'un status donne
715
     *
716
     * @param int $status Id status
717
     * @param int $mode   0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
718
     *
719
     * @return string                    Label of status
720
     */
721
    private function LibStatut($status, $mode = 0)
722
    {
723
        global $langs;
724
725
        $billDone = $langs->trans('Facturé');
726
        $billNotDone = $langs->trans('Ouvert');
727
728
        if ($mode == 0) {
729
            if ($status == 1) {
730
                return $billDone;
731
            }
732
            if ($status == 0) {
733
                return $billNotDone;
734
            }
735
        }
736
        if ($mode == 1) {
737
            if ($status == 1) {
738
                return $billDone;
739
            }
740
            if ($status == 0) {
741
                return $billNotDone;
742
            }
743
        }
744
        if ($mode == 2) {
745
            if ($status == 1) {
746
                return img_picto($billDone, 'statut4') . ' ' . $billDone;
747
            }
748
            if ($status == 0) {
749
                return img_picto($billNotDone, 'statut5') . ' ' . $billNotDone;
750
            }
751
        }
752
        if ($mode == 3) {
753
            if ($status == 1) {
754
                return img_picto($billDone, 'statut4');
755
            }
756
            if ($status == 0) {
757
                return img_picto($billNotDone, 'statut5');
758
            }
759
        }
760
        if ($mode == 4) {
761
            if ($status == 1) {
762
                return img_picto($billDone, 'statut4') . ' ' . $billDone;
763
            }
764
            if ($status == 0) {
765
                return img_picto($billNotDone, 'statut5') . ' ' . $billNotDone;
766
            }
767
        }
768
        if ($mode == 5) {
769
            if ($status == 1) {
770
                return $billDone . ' ' . img_picto($billDone, 'statut4');
771
            }
772
            if ($status == 0) {
773
                return $billNotDone . ' ' . img_picto($billNotDone, 'statut5');
774
            }
775
        }
776
777
        return "";
778
    }
779
780
    /**
781
     * @return string
782
     */
783
    public function __toString()
784
    {
785
        return $this->idBBC_vols . " " . $this->getPlaces();
786
    }
787
788
    /**
789
     * @return string
790
     */
791
    public function toString()
792
    {
793
        return "" . $this;
794
    }
795
796
    /**
797
     * @return string
798
     */
799
    public function getStatus()
800
    {
801
        return $this->statut;
802
    }
803
804
    /**
805
     * @return boolean
806
     */
807
    public function hasFacture()
808
    {
809
        return count($this->linkedObjectsIds) > 0;
810
    }
811
812
    /**
813
     * @param int $userId
814
     *
815
     * @return User
816
     */
817
    private function fetchUser($userId)
818
    {
819
        $user = new User($this->db);
820
        $user->fetch($userId);
821
822
        return $user;
823
    }
824
825
    /**
826
     * @return Bbc_ballons
827
     */
828
    private function fetchBalloon()
829
    {
830
        $balloon = new Bbc_ballons($this->db);
831
        $balloon->fetch($this->BBC_ballons_idBBC_ballons);
832
833
        return $balloon;
834
    }
835
836
    /**
837
     * @return Bbc_ballons
838
     */
839
    public function getBalloon()
840
    {
841
        if (!$this->balloon) {
842
            $this->balloon = $this->fetchBalloon();
843
        }
844
845
        return $this->balloon;
846
    }
847
848
    /**
849
     * @return User
850
     */
851
    public function getPilot()
852
    {
853
        if (!$this->pilot) {
854
            $this->pilot = $this->fetchUser($this->fk_pilot);
855
        }
856
857
        return $this->pilot;
858
    }
859
860
    /**
861
     * @return Bbctypes
862
     */
863
    public function getFlightType()
864
    {
865
        $flightType = new Bbctypes($this->db);
866
        $flightType->fetch($this->fk_type);
867
868
        return $flightType;
869
    }
870
871
    /**
872
     * @return string
873
     */
874
    public function getComment()
875
    {
876
        return $this->remarque;
877
    }
878
879
    /**
880
     * @return string
881
     */
882
    public function getIncident()
883
    {
884
        return $this->incidents;
885
    }
886
887
    /**
888
     * Return true if the number of pax is greater than 0
889
     *
890
     * @return boolean
891
     */
892
    public function hasPax()
893
    {
894
        return (int) $this->nbrPax > 0;
895
    }
896
897
    /**
898
     * Regarding the type of the flight give an indication if the flight must have pax to be valid.
899
     *
900
     * @return boolean
901
     */
902
    public function mustHavePax()
903
    {
904
        return $this->getFlightType()->isPaxRequired();
905
    }
906
907
    /**
908
     * Returns true if the amount requested by the flight is 0.
909
     *
910
     * @return boolean
911
     */
912
    public function isFree()
913
    {
914
        return empty($this->cost);
915
    }
916
917
    /**
918
     * @return int
919
     */
920
    public function getAmountReceived()
921
    {
922
        return $this->cost;
923
    }
924
925
    /**
926
     * @return int
927
     */
928
    public function getAmountPerPassenger()
929
    {
930
        return $this->cost / $this->nbrPax;
931
    }
932
933
    /**
934
     * @return boolean
935
     */
936
    public function hasReceiver()
937
    {
938
        return !empty($this->fk_receiver);
939
    }
940
941
    /**
942
     * @return boolean
943
     */
944
    public function hasKilometers()
945
    {
946
        return !empty($this->kilometers);
947
    }
948
949
    /**
950
     * @return boolean
951
     */
952
    public function hasKilometersDescription()
953
    {
954
        return !empty(trim($this->justif_kilometers));
955
    }
956
957
    /**
958
     * @return int
959
     */
960
    public function getKilometers()
961
    {
962
        return (int) $this->kilometers;
963
    }
964
965
    /**
966
     * @return string
967
     */
968
    public function getPlaces()
969
    {
970
        return $this->lieuD.' -> '.$this->lieuA;
971
    }
972
}
973
974
/**
975
 * Class BbcvolsLine
976
 */
977
class BbcvolsLine
978
{
979
    /**
980
     * @var int ID
981
     */
982
    public $id;
983
    /**
984
     * @var mixed Sample line property 1
985
     */
986
987
    public $idBBC_vols;
988
    public $date = '';
989
    public $lieuD;
990
    public $lieuA;
991
    public $heureD;
992
    public $heureA;
993
    public $BBC_ballons_idBBC_ballons;
994
    public $nbrPax;
995
    public $remarque;
996
    public $incidents;
997
    public $fk_type;
998
    public $fk_pilot;
999
    public $fk_organisateur;
1000
    public $is_facture;
1001
    public $kilometers;
1002
    public $cost;
1003
    public $fk_receiver;
1004
    public $justif_kilometers;
1005
    public $date_creation;
1006
    public $date_update;
1007
}
1008