Projekt

Obecné

Profil

Stáhnout (32.6 KB) Statistiky
| Větev: | Revize:
1 3b343aea Tomáš Pašek
<?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 bc83b448 Anděl Ondřej
    function select($sql, $params){
27 3b343aea Tomáš Pašek
        $result = false;
28
        try {
29
            $this->stmt = $this->pdo->prepare($sql);
30 2a99773b Milan Vacek
            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 12de28ea Tomáš Pašek
            if (array_key_exists("description2", $params)) {
52
                $this->stmt->bindParam(':description2',$params['description2']);
53
            }
54 2a99773b Milan Vacek
            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 3b343aea Tomáš Pašek
            $result = $this->stmt->fetchAll();
61
            return $result;
62
        } catch (Exception $ex) {
63
            $this->error = $ex->getMessage();
64
            return false;
65
        }
66
    }
67 2a99773b Milan Vacek
68 b748cb5f Tomáš Pašek
    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 5aceeb68 Tomáš Pašek
    function createChangeRequest($request) {
86
        $this->stmt = $this->pdo->prepare('INSERT INTO changes (wordform_id, message) VALUES (:wordform_id, :message)');
87
        $this->stmt->bindParam(':wordform_id', $request['wordform_id']);
88
        $this->stmt->bindParam(':message',$request['message']);
89
        $this->stmt->execute();
90
        return $this->stmt->fetchAll();
91
    }
92
93
    function removeChangeRequest($requestId) {
94
        $this->stmt = $this->pdo->prepare('DELETE FROM changes WHERE id = :requestId');
95
        $this->stmt->bindParam(':requestId', $requestId);
96
        $this->stmt->execute();
97
        return $this->stmt->fetchAll();
98
    }
99
100
    function getChangeRequests() {
101
        $this->stmt = $this->pdo->prepare('SELECT c.id as changeId, c.message, w.id as wordformId, w.word, l.lemma FROM changes as c left join dd_wordform as w on c.wordform_id = w.id left join dd_lemma as l on w.lemma_id = l.id');
102
        $this->stmt->execute();
103
        return $this->stmt->fetchAll();
104
    }
105
106 b748cb5f Tomáš Pašek
    function updateUserRole($userId, $role) {
107
        $this->stmt = $this->pdo->prepare("UPDATE users SET role = :role WHERE id = :userId");
108
        $this->stmt->bindParam(':role', $role);
109
        $this->stmt->bindParam(':userId',$userId);
110
        $this->stmt->execute();
111
        return $this->stmt->fetchAll();
112
    }
113
114
    function deleteUser($userId) {
115
        $this->stmt = $this->pdo->prepare("DELETE from users WHERE id = :userId");
116
        $this->stmt->bindParam(':userId',$userId);
117
        $this->stmt->execute();
118
        return $this->stmt->fetchAll();
119
    }
120
121 cd67c936 Anděl Ondřej
    function update(){
122 82f8d4ca Milan Vacek
        $query = "SELECT id FROM dd_lemma
123
                  WHERE lemma = :lemma;";
124
        $this->stmt = $this->pdo->prepare($query);
125
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
126
                $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
127
            }
128
        $this->stmt->execute();
129
        $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
130
131
        $query = "SELECT id FROM dd_tag
132
                  WHERE tag = :tag;";
133
        $this->stmt = $this->pdo->prepare($query);
134
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
135
                $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
136
            }
137
        $this->stmt->execute();
138
        $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
139 2a99773b Milan Vacek
140 cd67c936 Anděl Ondřej
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
141
        if($lemma == null){
142
            $query = "SELECT MAX(id) FROM dd_lemma;";
143
            $this->stmt = $this->pdo->prepare($query);
144
            $this->stmt->execute();
145
            $result = $this->stmt->fetchAll();
146
            $result[0]["max"]+=1;
147 2a99773b Milan Vacek
148 cd67c936 Anděl Ondřej
            $query = "INSERT INTO dd_lemma ( id, ";
149
            $values = "VALUES (" . $result[0]["max"] . ", ";
150
                if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
151
                    $query .= " lemma, ";
152
                    $values .= " :lemma, ";
153
                }
154
                if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
155
                    $query .= " pos ) ";
156
                    $values .= " :pos ); ";
157
                }
158 2a99773b Milan Vacek
159 cd67c936 Anděl Ondřej
            $query .= $values;
160
            $this->stmt = $this->pdo->prepare($query);
161
                if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
162
                    $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
163
                }
164
                if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
165
                    $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
166
                }
167
            $this->stmt->execute();
168 2a99773b Milan Vacek
169 cd67c936 Anděl Ondřej
            $query = "SELECT id FROM dd_lemma
170
                      WHERE lemma = :lemma;";
171
            $this->stmt = $this->pdo->prepare($query);
172
                if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
173
                    $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
174
                }
175
            $this->stmt->execute();
176
            $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
177
        }
178 2a99773b Milan Vacek
179 cd67c936 Anděl Ondřej
        $query = "UPDATE dd_lemma
180
                     SET ";
181 2a99773b Milan Vacek
182 cd67c936 Anděl Ondřej
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
183
            $query .= " lemma = :lemma , ";
184 2a99773b Milan Vacek
        }
185 cd67c936 Anděl Ondřej
        if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
186
            $query .= " pos = :pos  ";
187 2a99773b Milan Vacek
        }
188
189 cd67c936 Anděl Ondřej
        $query .= "WHERE ";
190
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
191
            $query .= " id = :lemma_id ;";
192 2a99773b Milan Vacek
        }
193
194
195
        $this->stmt = $this->pdo->prepare($query);
196 82f8d4ca Milan Vacek
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
197
            $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
198
        }
199 cd67c936 Anděl Ondřej
        if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
200
            $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
201
        }
202
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
203
            $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
204
        }
205 82f8d4ca Milan Vacek
        $this->stmt->execute();
206
207 cd67c936 Anděl Ondřej
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
208
        if($tag == null){
209
         $query = "SELECT MAX(id) FROM dd_tag;";
210
            $this->stmt = $this->pdo->prepare($query);
211
            $this->stmt->execute();
212
            $result = $this->stmt->fetchAll();
213
            $result[0]["max"]+=1;
214 2a99773b Milan Vacek
215
216 cd67c936 Anděl Ondřej
            $query = "INSERT INTO dd_tag (id, ";
217
            $values = "VALUES (" . $result[0]["max"] . ", ";
218 2a99773b Milan Vacek
219 cd67c936 Anděl Ondřej
                if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
220
                    $query .= " tag ) ";
221
                    $values .= " :tag ); ";
222
                }
223 2a99773b Milan Vacek
224 cd67c936 Anděl Ondřej
            $query .= $values;
225
            $this->stmt = $this->pdo->prepare($query);
226
                if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
227
                    $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
228
                }
229
            $this->stmt->execute();
230
231
            $query = "SELECT id FROM dd_tag
232
                      WHERE tag = :tag;";
233
            $this->stmt = $this->pdo->prepare($query);
234
                if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
235
                    $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
236
                }
237
            $this->stmt->execute();
238
            $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
239 82f8d4ca Milan Vacek
        }
240 b315cd75 Milan Vacek
241 cd67c936 Anděl Ondřej
        $query = "UPDATE dd_tag
242
                     SET ";
243 82f8d4ca Milan Vacek
244 cd67c936 Anděl Ondřej
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
245
            $query .= " tag = :tag  ";
246
        }
247 82f8d4ca Milan Vacek
248 cd67c936 Anděl Ondřej
        $query .= "WHERE ";
249
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
250
            $query .= " id = :tag_id ;";
251
        }
252 82f8d4ca Milan Vacek
253
254
        $this->stmt = $this->pdo->prepare($query);
255 cd67c936 Anděl Ondřej
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
256
            $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
257
        }
258
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
259
            $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
260
        }
261 82f8d4ca Milan Vacek
        $this->stmt->execute();
262 b315cd75 Milan Vacek
263 cd67c936 Anděl Ondřej
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
264
        $query = "UPDATE dd_wordform
265
                    SET ";
266 b315cd75 Milan Vacek
267 cd67c936 Anděl Ondřej
268
        if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
269
            $query .= " context = :context, ";
270 82f8d4ca Milan Vacek
        }
271 cd67c936 Anděl Ondřej
            $query .= " date = CURRENT_DATE,";
272 82f8d4ca Milan Vacek
273 cd67c936 Anděl Ondřej
        if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
274
            $query .= " description = :description, ";
275
        }
276
        if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
277
            $query .= " description2 = :description2, ";
278
        }
279
        if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
280
            $query .= " description3 = :description3, ";
281
        }
282
        if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
283
            $query .= " ending = :ending, ";
284
        }
285
        if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
286
            $query .= " finished = :finished, ";
287
        }
288
        if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
289
            $query .= " namedentity = :namedentity, ";
290
        }
291
        if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
292
            $query .= " position1 = :position1, ";
293
        }
294
        if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
295
            $query .= " position2 = :position2, ";
296
        }
297
        if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
298
            $query .= " positiondetail = :positiondetail ,";
299
        }
300
        if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
301
            $query .= " prefix = :prefix, ";
302
        }
303
        if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
304
            $query .= " suffix = :suffix, ";
305
        }
306
        if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
307
            $query .= " word = :word, ";
308
        }
309
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
310
            $query .= " lemma_id = :lemma_id, ";
311
        }
312 82f8d4ca Milan Vacek
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
313 cd67c936 Anděl Ondřej
            $query .= " tag_id = :tag_id ";
314 82f8d4ca Milan Vacek
        }
315 b315cd75 Milan Vacek
316 cd67c936 Anděl Ondřej
        $query .= " WHERE ";
317
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
318
            $query .= " id = :id ;";
319 b315cd75 Milan Vacek
        }
320
321 cd67c936 Anděl Ondřej
        $this->stmt = $this->pdo->prepare($query);
322
323
        if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
324
            $this->stmt->bindParam(':context', $_POST['context'], PDO::PARAM_STR);
325
        }
326 b315cd75 Milan Vacek
        if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
327 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
328 b315cd75 Milan Vacek
        }
329
        if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
330 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':description2', $_POST['description2'], PDO::PARAM_STR);
331 b315cd75 Milan Vacek
        }
332
        if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
333 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':description3', $_POST['description3'], PDO::PARAM_STR);
334 b315cd75 Milan Vacek
        }
335
        if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
336 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':ending', $_POST['ending'], PDO::PARAM_STR);
337 b315cd75 Milan Vacek
        }
338
        if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
339 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':finished', $_POST['finished']);
340 b315cd75 Milan Vacek
        }
341
        if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
342 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':namedentity', $_POST['namedentity'], PDO::PARAM_INT);
343 b315cd75 Milan Vacek
        }
344
        if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
345 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':position1', $_POST['position1'], PDO::PARAM_STR);
346 b315cd75 Milan Vacek
        }
347
        if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
348 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':position2', $_POST['position2'], PDO::PARAM_STR);
349 b315cd75 Milan Vacek
        }
350
        if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
351 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':positiondetail', $_POST['positiondetail'], PDO::PARAM_STR);
352 b315cd75 Milan Vacek
        }
353
        if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
354 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':prefix', $_POST['prefix'], PDO::PARAM_STR);
355 b315cd75 Milan Vacek
        }
356
        if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
357 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':suffix', $_POST['suffix'], PDO::PARAM_STR);
358 b315cd75 Milan Vacek
        }
359
        if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
360 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':word', $_POST['word'], PDO::PARAM_STR);
361 b315cd75 Milan Vacek
        }
362 82f8d4ca Milan Vacek
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
363 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
364 b315cd75 Milan Vacek
        }
365 82f8d4ca Milan Vacek
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
366 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
367
        }
368
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
369
            $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
370
        }
371
372
        $this->stmt->execute();
373
374
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
375
376 7dd550bc Tomáš Pašek
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
377 cd67c936 Anděl Ondřej
            $query = "SELECT * FROM dd_manuscript WHERE ";
378
            $query .= " wordform_id = :wordform_id ;";
379 b315cd75 Milan Vacek
        }
380
381
        $this->stmt = $this->pdo->prepare($query);
382 7dd550bc Tomáš Pašek
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
383
            $this->stmt->bindParam(':wordform_id', $_POST['id']);
384 cd67c936 Anděl Ondřej
        }
385
        $this->stmt->execute();
386
        $result = $this->stmt->fetchAll();
387
388
        $to_insert = [];
389
        $to_delete = [];
390
        $contained = [];
391
        $found = false;
392
        $integerIDs = [];
393
394
        foreach ($result as $res) {
395
            $integerIDs = array_map('intval', explode(',', $_POST['manuscript']));
396
            foreach ($integerIDs as $new_value){
397
                if($new_value == $res['manuscript']){
398
                    $found = true;
399
                    array_push($contained, $new_value);
400
                }
401
            }
402
            if($found == false){
403
                array_push($to_delete, $res);
404
            }
405
            $found = false;
406
        }
407
        $to_insert = array_diff($integerIDs, $contained);
408
409
        foreach ($to_delete as $id_to_delete){
410
            $query = "DELETE FROM dd_manuscript WHERE ";
411
            $query .= "manuscript = " . $id_to_delete['manuscript'] . " AND ";
412
            $query .= " wordform_id = :wordform_id ;";
413
414
415
            $this->stmt = $this->pdo->prepare($query);
416 7dd550bc Tomáš Pašek
            if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
417
                $this->stmt->bindParam(':wordform_id', $_POST['id'], PDO::PARAM_INT);
418 cd67c936 Anděl Ondřej
            }
419
            //
420
            $this->stmt->execute();
421
            var_dump($query);
422
        }
423
424
        foreach ($to_insert as $id_to_insert){
425
            $query = "INSERT INTO dd_manuscript VALUES ( ";
426
            $query .= " :wordform_id , ";
427
            $query .= " " . $id_to_insert . " ); ";
428
429
            $this->stmt = $this->pdo->prepare($query);
430 7dd550bc Tomáš Pašek
            if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
431
                $this->stmt->bindParam(':wordform_id', $_POST['id'], PDO::PARAM_INT);
432 cd67c936 Anděl Ondřej
            }
433
            $this->stmt->execute();
434
        }
435
    }
436 b315cd75 Milan Vacek
437 cd67c936 Anděl Ondřej
    function insert(){
438
      print_r($_POST);
439
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
440
        $query = "SELECT id FROM dd_lemma
441
                  WHERE lemma = :lemma;";
442
        $this->stmt = $this->pdo->prepare($query);
443
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
444
                $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
445 b315cd75 Milan Vacek
            }
446 cd67c936 Anděl Ondřej
        $this->stmt->execute();
447
        $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
448
449
        if($lemma == null){
450
            $query = "SELECT MAX(id) FROM dd_lemma;";
451
            $this->stmt = $this->pdo->prepare($query);
452
            $this->stmt->execute();
453
            $result = $this->stmt->fetchAll();
454
            $result[0]["max"]+=1;
455
456
            $query = "INSERT INTO dd_lemma ( id, ";
457
            $values = "VALUES (" . $result[0]["max"] . ", ";
458
                if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
459
                    $query .= " lemma, ";
460
                    $values .= " :lemma, ";
461
                }
462
                if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
463
                    $query .= " pos ) ";
464
                    $values .= " :pos ); ";
465
                }
466
467
            $query .= $values;
468
            $this->stmt = $this->pdo->prepare($query);
469
                if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
470
                    $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
471
                }
472
                if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
473
                    $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
474
                }
475
            $this->stmt->execute();
476
        }
477
478
479
480
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
481
        $query = "SELECT id FROM dd_tag
482
                  WHERE tag = :tag;";
483
        $this->stmt = $this->pdo->prepare($query);
484
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
485
                $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
486
            }
487
        $this->stmt->execute();
488
        $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
489
490
        if($tag == null){
491
         $query = "SELECT MAX(id) FROM dd_tag;";
492
            $this->stmt = $this->pdo->prepare($query);
493
            $this->stmt->execute();
494
            $result = $this->stmt->fetchAll();
495
            $result[0]["max"]+=1;
496
497
498
            $query = "INSERT INTO dd_tag (id, ";
499
            $values = "VALUES (" . $result[0]["max"] . ", ";
500
501
                if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
502
                    $query .= " tag ) ";
503
                    $values .= " :tag ); ";
504
                }
505
506
            $query .= $values;
507
            $this->stmt = $this->pdo->prepare($query);
508
                if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
509
                    $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
510
                }
511
            $this->stmt->execute();
512
        }
513
514
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
515
        $query = "SELECT MAX(id) FROM dd_wordform;";
516
        $this->stmt = $this->pdo->prepare($query);
517
        $this->stmt->execute();
518
        $result = $this->stmt->fetchAll();
519
        $result[0]["max"]+=1;
520
521
        $query = "SELECT id FROM dd_lemma
522
                  WHERE lemma = :lemma;";
523
        $this->stmt = $this->pdo->prepare($query);
524
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
525
                $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
526
            }
527
        $this->stmt->execute();
528
        $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
529
530
        $query = "SELECT id FROM dd_tag
531
                  WHERE tag = :tag;";
532
        $this->stmt = $this->pdo->prepare($query);
533
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
534
                $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
535
            }
536
        $this->stmt->execute();
537
        $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
538
539
        $query = "INSERT INTO dd_wordform ( id, ";
540
        $values = "VALUES (" . $result[0]["max"] . ", ";
541
542
         if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
543
                $query .= " context, ";
544
                $values .= " :context, ";
545
            }
546
                $query .= " date, ";
547
                $values .=  " CURRENT_DATE, ";
548
549 b315cd75 Milan Vacek
            if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
550 cd67c936 Anděl Ondřej
                $query .= " description, ";
551
                $values .= " :description, ";
552 b315cd75 Milan Vacek
            }
553
            if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
554 cd67c936 Anděl Ondřej
                $query .= " description2, ";
555
                $values .= " :description2, ";
556 b315cd75 Milan Vacek
            }
557
            if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
558 cd67c936 Anděl Ondřej
                $query .= " description3, ";
559
                $values .= " :description3, ";
560 b315cd75 Milan Vacek
            }
561
            if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
562 cd67c936 Anděl Ondřej
                $query .= " ending, ";
563
                $values .= " :ending, ";
564 b315cd75 Milan Vacek
            }
565
            if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
566 cd67c936 Anděl Ondřej
                $query .= " finished, ";
567
                $values .= " :finished, ";
568 b315cd75 Milan Vacek
            }
569
            if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
570 cd67c936 Anděl Ondřej
                $query .= " namedentity, ";
571
                $values .= " :namedentity, ";
572 b315cd75 Milan Vacek
            }
573
            if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
574 cd67c936 Anděl Ondřej
                $query .= " position1, ";
575
                $values .= " :position1, ";
576 b315cd75 Milan Vacek
            }
577
            if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
578 cd67c936 Anděl Ondřej
                $query .= " position2, ";
579
                $values .= " :position2, ";
580 b315cd75 Milan Vacek
            }
581
            if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
582 cd67c936 Anděl Ondřej
                $query .= " positiondetail, ";
583
                $values .= " :positiondetail, ";
584 b315cd75 Milan Vacek
            }
585
            if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
586 cd67c936 Anděl Ondřej
                $query .= " prefix, ";
587
                $values .= " :prefix, ";
588 b315cd75 Milan Vacek
            }
589
            if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
590 cd67c936 Anděl Ondřej
                $query .= " suffix, ";
591
                $values .= " :suffix, ";
592 b315cd75 Milan Vacek
            }
593
            if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
594 cd67c936 Anděl Ondřej
                $query .= " word, ";
595
                $values .= " :word, ";
596 b315cd75 Milan Vacek
            }
597 82f8d4ca Milan Vacek
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
598 cd67c936 Anděl Ondřej
                $query .= " lemma_id, ";
599
                $values .= " :lemma_id, ";
600 b315cd75 Milan Vacek
            }
601 82f8d4ca Milan Vacek
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
602 cd67c936 Anděl Ondřej
                $query .= " tag_id ) ";
603
                $values .= " :tag_id ); ";
604 b315cd75 Milan Vacek
            }
605
606 cd67c936 Anděl Ondřej
            $query .= $values;
607
            $this->stmt = $this->pdo->prepare($query);
608 b315cd75 Milan Vacek
609 cd67c936 Anděl Ondřej
                if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
610
                    $this->stmt->bindParam(':context', $_POST['context'], PDO::PARAM_STR);
611
                }
612
                if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
613
                    $this->stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
614
                }
615
                if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
616
                    $this->stmt->bindParam(':description2', $_POST['description2'], PDO::PARAM_STR);
617
                }
618
                if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
619
                    $this->stmt->bindParam(':description3', $_POST['description3'], PDO::PARAM_STR);
620
                }
621
                if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
622
                    $this->stmt->bindParam(':ending', $_POST['ending'], PDO::PARAM_STR);
623
                }
624
                if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
625
                    $this->stmt->bindParam(':finished', $_POST['finished']);
626
                }
627
                if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
628
                    $this->stmt->bindParam(':namedentity', $_POST['namedentity'], PDO::PARAM_INT);
629
                }
630
                if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
631
                    $this->stmt->bindParam(':position1', $_POST['position1'], PDO::PARAM_STR);
632
                }
633
                if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
634
                    $this->stmt->bindParam(':position2', $_POST['position2'], PDO::PARAM_STR);
635
                }
636
                if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
637
                    $this->stmt->bindParam(':positiondetail', $_POST['positiondetail'], PDO::PARAM_STR);
638
                }
639
                if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
640
                    $this->stmt->bindParam(':prefix', $_POST['prefix'], PDO::PARAM_STR);
641
                }
642
                if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
643
                    $this->stmt->bindParam(':suffix', $_POST['suffix'], PDO::PARAM_STR);
644
                }
645
                if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
646
                    $this->stmt->bindParam(':word', $_POST['word'], PDO::PARAM_STR);
647
                }
648
                if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
649
                    $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
650
                }
651
                if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
652
                    $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
653
                }
654 b315cd75 Milan Vacek
655 cd67c936 Anděl Ondřej
                $this->stmt->execute();
656 b315cd75 Milan Vacek
657
658 cd67c936 Anděl Ondřej
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
659 b315cd75 Milan Vacek
660 cd67c936 Anděl Ondřej
        if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
661
            $query = "SELECT * FROM dd_manuscript WHERE ";
662
            $query .= " wordform_id = :wordform_id ;";
663 b315cd75 Milan Vacek
664 cd67c936 Anděl Ondřej
            $this->stmt = $this->pdo->prepare($query);
665 8fc55000 Milan Vacek
                if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
666
                    $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
667
                }
668
                $this->stmt->execute();
669 cd67c936 Anděl Ondřej
                $result = $this->stmt->fetchAll();
670 b315cd75 Milan Vacek
671 cd67c936 Anděl Ondřej
                $to_insert = [];
672
                $contained = [];
673
                $found = false;
674
                $integerIDs = [];
675
676
                foreach ($result as $res) {
677
                    $integerIDs = array_map('intval', explode(',', $_POST['manuscript']));
678
                    foreach ($integerIDs as $new_value){
679
                        if($new_value == $res['manuscript']){
680
                            $found = true;
681
                            array_push($contained, $new_value);
682
                        }
683
                    }
684
                    if($found == false){
685
                        array_push($to_delete, $res);
686
                    }
687
                    $found = false;
688
                }
689
                $to_insert = array_diff($integerIDs, $contained);
690
                foreach ($to_insert as $id_to_insert){
691
                    $query = "INSERT INTO dd_manuscript VALUES ( ";
692
                    $query .= " :wordform_id , ";
693
                    $query .= " " . $id_to_insert . " ); ";
694
695
                    $this->stmt = $this->pdo->prepare($query);
696
                    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
697
                        $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
698
                    }
699
                    $this->stmt->execute();
700
                }
701
        }
702 8fc55000 Milan Vacek
703 cd67c936 Anděl Ondřej
704
    }
705 3a071bba Anděl Ondřej
706 f95d4d7c Tomáš Pašek
    function remove(){
707
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
708
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
709
            $query = "DELETE FROM dd_manuscript WHERE";
710
            $query .= " wordform_id = :wordform_id ;";
711
            $this->stmt = $this->pdo->prepare($query);
712
            $this->stmt->bindParam(':wordform_id', $_POST['id'], PDO::PARAM_INT);
713
            $this->stmt->execute();
714
        }
715 3a071bba Anděl Ondřej
716 f95d4d7c Tomáš Pašek
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
717 79c140ee Milan Vacek
718 6c152ae0 Ondrej Drtina
        /*if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
719 5418813c Milan Vacek
            $query = "DELETE FROM dd_lemma WHERE";
720 79c140ee Milan Vacek
            $query .= " id = :lemma_id ;";
721 5418813c Milan Vacek
            $this->stmt = $this->pdo->prepare($query);
722 82f8d4ca Milan Vacek
            $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
723 5418813c Milan Vacek
            $this->stmt->execute();
724 f20e0b8b Anděl Ondřej
        } TODO: fix lemma*/
725 f95d4d7c Tomáš Pašek
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
726 a4f82b78 Milan Vacek
727 6c152ae0 Ondrej Drtina
        /*if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
728 a4f82b78 Milan Vacek
            $query = "DELETE FROM dd_tag WHERE";
729
            $query .= " id = :tag_id ;";
730
            $this->stmt = $this->pdo->prepare($query);
731
            $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
732
            $this->stmt->execute();
733 6c152ae0 Ondrej Drtina
        } TODO: fix tag */
734 79c140ee Milan Vacek
735 f95d4d7c Tomáš Pašek
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
736
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
737
            $query = "DELETE FROM dd_wordform WHERE";
738
            $query .= " id = :id ;";
739
            $this->stmt = $this->pdo->prepare($query);
740
            $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
741
            $this->stmt->execute();
742
        }
743 3a071bba Anděl Ondřej
    }
744 f95d4d7c Tomáš Pašek
745 b315cd75 Milan Vacek
}
746 3b343aea Tomáš Pašek
747
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
748
define('DB_HOST', 'localhost');
749 5aceeb68 Tomáš Pašek
define('DB_NAME', 'dalimil2');
750 3b343aea Tomáš Pašek
define('DB_CHARSET', 'utf8');
751
define('DB_USER', 'postgres');
752 f95d4d7c Tomáš Pašek
define('DB_PASSWORD', 'a');