Projekt

Obecné

Profil

Stáhnout (32.4 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 c058c65c Anděl Ondřej
        ini_set('memory_limit', '1024M');
9 3b343aea Tomáš Pašek
        try {
10
            $this->pdo = new PDO(
11
                "pgsql:host=".DB_HOST.";dbname=".DB_NAME,
12
                DB_USER, DB_PASSWORD, [
13
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
14
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
15
                ]
16
            );
17
        } catch (Exception $ex) { die($ex->getMessage()); }
18
    }
19
20
    // (B) CLOSE CONNECTION
21
    function __destruct(){
22
        if ($this->stmt!==null) { $this->stmt = null; }
23
        if ($this->pdo!==null) { $this->pdo = null; }
24
    }
25
26
    // (C) RUN A SELECT QUERY
27 bc83b448 Anděl Ondřej
    function select($sql, $params){
28 3b343aea Tomáš Pašek
        $result = false;
29
        try {
30
            $this->stmt = $this->pdo->prepare($sql);
31 2a99773b Milan Vacek
            if (array_key_exists("lemma", $params)) {
32
                $this->stmt->bindParam(':lemma',$params['lemma'], PDO::PARAM_STR);
33
            }
34
            if (array_key_exists("word", $params)) {
35
                $this->stmt->bindParam(':word',$params['word'], PDO::PARAM_STR);
36
            }
37
            if (array_key_exists("position1", $params)) {
38
                $this->stmt->bindParam(':position1',$params['position1'], PDO::PARAM_INT);
39
            }
40
            if (array_key_exists("position2", $params)) {
41
                $this->stmt->bindParam(':position2',$params['position2'], PDO::PARAM_INT);
42
            }
43
            if (array_key_exists("positiondetail", $params)) {
44
                $this->stmt->bindParam(':positiondetail',$params['positiondetail'], PDO::PARAM_INT);
45
            }
46
            if (array_key_exists("tag", $params)) {
47
                $this->stmt->bindParam(':tag',$params['tag'], PDO::PARAM_STR);
48
            }
49
            if (array_key_exists("tag", $params)) {
50
                $this->stmt->bindParam(':tag',$params['tag'], PDO::PARAM_STR);
51
            }
52 12de28ea Tomáš Pašek
            if (array_key_exists("description2", $params)) {
53
                $this->stmt->bindParam(':description2',$params['description2']);
54
            }
55 2a99773b Milan Vacek
            if (array_key_exists("manuscript", $params)) {
56
                for ($x = 0; $x < count($params["manuscript"]); $x += 1) {
57
                    $this->stmt->bindParam(':manuscript'.$x,$params["manuscript"][$x], PDO::PARAM_INT);
58
                }
59
            }
60
            $this->stmt->execute();
61 3b343aea Tomáš Pašek
            $result = $this->stmt->fetchAll();
62
            return $result;
63
        } catch (Exception $ex) {
64
            $this->error = $ex->getMessage();
65
            return false;
66
        }
67
    }
68 2a99773b Milan Vacek
69 b748cb5f Tomáš Pašek
    function getUser($username) {
70
        $this->stmt = $this->pdo->prepare('SELECT id, username, password, role FROM users WHERE username = :username');
71
        $this->stmt->bindParam(':username', $username);
72
        $this->stmt->execute();
73
        return $this->stmt->fetchAll();
74
    }
75
76
    function createUser($userDetails) {
77
        $this->stmt = $this->pdo->prepare('INSERT INTO users (username, password, role) VALUES (:username, :password, :role)');
78 77d279e5 Tomáš Pašek
        $password = password_hash($userDetails['password'], PASSWORD_DEFAULT);
79 b748cb5f Tomáš Pašek
        $this->stmt->bindParam(':username', $userDetails['username']);
80
        $this->stmt->bindParam(':password',$password);
81
        $this->stmt->bindParam(':role',$userDetails['role']);
82
        $this->stmt->execute();
83
        return $this->stmt->fetchAll();
84
    }
85
86 5aceeb68 Tomáš Pašek
    function createChangeRequest($request) {
87
        $this->stmt = $this->pdo->prepare('INSERT INTO changes (wordform_id, message) VALUES (:wordform_id, :message)');
88
        $this->stmt->bindParam(':wordform_id', $request['wordform_id']);
89
        $this->stmt->bindParam(':message',$request['message']);
90
        $this->stmt->execute();
91
        return $this->stmt->fetchAll();
92
    }
93
94
    function removeChangeRequest($requestId) {
95
        $this->stmt = $this->pdo->prepare('DELETE FROM changes WHERE id = :requestId');
96
        $this->stmt->bindParam(':requestId', $requestId);
97
        $this->stmt->execute();
98
        return $this->stmt->fetchAll();
99
    }
100
101
    function getChangeRequests() {
102
        $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');
103
        $this->stmt->execute();
104
        return $this->stmt->fetchAll();
105
    }
106
107 b748cb5f Tomáš Pašek
    function updateUserRole($userId, $role) {
108
        $this->stmt = $this->pdo->prepare("UPDATE users SET role = :role WHERE id = :userId");
109
        $this->stmt->bindParam(':role', $role);
110
        $this->stmt->bindParam(':userId',$userId);
111
        $this->stmt->execute();
112
        return $this->stmt->fetchAll();
113
    }
114
115
    function deleteUser($userId) {
116
        $this->stmt = $this->pdo->prepare("DELETE from users WHERE id = :userId");
117
        $this->stmt->bindParam(':userId',$userId);
118
        $this->stmt->execute();
119
        return $this->stmt->fetchAll();
120
    }
121
122 cd67c936 Anděl Ondřej
    function update(){
123 82f8d4ca Milan Vacek
        $query = "SELECT id FROM dd_lemma
124
                  WHERE lemma = :lemma;";
125
        $this->stmt = $this->pdo->prepare($query);
126
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
127
                $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
128
            }
129
        $this->stmt->execute();
130
        $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
131
132
        $query = "SELECT id FROM dd_tag
133
                  WHERE tag = :tag;";
134
        $this->stmt = $this->pdo->prepare($query);
135
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
136
                $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
137
            }
138
        $this->stmt->execute();
139
        $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
140 2a99773b Milan Vacek
141 cd67c936 Anděl Ondřej
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
142
        if($lemma == null){
143
            $query = "SELECT MAX(id) FROM dd_lemma;";
144
            $this->stmt = $this->pdo->prepare($query);
145
            $this->stmt->execute();
146
            $result = $this->stmt->fetchAll();
147
            $result[0]["max"]+=1;
148 2a99773b Milan Vacek
149 cd67c936 Anděl Ondřej
            $query = "INSERT INTO dd_lemma ( id, ";
150
            $values = "VALUES (" . $result[0]["max"] . ", ";
151
                if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
152
                    $query .= " lemma, ";
153
                    $values .= " :lemma, ";
154
                }
155
                if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
156
                    $query .= " pos ) ";
157
                    $values .= " :pos ); ";
158
                }
159 2a99773b Milan Vacek
160 cd67c936 Anděl Ondřej
            $query .= $values;
161
            $this->stmt = $this->pdo->prepare($query);
162
                if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
163
                    $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
164
                }
165
                if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
166
                    $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
167
                }
168
            $this->stmt->execute();
169 2a99773b Milan Vacek
170 cd67c936 Anděl Ondřej
            $query = "SELECT id FROM dd_lemma
171
                      WHERE lemma = :lemma;";
172
            $this->stmt = $this->pdo->prepare($query);
173
                if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
174
                    $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
175
                }
176
            $this->stmt->execute();
177
            $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
178
        }
179 2a99773b Milan Vacek
180 cd67c936 Anděl Ondřej
        $query = "UPDATE dd_lemma
181
                     SET ";
182 2a99773b Milan Vacek
183 cd67c936 Anděl Ondřej
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
184
            $query .= " lemma = :lemma , ";
185 2a99773b Milan Vacek
        }
186 cd67c936 Anděl Ondřej
        if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
187
            $query .= " pos = :pos  ";
188 2a99773b Milan Vacek
        }
189
190 cd67c936 Anděl Ondřej
        $query .= "WHERE ";
191
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
192
            $query .= " id = :lemma_id ;";
193 2a99773b Milan Vacek
        }
194
195
196
        $this->stmt = $this->pdo->prepare($query);
197 82f8d4ca Milan Vacek
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
198
            $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
199
        }
200 cd67c936 Anděl Ondřej
        if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
201
            $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
202
        }
203
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
204
            $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
205
        }
206 82f8d4ca Milan Vacek
        $this->stmt->execute();
207
208 cd67c936 Anděl Ondřej
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
209
        if($tag == null){
210
         $query = "SELECT MAX(id) FROM dd_tag;";
211
            $this->stmt = $this->pdo->prepare($query);
212
            $this->stmt->execute();
213
            $result = $this->stmt->fetchAll();
214
            $result[0]["max"]+=1;
215 2a99773b Milan Vacek
216
217 cd67c936 Anděl Ondřej
            $query = "INSERT INTO dd_tag (id, ";
218
            $values = "VALUES (" . $result[0]["max"] . ", ";
219 2a99773b Milan Vacek
220 cd67c936 Anděl Ondřej
                if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
221
                    $query .= " tag ) ";
222
                    $values .= " :tag ); ";
223
                }
224 2a99773b Milan Vacek
225 cd67c936 Anděl Ondřej
            $query .= $values;
226
            $this->stmt = $this->pdo->prepare($query);
227
                if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
228
                    $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
229
                }
230
            $this->stmt->execute();
231
232
            $query = "SELECT id FROM dd_tag
233
                      WHERE tag = :tag;";
234
            $this->stmt = $this->pdo->prepare($query);
235
                if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
236
                    $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
237
                }
238
            $this->stmt->execute();
239
            $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
240 82f8d4ca Milan Vacek
        }
241 b315cd75 Milan Vacek
242 cd67c936 Anděl Ondřej
        $query = "UPDATE dd_tag
243
                     SET ";
244 82f8d4ca Milan Vacek
245 cd67c936 Anděl Ondřej
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
246
            $query .= " tag = :tag  ";
247
        }
248 82f8d4ca Milan Vacek
249 cd67c936 Anděl Ondřej
        $query .= "WHERE ";
250
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
251
            $query .= " id = :tag_id ;";
252
        }
253 82f8d4ca Milan Vacek
254
255
        $this->stmt = $this->pdo->prepare($query);
256 cd67c936 Anděl Ondřej
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
257
            $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
258
        }
259
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
260
            $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
261
        }
262 82f8d4ca Milan Vacek
        $this->stmt->execute();
263 b315cd75 Milan Vacek
264 cd67c936 Anděl Ondřej
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
265
        $query = "UPDATE dd_wordform
266
                    SET ";
267 b315cd75 Milan Vacek
268 cd67c936 Anděl Ondřej
269
        if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
270
            $query .= " context = :context, ";
271 82f8d4ca Milan Vacek
        }
272 cd67c936 Anděl Ondřej
            $query .= " date = CURRENT_DATE,";
273 82f8d4ca Milan Vacek
274 cd67c936 Anděl Ondřej
        if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
275
            $query .= " description = :description, ";
276
        }
277
        if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
278
            $query .= " description2 = :description2, ";
279
        }
280
        if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
281
            $query .= " description3 = :description3, ";
282
        }
283
        if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
284
            $query .= " ending = :ending, ";
285
        }
286
        if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
287
            $query .= " finished = :finished, ";
288
        }
289
        if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
290
            $query .= " namedentity = :namedentity, ";
291
        }
292
        if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
293
            $query .= " position1 = :position1, ";
294
        }
295
        if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
296
            $query .= " position2 = :position2, ";
297
        }
298
        if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
299
            $query .= " positiondetail = :positiondetail ,";
300
        }
301
        if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
302
            $query .= " prefix = :prefix, ";
303
        }
304
        if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
305
            $query .= " suffix = :suffix, ";
306
        }
307
        if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
308
            $query .= " word = :word, ";
309
        }
310
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
311
            $query .= " lemma_id = :lemma_id, ";
312
        }
313 82f8d4ca Milan Vacek
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
314 cd67c936 Anděl Ondřej
            $query .= " tag_id = :tag_id ";
315 82f8d4ca Milan Vacek
        }
316 b315cd75 Milan Vacek
317 cd67c936 Anděl Ondřej
        $query .= " WHERE ";
318
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
319
            $query .= " id = :id ;";
320 b315cd75 Milan Vacek
        }
321
322 cd67c936 Anděl Ondřej
        $this->stmt = $this->pdo->prepare($query);
323
324
        if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
325
            $this->stmt->bindParam(':context', $_POST['context'], PDO::PARAM_STR);
326
        }
327 b315cd75 Milan Vacek
        if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
328 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
329 b315cd75 Milan Vacek
        }
330
        if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
331 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':description2', $_POST['description2'], PDO::PARAM_STR);
332 b315cd75 Milan Vacek
        }
333
        if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
334 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':description3', $_POST['description3'], PDO::PARAM_STR);
335 b315cd75 Milan Vacek
        }
336
        if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
337 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':ending', $_POST['ending'], PDO::PARAM_STR);
338 b315cd75 Milan Vacek
        }
339
        if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
340 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':finished', $_POST['finished']);
341 b315cd75 Milan Vacek
        }
342
        if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
343 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':namedentity', $_POST['namedentity'], PDO::PARAM_INT);
344 b315cd75 Milan Vacek
        }
345
        if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
346 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':position1', $_POST['position1'], PDO::PARAM_STR);
347 b315cd75 Milan Vacek
        }
348
        if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
349 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':position2', $_POST['position2'], PDO::PARAM_STR);
350 b315cd75 Milan Vacek
        }
351
        if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
352 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':positiondetail', $_POST['positiondetail'], PDO::PARAM_STR);
353 b315cd75 Milan Vacek
        }
354
        if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
355 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':prefix', $_POST['prefix'], PDO::PARAM_STR);
356 b315cd75 Milan Vacek
        }
357
        if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
358 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':suffix', $_POST['suffix'], PDO::PARAM_STR);
359 b315cd75 Milan Vacek
        }
360
        if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
361 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':word', $_POST['word'], PDO::PARAM_STR);
362 b315cd75 Milan Vacek
        }
363 82f8d4ca Milan Vacek
        if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
364 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':lemma_id', $lemma["id"], PDO::PARAM_INT);
365 b315cd75 Milan Vacek
        }
366 82f8d4ca Milan Vacek
        if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
367 cd67c936 Anděl Ondřej
            $this->stmt->bindParam(':tag_id', $tag["id"], PDO::PARAM_INT);
368
        }
369
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
370
            $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
371
        }
372
373
        $this->stmt->execute();
374
375
        // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
376
377 7dd550bc Tomáš Pašek
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
378 cd67c936 Anděl Ondřej
            $query = "SELECT * FROM dd_manuscript WHERE ";
379
            $query .= " wordform_id = :wordform_id ;";
380 b315cd75 Milan Vacek
        }
381
382
        $this->stmt = $this->pdo->prepare($query);
383 7dd550bc Tomáš Pašek
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
384
            $this->stmt->bindParam(':wordform_id', $_POST['id']);
385 cd67c936 Anděl Ondřej
        }
386
        $this->stmt->execute();
387
        $result = $this->stmt->fetchAll();
388
389
        $to_insert = [];
390
        $to_delete = [];
391
        $contained = [];
392
        $found = false;
393
        $integerIDs = [];
394
395
        foreach ($result as $res) {
396
            $integerIDs = array_map('intval', explode(',', $_POST['manuscript']));
397
            foreach ($integerIDs as $new_value){
398
                if($new_value == $res['manuscript']){
399
                    $found = true;
400
                    array_push($contained, $new_value);
401
                }
402
            }
403
            if($found == false){
404
                array_push($to_delete, $res);
405
            }
406
            $found = false;
407
        }
408
        $to_insert = array_diff($integerIDs, $contained);
409
410
        foreach ($to_delete as $id_to_delete){
411
            $query = "DELETE FROM dd_manuscript WHERE ";
412
            $query .= "manuscript = " . $id_to_delete['manuscript'] . " AND ";
413
            $query .= " wordform_id = :wordform_id ;";
414
415
416
            $this->stmt = $this->pdo->prepare($query);
417 7dd550bc Tomáš Pašek
            if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
418
                $this->stmt->bindParam(':wordform_id', $_POST['id'], PDO::PARAM_INT);
419 cd67c936 Anděl Ondřej
            }
420
            //
421
            $this->stmt->execute();
422
            var_dump($query);
423
        }
424
425
        foreach ($to_insert as $id_to_insert){
426
            $query = "INSERT INTO dd_manuscript VALUES ( ";
427
            $query .= " :wordform_id , ";
428
            $query .= " " . $id_to_insert . " ); ";
429
430
            $this->stmt = $this->pdo->prepare($query);
431 7dd550bc Tomáš Pašek
            if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
432
                $this->stmt->bindParam(':wordform_id', $_POST['id'], PDO::PARAM_INT);
433 cd67c936 Anděl Ondřej
            }
434
            $this->stmt->execute();
435
        }
436
    }
437 b315cd75 Milan Vacek
438 cd67c936 Anděl Ondřej
    function insert(){
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 b315cd75 Milan Vacek
            }
446 cd67c936 Anděl Ondřej
        $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 8ef8fa46 Tomáš Pašek
         }
546 cd67c936 Anděl Ondřej
                $query .= " date, ";
547
                $values .=  " CURRENT_DATE, ";
548
549 b315cd75 Milan Vacek
            if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
550 cd67c936 Anděl Ondřej
                $query .= " description, ";
551
                $values .= " :description, ";
552 b315cd75 Milan Vacek
            }
553
            if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
554 cd67c936 Anděl Ondřej
                $query .= " description2, ";
555
                $values .= " :description2, ";
556 b315cd75 Milan Vacek
            }
557
            if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
558 cd67c936 Anděl Ondřej
                $query .= " description3, ";
559
                $values .= " :description3, ";
560 b315cd75 Milan Vacek
            }
561
            if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
562 cd67c936 Anděl Ondřej
                $query .= " ending, ";
563
                $values .= " :ending, ";
564 b315cd75 Milan Vacek
            }
565
            if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
566 cd67c936 Anděl Ondřej
                $query .= " finished, ";
567
                $values .= " :finished, ";
568 b315cd75 Milan Vacek
            }
569
            if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
570 cd67c936 Anděl Ondřej
                $query .= " namedentity, ";
571
                $values .= " :namedentity, ";
572 b315cd75 Milan Vacek
            }
573
            if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
574 cd67c936 Anděl Ondřej
                $query .= " position1, ";
575
                $values .= " :position1, ";
576 b315cd75 Milan Vacek
            }
577
            if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
578 cd67c936 Anděl Ondřej
                $query .= " position2, ";
579
                $values .= " :position2, ";
580 b315cd75 Milan Vacek
            }
581
            if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
582 cd67c936 Anděl Ondřej
                $query .= " positiondetail, ";
583
                $values .= " :positiondetail, ";
584 b315cd75 Milan Vacek
            }
585
            if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
586 cd67c936 Anděl Ondřej
                $query .= " prefix, ";
587
                $values .= " :prefix, ";
588 b315cd75 Milan Vacek
            }
589
            if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
590 cd67c936 Anděl Ondřej
                $query .= " suffix, ";
591
                $values .= " :suffix, ";
592 b315cd75 Milan Vacek
            }
593
            if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
594 cd67c936 Anděl Ondřej
                $query .= " word, ";
595
                $values .= " :word, ";
596 b315cd75 Milan Vacek
            }
597 82f8d4ca Milan Vacek
            if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
598 cd67c936 Anděl Ondřej
                $query .= " lemma_id, ";
599
                $values .= " :lemma_id, ";
600 b315cd75 Milan Vacek
            }
601 82f8d4ca Milan Vacek
            if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
602 cd67c936 Anděl Ondřej
                $query .= " tag_id ) ";
603
                $values .= " :tag_id ); ";
604 b315cd75 Milan Vacek
            }
605
606 cd67c936 Anděl Ondřej
            $query .= $values;
607
            $this->stmt = $this->pdo->prepare($query);
608 b315cd75 Milan Vacek
609 cd67c936 Anděl Ondřej
                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 b315cd75 Milan Vacek
655 cd67c936 Anděl Ondřej
                $this->stmt->execute();
656 b315cd75 Milan Vacek
657
658 cd67c936 Anděl Ondřej
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
659 b315cd75 Milan Vacek
660 8ef8fa46 Tomáš Pašek
                $to_insert = array_map('intval', explode(',', $_POST['manuscript']));
661 cd67c936 Anděl Ondřej
                foreach ($to_insert as $id_to_insert){
662
                    $query = "INSERT INTO dd_manuscript VALUES ( ";
663
                    $query .= " :wordform_id , ";
664
                    $query .= " " . $id_to_insert . " ); ";
665
666
                    $this->stmt = $this->pdo->prepare($query);
667 8ef8fa46 Tomáš Pašek
                    $this->stmt->bindParam(':wordform_id', $result[0]["max"], PDO::PARAM_INT);
668 cd67c936 Anděl Ondřej
                    $this->stmt->execute();
669
                }
670
        }
671 8fc55000 Milan Vacek
672 cd67c936 Anděl Ondřej
673 79c140ee Milan Vacek
function remove(){
674 53a9c7e2 Milan Vacek
        $query = "SELECT lemma_id FROM dd_wordform
675
                WHERE id = :id;";
676
        $this->stmt = $this->pdo->prepare($query);
677 f95d4d7c Tomáš Pašek
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
678 53a9c7e2 Milan Vacek
            $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_STR);
679 f95d4d7c Tomáš Pašek
        }
680 53a9c7e2 Milan Vacek
        $this->stmt->execute();
681
        $lemma = $this->stmt->fetch(PDO::FETCH_ASSOC);
682 3a071bba Anděl Ondřej
683 53a9c7e2 Milan Vacek
        $query = "SELECT tag_id FROM dd_wordform
684
                WHERE id = :id;";
685
        $this->stmt = $this->pdo->prepare($query);
686
        if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
687
            $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_STR);
688
        }
689
        $this->stmt->execute();
690
        $tag = $this->stmt->fetch(PDO::FETCH_ASSOC);
691
692 6c152ae0 Ondrej Drtina
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
693
    if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
694
        $query = "DELETE FROM dd_manuscript WHERE";
695
        $query .= " wordform_id = :wordform_id ;";
696
        $this->stmt = $this->pdo->prepare($query);
697
        $this->stmt->bindParam(':wordform_id', $_POST['id'], PDO::PARAM_INT);
698
        $this->stmt->execute();
699
    }
700 79c140ee Milan Vacek
701 53a9c7e2 Milan Vacek
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
702
    if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
703
        $query = "DELETE FROM dd_wordform WHERE";
704
        $query .= " id = :id ;";
705
        $this->stmt = $this->pdo->prepare($query);
706
        $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
707
        $this->stmt->execute();
708
    }
709 79c140ee Milan Vacek
710 53a9c7e2 Milan Vacek
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
711
        $query = "SELECT id FROM dd_wordform WHERE lemma_id = :lemma_id;";
712
        $this->stmt = $this->pdo->prepare($query);
713
        $this->stmt->bindParam(':lemma_id', $lemma["lemma_id"], PDO::PARAM_INT);
714
        $this->stmt->execute();
715
        $result = $this->stmt->fetchAll();
716
        if(sizeof($result)==0){
717 5418813c Milan Vacek
            $query = "DELETE FROM dd_lemma WHERE";
718 79c140ee Milan Vacek
            $query .= " id = :lemma_id ;";
719 5418813c Milan Vacek
            $this->stmt = $this->pdo->prepare($query);
720 53a9c7e2 Milan Vacek
            $this->stmt->bindParam(':lemma_id', $lemma["lemma_id"], PDO::PARAM_INT);
721 5418813c Milan Vacek
            $this->stmt->execute();
722 53a9c7e2 Milan Vacek
        }
723 a4f82b78 Milan Vacek
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
724 53a9c7e2 Milan Vacek
        $query = "SELECT id FROM dd_wordform WHERE tag_id = :tag_id;";
725
        $this->stmt = $this->pdo->prepare($query);
726
        $this->stmt->bindParam(':tag_id', $tag["tag_id"], PDO::PARAM_INT);
727
        $this->stmt->execute();
728
        $result = $this->stmt->fetchAll();
729
        if(sizeof($result)==0){
730 a4f82b78 Milan Vacek
            $query = "DELETE FROM dd_tag WHERE";
731
            $query .= " id = :tag_id ;";
732
            $this->stmt = $this->pdo->prepare($query);
733 53a9c7e2 Milan Vacek
            $this->stmt->bindParam(':tag_id', $tag["tag_id"], PDO::PARAM_INT);
734 f95d4d7c Tomáš Pašek
            $this->stmt->execute();
735 53a9c7e2 Milan Vacek
        }else{
736
            echo "fail";
737 f95d4d7c Tomáš Pašek
        }
738 3a071bba Anděl Ondřej
    }
739 f95d4d7c Tomáš Pašek
740 b315cd75 Milan Vacek
}
741 3b343aea Tomáš Pašek
742
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
743
define('DB_HOST', 'localhost');
744 5aceeb68 Tomáš Pašek
define('DB_NAME', 'dalimil2');
745 3b343aea Tomáš Pašek
define('DB_CHARSET', 'utf8');
746
define('DB_USER', 'postgres');
747 f95d4d7c Tomáš Pašek
define('DB_PASSWORD', 'a');