Projekt

Obecné

Profil

Stáhnout (32.3 KB) Statistiky
| Větev: | Revize:
1 3b343aea Tomáš Pašek
<?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 bc83b448 Anděl Ondřej
    function select($sql, $params){
27 3b343aea Tomáš Pašek
        $result = false;
28
        try {
29
            $this->stmt = $this->pdo->prepare($sql);
30 2a99773b Milan Vacek
            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 12de28ea Tomáš Pašek
            if (array_key_exists("description2", $params)) {
52
                $this->stmt->bindParam(':description2',$params['description2']);
53
            }
54 2a99773b Milan Vacek
            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 3b343aea Tomáš Pašek
            $result = $this->stmt->fetchAll();
61
            return $result;
62
        } catch (Exception $ex) {
63
            $this->error = $ex->getMessage();
64
            return false;
65
        }
66
    }
67 2a99773b Milan Vacek
68 b748cb5f Tomáš Pašek
    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 5aceeb68 Tomáš Pašek
    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 b748cb5f Tomáš Pašek
    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 cd67c936 Anděl Ondřej
    function update(){
122 82f8d4ca Milan Vacek
        $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 2a99773b Milan Vacek
140 cd67c936 Anděl Ondřej
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 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 2a99773b Milan Vacek
148 cd67c936 Anděl Ondřej
            $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 2a99773b Milan Vacek
159 cd67c936 Anděl Ondřej
            $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 2a99773b Milan Vacek
169 cd67c936 Anděl Ondřej
            $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 2a99773b Milan Vacek
179 cd67c936 Anděl Ondřej
        $query = "UPDATE dd_lemma
180
                     SET ";
181 2a99773b Milan Vacek
182 cd67c936 Anděl Ondřej
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
183
            $query .= " lemma = :lemma , ";
184 2a99773b Milan Vacek
        }
185 cd67c936 Anděl Ondřej
        if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
186
            $query .= " pos = :pos  ";
187 2a99773b Milan Vacek
        }
188
189 cd67c936 Anděl Ondřej
        $query .= "WHERE ";
190
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
191
            $query .= " id = :lemma_id ;";
192 2a99773b Milan Vacek
        }
193
194
195
        $this->stmt = $this->pdo->prepare($query);
196 82f8d4ca Milan Vacek
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
197
            $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
198
        }
199 cd67c936 Anděl Ondřej
        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 82f8d4ca Milan Vacek
        $this->stmt->execute();
206
207 cd67c936 Anděl Ondřej
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 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 2a99773b Milan Vacek
215
216 cd67c936 Anděl Ondřej
            $query = "INSERT INTO dd_tag (id, ";
217
            $values = "VALUES (" . $result[0]["max"] . ", ";
218 2a99773b Milan Vacek
219 cd67c936 Anděl Ondřej
                if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
220
                    $query .= " tag ) ";
221
                    $values .= " :tag ); ";
222
                }
223 2a99773b Milan Vacek
224 cd67c936 Anděl Ondřej
            $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 82f8d4ca Milan Vacek
        }
240 b315cd75 Milan Vacek
241 cd67c936 Anděl Ondřej
        $query = "UPDATE dd_tag
242
                     SET ";
243 82f8d4ca Milan Vacek
244 cd67c936 Anděl Ondřej
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
245
            $query .= " tag = :tag  ";
246
        }
247 82f8d4ca Milan Vacek
248 cd67c936 Anděl Ondřej
        $query .= "WHERE ";
249
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
250
            $query .= " id = :tag_id ;";
251
        }
252 82f8d4ca Milan Vacek
253
254
        $this->stmt = $this->pdo->prepare($query);
255 cd67c936 Anděl Ondřej
        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 82f8d4ca Milan Vacek
        $this->stmt->execute();
262 b315cd75 Milan Vacek
263 cd67c936 Anděl Ondřej
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
264
        $query = "UPDATE dd_wordform
265
                    SET ";
266 b315cd75 Milan Vacek
267 cd67c936 Anděl Ondřej
268
        if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
269
            $query .= " context = :context, ";
270 82f8d4ca Milan Vacek
        }
271 cd67c936 Anděl Ondřej
            $query .= " date = CURRENT_DATE,";
272 82f8d4ca Milan Vacek
273 cd67c936 Anděl Ondřej
        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 82f8d4ca Milan Vacek
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
313 cd67c936 Anděl Ondřej
            $query .= " tag_id = :tag_id ";
314 82f8d4ca Milan Vacek
        }
315 b315cd75 Milan Vacek
316 cd67c936 Anděl Ondřej
        $query .= " WHERE ";
317
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
318
            $query .= " id = :id ;";
319 b315cd75 Milan Vacek
        }
320
321 cd67c936 Anděl Ondřej
        $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 b315cd75 Milan Vacek
        if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
327 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
328 b315cd75 Milan Vacek
        }
329
        if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
330 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':description2', $_POST['description2'], PDO::PARAM_STR);
331 b315cd75 Milan Vacek
        }
332
        if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
333 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':description3', $_POST['description3'], PDO::PARAM_STR);
334 b315cd75 Milan Vacek
        }
335
        if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
336 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':ending', $_POST['ending'], PDO::PARAM_STR);
337 b315cd75 Milan Vacek
        }
338
        if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
339 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':finished', $_POST['finished']);
340 b315cd75 Milan Vacek
        }
341
        if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
342 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':namedentity', $_POST['namedentity'], PDO::PARAM_INT);
343 b315cd75 Milan Vacek
        }
344
        if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
345 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':position1', $_POST['position1'], PDO::PARAM_STR);
346 b315cd75 Milan Vacek
        }
347
        if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
348 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':position2', $_POST['position2'], PDO::PARAM_STR);
349 b315cd75 Milan Vacek
        }
350
        if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
351 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':positiondetail', $_POST['positiondetail'], PDO::PARAM_STR);
352 b315cd75 Milan Vacek
        }
353
        if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
354 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':prefix', $_POST['prefix'], PDO::PARAM_STR);
355 b315cd75 Milan Vacek
        }
356
        if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
357 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':suffix', $_POST['suffix'], PDO::PARAM_STR);
358 b315cd75 Milan Vacek
        }
359
        if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
360 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':word', $_POST['word'], PDO::PARAM_STR);
361 b315cd75 Milan Vacek
        }
362 82f8d4ca Milan Vacek
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
363 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
364 b315cd75 Milan Vacek
        }
365 82f8d4ca Milan Vacek
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
366 cd67c936 Anděl Ondřej
            $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 7dd550bc Tomáš Pašek
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
377 cd67c936 Anděl Ondřej
            $query = "SELECT * FROM dd_manuscript WHERE ";
378
            $query .= " wordform_id = :wordform_id ;";
379 b315cd75 Milan Vacek
        }
380
381
        $this->stmt = $this->pdo->prepare($query);
382 7dd550bc Tomáš Pašek
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
383
            $this->stmt->bindParam(':wordform_id', $_POST['id']);
384 cd67c936 Anděl Ondřej
        }
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 7dd550bc Tomáš Pašek
            if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
417
                $this->stmt->bindParam(':wordform_id', $_POST['id'], PDO::PARAM_INT);
418 cd67c936 Anděl Ondřej
            }
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 7dd550bc Tomáš Pašek
            if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
431
                $this->stmt->bindParam(':wordform_id', $_POST['id'], PDO::PARAM_INT);
432 cd67c936 Anděl Ondřej
            }
433
            $this->stmt->execute();
434
        }
435
    }
436 b315cd75 Milan Vacek
437 cd67c936 Anděl Ondřej
    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 b315cd75 Milan Vacek
            }
445 cd67c936 Anděl Ondřej
        $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 8ef8fa46 Tomáš Pašek
         }
545 cd67c936 Anděl Ondřej
                $query .= " date, ";
546
                $values .=  " CURRENT_DATE, ";
547
548 b315cd75 Milan Vacek
            if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
549 cd67c936 Anděl Ondřej
                $query .= " description, ";
550
                $values .= " :description, ";
551 b315cd75 Milan Vacek
            }
552
            if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
553 cd67c936 Anděl Ondřej
                $query .= " description2, ";
554
                $values .= " :description2, ";
555 b315cd75 Milan Vacek
            }
556
            if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
557 cd67c936 Anděl Ondřej
                $query .= " description3, ";
558
                $values .= " :description3, ";
559 b315cd75 Milan Vacek
            }
560
            if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
561 cd67c936 Anděl Ondřej
                $query .= " ending, ";
562
                $values .= " :ending, ";
563 b315cd75 Milan Vacek
            }
564
            if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
565 cd67c936 Anděl Ondřej
                $query .= " finished, ";
566
                $values .= " :finished, ";
567 b315cd75 Milan Vacek
            }
568
            if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
569 cd67c936 Anděl Ondřej
                $query .= " namedentity, ";
570
                $values .= " :namedentity, ";
571 b315cd75 Milan Vacek
            }
572
            if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
573 cd67c936 Anděl Ondřej
                $query .= " position1, ";
574
                $values .= " :position1, ";
575 b315cd75 Milan Vacek
            }
576
            if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
577 cd67c936 Anděl Ondřej
                $query .= " position2, ";
578
                $values .= " :position2, ";
579 b315cd75 Milan Vacek
            }
580
            if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
581 cd67c936 Anděl Ondřej
                $query .= " positiondetail, ";
582
                $values .= " :positiondetail, ";
583 b315cd75 Milan Vacek
            }
584
            if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
585 cd67c936 Anděl Ondřej
                $query .= " prefix, ";
586
                $values .= " :prefix, ";
587 b315cd75 Milan Vacek
            }
588
            if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
589 cd67c936 Anděl Ondřej
                $query .= " suffix, ";
590
                $values .= " :suffix, ";
591 b315cd75 Milan Vacek
            }
592
            if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
593 cd67c936 Anděl Ondřej
                $query .= " word, ";
594
                $values .= " :word, ";
595 b315cd75 Milan Vacek
            }
596 82f8d4ca Milan Vacek
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
597 cd67c936 Anděl Ondřej
                $query .= " lemma_id, ";
598
                $values .= " :lemma_id, ";
599 b315cd75 Milan Vacek
            }
600 82f8d4ca Milan Vacek
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
601 cd67c936 Anděl Ondřej
                $query .= " tag_id ) ";
602
                $values .= " :tag_id ); ";
603 b315cd75 Milan Vacek
            }
604
605 cd67c936 Anděl Ondřej
            $query .= $values;
606
            $this->stmt = $this->pdo->prepare($query);
607 b315cd75 Milan Vacek
608 cd67c936 Anděl Ondřej
                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 b315cd75 Milan Vacek
654 cd67c936 Anděl Ondřej
                $this->stmt->execute();
655 b315cd75 Milan Vacek
656
657 cd67c936 Anděl Ondřej
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
658 b315cd75 Milan Vacek
659 8ef8fa46 Tomáš Pašek
                $to_insert = array_map('intval', explode(',', $_POST['manuscript']));
660 cd67c936 Anděl Ondřej
                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 8ef8fa46 Tomáš Pašek
                    $this->stmt->bindParam(':wordform_id', $result[0]["max"], PDO::PARAM_INT);
667 cd67c936 Anděl Ondřej
                    $this->stmt->execute();
668
                }
669
        }
670 8fc55000 Milan Vacek
671 cd67c936 Anděl Ondřej
672 79c140ee Milan Vacek
function remove(){
673 53a9c7e2 Milan Vacek
        $query = "SELECT lemma_id FROM dd_wordform
674
                WHERE id = :id;";
675
        $this->stmt = $this->pdo->prepare($query);
676 f95d4d7c Tomáš Pašek
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
677 53a9c7e2 Milan Vacek
            $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_STR);
678 f95d4d7c Tomáš Pašek
        }
679 53a9c7e2 Milan Vacek
        $this->stmt->execute();
680
        $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
681 3a071bba Anděl Ondřej
682 53a9c7e2 Milan Vacek
        $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 6c152ae0 Ondrej Drtina
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 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 79c140ee Milan Vacek
700 53a9c7e2 Milan Vacek
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 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 79c140ee Milan Vacek
709 53a9c7e2 Milan Vacek
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 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 5418813c Milan Vacek
            $query = "DELETE FROM dd_lemma WHERE";
717 79c140ee Milan Vacek
            $query .= " id = :lemma_id ;";
718 5418813c Milan Vacek
            $this->stmt = $this->pdo->prepare($query);
719 53a9c7e2 Milan Vacek
            $this->stmt->bindParam(':lemma_id', $lemma["lemma_id"], PDO::PARAM_INT);
720 5418813c Milan Vacek
            $this->stmt->execute();
721 53a9c7e2 Milan Vacek
        }
722 a4f82b78 Milan Vacek
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
723 53a9c7e2 Milan Vacek
        $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 a4f82b78 Milan Vacek
            $query = "DELETE FROM dd_tag WHERE";
730
            $query .= " id = :tag_id ;";
731
            $this->stmt = $this->pdo->prepare($query);
732 53a9c7e2 Milan Vacek
            $this->stmt->bindParam(':tag_id', $tag["tag_id"], PDO::PARAM_INT);
733 f95d4d7c Tomáš Pašek
            $this->stmt->execute();
734 53a9c7e2 Milan Vacek
        }else{
735
            echo "fail";
736 f95d4d7c Tomáš Pašek
        }
737 3a071bba Anděl Ondřej
    }
738 f95d4d7c Tomáš Pašek
739 b315cd75 Milan Vacek
}
740 3b343aea Tomáš Pašek
741
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
742
define('DB_HOST', 'localhost');
743 5aceeb68 Tomáš Pašek
define('DB_NAME', 'dalimil2');
744 3b343aea Tomáš Pašek
define('DB_CHARSET', 'utf8');
745
define('DB_USER', 'postgres');
746 f95d4d7c Tomáš Pašek
define('DB_PASSWORD', 'a');