Bladeren bron

Merge branch 'master' of https://github.com/babolivier/isen-rentree

LaurentTreguier 9 jaren geleden
bovenliggende
commit
baeb19965d
3 gewijzigde bestanden met toevoegingen van 251 en 16 verwijderingen
  1. 30
    16
      models/connector.class.php
  2. 213
    0
      models/data.class.php
  3. 8
    0
      test.php

+ 30
- 16
models/connector.class.php Bestand weergeven

@@ -15,25 +15,25 @@
15 15
 *        Insert()                                                              *
16 16
 *        Update()                                                              *
17 17
 *******************************************************************************/
18
+
19
+include(dirname(__DIR__)."../../DbIds.php");
20
+
18 21
 class Connector {
19 22
 
20 23
     private $bdd;
21 24
 
22 25
     function __construct() {
23
-        // TODO : Select params to use
24
-        $dbconnect = array();
25
-        for($i = 0; $i < sizeof($matches[0]); $i++) {
26
-            $dbconnect[$matches[1][$i]] = $matches[2][$i];
27
-        }
26
+        $dbconnect = getParams();
28 27
 
29 28
         $options = array(
30
-            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
29
+            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
31 30
         );
32 31
 
33
-        $this->bdd = new PDO("mysql:host=".$dbconnect["host"].";dbname="
34
-                .$dbconnect["dbname"], $dbconnect["user"], $dbconnect["pass"],
35
-                $options);
32
+        $this->bdd = new PDO("mysql:host=".$dbconnect[0].";dbname=".$dbconnect[1],
33
+            $dbconnect[2], $dbconnect[3], $options);
34
+
36 35
         $this->bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
36
+        $this->bdd->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
37 37
     }
38 38
 
39 39
     function Select($fields, $tables, $options = array()) {
@@ -44,7 +44,7 @@ class Connector {
44 44
                 $whereClause = " $upName ";
45 45
                 foreach($value as $array) {
46 46
                     if(sizeof($array) != 3 && sizeof($array) != 4) {
47
-                        throw new Exception('wrong_arg_nmbr_where');
47
+                        throw new Exception("wrong_arg_nmbr_where");
48 48
                     }
49 49
                     if(sizeof($array) == 3) {
50 50
                         $whereClause .= $array[0]." ".$array[1]." ? AND ";
@@ -57,10 +57,10 @@ class Connector {
57 57
                 $request .= substr($whereClause, 0, -5);
58 58
             } else if(($upName = strtoupper($name)) == "ORDER BY") {
59 59
                 if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
60
-                    throw new Exception('wrong_arg_nmbr_order_by');
60
+                    throw new Exception("wrong_arg_nmbr_order_by");
61 61
                 }
62 62
 
63
-                $request .= " ".$upName." ".implode(' ', $value);
63
+                $request .= " ".$upName." ".implode(" ", $value);
64 64
             } else if(($upName = strtoupper($name)) == "LIMIT") {
65 65
                 if(sizeof($value) == 1) {
66 66
                     // La colonne "limit" ne contient qu'un nombre de champs
@@ -70,10 +70,10 @@ class Connector {
70 70
                     // nombre de champs
71 71
                     $request .= " $upName ".$value[0].",".$value[1];
72 72
                 } else {
73
-                    throw new Exception('wrong_arg_numbr_limit');
73
+                    throw new Exception("wrong_arg_numbr_limit");
74 74
                 }
75 75
             } else {
76
-                throw new Exception('unknown_arg');
76
+                throw new Exception("unknown_arg");
77 77
             }
78 78
         }
79 79
 
@@ -106,12 +106,26 @@ class Connector {
106 106
     function Update($table, $update) {
107 107
         $request = "UPDATE $table SET ";
108 108
         $arrayVerif = array();
109
-        foreach($update['set'] as $name=>$value) {
109
+        foreach($update["set"] as $name=>$value) {
110 110
             $request .= $name."=?,";
111 111
             array_push($arrayVerif, $value);
112 112
         }
113 113
         $request = substr($request, 0, -1)." WHERE ";
114
-        foreach($update['where'] as $value) {
114
+        foreach($update["where"] as $value) {
115
+            $request .= $value[0].$value[1]."? AND ";
116
+            array_push($arrayVerif, $value[2]);
117
+        }
118
+        $request = substr($request, 0, -5);
119
+
120
+        $stmt = $this->bdd->prepare($request);
121
+        $stmt->execute($arrayVerif);
122
+    }
123
+
124
+    function Delete($table, $where = array()) {
125
+        $request = "DELETE FROM $table";
126
+        $arrayVerif = array();
127
+        $request .= " WHERE ";
128
+        foreach($where as $value) {
115 129
             $request .= $value[0].$value[1]."? AND ";
116 130
             array_push($arrayVerif, $value[2]);
117 131
         }

+ 213
- 0
models/data.class.php Bestand weergeven

@@ -0,0 +1,213 @@
1
+<?php
2
+
3
+require_once("connector.class.php");
4
+
5
+class Data {
6
+    private $identifiant;
7
+    private $nom_fils;
8
+    private $prenom_fils;
9
+    private $ddn_fils;
10
+    private $tel_mobile;
11
+    private $courriel;
12
+    private $date;
13
+    private $ip;
14
+
15
+    function __construct($email) {
16
+        $bdd = new Connector();
17
+
18
+        $data = $bdd->Select("*", "data", array(
19
+            "where" => array(
20
+                array("identifiant", "=", $email)
21
+            )
22
+        ))[0];
23
+
24
+        // Chargement des informations
25
+        $this->identifiant = $email;
26
+        $this->nom_fils = $data["nom_fils"];
27
+        $this->prenom_fils = $data["prenom_fils"];
28
+        $this->ddn_fils = $data["ddn_fils"];
29
+        $this->tel_mobile = $data["tel_mobile"];
30
+        $this->courriel = $data["courriel"];
31
+        $this->date = $data["date"];
32
+        $this->ip = $data["ip"];
33
+    }
34
+
35
+    /**
36
+     * @return string
37
+     */
38
+    public function getIdentifiant()
39
+    {
40
+        return $this->identifiant;
41
+    }
42
+
43
+    /**
44
+     * @return string
45
+     */
46
+    public function getNomFils()
47
+    {
48
+        return $this->nom_fils;
49
+    }
50
+
51
+    /**
52
+     * @return string
53
+     */
54
+    public function getPrenomFils()
55
+    {
56
+        return $this->prenom_fils;
57
+    }
58
+
59
+    /**
60
+     * @return string
61
+     */
62
+    public function getDdnFils()
63
+    {
64
+        return $this->ddn_fils;
65
+    }
66
+
67
+    /**
68
+     * @return string
69
+     */
70
+    public function getTelMobile()
71
+    {
72
+        return $this->tel_mobile;
73
+    }
74
+
75
+    /**
76
+     * @return string
77
+     */
78
+    public function getCourriel()
79
+    {
80
+        return $this->courriel;
81
+    }
82
+
83
+    /**
84
+     * @return string
85
+     */
86
+    public function getDate()
87
+    {
88
+        return $this->date;
89
+    }
90
+
91
+    /**
92
+     * @return string
93
+     */
94
+    public function getIp()
95
+    {
96
+        return $this->ip;
97
+    }
98
+
99
+    /**
100
+     * @param string $identifiant
101
+     */
102
+    public function setIdentifiant($identifiant)
103
+    {
104
+        $this->identifiant = $identifiant;
105
+    }
106
+
107
+    /**
108
+     * @param string $nom_fils
109
+     */
110
+    public function setNomFils($nom_fils)
111
+    {
112
+        $this->nom_fils = $nom_fils;
113
+    }
114
+
115
+    /**
116
+     * @param string $prenom_fils
117
+     */
118
+    public function setPrenomFils($prenom_fils)
119
+    {
120
+        $this->prenom_fils = $prenom_fils;
121
+    }
122
+
123
+    /**
124
+     * @param string $ddn_fils
125
+     */
126
+    public function setDdnFils($ddn_fils)
127
+    {
128
+        $this->ddn_fils = $ddn_fils;
129
+    }
130
+
131
+    /**
132
+     * @param string $tel_mobile
133
+     */
134
+    public function setTelMobile($tel_mobile)
135
+    {
136
+        $this->tel_mobile = $tel_mobile;
137
+    }
138
+
139
+    /**
140
+     * @param string $courriel
141
+     */
142
+    public function setCourriel($courriel)
143
+    {
144
+        $this->courriel = $courriel;
145
+    }
146
+
147
+    /**
148
+     * @param string $date
149
+     */
150
+    public function setDate($date)
151
+    {
152
+        $this->date = $date;
153
+    }
154
+
155
+    /**
156
+     * @param string $ip
157
+     */
158
+    public function setIp($ip)
159
+    {
160
+        $this->ip = $ip;
161
+    }
162
+
163
+    function write() {
164
+        $bdd = new Connector();
165
+
166
+        $data = $bdd->Select("*", "data", array(
167
+            "where" => array(
168
+                array("identifiant", "=", $this->identifiant)
169
+            )
170
+        ))[0];
171
+
172
+        $attrs = get_object_vars($this);
173
+        $toUpdate = array();
174
+
175
+        foreach($attrs as $key=>$value) {
176
+            if($value != $data[$key]) {
177
+                $toUpdate[$key] = $value;
178
+            }
179
+        }
180
+
181
+        $bdd->Update("data", array(
182
+            "set" => $toUpdate,
183
+            "where" => array(array("identifiant", "=", $this->identifiant))
184
+        ));
185
+    }
186
+
187
+    public static function extract() {
188
+        $bdd = new Connector();
189
+        $data = $bdd->Select("*", "data");
190
+
191
+        $csv = "";
192
+
193
+        // Head line
194
+        $keys = array();
195
+        foreach($data[0] as $key=>$value) {
196
+            array_push($keys, $key);
197
+        }
198
+        $csv .= implode(",", $keys)."\n";
199
+
200
+        // Content
201
+        foreach($data as $student) {
202
+            $csv .= implode(",", $student);
203
+            $csv .= "\n";
204
+        }
205
+
206
+        return $csv;
207
+    }
208
+
209
+    function erase() {
210
+        $bdd = new Connector();
211
+        $bdd->Delete("data", array(array("identifiant", "=", $this->identifiant)));
212
+    }
213
+}

+ 8
- 0
test.php Bestand weergeven

@@ -0,0 +1,8 @@
1
+<?php
2
+try {
3
+    include("models/data.class.php");
4
+    $csv = Data::extract();
5
+    echo $csv;
6
+} catch(Exception $e) {
7
+    echo $e;
8
+}