Projekt

Obecné

Profil

Stáhnout (29.6 KB) Statistiky
| Větev: | Revize:
1
<?php
2
class DB {
3
    // (A) CONNECT TO DATABASE
4
    public $error = "";
5
    private $pdo = null;
6
    private $stmt = null;
7
    function __construct () {
8
        try {
9
            $this->pdo = new PDO(
10
                "pgsql:host=".DB_HOST.";dbname=".DB_NAME,
11
                DB_USER, DB_PASSWORD, [
12
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
13
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
14
                ]
15
            );
16
        } catch (Exception $ex) { die($ex->getMessage()); }
17
    }
18

    
19
    // (B) CLOSE CONNECTION
20
    function __destruct(){
21
        if ($this->stmt!==null) { $this->stmt = null; }
22
        if ($this->pdo!==null) { $this->pdo = null; }
23
    }
24

    
25
    // (C) RUN A SELECT QUERY
26
    function select($sql, $params){
27
        $result = false;
28
        try {
29
            $this->stmt = $this->pdo->prepare($sql);
30
            if (array_key_exists("lemma", $params)) {
31
                $this->stmt->bindParam(':lemma',$params['lemma'], PDO::PARAM_STR);
32
            }
33
            if (array_key_exists("word", $params)) {
34
                $this->stmt->bindParam(':word',$params['word'], PDO::PARAM_STR);
35
            }
36
            if (array_key_exists("position1", $params)) {
37
                $this->stmt->bindParam(':position1',$params['position1'], PDO::PARAM_INT);
38
            }
39
            if (array_key_exists("position2", $params)) {
40
                $this->stmt->bindParam(':position2',$params['position2'], PDO::PARAM_INT);
41
            }
42
            if (array_key_exists("positiondetail", $params)) {
43
                $this->stmt->bindParam(':positiondetail',$params['positiondetail'], PDO::PARAM_INT);
44
            }
45
            if (array_key_exists("tag", $params)) {
46
                $this->stmt->bindParam(':tag',$params['tag'], PDO::PARAM_STR);
47
            }
48
            if (array_key_exists("tag", $params)) {
49
                $this->stmt->bindParam(':tag',$params['tag'], PDO::PARAM_STR);
50
            }
51
            if (array_key_exists("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
  print_r($_POST);
420
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
421
    $query = "SELECT id FROM dd_lemma
422
              WHERE lemma = :lemma;";
423
    $this->stmt = $this->pdo->prepare($query);
424
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
425
            $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
426
        }
427
    $this->stmt->execute();
428
    $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
429

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

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

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

    
459

    
460

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

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

    
478

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

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

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

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

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

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

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

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

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

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

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

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

    
638

    
639
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
640

    
641
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
642
        $query = "SELECT * FROM dd_manuscript WHERE ";
643
        $query .= " wordform_id = :wordform_id ;";
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

    
685
}
686

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

    
697
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
698

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

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

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

    
726
}
727

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