Projekt

Obecné

Profil

Stáhnout (29.8 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("description2", $params)) {
55
                $this->stmt->bindParam(':description2',$params['description2']);
56
            }
57
            if (array_key_exists("manuscript", $params)) {
58
                for ($x = 0; $x < count($params["manuscript"]); $x += 1) {
59
                    $this->stmt->bindParam(':manuscript'.$x,$params["manuscript"][$x], PDO::PARAM_INT);
60
                }
61
            }
62
            $this->stmt->execute();
63
            $result = $this->stmt->fetchAll();
64
            return $result;
65
        } catch (Exception $ex) {
66
            $this->error = $ex->getMessage();
67
            return false;
68
        }
69
    }
70

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

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

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

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

    
103

    
104

    
105
function update(){
106
    $query = "SELECT id FROM dd_lemma
107
              WHERE lemma = :lemma;";
108
    $this->stmt = $this->pdo->prepare($query);
109
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
110
            $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
111
        }
112
    $this->stmt->execute();
113
    $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
114

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

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

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

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

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

    
163
    $query = "UPDATE dd_lemma
164
                 SET ";
165

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

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

    
178

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

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

    
199

    
200
        $query = "INSERT INTO dd_tag (id, ";
201
        $values = "VALUES (" . $result[0]["max"] . ", ";
202

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

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

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

    
225
    $query = "UPDATE dd_tag
226
                 SET ";
227

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

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

    
237

    
238
    $this->stmt = $this->pdo->prepare($query);
239
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
240
        $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
241
    }
242
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
243
        $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
244
    }
245
    $this->stmt->execute();
246

    
247
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
248
    $query = "UPDATE dd_wordform
249
                SET ";
250

    
251

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

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

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

    
305
    $this->stmt = $this->pdo->prepare($query);
306

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

    
356
    $this->stmt->execute();
357

    
358
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
359

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

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

    
372
    $to_insert = [];
373
    $to_delete = [];
374
    $contained = [];
375
    $found = false;
376
    $integerIDs = [];
377

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

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

    
398

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

    
408
    foreach ($to_insert as $id_to_insert){
409
        $query = "INSERT INTO dd_manuscript VALUES ( ";
410
        $query .= " :wordform_id , ";
411
        $query .= " " . $id_to_insert . " ); ";
412

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

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

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

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

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

    
462

    
463

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

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

    
481

    
482
        $query = "INSERT INTO dd_tag (id, ";
483
        $values = "VALUES (" . $result[0]["max"] . ", ";
484

    
485
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
486
                $query .= " tag ) ";
487
                $values .= " :tag ); ";
488
            }
489

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

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

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

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

    
523
    $query = "INSERT INTO dd_wordform ( id, ";
524
    $values = "VALUES (" . $result[0]["max"] . ", ";
525

    
526
     if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
527
            $query .= " context, ";
528
            $values .= " :context, ";
529
        }
530
            $query .= " date, ";
531
            $values .=  " CURRENT_DATE, ";
532

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

    
590
        $query .= $values;
591
        $this->stmt = $this->pdo->prepare($query);
592

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

    
639
            $this->stmt->execute();
640

    
641

    
642
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
643

    
644
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
645
        $query = "SELECT * FROM dd_manuscript WHERE ";
646
        $query .= " wordform_id = :wordform_id ;";
647

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

    
655
            $to_insert = [];
656
            $contained = [];
657
            $found = false;
658
            $integerIDs = [];
659

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

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

    
687

    
688
}
689

    
690
function remove(){
691
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
692
    if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
693
        $query = "DELETE FROM dd_manuscript WHERE";
694
        $query .= " wordform_id = :wordform_id ;";
695
        $this->stmt = $this->pdo->prepare($query);
696
        $this->stmt->bindParam(':wordform_id', $_POST['id'], PDO::PARAM_INT);
697
        $this->stmt->execute();
698
    }
699

    
700
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
701

    
702
        /*if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
703
            $query = "DELETE FROM dd_lemma WHERE";
704
            $query .= " id = :lemma_id ;";
705
            $this->stmt = $this->pdo->prepare($query);
706
            $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
707
            $this->stmt->execute();
708
        } TODO: fix lemma*/
709
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
710

    
711
        /*if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
712
            $query = "DELETE FROM dd_tag WHERE";
713
            $query .= " id = :tag_id ;";
714
            $this->stmt = $this->pdo->prepare($query);
715
            $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
716
            $this->stmt->execute();
717
        } TODO: fix tag */
718

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

    
729
}
730

    
731
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
732
define('DB_HOST', 'localhost');
733
define('DB_NAME', 'dalim2');
734
define('DB_CHARSET', 'utf8');
735
define('DB_USER', 'postgres');
736
define('DB_PASSWORD', 'a');
(1-1/4)