Completed
Push — feature/api ( de88d6...6cf568 )
by Laurent
02:01
created

Flights::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Luracast\Restler\RestException;
4
include_once 'View/Rest/Flight.php';
5
6
final class Flights extends DolibarrApi
7
{
8
    /**
9
     * @var DoliDB
10
     */
11
    private $connection;
12
13
    public function __construct($db = null, $cachedir = '', $refreshCache = false)
14
    {
15
        parent::__construct($db, $cachedir, $refreshCache);
16
17
        global $db;
18
        $this->connection = $db;
19
    }
20
21
22
    /**
23
     * @url GET /
24
     *
25
     * @return array
26
     *
27
     * @throws RestException
28
     */
29
    public function index(){
30
        $obj_ret = array();
31
32
        $sql = "SELECT ";
33
        $sql .= 'rowid,';
34
        $sql .= 'date,';
35
        $sql .= 'lieuD,';
36
        $sql .= 'lieuA,';
37
        $sql .= 'heureD,';
38
        $sql .= 'heureA,';
39
        $sql .= 'BBC_ballons_idBBC_ballons,';
40
        $sql .= 'nbrPax,';
41
        $sql .= 'remarque,';
42
        $sql .= 'incidents,';
43
        $sql .= 'fk_type,';
44
        $sql .= 'fk_pilot,';
45
        $sql .= 'fk_organisateur,';
46
        $sql .= 'is_facture,';
47
        $sql .= 'kilometers,';
48
        $sql .= 'cost,';
49
        $sql .= 'fk_receiver,';
50
        $sql .= 'justif_kilometers,';
51
        $sql .= 'date_creation,';
52
        $sql .= 'date_update,';
53
        $sql .= 'passenger_names';
54
55
        $sql.= " FROM ".MAIN_DB_PREFIX."bbc_vols";
56
57
        $sql.= ' WHERE 1=1 LIMIT 10';
58
59
        // Add sql filters
60
61
        $result = $this->connection->query($sql);
62
63
        if ($result)
64
        {
65
            $num = $this->connection->num_rows($result);
66
            $min = min($num, (-1 <= 0 ? $num : -1));
67
            $i=0;
68
            while ($i < $min)
69
            {
70
                $obj = $this->connection->fetch_object($result);
71
72
                $flight = new Flight();
73
                $flight->rowid = (int)$obj->rowid;
74
                $flight->date = $obj->date;
75
                $flight->lieuD = $obj->lieuD;
76
                $flight->lieuA = $obj->lieuA;
77
                $flight->heureD = $obj->heureD;
78
                $flight->heureA = $obj->heureA;
79
                $flight->balloon = (int)$obj->BBC_ballons_idBBC_ballons;
80
                $flight->nbrPax = (int)$obj->nbrPax;
81
                $flight->remarque = $obj->remarque;
82
                $flight->incidents = $obj->incidents;
83
                $flight->type = (int)$obj->fk_type;
84
                $flight->pilot = (int)$obj->fk_pilot;
85
                $flight->organisator = (int)$obj->fk_organisateur;
86
                $flight->billed = (bool)$obj->is_facture;
87
                $flight->cost = (int)$obj->cost;
88
                $flight->receiver = (int)$obj->fk_receiver;
89
                $flight->kilometers = (int)$obj->kilometers ;
90
                $flight->justifKilometers = $obj->justif_kilometers;
91
                $flight->createdAt = $obj->date_creation;
92
                $flight->updatedAt = $obj->date_update;
93
94
                $obj_ret[] = $flight;
95
96
                $i++;
97
            }
98
        }
99
        else {
100
            throw new RestException(500, 'Error when retrieve commande list : '.$this->connection->lasterror());
101
        }
102
103
        if( ! count($obj_ret)) {
104
            throw new RestException(404, 'No flight found');
105
        }
106
107
        return $obj_ret;
108
    }
109
}