Projekt

Obecné

Profil

Stáhnout (32.3 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
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
439
        $query = "SELECT id FROM dd_lemma
440
                  WHERE lemma = :lemma;";
441
        $this->stmt = $this->pdo->prepare($query);
442
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
443
                $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
444
            }
445
        $this->stmt->execute();
446
        $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
447

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

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

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

    
477

    
478

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

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

    
496

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

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

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

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

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

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

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

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

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

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

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

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

    
656

    
657
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
658

    
659
                $to_insert = array_map('intval', explode(',', $_POST['manuscript']));
660
                foreach ($to_insert as $id_to_insert){
661
                    $query = "INSERT INTO dd_manuscript VALUES ( ";
662
                    $query .= " :wordform_id , ";
663
                    $query .= " " . $id_to_insert . " ); ";
664

    
665
                    $this->stmt = $this->pdo->prepare($query);
666
                    $this->stmt->bindParam(':wordform_id', $result[0]["max"], PDO::PARAM_INT);
667
                    $this->stmt->execute();
668
                }
669
        }
670

    
671

    
672
function remove(){
673
        $query = "SELECT lemma_id FROM dd_wordform
674
                WHERE id = :id;";
675
        $this->stmt = $this->pdo->prepare($query);
676
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
677
            $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_STR);
678
        }
679
        $this->stmt->execute();
680
        $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
681

    
682
        $query = "SELECT tag_id FROM dd_wordform
683
                WHERE id = :id;";
684
        $this->stmt = $this->pdo->prepare($query);
685
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
686
            $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_STR);
687
        }
688
        $this->stmt->execute();
689
        $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
690

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

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

    
709
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
710
        $query = "SELECT id FROM dd_wordform WHERE lemma_id = :lemma_id;";
711
        $this->stmt = $this->pdo->prepare($query);
712
        $this->stmt->bindParam(':lemma_id', $lemma["lemma_id"], PDO::PARAM_INT);
713
        $this->stmt->execute();
714
        $result = $this->stmt->fetchAll();
715
        if(sizeof($result)==0){
716
            $query = "DELETE FROM dd_lemma WHERE";
717
            $query .= " id = :lemma_id ;";
718
            $this->stmt = $this->pdo->prepare($query);
719
            $this->stmt->bindParam(':lemma_id', $lemma["lemma_id"], PDO::PARAM_INT);
720
            $this->stmt->execute();
721
        }
722
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
723
        $query = "SELECT id FROM dd_wordform WHERE tag_id = :tag_id;";
724
        $this->stmt = $this->pdo->prepare($query);
725
        $this->stmt->bindParam(':tag_id', $tag["tag_id"], PDO::PARAM_INT);
726
        $this->stmt->execute();
727
        $result = $this->stmt->fetchAll();
728
        if(sizeof($result)==0){
729
            $query = "DELETE FROM dd_tag WHERE";
730
            $query .= " id = :tag_id ;";
731
            $this->stmt = $this->pdo->prepare($query);
732
            $this->stmt->bindParam(':tag_id', $tag["tag_id"], PDO::PARAM_INT);
733
            $this->stmt->execute();
734
        }else{
735
            echo "fail";
736
        }
737
    }
738

    
739
}
740

    
741
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
742
define('DB_HOST', 'localhost');
743
define('DB_NAME', 'dalimil2');
744
define('DB_CHARSET', 'utf8');
745
define('DB_USER', 'postgres');
746
define('DB_PASSWORD', 'a');
(1-1/4)