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

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

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

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

    
461

    
462

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

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

    
480

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

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

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

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

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

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

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

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

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

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

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

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

    
640

    
641
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
642

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

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

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

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

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

    
686

    
687
}
688

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

    
699
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
700

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

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

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

    
728
}
729

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