-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.php
More file actions
290 lines (251 loc) · 13.2 KB
/
form.php
File metadata and controls
290 lines (251 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* DDb - Copyright 2013 Yosko (www.yosko.net)
*
* This file is part of DDb.
*
* DDb is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DDb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with DDb. If not, see <http://www.gnu.org/licenses/>.
*
*/
include_once 'inc/functions.php';
initDDb($db, $settings, $tpl, $user);
if($user['isLoggedIn']) {
//if this is a modification of an existing dream
$dreamEdition = false;
if( isset($_GET['id']) ) {
$dreamId = intval($_GET['id']);
//if the current user is the author of the dream or is an admin
if(isAuthor($user['id'], $dreamId) || $user['role'] == 'admin') {
//the user can edit the dream
$dreamEdition = true;
} else {
//the user isn't allowed to edit this dream
header('Location: dream.php?id='.$dreamId);
exit;
}
}
//if the form was posted, save the new/existing dream to ddb
if( isset($_POST['text']) ) {
//sanitize entries
$values['newDreamer'] = htmlspecialchars(trim($_POST['newdreamer']));
$values['dreamerId'] = intval(isset($_POST['dreamer'])?$_POST['dreamer']:-1);
//user input is dd/mm/yyyy, but we need to use yyyy-mm-dd in the database
$dateArray = explode('/',$_POST['date']);
$values['date'] = "$dateArray[2]-$dateArray[1]-$dateArray[0]";
$values['title'] = htmlspecialchars(trim($_POST['title']));
$values['characters'] = htmlspecialchars(trim($_POST['characters']));
$values['place'] = htmlspecialchars(trim($_POST['place']));
$values['text'] = htmlspecialchars(trim($_POST['text']));
$values['pointOfVue'] = htmlspecialchars(trim($_POST['pointofvue']));
$values['funFacts'] = htmlspecialchars(trim($_POST['funfacts']));
$values['feelings'] = htmlspecialchars(trim($_POST['feelings']));
$values['tagList'] = htmlspecialchars(trim($_POST['tags']));
$values['status'] = isset($_POST['submitPublish']);
$errors['noDreamer'] = (!isset($_POST['dreamer']) && empty($_POST['newdreamer']));
if($errors['noDreamer'] === false) {
//1- save the new dreamer if set
if( !empty($values['newDreamer']) ) {
$qry = $db->prepare(
'INSERT INTO ddb_dreamer (dreamerName) VALUES (:name)');
$qry->bindParam(':name', $values['newDreamer'], PDO::PARAM_STR);
$qry->execute();
$values['dreamerId'] = $db->lastInsertId();
//link the newly created to the current user if he/she is not admin
if($user['role'] != 'admin') {
$qry = $db->prepare(
'INSERT INTO ddb_user_dreamer (dreamerId_FK, userId_FK) VALUES (:dreamerId, :userId)');
$qry->bindParam(':dreamerId', $values['dreamerId'], PDO::PARAM_STR);
$qry->bindParam(':userId', $user['id'], PDO::PARAM_STR);
$qry->execute();
}
}
//2- save the dream with the right dreamer id
if( $dreamEdition == true ) {
//if dream wasn't published yet update the publication date
$qryDream = $db->prepare(
"SELECT dreamStatus as status FROM ddb_dream WHERE dreamId = :dreamId");
$qryDream->bindParam(':dreamId', $dreamId, PDO::PARAM_INT);
$qryDream->execute();
$previousStatus = $qryDream->fetchColumn();
if($previousStatus == false && $values['status'] == true) {
$qry = $db->prepare(
'UPDATE ddb_dream SET dreamPublication = current_timestamp'
. ' WHERE dreamId = :dreamId');
$qry->bindParam(':dreamId', $dreamId, PDO::PARAM_INT);
$qry->execute();
}
$qry = $db->prepare(
'UPDATE ddb_dream SET dreamerId_FK = :dreamerId, dreamDate = :dreamDate, dreamTitle = :dreamTitle'
. ', dreamCharacters = :dreamCharacters, dreamPlace = :dreamPlace, dreamText = :dreamText'
. ', dreamPointOfVue = :dreamPointOfVue, dreamFunFacts = :dreamFunFacts, dreamFeelings = :dreamFeelings'
. ', dreamStatus = :dreamStatus'
. ' WHERE dreamId = :dreamId');
$qry->bindParam(':dreamerId', $values['dreamerId'], PDO::PARAM_INT);
$qry->bindParam(':dreamDate', $values['date'], PDO::PARAM_STR);
$qry->bindParam(':dreamTitle', $values['title'], PDO::PARAM_STR);
$qry->bindParam(':dreamCharacters', $values['characters'], PDO::PARAM_STR);
$qry->bindParam(':dreamPlace', $values['place'], PDO::PARAM_STR);
$qry->bindParam(':dreamText', $values['text'], PDO::PARAM_STR);
$qry->bindParam(':dreamPointOfVue', $values['pointOfVue'], PDO::PARAM_STR);
$qry->bindParam(':dreamFunFacts', $values['funFacts'], PDO::PARAM_STR);
$qry->bindParam(':dreamFeelings', $values['feelings'], PDO::PARAM_STR);
$qry->bindParam(':dreamStatus', $values['status'], PDO::PARAM_INT);
$qry->bindParam(':dreamId', $dreamId, PDO::PARAM_INT);
$qry->execute();
//delete all tags attached to the dream (will be recreated)
$qry = $db->prepare(
'DELETE FROM ddb_dream_tag WHERE dreamId_FK = :dreamId');
$qry->bindParam(':dreamId', $dreamId, PDO::PARAM_INT);
$qry->execute();
} else {
$qry = $db->prepare(
'INSERT INTO ddb_dream (dreamerId_FK, dreamDate, dreamTitle, dreamCharacters, dreamPlace, dreamText, dreamPointOfVue, dreamFunFacts, dreamFeelings, userId_FK, dreamStatus)'
. ' VALUES (:dreamerId, :dreamDate, :dreamTitle, :dreamCharacters, :dreamPlace, :dreamText, :dreamPointOfVue, :dreamFunFacts, :dreamFeelings, :userId, :dreamStatus)');
$qry->bindParam(':dreamerId', $values['dreamerId'], PDO::PARAM_INT);
$qry->bindParam(':dreamDate', $values['date'], PDO::PARAM_STR);
$qry->bindParam(':dreamTitle', $values['title'], PDO::PARAM_STR);
$qry->bindParam(':dreamCharacters', $values['characters'], PDO::PARAM_STR);
$qry->bindParam(':dreamPlace', $values['place'], PDO::PARAM_STR);
$qry->bindParam(':dreamText', $values['text'], PDO::PARAM_STR);
$qry->bindParam(':dreamPointOfVue', $values['pointOfVue'], PDO::PARAM_STR);
$qry->bindParam(':dreamFunFacts', $values['funFacts'], PDO::PARAM_STR);
$qry->bindParam(':dreamFeelings', $values['feelings'], PDO::PARAM_STR);
$qry->bindParam(':dreamStatus', $values['status'], PDO::PARAM_INT);
$qry->bindParam(':userId', $user['id'], PDO::PARAM_INT);
$qry->execute();
$dreamId = $db->lastInsertId();
}
$tags = explode(',', $values['tagList']);
if( count($tags)>0 ) {
//3- save new tags to the tag table
$tagIds = array();
for( $i=0; $i < count($tags); $i++ ) {
$tag = trim($tags[$i]);
if( strlen($tag) > 0 ) {
$qry = $db->prepare(
'SELECT tagId FROM ddb_tag WHERE tagName = :name');
$qry->bindParam(':name', $tag, PDO::PARAM_STR);
$qry->execute();
if( !($tagId = $qry->fetchColumn()) ) {
$qry = $db->prepare(
'INSERT INTO ddb_tag (tagName) VALUES (:name)');
$qry->bindParam(':name', $tag, PDO::PARAM_STR);
$qry->execute();
$tagId = $db->lastInsertId();
}
$tagIds[] = intval($tagId);
}
}
//4- save the dream tags
foreach( $tagIds as $tagId ) {
$qry = $db->prepare(
'INSERT INTO ddb_dream_tag (dreamId_FK, tagId_FK) VALUES (:dreamId, :tagId)');
$qry->bindParam(':dreamId', $dreamId, PDO::PARAM_INT);
$qry->bindParam(':tagId', $tagId, PDO::PARAM_INT);
$qry->execute();
}
}
//go to the dream page
header('Location: dream.php?id='.$dreamId);
exit;
}
//keep values and show errors
$values['date'] = $_POST['date'];
$tpl->assign( "errors", $errors );
$tpl->assign( "values", $values );
}
//load the dream to edit
if( $dreamEdition == true && !isset($values) ) {
$values = array();
$values['id'] = $_GET['id'];
//get dream informations
$qryDream = $db->prepare(
"SELECT a.dreamerName, a.dreamerId, strftime('%d/%m/%Y', d.dreamDate) AS dreamDate, d.dreamTitle, d.dreamCharacters, d.dreamPlace"
.", d.dreamText, d.dreamPointOfVue, d.dreamFunFacts, d.dreamFeelings, d.dreamStatus"
." FROM ddb_dream d LEFT JOIN ddb_dreamer a on d.dreamerId_FK = a.dreamerId"
." WHERE dreamId = :dreamId");
$qryDream->bindParam(':dreamId', $values['id'], PDO::PARAM_INT);
$qryDream->execute();
$qryDream->bindColumn('dreamerName', $values['dreamerName']);
$qryDream->bindColumn('dreamerId', $values['dreamerId']);
$qryDream->bindColumn('dreamDate', $values['date']);
$qryDream->bindColumn('dreamTitle', $values['title']);
$qryDream->bindColumn('dreamCharacters', $values['characters']);
$qryDream->bindColumn('dreamPlace', $values['place']);
$qryDream->bindColumn('dreamText', $values['text']);
$qryDream->bindColumn('dreamPointOfVue', $values['pointOfVue']);
$qryDream->bindColumn('dreamFunFacts', $values['funFacts']);
$qryDream->bindColumn('dreamFeelings', $values['feelings']);
$qryDream->bindColumn('dreamStatus', $values['status']);
//read the first line to feed the bind variables
$row = $qryDream->fetch(PDO::FETCH_BOUND);
$qry = $db->prepare(
"SELECT t.tagName FROM ddb_dream_tag dt INNER JOIN ddb_tag t on dt.tagId_FK = t.tagId"
." WHERE dt.dreamId_FK = :dreamId ORDER BY t.tagName");
$qry->bindParam(':dreamId', $values['id'], PDO::PARAM_INT);
$qry->execute();
$tagList = '';
while ( $row = $qry->fetch(PDO::FETCH_ASSOC) ) {
$tagList .= $row['tagName'].', ';
}
$values['tagList'] = $tagList;
}
//feed the dreamer accessible dreamers for the current user
if($user['role'] == 'admin') {
$qry = $db->prepare('SELECT dr.dreamerId, dr.dreamerName'
.' FROM ddb_dreamer dr'
.' ORDER BY dr.dreamerName ASC');
} else {
$qry = $db->prepare('SELECT dr.dreamerId, dr.dreamerName'
.' FROM ddb_dreamer dr'
.' INNER JOIN ddb_user_dreamer ud ON ud.dreamerId_FK = dr.dreamerId'
.' WHERE ud.userId_FK = :userId'
.' ORDER BY dr.dreamerName ASC');
$qry->bindParam(':userId', $user['id'], PDO::PARAM_INT);
}
$qry->execute();
$dreamers = $qry->fetchAll(PDO::FETCH_ASSOC);
//try to guess which dreamer to select by default
if(!empty($dreamers)) {
$levenshtein = array();
for($i = 0; $i < count($dreamers); $i++) {
$dreamers[$i]['selected'] = false;
$levenshtein[$i] = levenshtein(strtolower($dreamers[$i]['dreamerName']), strtolower($user['login']));
}
//select the one with the lowest Levenshtein distance
$keys = array_keys($levenshtein, min($levenshtein));
$dreamers[$keys[0]]['selected'] = true;
}
//feed the tag list with existing tags
$qry = $db->prepare(
"SELECT tagName FROM ddb_tag ORDER BY tagName ASC");
$qry->execute();
while ($row = $qry->fetch(PDO::FETCH_ASSOC)) {
$tags[] = $row['tagName'];
}
if(isset($values)) {
$tpl->assign( 'dream', $values );
}
$tpl->assign( 'dreamers', $dreamers );
$tpl->assign( 'js', true );
if(empty($tags)) {
$tagList = '';
$tags = array();
}
$tpl->assign( 'editButtons', $dreamEdition );
$tpl->assign( 'tagList', json_encode($tags) );
$tpl->assign( 'today', date('d/m/Y') );
$tpl->draw( 'form' );
}
?>