Projekt

Obecné

Profil

Stáhnout (13.1 KB) Statistiky
| Větev: | Revize:
1
<?php
2
class DB {
3
    // (A) CONNECT TO DATABASE
4
    public $error = "";
5
    private $pdo = null;
6
    private $stmt = null;
7
    function __construct () {
8
        try {
9
            $this->pdo = new PDO(
10
                "pgsql:host=".DB_HOST.";dbname=".DB_NAME,
11
                DB_USER, DB_PASSWORD, [
12
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
13
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
14
                ]
15
            );
16
        } catch (Exception $ex) { die($ex->getMessage()); }
17
    }
18

    
19
    // (B) CLOSE CONNECTION
20
    function __destruct(){
21
        if ($this->stmt!==null) { $this->stmt = null; }
22
        if ($this->pdo!==null) { $this->pdo = null; }
23
    }
24

    
25
    // (C) RUN A SELECT QUERY
26
    function select($sql, $params){
27
        $result = false;
28
        try {
29
            $this->stmt = $this->pdo->prepare($sql);
30
            if (array_key_exists("lemma", $params)) {
31
                $this->stmt->bindParam(':lemma',$params['lemma'], PDO::PARAM_STR);
32
            }
33
            if (array_key_exists("word", $params)) {
34
                $this->stmt->bindParam(':word',$params['word'], PDO::PARAM_STR);
35
            }
36
            if (array_key_exists("position1", $params)) {
37
                $this->stmt->bindParam(':position1',$params['position1'], PDO::PARAM_INT);
38
            }
39
            if (array_key_exists("position2", $params)) {
40
                $this->stmt->bindParam(':position2',$params['position2'], PDO::PARAM_INT);
41
            }
42
            if (array_key_exists("positiondetail", $params)) {
43
                $this->stmt->bindParam(':positiondetail',$params['positiondetail'], PDO::PARAM_INT);
44
            }
45
            if (array_key_exists("tag", $params)) {
46
                $this->stmt->bindParam(':tag',$params['tag'], PDO::PARAM_STR);
47
            }
48
            if (array_key_exists("tag", $params)) {
49
                $this->stmt->bindParam(':tag',$params['tag'], PDO::PARAM_STR);
50
            }
51
            if (array_key_exists("finished", $params)) {
52
                $this->stmt->bindParam(':finished',$params['finished']);
53
            }
54
            if (array_key_exists("manuscript", $params)) {
55
                for ($x = 0; $x < count($params["manuscript"]); $x += 1) {
56
                    $this->stmt->bindParam(':manuscript'.$x,$params["manuscript"][$x], PDO::PARAM_INT);
57
                }
58
            }
59
            $this->stmt->execute();
60
            $result = $this->stmt->fetchAll();
61
            return $result;
62
        } catch (Exception $ex) {
63
            $this->error = $ex->getMessage();
64
            return false;
65
        }
66
    }
67

    
68
    function getUser($username) {
69
        $this->stmt = $this->pdo->prepare('SELECT id, username, password, role FROM users WHERE username = :username');
70
        $this->stmt->bindParam(':username', $username);
71
        $this->stmt->execute();
72
        return $this->stmt->fetchAll();
73
    }
74

    
75
    function createUser($userDetails) {
76
        $this->stmt = $this->pdo->prepare('INSERT INTO users (username, password, role) VALUES (:username, :password, :role)');
77
        $password = password_hash($userDetails['username'], PASSWORD_DEFAULT);
78
        $this->stmt->bindParam(':username', $userDetails['username']);
79
        $this->stmt->bindParam(':password',$password);
80
        $this->stmt->bindParam(':role',$userDetails['role']);
81
        $this->stmt->execute();
82
        return $this->stmt->fetchAll();
83
    }
84

    
85
    function updateUserRole($userId, $role) {
86
        $this->stmt = $this->pdo->prepare("UPDATE users SET role = :role WHERE id = :userId");
87
        $this->stmt->bindParam(':role', $role);
88
        $this->stmt->bindParam(':userId',$userId);
89
        $this->stmt->execute();
90
        return $this->stmt->fetchAll();
91
    }
92

    
93
    function deleteUser($userId) {
94
        $this->stmt = $this->pdo->prepare("DELETE from users WHERE id = :userId");
95
        $this->stmt->bindParam(':userId',$userId);
96
        $this->stmt->execute();
97
        return $this->stmt->fetchAll();
98
    }
99

    
100

    
101

    
102
function update(){
103

    
104
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
105

    
106
    $query = "UPDATE dd_wordform
107
                SET ";
108

    
109

    
110
    if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
111
        $query .= " context = :context, ";
112
    }
113
        $query .= " date = CURRENT_DATE,";
114

    
115
    if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
116
        $query .= " description = :description, ";
117
    }
118
    if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
119
        $query .= " description2 = :description2, ";
120
    }
121
    if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
122
        $query .= " description3 = :description3, ";
123
    }
124
    if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
125
        $query .= " ending = :ending, ";
126
    }
127
    if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
128
        $query .= " finished = :finished, ";
129
    }
130
    if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
131
        $query .= " namedentity = :namedentity, ";
132
    }
133
    if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
134
        $query .= " position1 = :position1, ";
135
    }
136
    if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
137
        $query .= " position2 = :position2, ";
138
    }
139
    if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
140
        $query .= " positiondetail = :positiondetail ,";
141
    }
142
    if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
143
        $query .= " prefix = :prefix, ";
144
    }
145
    if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
146
        $query .= " suffix = :suffix, ";
147
    }
148
    if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
149
        $query .= " word = :word, ";
150
    }
151
    if (array_key_exists("lemma_id", $_POST) && $_POST['lemma_id'] != "") {
152
        $query .= " lemma_id = :lemma_id, ";
153
    }
154
    if (array_key_exists("tag_id", $_POST) && $_POST['tag_id'] != "") {
155
        $query .= " tag_id = :tag_id ";
156
    }
157

    
158
    $query .= " WHERE ";
159
    if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
160
        $query .= " id = :id ;";
161
    }
162

    
163
    $this->stmt = $this->pdo->prepare($query);
164

    
165
    if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
166
        $this->stmt->bindParam(':context', $_POST['context'], PDO::PARAM_STR);
167
    }
168
    if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
169
        $this->stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
170
    }
171
    if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
172
        $this->stmt->bindParam(':description2', $_POST['description2'], PDO::PARAM_STR);
173
    }
174
    if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
175
        $this->stmt->bindParam(':description3', $_POST['description3'], PDO::PARAM_STR);
176
    }
177
    if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
178
        $this->stmt->bindParam(':ending', $_POST['ending'], PDO::PARAM_STR);
179
    }
180
    if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
181
        $this->stmt->bindParam(':finished', $_POST['finished']);
182
    }
183
    if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
184
        $this->stmt->bindParam(':namedentity', $_POST['namedentity'], PDO::PARAM_INT);
185
    }
186
    if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
187
        $this->stmt->bindParam(':position1', $_POST['position1'], PDO::PARAM_STR);
188
    }
189
    if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
190
        $this->stmt->bindParam(':position2', $_POST['position2'], PDO::PARAM_STR);
191
    }
192
    if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
193
        $this->stmt->bindParam(':positiondetail', $_POST['positiondetail'], PDO::PARAM_STR);
194
    }
195
    if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
196
        $this->stmt->bindParam(':prefix', $_POST['prefix'], PDO::PARAM_STR);
197
    }
198
    if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
199
        $this->stmt->bindParam(':suffix', $_POST['suffix'], PDO::PARAM_STR);
200
    }
201
    if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
202
        $this->stmt->bindParam(':word', $_POST['word'], PDO::PARAM_STR);
203
    }
204
    if (array_key_exists("lemma_id", $_POST) && $_POST['lemma_id'] != "") {
205
        $this->stmt->bindParam(':lemma_id', $_POST['lemma_id'], PDO::PARAM_INT);
206
    }
207
    if (array_key_exists("tag_id", $_POST) && $_POST['tag_id'] != "") {
208
        $this->stmt->bindParam(':tag_id', $_POST['tag_id'], PDO::PARAM_INT);
209
    }
210
    if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
211
        $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
212
    }
213

    
214
    $this->stmt->execute();
215

    
216
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
217

    
218
    $query = "UPDATE dd_lemma
219
                 SET ";
220

    
221
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
222
        $query .= " lemma = :lemma , ";
223
    }
224
    if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
225
        $query .= " pos = :pos  ";
226
    }
227

    
228
    $query .= "WHERE ";
229
    if (array_key_exists("lemma_id", $_POST) && $_POST['lemma_id'] != "") {
230
        $query .= " id = :lemma_id ;";
231
    }
232

    
233

    
234
    $this->stmt = $this->pdo->prepare($query);
235
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
236
        $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
237
    }
238
    if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
239
        $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
240
    }
241
    if (array_key_exists("lemma_id", $_POST) && $_POST['lemma_id'] != "") {
242
        $this->stmt->bindParam(':lemma_id', $_POST['lemma_id'], PDO::PARAM_INT);
243
    }
244
    $this->stmt->execute();
245

    
246
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
247

    
248
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
249
        $query = "SELECT * FROM dd_manuscript WHERE ";
250
        $query .= " wordform_id = :wordform_id ;";
251
    }
252

    
253
    $this->stmt = $this->pdo->prepare($query);
254
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
255
        $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
256
    }
257
    $this->stmt->execute();
258
    $result = $this->stmt->fetchAll();
259

    
260
    $to_insert = [];
261
    $to_delete = [];
262
    $contained = [];
263
    $found = false;
264

    
265
    foreach ($result as $res) {
266
        $integerIDs = array_map('intval', explode(',', $_POST['manuscript']));
267
        foreach ($integerIDs as $new_value){
268
            if($new_value == $res['manuscript']){
269
                $found = true;
270
                array_push($contained, $new_value);
271
            }
272
        }
273
        if($found == false){
274
            array_push($to_delete, $res);
275
        }
276
        $found = false;
277
    }
278
    $to_insert = array_diff($integerIDs, $contained);
279

    
280
    foreach ($to_delete as $id_to_delete){
281
        $query = "DELETE FROM dd_manuscript WHERE ";
282
        $query .= "manuscript = " . $id_to_delete['manuscript'] . " AND ";
283
        $query .= " wordform_id = :wordform_id ;";
284

    
285

    
286
        $this->stmt = $this->pdo->prepare($query);
287
        if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
288
            $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
289
        }
290
        //
291
        $this->stmt->execute();
292
        var_dump($query);
293
    }
294

    
295
    foreach ($to_insert as $id_to_insert){
296
        $query = "INSERT INTO dd_manuscript VALUES ( ";
297
        $query .= " :wordform_id , ";
298
        $query .= " " . $id_to_insert . " ); ";
299

    
300
        $this->stmt = $this->pdo->prepare($query);
301
        if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
302
            $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
303
        }
304
        $this->stmt->execute();
305
    }
306

    
307
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
308

    
309
    $query = "UPDATE dd_tag
310
                 SET ";
311

    
312
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
313
        $query .= " tag = :tag  ";
314
    }
315

    
316
    $query .= "WHERE ";
317
    if (array_key_exists("tag_id", $_POST) && $_POST['tag_id'] != "") {
318
        $query .= " id = :tag_id ;";
319
    }
320

    
321

    
322
    $this->stmt = $this->pdo->prepare($query);
323
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
324
        $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
325
    }
326
    if (array_key_exists("tag_id", $_POST) && $_POST['tag_id'] != "") {
327
        $this->stmt->bindParam(':tag_id', $_POST['tag_id'], PDO::PARAM_INT);
328
    }
329
    $this->stmt->execute();
330
}}
331

    
332
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
333
define('DB_HOST', 'localhost');
334
define('DB_NAME', 'dalimil2');
335
define('DB_CHARSET', 'utf8');
336
define('DB_USER', 'postgres');
337
define('DB_PASSWORD', 'a');
(1-1/4)