summaryrefslogtreecommitdiff
path: root/src/model/Clients.php
blob: aeb39c101b958ed0bd5a1891fe91f3ab6373017a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
// model/Clients.php

class Clients extends Model
{
    // lecture des données ou hydratation
    protected $ID; // auto-incrémentée
    protected $prenom_nom = '';
    protected $code_client = '';
    protected $adresse = '';
    protected $telephone = '';
    protected $courriel = '';
    protected $commentaires = '';
	
    use ModelChildren; // renseigne parent::table
    
    public function __construct()
    {
        $this->table = 'clients';
    }
    
    //~ public function set(string $variable, $value)
    //~ {
        //~ $this->$variable = $value;
        //~ return($this);
    //~ }
    
    // getters
    public function getID(): int
    {
        return $this->ID;
    }
    public function getCodeClient(): string
    {
        return $this->code_client;
    }
    
    // setters
    public function setID(int $value) // inutile? il s'autoincrémente
    {
        $this->ID = $value;
        return($this);
    }
    public function setPrenom_nom(string $value)
    {
        $this->prenom_nom = $value;
        return($this);
    }
    public function setCode_client(string $value)
    {
        $this->code_client = $value;
        return($this);
    }
    public function setAdresse(string $value)
    {
        $this->adresse = $value;
        return($this);
    }
    public function setTelephone(string $value) // chaine parce que zenity renvoie une chaine et parce qu'on garde le 0 au début
    {
        if(is_numeric($value))
        {
            $this->telephone = $value;
        }
        return($this);
    }
    public function setCourriel(string $value)
    {
        $this->courriel = $value;
        return($this);
    }
    public function setCommentaires(string $value)
    {
        $this->commentaires = $value;
        return($this);
    }
    
    
    public function newRow(array $input)
    {
        $this->hydrate(['prenom_nom' => $input[0], 'code_client' => $input[1], 'adresse' => $input[2], 'telephone' => $input[3], 'courriel' => $input[4], 'commentaires' => $input[5]]);
        $this->create();
        // ID obtenu par auto-incrémentation
        $this->ID = $this->db->lastInsertId(); // méthode de PDO
    }
    //~ public function setIdFromLastInsertID() // à faire juste après l'écriture d'une nouvelle entrée
    //~ {
        //~ $this->db = parent::getInstance(); // $db est créée dans Model::execQuery()
        //~ $this->ID = $this->db->lastInsertId(); // méthode de PDO
    //~ }
    
    public function findByKeywords(array $keywords, string $field): array // n'hydrate pas les variables, on doit choisir un client et hydrater ensuite
    {
        $result = [];
        for($i = 0; $i < count($keywords); $i++)
        {
            // tableau à deux dimensions obtenu pour un mot clé
            $query_result = $this->execQuery('SELECT * FROM ' . $this->table . ' WHERE ' . $field . ' LIKE "%' . $keywords[$i] . '%"')->fetchAll();
            foreach($query_result as $one_array) // pour chaque sous tableau
            {
                $already_exist = false;
                for($j = 0; $j < count($result); $j++) // pour chaque tableau déjà enregistré dans le tableau $result
                {
                    if($result[$j]['ID'] === $one_array['ID'])
                    {
                        $already_exist = true;
                    }
                }
                if(!$already_exist)
                {
                    $result[] = $one_array;
                }
            }
        }
        return($result);
    }
}