Projekt

Obecné

Profil

Stáhnout (29.4 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
    $query = "SELECT id FROM dd_lemma
104
              WHERE lemma = :lemma;";
105
    $this->stmt = $this->pdo->prepare($query);
106
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
107
            $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
108
        }
109
    $this->stmt->execute();
110
    $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
111

    
112
    $query = "SELECT id FROM dd_tag
113
              WHERE tag = :tag;";
114
    $this->stmt = $this->pdo->prepare($query);
115
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
116
            $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
117
        }
118
    $this->stmt->execute();
119
    $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
120

    
121
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
122
    if($lemma == null){
123
        $query = "SELECT MAX(id) FROM dd_lemma;";
124
        $this->stmt = $this->pdo->prepare($query);
125
        $this->stmt->execute();
126
        $result = $this->stmt->fetchAll();
127
        $result[0]["max"]+=1;
128

    
129
        $query = "INSERT INTO dd_lemma ( id, ";
130
        $values = "VALUES (" . $result[0]["max"] . ", ";
131
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
132
                $query .= " lemma, ";
133
                $values .= " :lemma, ";
134
            }
135
            if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
136
                $query .= " pos ) ";
137
                $values .= " :pos ); ";
138
            }
139

    
140
        $query .= $values;
141
        $this->stmt = $this->pdo->prepare($query);
142
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
143
                $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
144
            }
145
            if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
146
                $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
147
            }
148
        $this->stmt->execute();
149

    
150
        $query = "SELECT id FROM dd_lemma
151
                  WHERE lemma = :lemma;";
152
        $this->stmt = $this->pdo->prepare($query);
153
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
154
                $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
155
            }
156
        $this->stmt->execute();
157
        $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
158
    }
159

    
160
    $query = "UPDATE dd_lemma
161
                 SET ";
162

    
163
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
164
        $query .= " lemma = :lemma , ";
165
    }
166
    if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
167
        $query .= " pos = :pos  ";
168
    }
169

    
170
    $query .= "WHERE ";
171
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
172
        $query .= " id = :lemma_id ;";
173
    }
174

    
175

    
176
    $this->stmt = $this->pdo->prepare($query);
177
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
178
        $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
179
    }
180
    if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
181
        $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
182
    }
183
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
184
        $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
185
    }
186
    $this->stmt->execute();
187

    
188
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
189
    if($tag == null){
190
     $query = "SELECT MAX(id) FROM dd_tag;";
191
        $this->stmt = $this->pdo->prepare($query);
192
        $this->stmt->execute();
193
        $result = $this->stmt->fetchAll();
194
        $result[0]["max"]+=1;
195

    
196

    
197
        $query = "INSERT INTO dd_tag (id, ";
198
        $values = "VALUES (" . $result[0]["max"] . ", ";
199

    
200
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
201
                $query .= " tag ) ";
202
                $values .= " :tag ); ";
203
            }
204

    
205
        $query .= $values;
206
        $this->stmt = $this->pdo->prepare($query);
207
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
208
                $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
209
            }
210
        $this->stmt->execute();
211

    
212
        $query = "SELECT id FROM dd_tag
213
                  WHERE tag = :tag;";
214
        $this->stmt = $this->pdo->prepare($query);
215
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
216
                $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
217
            }
218
        $this->stmt->execute();
219
        $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
220
    }
221

    
222
    $query = "UPDATE dd_tag
223
                 SET ";
224

    
225
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
226
        $query .= " tag = :tag  ";
227
    }
228

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

    
234

    
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
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
240
        $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
241
    }
242
    $this->stmt->execute();
243

    
244
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
245
    $query = "UPDATE dd_wordform
246
                SET ";
247

    
248

    
249
    if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
250
        $query .= " context = :context, ";
251
    }
252
        $query .= " date = CURRENT_DATE,";
253

    
254
    if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
255
        $query .= " description = :description, ";
256
    }
257
    if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
258
        $query .= " description2 = :description2, ";
259
    }
260
    if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
261
        $query .= " description3 = :description3, ";
262
    }
263
    if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
264
        $query .= " ending = :ending, ";
265
    }
266
    if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
267
        $query .= " finished = :finished, ";
268
    }
269
    if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
270
        $query .= " namedentity = :namedentity, ";
271
    }
272
    if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
273
        $query .= " position1 = :position1, ";
274
    }
275
    if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
276
        $query .= " position2 = :position2, ";
277
    }
278
    if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
279
        $query .= " positiondetail = :positiondetail ,";
280
    }
281
    if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
282
        $query .= " prefix = :prefix, ";
283
    }
284
    if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
285
        $query .= " suffix = :suffix, ";
286
    }
287
    if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
288
        $query .= " word = :word, ";
289
    }
290
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
291
        $query .= " lemma_id = :lemma_id, ";
292
    }
293
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
294
        $query .= " tag_id = :tag_id ";
295
    }
296

    
297
    $query .= " WHERE ";
298
    if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
299
        $query .= " id = :id ;";
300
    }
301

    
302
    $this->stmt = $this->pdo->prepare($query);
303

    
304
    if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
305
        $this->stmt->bindParam(':context', $_POST['context'], PDO::PARAM_STR);
306
    }
307
    if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
308
        $this->stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
309
    }
310
    if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
311
        $this->stmt->bindParam(':description2', $_POST['description2'], PDO::PARAM_STR);
312
    }
313
    if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
314
        $this->stmt->bindParam(':description3', $_POST['description3'], PDO::PARAM_STR);
315
    }
316
    if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
317
        $this->stmt->bindParam(':ending', $_POST['ending'], PDO::PARAM_STR);
318
    }
319
    if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
320
        $this->stmt->bindParam(':finished', $_POST['finished']);
321
    }
322
    if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
323
        $this->stmt->bindParam(':namedentity', $_POST['namedentity'], PDO::PARAM_INT);
324
    }
325
    if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
326
        $this->stmt->bindParam(':position1', $_POST['position1'], PDO::PARAM_STR);
327
    }
328
    if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
329
        $this->stmt->bindParam(':position2', $_POST['position2'], PDO::PARAM_STR);
330
    }
331
    if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
332
        $this->stmt->bindParam(':positiondetail', $_POST['positiondetail'], PDO::PARAM_STR);
333
    }
334
    if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
335
        $this->stmt->bindParam(':prefix', $_POST['prefix'], PDO::PARAM_STR);
336
    }
337
    if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
338
        $this->stmt->bindParam(':suffix', $_POST['suffix'], PDO::PARAM_STR);
339
    }
340
    if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
341
        $this->stmt->bindParam(':word', $_POST['word'], PDO::PARAM_STR);
342
    }
343
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
344
        $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
345
    }
346
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
347
        $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
348
    }
349
    if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
350
        $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
351
    }
352

    
353
    $this->stmt->execute();
354

    
355
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
356

    
357
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
358
        $query = "SELECT * FROM dd_manuscript WHERE ";
359
        $query .= " wordform_id = :wordform_id ;";
360
    }
361

    
362
    $this->stmt = $this->pdo->prepare($query);
363
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
364
        $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
365
    }
366
    $this->stmt->execute();
367
    $result = $this->stmt->fetchAll();
368

    
369
    $to_insert = [];
370
    $to_delete = [];
371
    $contained = [];
372
    $found = false;
373
    $integerIDs = [];
374

    
375
    foreach ($result as $res) {
376
        $integerIDs = array_map('intval', explode(',', $_POST['manuscript']));
377
        foreach ($integerIDs as $new_value){
378
            if($new_value == $res['manuscript']){
379
                $found = true;
380
                array_push($contained, $new_value);
381
            }
382
        }
383
        if($found == false){
384
            array_push($to_delete, $res);
385
        }
386
        $found = false;
387
    }
388
    $to_insert = array_diff($integerIDs, $contained);
389

    
390
    foreach ($to_delete as $id_to_delete){
391
        $query = "DELETE FROM dd_manuscript WHERE ";
392
        $query .= "manuscript = " . $id_to_delete['manuscript'] . " AND ";
393
        $query .= " wordform_id = :wordform_id ;";
394

    
395

    
396
        $this->stmt = $this->pdo->prepare($query);
397
        if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
398
            $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
399
        }
400
        //
401
        $this->stmt->execute();
402
        var_dump($query);
403
    }
404

    
405
    foreach ($to_insert as $id_to_insert){
406
        $query = "INSERT INTO dd_manuscript VALUES ( ";
407
        $query .= " :wordform_id , ";
408
        $query .= " " . $id_to_insert . " ); ";
409

    
410
        $this->stmt = $this->pdo->prepare($query);
411
        if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
412
            $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
413
        }
414
        $this->stmt->execute();
415
    }
416
}
417

    
418
function insert(){
419
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
420
    $query = "SELECT id FROM dd_lemma
421
              WHERE lemma = :lemma;";
422
    $this->stmt = $this->pdo->prepare($query);
423
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
424
            $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
425
        }
426
    $this->stmt->execute();
427
    $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
428

    
429
    if($lemma == null){
430
        $query = "SELECT MAX(id) FROM dd_lemma;";
431
        $this->stmt = $this->pdo->prepare($query);
432
        $this->stmt->execute();
433
        $result = $this->stmt->fetchAll();
434
        $result[0]["max"]+=1;
435

    
436
        $query = "INSERT INTO dd_lemma ( id, ";
437
        $values = "VALUES (" . $result[0]["max"] . ", ";
438
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
439
                $query .= " lemma, ";
440
                $values .= " :lemma, ";
441
            }
442
            if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
443
                $query .= " pos ) ";
444
                $values .= " :pos ); ";
445
            }
446

    
447
        $query .= $values;
448
        $this->stmt = $this->pdo->prepare($query);
449
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
450
                $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
451
            }
452
            if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
453
                $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
454
            }
455
        $this->stmt->execute();
456
    }
457

    
458

    
459

    
460
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
461
    $query = "SELECT id FROM dd_tag
462
              WHERE tag = :tag;";
463
    $this->stmt = $this->pdo->prepare($query);
464
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
465
            $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
466
        }
467
    $this->stmt->execute();
468
    $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
469

    
470
    if($tag == null){
471
     $query = "SELECT MAX(id) FROM dd_tag;";
472
        $this->stmt = $this->pdo->prepare($query);
473
        $this->stmt->execute();
474
        $result = $this->stmt->fetchAll();
475
        $result[0]["max"]+=1;
476

    
477

    
478
        $query = "INSERT INTO dd_tag (id, ";
479
        $values = "VALUES (" . $result[0]["max"] . ", ";
480

    
481
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
482
                $query .= " tag ) ";
483
                $values .= " :tag ); ";
484
            }
485

    
486
        $query .= $values;
487
        $this->stmt = $this->pdo->prepare($query);
488
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
489
                $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
490
            }
491
        $this->stmt->execute();
492
    }
493

    
494
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
495
    $query = "SELECT MAX(id) FROM dd_wordform;";
496
    $this->stmt = $this->pdo->prepare($query);
497
    $this->stmt->execute();
498
    $result = $this->stmt->fetchAll();
499
    $result[0]["max"]+=1;
500

    
501
    $query = "SELECT id FROM dd_lemma
502
              WHERE lemma = :lemma;";
503
    $this->stmt = $this->pdo->prepare($query);
504
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
505
            $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
506
        }
507
    $this->stmt->execute();
508
    $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
509

    
510
    $query = "SELECT id FROM dd_tag
511
              WHERE tag = :tag;";
512
    $this->stmt = $this->pdo->prepare($query);
513
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
514
            $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
515
        }
516
    $this->stmt->execute();
517
    $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
518

    
519
    $query = "INSERT INTO dd_wordform ( id, ";
520
    $values = "VALUES (" . $result[0]["max"] . ", ";
521

    
522
     if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
523
            $query .= " context, ";
524
            $values .= " :context, ";
525
        }
526
            $query .= " date, ";
527
            $values .=  " CURRENT_DATE, ";
528

    
529
        if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
530
            $query .= " description, ";
531
            $values .= " :description, ";
532
        }
533
        if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
534
            $query .= " description2, ";
535
            $values .= " :description2, ";
536
        }
537
        if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
538
            $query .= " description3, ";
539
            $values .= " :description3, ";
540
        }
541
        if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
542
            $query .= " ending, ";
543
            $values .= " :ending, ";
544
        }
545
        if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
546
            $query .= " finished, ";
547
            $values .= " :finished, ";
548
        }
549
        if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
550
            $query .= " namedentity, ";
551
            $values .= " :namedentity, ";
552
        }
553
        if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
554
            $query .= " position1, ";
555
            $values .= " :position1, ";
556
        }
557
        if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
558
            $query .= " position2, ";
559
            $values .= " :position2, ";
560
        }
561
        if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
562
            $query .= " positiondetail, ";
563
            $values .= " :positiondetail, ";
564
        }
565
        if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
566
            $query .= " prefix, ";
567
            $values .= " :prefix, ";
568
        }
569
        if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
570
            $query .= " suffix, ";
571
            $values .= " :suffix, ";
572
        }
573
        if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
574
            $query .= " word, ";
575
            $values .= " :word, ";
576
        }
577
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
578
            $query .= " lemma_id, ";
579
            $values .= " :lemma_id, ";
580
        }
581
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
582
            $query .= " tag_id ) ";
583
            $values .= " :tag_id ); ";
584
        }
585

    
586
        $query .= $values;
587
        $this->stmt = $this->pdo->prepare($query);
588

    
589
            if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
590
                $this->stmt->bindParam(':context', $_POST['context'], PDO::PARAM_STR);
591
            }
592
            if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
593
                $this->stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
594
            }
595
            if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
596
                $this->stmt->bindParam(':description2', $_POST['description2'], PDO::PARAM_STR);
597
            }
598
            if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
599
                $this->stmt->bindParam(':description3', $_POST['description3'], PDO::PARAM_STR);
600
            }
601
            if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
602
                $this->stmt->bindParam(':ending', $_POST['ending'], PDO::PARAM_STR);
603
            }
604
            if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
605
                $this->stmt->bindParam(':finished', $_POST['finished']);
606
            }
607
            if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
608
                $this->stmt->bindParam(':namedentity', $_POST['namedentity'], PDO::PARAM_INT);
609
            }
610
            if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
611
                $this->stmt->bindParam(':position1', $_POST['position1'], PDO::PARAM_STR);
612
            }
613
            if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
614
                $this->stmt->bindParam(':position2', $_POST['position2'], PDO::PARAM_STR);
615
            }
616
            if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
617
                $this->stmt->bindParam(':positiondetail', $_POST['positiondetail'], PDO::PARAM_STR);
618
            }
619
            if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
620
                $this->stmt->bindParam(':prefix', $_POST['prefix'], PDO::PARAM_STR);
621
            }
622
            if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
623
                $this->stmt->bindParam(':suffix', $_POST['suffix'], PDO::PARAM_STR);
624
            }
625
            if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
626
                $this->stmt->bindParam(':word', $_POST['word'], PDO::PARAM_STR);
627
            }
628
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
629
                $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
630
            }
631
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
632
                $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
633
            }
634

    
635
            $this->stmt->execute();
636

    
637

    
638
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
639

    
640
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
641
        $query = "SELECT * FROM dd_manuscript WHERE ";
642
        $query .= " wordform_id = :wordform_id ;";
643
    }
644

    
645
    $this->stmt = $this->pdo->prepare($query);
646
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
647
        $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
648
    }
649
    $this->stmt->execute();
650
    $result = $this->stmt->fetchAll();
651

    
652
    $to_insert = [];
653
    $contained = [];
654
    $found = false;
655
    $integerIDs = [];
656

    
657
    foreach ($result as $res) {
658
        $integerIDs = array_map('intval', explode(',', $_POST['manuscript']));
659
        foreach ($integerIDs as $new_value){
660
            if($new_value == $res['manuscript']){
661
                $found = true;
662
                array_push($contained, $new_value);
663
            }
664
        }
665
        if($found == false){
666
            array_push($to_delete, $res);
667
        }
668
        $found = false;
669
    }
670
    $to_insert = array_diff($integerIDs, $contained);
671
    foreach ($to_insert as $id_to_insert){
672
        $query = "INSERT INTO dd_manuscript VALUES ( ";
673
        $query .= " :wordform_id , ";
674
        $query .= " " . $id_to_insert . " ); ";
675

    
676
        $this->stmt = $this->pdo->prepare($query);
677
        if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
678
            $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
679
        }
680
        $this->stmt->execute();
681
    }
682
}
683

    
684
function remove(){
685
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
686
    if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
687
        $query = "DELETE FROM dd_manuscript WHERE";
688
        $query .= " wordform_id = :wordform_id ;";
689
        $this->stmt = $this->pdo->prepare($query);
690
        $this->stmt->bindParam(':wordform_id', $_POST['id'], PDO::PARAM_INT);
691
        $this->stmt->execute();
692
    }
693

    
694
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
695

    
696
        /*if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
697
            $query = "DELETE FROM dd_lemma WHERE";
698
            $query .= " id = :lemma_id ;";
699
            $this->stmt = $this->pdo->prepare($query);
700
            $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
701
            $this->stmt->execute();
702
        } TODO: fix lemma*/
703
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
704

    
705
        /*if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
706
            $query = "DELETE FROM dd_tag WHERE";
707
            $query .= " id = :tag_id ;";
708
            $this->stmt = $this->pdo->prepare($query);
709
            $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
710
            $this->stmt->execute();
711
        } TODO: fix tag */
712

    
713
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
714
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
715
            $query = "DELETE FROM dd_wordform WHERE";
716
            $query .= " id = :id ;";
717
            $this->stmt = $this->pdo->prepare($query);
718
            $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
719
            $this->stmt->execute();
720
        }
721
}
722

    
723
}
724

    
725
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
726
define('DB_HOST', 'localhost');
727
define('DB_NAME', 'dalimil2');
728
define('DB_CHARSET', 'utf8');
729
define('DB_USER', 'postgres');
730
define('DB_PASSWORD', 'ahavole');
(1-1/4)