Projekt

Obecné

Profil

Stáhnout (30.6 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("description2", $params)) {
52
                $this->stmt->bindParam(':description2',$params['description2']);
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 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
    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

    
122

    
123
function update(){
124
    $query = "SELECT id FROM dd_lemma
125
              WHERE lemma = :lemma;";
126
    $this->stmt = $this->pdo->prepare($query);
127
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
128
            $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
129
        }
130
    $this->stmt->execute();
131
    $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
132

    
133
    $query = "SELECT id FROM dd_tag
134
              WHERE tag = :tag;";
135
    $this->stmt = $this->pdo->prepare($query);
136
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
137
            $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
138
        }
139
    $this->stmt->execute();
140
    $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
141

    
142
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
143
    if($lemma == null){
144
        $query = "SELECT MAX(id) FROM dd_lemma;";
145
        $this->stmt = $this->pdo->prepare($query);
146
        $this->stmt->execute();
147
        $result = $this->stmt->fetchAll();
148
        $result[0]["max"]+=1;
149

    
150
        $query = "INSERT INTO dd_lemma ( id, ";
151
        $values = "VALUES (" . $result[0]["max"] . ", ";
152
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
153
                $query .= " lemma, ";
154
                $values .= " :lemma, ";
155
            }
156
            if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
157
                $query .= " pos ) ";
158
                $values .= " :pos ); ";
159
            }
160

    
161
        $query .= $values;
162
        $this->stmt = $this->pdo->prepare($query);
163
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
164
                $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
165
            }
166
            if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
167
                $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
168
            }
169
        $this->stmt->execute();
170

    
171
        $query = "SELECT id FROM dd_lemma
172
                  WHERE lemma = :lemma;";
173
        $this->stmt = $this->pdo->prepare($query);
174
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
175
                $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
176
            }
177
        $this->stmt->execute();
178
        $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
179
    }
180

    
181
    $query = "UPDATE dd_lemma
182
                 SET ";
183

    
184
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
185
        $query .= " lemma = :lemma , ";
186
    }
187
    if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
188
        $query .= " pos = :pos  ";
189
    }
190

    
191
    $query .= "WHERE ";
192
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
193
        $query .= " id = :lemma_id ;";
194
    }
195

    
196

    
197
    $this->stmt = $this->pdo->prepare($query);
198
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
199
        $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
200
    }
201
    if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
202
        $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
203
    }
204
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
205
        $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
206
    }
207
    $this->stmt->execute();
208

    
209
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
210
    if($tag == null){
211
     $query = "SELECT MAX(id) FROM dd_tag;";
212
        $this->stmt = $this->pdo->prepare($query);
213
        $this->stmt->execute();
214
        $result = $this->stmt->fetchAll();
215
        $result[0]["max"]+=1;
216

    
217

    
218
        $query = "INSERT INTO dd_tag (id, ";
219
        $values = "VALUES (" . $result[0]["max"] . ", ";
220

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

    
226
        $query .= $values;
227
        $this->stmt = $this->pdo->prepare($query);
228
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
229
                $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
230
            }
231
        $this->stmt->execute();
232

    
233
        $query = "SELECT id FROM dd_tag
234
                  WHERE tag = :tag;";
235
        $this->stmt = $this->pdo->prepare($query);
236
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
237
                $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
238
            }
239
        $this->stmt->execute();
240
        $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
241
    }
242

    
243
    $query = "UPDATE dd_tag
244
                 SET ";
245

    
246
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
247
        $query .= " tag = :tag  ";
248
    }
249

    
250
    $query .= "WHERE ";
251
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
252
        $query .= " id = :tag_id ;";
253
    }
254

    
255

    
256
    $this->stmt = $this->pdo->prepare($query);
257
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
258
        $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
259
    }
260
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
261
        $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
262
    }
263
    $this->stmt->execute();
264

    
265
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
266
    $query = "UPDATE dd_wordform
267
                SET ";
268

    
269

    
270
    if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
271
        $query .= " context = :context, ";
272
    }
273
        $query .= " date = CURRENT_DATE,";
274

    
275
    if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
276
        $query .= " description = :description, ";
277
    }
278
    if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
279
        $query .= " description2 = :description2, ";
280
    }
281
    if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
282
        $query .= " description3 = :description3, ";
283
    }
284
    if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
285
        $query .= " ending = :ending, ";
286
    }
287
    if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
288
        $query .= " finished = :finished, ";
289
    }
290
    if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
291
        $query .= " namedentity = :namedentity, ";
292
    }
293
    if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
294
        $query .= " position1 = :position1, ";
295
    }
296
    if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
297
        $query .= " position2 = :position2, ";
298
    }
299
    if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
300
        $query .= " positiondetail = :positiondetail ,";
301
    }
302
    if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
303
        $query .= " prefix = :prefix, ";
304
    }
305
    if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
306
        $query .= " suffix = :suffix, ";
307
    }
308
    if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
309
        $query .= " word = :word, ";
310
    }
311
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
312
        $query .= " lemma_id = :lemma_id, ";
313
    }
314
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
315
        $query .= " tag_id = :tag_id ";
316
    }
317

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

    
323
    $this->stmt = $this->pdo->prepare($query);
324

    
325
    if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
326
        $this->stmt->bindParam(':context', $_POST['context'], PDO::PARAM_STR);
327
    }
328
    if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
329
        $this->stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
330
    }
331
    if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
332
        $this->stmt->bindParam(':description2', $_POST['description2'], PDO::PARAM_STR);
333
    }
334
    if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
335
        $this->stmt->bindParam(':description3', $_POST['description3'], PDO::PARAM_STR);
336
    }
337
    if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
338
        $this->stmt->bindParam(':ending', $_POST['ending'], PDO::PARAM_STR);
339
    }
340
    if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
341
        $this->stmt->bindParam(':finished', $_POST['finished']);
342
    }
343
    if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
344
        $this->stmt->bindParam(':namedentity', $_POST['namedentity'], PDO::PARAM_INT);
345
    }
346
    if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
347
        $this->stmt->bindParam(':position1', $_POST['position1'], PDO::PARAM_STR);
348
    }
349
    if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
350
        $this->stmt->bindParam(':position2', $_POST['position2'], PDO::PARAM_STR);
351
    }
352
    if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
353
        $this->stmt->bindParam(':positiondetail', $_POST['positiondetail'], PDO::PARAM_STR);
354
    }
355
    if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
356
        $this->stmt->bindParam(':prefix', $_POST['prefix'], PDO::PARAM_STR);
357
    }
358
    if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
359
        $this->stmt->bindParam(':suffix', $_POST['suffix'], PDO::PARAM_STR);
360
    }
361
    if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
362
        $this->stmt->bindParam(':word', $_POST['word'], PDO::PARAM_STR);
363
    }
364
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
365
        $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
366
    }
367
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
368
        $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
369
    }
370
    if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
371
        $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
372
    }
373

    
374
    $this->stmt->execute();
375

    
376
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
377

    
378
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
379
        $query = "SELECT * FROM dd_manuscript WHERE ";
380
        $query .= " wordform_id = :wordform_id ;";
381
    }
382

    
383
    $this->stmt = $this->pdo->prepare($query);
384
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
385
        $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
386
    }
387
    $this->stmt->execute();
388
    $result = $this->stmt->fetchAll();
389

    
390
    $to_insert = [];
391
    $to_delete = [];
392
    $contained = [];
393
    $found = false;
394
    $integerIDs = [];
395

    
396
    foreach ($result as $res) {
397
        $integerIDs = array_map('intval', explode(',', $_POST['manuscript']));
398
        foreach ($integerIDs as $new_value){
399
            if($new_value == $res['manuscript']){
400
                $found = true;
401
                array_push($contained, $new_value);
402
            }
403
        }
404
        if($found == false){
405
            array_push($to_delete, $res);
406
        }
407
        $found = false;
408
    }
409
    $to_insert = array_diff($integerIDs, $contained);
410

    
411
    foreach ($to_delete as $id_to_delete){
412
        $query = "DELETE FROM dd_manuscript WHERE ";
413
        $query .= "manuscript = " . $id_to_delete['manuscript'] . " AND ";
414
        $query .= " wordform_id = :wordform_id ;";
415

    
416

    
417
        $this->stmt = $this->pdo->prepare($query);
418
        if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
419
            $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
420
        }
421
        //
422
        $this->stmt->execute();
423
        var_dump($query);
424
    }
425

    
426
    foreach ($to_insert as $id_to_insert){
427
        $query = "INSERT INTO dd_manuscript VALUES ( ";
428
        $query .= " :wordform_id , ";
429
        $query .= " " . $id_to_insert . " ); ";
430

    
431
        $this->stmt = $this->pdo->prepare($query);
432
        if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
433
            $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
434
        }
435
        $this->stmt->execute();
436
    }
437
}
438

    
439
function insert(){
440
  print_r($_POST);
441
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
442
    $query = "SELECT id FROM dd_lemma
443
              WHERE lemma = :lemma;";
444
    $this->stmt = $this->pdo->prepare($query);
445
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
446
            $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
447
        }
448
    $this->stmt->execute();
449
    $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
450

    
451
    if($lemma == null){
452
        $query = "SELECT MAX(id) FROM dd_lemma;";
453
        $this->stmt = $this->pdo->prepare($query);
454
        $this->stmt->execute();
455
        $result = $this->stmt->fetchAll();
456
        $result[0]["max"]+=1;
457

    
458
        $query = "INSERT INTO dd_lemma ( id, ";
459
        $values = "VALUES (" . $result[0]["max"] . ", ";
460
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
461
                $query .= " lemma, ";
462
                $values .= " :lemma, ";
463
            }
464
            if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
465
                $query .= " pos ) ";
466
                $values .= " :pos ); ";
467
            }
468

    
469
        $query .= $values;
470
        $this->stmt = $this->pdo->prepare($query);
471
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
472
                $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
473
            }
474
            if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
475
                $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
476
            }
477
        $this->stmt->execute();
478
    }
479

    
480

    
481

    
482
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
483
    $query = "SELECT id FROM dd_tag
484
              WHERE tag = :tag;";
485
    $this->stmt = $this->pdo->prepare($query);
486
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
487
            $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
488
        }
489
    $this->stmt->execute();
490
    $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
491

    
492
    if($tag == null){
493
     $query = "SELECT MAX(id) FROM dd_tag;";
494
        $this->stmt = $this->pdo->prepare($query);
495
        $this->stmt->execute();
496
        $result = $this->stmt->fetchAll();
497
        $result[0]["max"]+=1;
498

    
499

    
500
        $query = "INSERT INTO dd_tag (id, ";
501
        $values = "VALUES (" . $result[0]["max"] . ", ";
502

    
503
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
504
                $query .= " tag ) ";
505
                $values .= " :tag ); ";
506
            }
507

    
508
        $query .= $values;
509
        $this->stmt = $this->pdo->prepare($query);
510
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
511
                $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
512
            }
513
        $this->stmt->execute();
514
    }
515

    
516
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
517
    $query = "SELECT MAX(id) FROM dd_wordform;";
518
    $this->stmt = $this->pdo->prepare($query);
519
    $this->stmt->execute();
520
    $result = $this->stmt->fetchAll();
521
    $result[0]["max"]+=1;
522

    
523
    $query = "SELECT id FROM dd_lemma
524
              WHERE lemma = :lemma;";
525
    $this->stmt = $this->pdo->prepare($query);
526
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
527
            $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
528
        }
529
    $this->stmt->execute();
530
    $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
531

    
532
    $query = "SELECT id FROM dd_tag
533
              WHERE tag = :tag;";
534
    $this->stmt = $this->pdo->prepare($query);
535
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
536
            $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
537
        }
538
    $this->stmt->execute();
539
    $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
540

    
541
    $query = "INSERT INTO dd_wordform ( id, ";
542
    $values = "VALUES (" . $result[0]["max"] . ", ";
543

    
544
     if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
545
            $query .= " context, ";
546
            $values .= " :context, ";
547
        }
548
            $query .= " date, ";
549
            $values .=  " CURRENT_DATE, ";
550

    
551
        if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
552
            $query .= " description, ";
553
            $values .= " :description, ";
554
        }
555
        if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
556
            $query .= " description2, ";
557
            $values .= " :description2, ";
558
        }
559
        if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
560
            $query .= " description3, ";
561
            $values .= " :description3, ";
562
        }
563
        if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
564
            $query .= " ending, ";
565
            $values .= " :ending, ";
566
        }
567
        if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
568
            $query .= " finished, ";
569
            $values .= " :finished, ";
570
        }
571
        if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
572
            $query .= " namedentity, ";
573
            $values .= " :namedentity, ";
574
        }
575
        if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
576
            $query .= " position1, ";
577
            $values .= " :position1, ";
578
        }
579
        if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
580
            $query .= " position2, ";
581
            $values .= " :position2, ";
582
        }
583
        if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
584
            $query .= " positiondetail, ";
585
            $values .= " :positiondetail, ";
586
        }
587
        if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
588
            $query .= " prefix, ";
589
            $values .= " :prefix, ";
590
        }
591
        if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
592
            $query .= " suffix, ";
593
            $values .= " :suffix, ";
594
        }
595
        if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
596
            $query .= " word, ";
597
            $values .= " :word, ";
598
        }
599
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
600
            $query .= " lemma_id, ";
601
            $values .= " :lemma_id, ";
602
        }
603
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
604
            $query .= " tag_id ) ";
605
            $values .= " :tag_id ); ";
606
        }
607

    
608
        $query .= $values;
609
        $this->stmt = $this->pdo->prepare($query);
610

    
611
            if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
612
                $this->stmt->bindParam(':context', $_POST['context'], PDO::PARAM_STR);
613
            }
614
            if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
615
                $this->stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
616
            }
617
            if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
618
                $this->stmt->bindParam(':description2', $_POST['description2'], PDO::PARAM_STR);
619
            }
620
            if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
621
                $this->stmt->bindParam(':description3', $_POST['description3'], PDO::PARAM_STR);
622
            }
623
            if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
624
                $this->stmt->bindParam(':ending', $_POST['ending'], PDO::PARAM_STR);
625
            }
626
            if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
627
                $this->stmt->bindParam(':finished', $_POST['finished']);
628
            }
629
            if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
630
                $this->stmt->bindParam(':namedentity', $_POST['namedentity'], PDO::PARAM_INT);
631
            }
632
            if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
633
                $this->stmt->bindParam(':position1', $_POST['position1'], PDO::PARAM_STR);
634
            }
635
            if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
636
                $this->stmt->bindParam(':position2', $_POST['position2'], PDO::PARAM_STR);
637
            }
638
            if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
639
                $this->stmt->bindParam(':positiondetail', $_POST['positiondetail'], PDO::PARAM_STR);
640
            }
641
            if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
642
                $this->stmt->bindParam(':prefix', $_POST['prefix'], PDO::PARAM_STR);
643
            }
644
            if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
645
                $this->stmt->bindParam(':suffix', $_POST['suffix'], PDO::PARAM_STR);
646
            }
647
            if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
648
                $this->stmt->bindParam(':word', $_POST['word'], PDO::PARAM_STR);
649
            }
650
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
651
                $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
652
            }
653
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
654
                $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
655
            }
656

    
657
            $this->stmt->execute();
658

    
659

    
660
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
661

    
662
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
663
        $query = "SELECT * FROM dd_manuscript WHERE ";
664
        $query .= " wordform_id = :wordform_id ;";
665

    
666
        $this->stmt = $this->pdo->prepare($query);
667
            if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
668
                $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
669
            }
670
            $this->stmt->execute();
671
            $result = $this->stmt->fetchAll();
672

    
673
            $to_insert = [];
674
            $contained = [];
675
            $found = false;
676
            $integerIDs = [];
677

    
678
            foreach ($result as $res) {
679
                $integerIDs = array_map('intval', explode(',', $_POST['manuscript']));
680
                foreach ($integerIDs as $new_value){
681
                    if($new_value == $res['manuscript']){
682
                        $found = true;
683
                        array_push($contained, $new_value);
684
                    }
685
                }
686
                if($found == false){
687
                    array_push($to_delete, $res);
688
                }
689
                $found = false;
690
            }
691
            $to_insert = array_diff($integerIDs, $contained);
692
            foreach ($to_insert as $id_to_insert){
693
                $query = "INSERT INTO dd_manuscript VALUES ( ";
694
                $query .= " :wordform_id , ";
695
                $query .= " " . $id_to_insert . " ); ";
696

    
697
                $this->stmt = $this->pdo->prepare($query);
698
                if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
699
                    $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
700
                }
701
                $this->stmt->execute();
702
            }
703
    }
704

    
705

    
706
}
707

    
708
function remove(){
709
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
710
    if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
711
        $query = "DELETE FROM dd_manuscript WHERE";
712
        $query .= " wordform_id = :wordform_id ;";
713
        $this->stmt = $this->pdo->prepare($query);
714
        $this->stmt->bindParam(':wordform_id', $_POST['id'], PDO::PARAM_INT);
715
        $this->stmt->execute();
716
    }
717

    
718
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
719

    
720
        /*if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
721
            $query = "DELETE FROM dd_lemma WHERE";
722
            $query .= " id = :lemma_id ;";
723
            $this->stmt = $this->pdo->prepare($query);
724
            $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
725
            $this->stmt->execute();
726
        } TODO: fix lemma*/
727
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
728

    
729
        /*if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
730
            $query = "DELETE FROM dd_tag WHERE";
731
            $query .= " id = :tag_id ;";
732
            $this->stmt = $this->pdo->prepare($query);
733
            $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
734
            $this->stmt->execute();
735
        } TODO: fix tag */
736

    
737
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
738
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
739
            $query = "DELETE FROM dd_wordform WHERE";
740
            $query .= " id = :id ;";
741
            $this->stmt = $this->pdo->prepare($query);
742
            $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
743
            $this->stmt->execute();
744
        }
745
}
746

    
747
}
748

    
749
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
750
define('DB_HOST', 'localhost');
751
define('DB_NAME', 'dalimil2');
752
define('DB_CHARSET', 'utf8');
753
define('DB_USER', 'postgres');
754
define('DB_PASSWORD', 'a');
(1-1/4)