Projekt

Obecné

Profil

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

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

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

    
68
function update(){
69

    
70
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_wordform |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
71

    
72
    $query = "UPDATE dd_wordform
73
                SET ";
74

    
75

    
76
    if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
77
        $query .= " context = :context, ";
78
    }
79
        $query .= " date = CURRENT_DATE,";
80

    
81
    if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
82
        $query .= " description = :description, ";
83
    }
84
    if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
85
        $query .= " description2 = :description2, ";
86
    }
87
    if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
88
        $query .= " description3 = :description3, ";
89
    }
90
    if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
91
        $query .= " ending = :ending, ";
92
    }
93
    if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
94
        $query .= " finished = :finished, ";
95
    }
96
    if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
97
        $query .= " namedentity = :namedentity, ";
98
    }
99
    if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
100
        $query .= " position1 = :position1, ";
101
    }
102
    if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
103
        $query .= " position2 = :position2, ";
104
    }
105
    if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
106
        $query .= " positiondetail = :positiondetail ,";
107
    }
108
    if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
109
        $query .= " prefix = :prefix, ";
110
    }
111
    if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
112
        $query .= " suffix = :suffix, ";
113
    }
114
    if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
115
        $query .= " word = :word, ";
116
    }
117
    if (array_key_exists("lemma_id", $_POST) && $_POST['lemma_id'] != "") {
118
        $query .= " lemma_id = :lemma_id, ";
119
    }
120
    if (array_key_exists("tag_id", $_POST) && $_POST['tag_id'] != "") {
121
        $query .= " tag_id = :tag_id ";
122
    }
123

    
124
    $query .= " WHERE ";
125
    if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
126
        $query .= " id = :id ;";
127
    }
128

    
129
    $this->stmt = $this->pdo->prepare($query);
130

    
131
    if (array_key_exists("context", $_POST) && $_POST['context'] != "") {
132
        $this->stmt->bindParam(':context', $_POST['context'], PDO::PARAM_STR);
133
    }
134
    if (array_key_exists("description", $_POST) && $_POST['description'] != "") {
135
        $this->stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
136
    }
137
    if (array_key_exists("description2", $_POST) && $_POST['description2'] != "") {
138
        $this->stmt->bindParam(':description2', $_POST['description2'], PDO::PARAM_STR);
139
    }
140
    if (array_key_exists("description3", $_POST) && $_POST['description3'] != "") {
141
        $this->stmt->bindParam(':description3', $_POST['description3'], PDO::PARAM_STR);
142
    }
143
    if (array_key_exists("ending", $_POST) && $_POST['ending'] != "") {
144
        $this->stmt->bindParam(':ending', $_POST['ending'], PDO::PARAM_STR);
145
    }
146
    if (array_key_exists("finished", $_POST) && $_POST['finished'] != "") {
147
        $this->stmt->bindParam(':finished', $_POST['finished']);
148
    }
149
    if (array_key_exists("namedentity", $_POST) && $_POST['namedentity'] != "") {
150
        $this->stmt->bindParam(':namedentity', $_POST['namedentity'], PDO::PARAM_INT);
151
    }
152
    if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
153
        $this->stmt->bindParam(':position1', $_POST['position1'], PDO::PARAM_STR);
154
    }
155
    if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
156
        $this->stmt->bindParam(':position2', $_POST['position2'], PDO::PARAM_STR);
157
    }
158
    if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
159
        $this->stmt->bindParam(':positiondetail', $_POST['positiondetail'], PDO::PARAM_STR);
160
    }
161
    if (array_key_exists("prefix", $_POST) && $_POST['prefix'] != "") {
162
        $this->stmt->bindParam(':prefix', $_POST['prefix'], PDO::PARAM_STR);
163
    }
164
    if (array_key_exists("suffix", $_POST) && $_POST['suffix'] != "") {
165
        $this->stmt->bindParam(':suffix', $_POST['suffix'], PDO::PARAM_STR);
166
    }
167
    if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
168
        $this->stmt->bindParam(':word', $_POST['word'], PDO::PARAM_STR);
169
    }
170
    if (array_key_exists("lemma_id", $_POST) && $_POST['lemma_id'] != "") {
171
        $this->stmt->bindParam(':lemma_id', $_POST['lemma_id'], PDO::PARAM_INT);
172
    }
173
    if (array_key_exists("tag_id", $_POST) && $_POST['tag_id'] != "") {
174
        $this->stmt->bindParam(':tag_id', $_POST['tag_id'], PDO::PARAM_INT);
175
    }
176
    if (array_key_exists("id", $_POST) && $_POST['id'] != "") {
177
        $this->stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
178
    }
179

    
180
    $this->stmt->execute();
181

    
182
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_lemma |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
183

    
184
    $query = "UPDATE dd_lemma
185
                 SET ";
186

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

    
194
    $query .= "WHERE ";
195
    if (array_key_exists("lemma_id", $_POST) && $_POST['lemma_id'] != "") {
196
        $query .= " id = :lemma_id ;";
197
    }
198

    
199

    
200
    $this->stmt = $this->pdo->prepare($query);
201
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
202
        $this->stmt->bindParam(':lemma', $_POST['lemma'], PDO::PARAM_STR);
203
    }
204
    if (array_key_exists("pos", $_POST) && $_POST['pos'] != "") {
205
        $this->stmt->bindParam(':pos', $_POST['pos'], PDO::PARAM_INT);
206
    }
207
    if (array_key_exists("lemma_id", $_POST) && $_POST['lemma_id'] != "") {
208
        $this->stmt->bindParam(':lemma_id', $_POST['lemma_id'], PDO::PARAM_INT);
209
    }
210
    $this->stmt->execute();
211

    
212
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_manuscript |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
213

    
214
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
215
        $query = "SELECT * FROM dd_manuscript WHERE ";
216
        $query .= " wordform_id = :wordform_id ;";
217
    }
218

    
219
    $this->stmt = $this->pdo->prepare($query);
220
    if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
221
        $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
222
    }
223
    $this->stmt->execute();
224
    $result = $this->stmt->fetchAll();
225

    
226
    $to_insert = [];
227
    $to_delete = [];
228
    $contained = [];
229
    $found = false;
230

    
231
    foreach ($result as $res) {
232
        $integerIDs = array_map('intval', explode(',', $_POST['manuscript']));
233
        foreach ($integerIDs as $new_value){
234
            if($new_value == $res['manuscript']){
235
                $found = true;
236
                array_push($contained, $new_value);
237
            }
238
        }
239
        if($found == false){
240
            array_push($to_delete, $res);
241
        }
242
        $found = false;
243
    }
244
    $to_insert = array_diff($integerIDs, $contained);
245

    
246
    foreach ($to_delete as $id_to_delete){
247
        $query = "DELETE FROM dd_manuscript WHERE ";
248
        $query .= "manuscript = " . $id_to_delete['manuscript'] . " AND ";
249
        $query .= " wordform_id = :wordform_id ;";
250

    
251

    
252
        $this->stmt = $this->pdo->prepare($query);
253
        if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
254
            $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
255
        }
256
        //
257
        $this->stmt->execute();
258
        var_dump($query);
259
    }
260

    
261
    foreach ($to_insert as $id_to_insert){
262
        $query = "INSERT INTO dd_manuscript VALUES ( ";
263
        $query .= " :wordform_id , ";
264
        $query .= " " . $id_to_insert . " ); ";
265

    
266
        $this->stmt = $this->pdo->prepare($query);
267
        if (array_key_exists("wordform_id", $_POST) && $_POST['wordform_id'] != "") {
268
            $this->stmt->bindParam(':wordform_id', $_POST['wordform_id'], PDO::PARAM_INT);
269
        }
270
        $this->stmt->execute();
271
    }
272

    
273
    // ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Tabulka dd_tag |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
274

    
275
    $query = "UPDATE dd_tag
276
                 SET ";
277

    
278
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
279
        $query .= " tag = :tag  ";
280
    }
281

    
282
    $query .= "WHERE ";
283
    if (array_key_exists("tag_id", $_POST) && $_POST['tag_id'] != "") {
284
        $query .= " id = :tag_id ;";
285
    }
286

    
287

    
288
    $this->stmt = $this->pdo->prepare($query);
289
    if (array_key_exists("tag", $_POST) && $_POST['tag'] != "") {
290
        $this->stmt->bindParam(':tag', $_POST['tag'], PDO::PARAM_STR);
291
    }
292
    if (array_key_exists("tag_id", $_POST) && $_POST['tag_id'] != "") {
293
        $this->stmt->bindParam(':tag_id', $_POST['tag_id'], PDO::PARAM_INT);
294
    }
295
    $this->stmt->execute();
296
}}
297

    
298
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
299
define('DB_HOST', 'localhost');
300
define('DB_NAME', 'dalimil1');
301
define('DB_CHARSET', 'utf8');
302
define('DB_USER', 'postgres');
303
define('DB_PASSWORD', 'a');
(1-1/4)