Projekt

Obecné

Profil

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

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

    
25
    // (C) RUN A SELECT QUERY
26
    function select($sql, $params){
27
        $result = false;
28
        try {
29
            $this->stmt = $this->pdo->prepare($sql);
30
            if (array_key_exists("lemma", $params)) {
31
                $this->stmt->bindParam(':lemma',$params['lemma'], PDO::PARAM_STR);
32
            }
33
            if (array_key_exists("word", $params)) {
34
                $this->stmt->bindParam(':word',$params['word'], PDO::PARAM_STR);
35
            }
36
            if (array_key_exists("position1", $params)) {
37
                $this->stmt->bindParam(':position1',$params['position1'], PDO::PARAM_INT);
38
            }
39
            if (array_key_exists("position2", $params)) {
40
                $this->stmt->bindParam(':position2',$params['position2'], PDO::PARAM_INT);
41
            }
42
            if (array_key_exists("positiondetail", $params)) {
43
                $this->stmt->bindParam(':positiondetail',$params['positiondetail'], PDO::PARAM_INT);
44
            }
45
            if (array_key_exists("tag", $params)) {
46
                $this->stmt->bindParam(':tag',$params['tag'], PDO::PARAM_STR);
47
            }
48
            if (array_key_exists("tag", $params)) {
49
                $this->stmt->bindParam(':tag',$params['tag'], PDO::PARAM_STR);
50
            }
51
            if (array_key_exists("description2", $params)) {
52
                $this->stmt->bindParam(':description2',$params['description2']);
53
            }
54
            if (array_key_exists("manuscript", $params)) {
55
                for ($x = 0; $x < count($params["manuscript"]); $x += 1) {
56
                    $this->stmt->bindParam(':manuscript'.$x,$params["manuscript"][$x], PDO::PARAM_INT);
57
                }
58
            }
59
            $this->stmt->execute();
60
            $result = $this->stmt->fetchAll();
61
            return $result;
62
        } catch (Exception $ex) {
63
            $this->error = $ex->getMessage();
64
            return false;
65
        }
66
    }
67

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

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

    
85
    function createChangeRequest($request) {
86
        $this->stmt = $this->pdo->prepare('INSERT INTO changes (wordform_id, message) VALUES (:wordform_id, :message)');
87
        $this->stmt->bindParam(':wordform_id', $request['wordform_id']);
88
        $this->stmt->bindParam(':message',$request['message']);
89
        $this->stmt->execute();
90
        return $this->stmt->fetchAll();
91
    }
92

    
93
    function removeChangeRequest($requestId) {
94
        $this->stmt = $this->pdo->prepare('DELETE FROM changes WHERE id = :requestId');
95
        $this->stmt->bindParam(':requestId', $requestId);
96
        $this->stmt->execute();
97
        return $this->stmt->fetchAll();
98
    }
99

    
100
    function getChangeRequests() {
101
        $this->stmt = $this->pdo->prepare('SELECT c.id as changeId, c.message, w.id as wordformId, w.word, l.lemma FROM changes as c left join dd_wordform as w on c.wordform_id = w.id left join dd_lemma as l on w.lemma_id = l.id');
102
        $this->stmt->execute();
103
        return $this->stmt->fetchAll();
104
    }
105

    
106
    function updateUserRole($userId, $role) {
107
        $this->stmt = $this->pdo->prepare("UPDATE users SET role = :role WHERE id = :userId");
108
        $this->stmt->bindParam(':role', $role);
109
        $this->stmt->bindParam(':userId',$userId);
110
        $this->stmt->execute();
111
        return $this->stmt->fetchAll();
112
    }
113

    
114
    function deleteUser($userId) {
115
        $this->stmt = $this->pdo->prepare("DELETE from users WHERE id = :userId");
116
        $this->stmt->bindParam(':userId',$userId);
117
        $this->stmt->execute();
118
        return $this->stmt->fetchAll();
119
    }
120

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

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

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

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

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

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

    
179
        $query = "UPDATE dd_lemma
180
                     SET ";
181

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

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

    
194

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

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

    
215

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

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

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

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

    
241
        $query = "UPDATE dd_tag
242
                     SET ";
243

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

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

    
253

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

    
263
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
264
        $query = "UPDATE dd_wordform
265
                    SET ";
266

    
267

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

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

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

    
321
        $this->stmt = $this->pdo->prepare($query);
322

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

    
372
        $this->stmt->execute();
373

    
374
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
375

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

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

    
388
        $to_insert = [];
389
        $to_delete = [];
390
        $contained = [];
391
        $found = false;
392
        $integerIDs = [];
393

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

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

    
414

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

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

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

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

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

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

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

    
478

    
479

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

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

    
497

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

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

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

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

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

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

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

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

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

    
606
            $query .= $values;
607
            $this->stmt = $this->pdo->prepare($query);
608

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

    
655
                $this->stmt->execute();
656

    
657

    
658
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
659

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

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

    
671
                $to_insert = [];
672
                $contained = [];
673
                $found = false;
674
                $integerIDs = [];
675

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

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

    
703

    
704
    }
705

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

    
716
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
717

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

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

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

    
745
}
746

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