|
| 1 | +<?php |
| 2 | + |
| 3 | +session_start(); |
| 4 | + |
| 5 | +include 'helpers/login_helper.php'; |
| 6 | + |
| 7 | +include 'helpers/pdo_helper.php'; |
| 8 | +include 'helpers/validation_helper.php'; |
| 9 | + |
| 10 | +// If the user requested logout go back to index.php |
| 11 | +if ( isset($_POST['cancel']) ) { |
| 12 | + header('Location: index.php'); |
| 13 | + return; |
| 14 | +} |
| 15 | + |
| 16 | +$status = false; |
| 17 | + |
| 18 | +if ( isset($_SESSION['status']) ) { |
| 19 | + $status = htmlentities($_SESSION['status']); |
| 20 | + $status_color = htmlentities($_SESSION['color']); |
| 21 | + |
| 22 | + unset($_SESSION['status']); |
| 23 | + unset($_SESSION['color']); |
| 24 | +} |
| 25 | + |
| 26 | +try |
| 27 | +{ |
| 28 | + $pdo = pdoHelper(); |
| 29 | +} |
| 30 | +catch(PDOException $e) |
| 31 | +{ |
| 32 | + echo "Connection failed: " . $e->getMessage(); |
| 33 | + die(); |
| 34 | +} |
| 35 | + |
| 36 | +$name = htmlentities($_SESSION['name']); |
| 37 | + |
| 38 | +$_SESSION['color'] = 'red'; |
| 39 | + |
| 40 | +// Check to see if we have some POST data, if we do process it |
| 41 | +if (isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email']) && isset($_POST['headline']) && isset($_POST['summary'])) |
| 42 | +{ |
| 43 | + |
| 44 | + if(!validationHelper()) |
| 45 | + { |
| 46 | + header("Location: add.php"); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + $first_name = htmlentities($_POST['first_name']); |
| 51 | + $last_name = htmlentities($_POST['last_name']); |
| 52 | + $email = htmlentities($_POST['email']); |
| 53 | + $headline = htmlentities($_POST['headline']); |
| 54 | + $summary = htmlentities($_POST['summary']); |
| 55 | + |
| 56 | + $stmt = $pdo->prepare(" |
| 57 | + INSERT INTO profile (user_id, first_name, last_name, email, headline, summary) |
| 58 | + VALUES (:user_id, :first_name, :last_name, :email, :headline, :summary) |
| 59 | + "); |
| 60 | + |
| 61 | + $stmt->execute([ |
| 62 | + ':user_id' => $_SESSION['user_id'], |
| 63 | + ':first_name' => $first_name, |
| 64 | + ':last_name' => $last_name, |
| 65 | + ':email' => $email, |
| 66 | + ':headline' => $headline, |
| 67 | + ':summary' => $summary, |
| 68 | + ]); |
| 69 | + |
| 70 | + $profile_id = $pdo->lastInsertId(); |
| 71 | + |
| 72 | + $rank = 1; |
| 73 | + |
| 74 | + for ($i=1; $i<=9; $i++) |
| 75 | + { |
| 76 | + if ( ! isset($_POST['year'.$i]) ) continue; |
| 77 | + if ( ! isset($_POST['desc'.$i]) ) continue; |
| 78 | + |
| 79 | + $year = htmlentities($_POST['year'.$i]); |
| 80 | + $desc = htmlentities($_POST['desc'.$i]); |
| 81 | + |
| 82 | + $stmt = $pdo->prepare(" |
| 83 | + INSERT INTO position (profile_id, rank, year, description) |
| 84 | + VALUES (:profile_id, :rank, :year, :description) |
| 85 | + "); |
| 86 | + |
| 87 | + $stmt->execute([ |
| 88 | + ':profile_id' => $profile_id, |
| 89 | + ':rank' => $rank, |
| 90 | + ':year' => $year, |
| 91 | + ':description' => $desc, |
| 92 | + ]); |
| 93 | + |
| 94 | + $rank++; |
| 95 | + } |
| 96 | + |
| 97 | + $rank = 1; |
| 98 | + |
| 99 | + for ($i=1; $i<=9; $i++) |
| 100 | + { |
| 101 | + if ( ! isset($_POST['edu_year'.$i]) ) continue; |
| 102 | + if ( ! isset($_POST['edu_school'.$i]) ) continue; |
| 103 | + |
| 104 | + $edu_year = htmlentities($_POST['edu_year'.$i]); |
| 105 | + $edu_school = htmlentities($_POST['edu_school'.$i]); |
| 106 | + |
| 107 | + $stmt = $pdo->prepare(" |
| 108 | + SELECT * FROM institution |
| 109 | + WHERE name = :edu_school LIMIT 1 |
| 110 | + "); |
| 111 | + |
| 112 | + $stmt->execute([ |
| 113 | + ':edu_school' => $edu_school, |
| 114 | + ]); |
| 115 | + |
| 116 | + $result = $stmt->fetch(PDO::FETCH_OBJ); |
| 117 | + |
| 118 | + if ($result) |
| 119 | + { |
| 120 | + $institution_id = $result->institution_id; |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + $stmt = $pdo->prepare(" |
| 125 | + INSERT INTO institution (name) |
| 126 | + VALUES (:name) |
| 127 | + "); |
| 128 | + |
| 129 | + $stmt->execute([ |
| 130 | + ':name' => $edu_school, |
| 131 | + ]); |
| 132 | + |
| 133 | + $institution_id = $pdo->lastInsertId(); |
| 134 | + } |
| 135 | + |
| 136 | + $stmt = $pdo->prepare(" |
| 137 | + INSERT INTO education (profile_id, institution_id, rank, year) |
| 138 | + VALUES (:profile_id, :institution_id, :rank, :year) |
| 139 | + "); |
| 140 | + |
| 141 | + $stmt->execute([ |
| 142 | + ':profile_id' => $profile_id, |
| 143 | + ':institution_id' => $institution_id, |
| 144 | + ':rank' => $rank, |
| 145 | + ':year' => $edu_year, |
| 146 | + ]); |
| 147 | + |
| 148 | + $rank++; |
| 149 | + } |
| 150 | + |
| 151 | + $_SESSION['status'] = 'Record added'; |
| 152 | + $_SESSION['color'] = 'green'; |
| 153 | + |
| 154 | + header('Location: index.php'); |
| 155 | + return; |
| 156 | + |
| 157 | +} |
| 158 | + |
| 159 | +?> |
| 160 | +<!DOCTYPE html> |
| 161 | +<html> |
| 162 | + <head> |
| 163 | + <title>Ivan Neradovic Autos</title> |
| 164 | + |
| 165 | + <?php include 'helpers/head_helper.php'; ?> |
| 166 | + </head> |
| 167 | + <body> |
| 168 | + <div class="container"> |
| 169 | + <h1>Adding Profile for <?php echo $name; ?></h1> |
| 170 | + <?php |
| 171 | + if ( $status !== false ) |
| 172 | + { |
| 173 | + // Look closely at the use of single and double quotes |
| 174 | + echo( |
| 175 | + '<p style="color: ' .$status_color. ';" class="col-sm-10 col-sm-offset-2">'. |
| 176 | + htmlentities($status). |
| 177 | + "</p>\n" |
| 178 | + ); |
| 179 | + } |
| 180 | + ?> |
| 181 | + <form method="post" class="form-horizontal"> |
| 182 | + <div class="form-group"> |
| 183 | + <label class="control-label col-sm-2" for="first_name">First Name:</label> |
| 184 | + <div class="col-sm-5"> |
| 185 | + <input class="form-control" type="text" name="first_name" id="first_name"> |
| 186 | + </div> |
| 187 | + </div> |
| 188 | + <div class="form-group"> |
| 189 | + <label class="control-label col-sm-2" for="last_name">Last Name:</label> |
| 190 | + <div class="col-sm-5"> |
| 191 | + <input class="form-control" type="text" name="last_name" id="last_name"> |
| 192 | + </div> |
| 193 | + </div> |
| 194 | + <div class="form-group"> |
| 195 | + <label class="control-label col-sm-2" for="email">Email:</label> |
| 196 | + <div class="col-sm-5"> |
| 197 | + <input class="form-control" type="text" name="email" id="email"> |
| 198 | + </div> |
| 199 | + </div> |
| 200 | + <div class="form-group"> |
| 201 | + <label class="control-label col-sm-2" for="headline">Headline:</label> |
| 202 | + <div class="col-sm-5"> |
| 203 | + <input class="form-control" type="text" name="headline" id="headline"> |
| 204 | + </div> |
| 205 | + </div> |
| 206 | + <div class="form-group"> |
| 207 | + <label class="control-label col-sm-2" for="summary">Summary:</label> |
| 208 | + <div class="col-sm-5"> |
| 209 | + <textarea class="form-control" name="summary" id="summary" rows="8"></textarea> |
| 210 | + </div> |
| 211 | + </div> |
| 212 | + <div class="form-group"> |
| 213 | + <label class="control-label col-sm-2">Education:</label> |
| 214 | + <div class="col-sm-5"> |
| 215 | + <button id="addEdu" class="btn btn-default">+</button> |
| 216 | + </div> |
| 217 | + </div> |
| 218 | + |
| 219 | + <div id="edu_fields"></div> |
| 220 | + |
| 221 | + <div class="form-group"> |
| 222 | + <label class="control-label col-sm-2">Position:</label> |
| 223 | + <div class="col-sm-5"> |
| 224 | + <button id="addPos" class="btn btn-default">+</button> |
| 225 | + </div> |
| 226 | + </div> |
| 227 | + |
| 228 | + <div id="position_fields"></div> |
| 229 | + |
| 230 | + <div class="form-group"> |
| 231 | + <div class="col-sm-2 col-sm-offset-2"> |
| 232 | + <input class="btn btn-primary" type="submit" value="Add"> |
| 233 | + <input class="btn" type="submit" name="cancel" value="Cancel"> |
| 234 | + </div> |
| 235 | + </div> |
| 236 | + </form> |
| 237 | + |
| 238 | + </div> |
| 239 | + |
| 240 | + <script> |
| 241 | + var countPos = 0; |
| 242 | + var countEdu = 0; |
| 243 | + |
| 244 | + // http://stackoverflow.com/questions/17650776/add-remove-html-inside-div-using-javascript |
| 245 | + $(document).ready(function(){ |
| 246 | + |
| 247 | + window.console && console.log('Document ready called'); |
| 248 | + |
| 249 | + $('#addPos').click(function(event){ |
| 250 | + // http://api.jquery.com/event.preventdefault/ |
| 251 | + event.preventDefault(); |
| 252 | + if ( countPos >= 9 ) { |
| 253 | + alert("Maximum of nine position entries exceeded"); |
| 254 | + return; |
| 255 | + } |
| 256 | + countPos++; |
| 257 | + window.console && console.log("Adding position "+countPos); |
| 258 | + |
| 259 | + $('#position_fields').append( |
| 260 | + '<div id="position'+countPos+'"> \ |
| 261 | + <div class="form-group"> \ |
| 262 | + <label class="control-label col-sm-2">Year:</label> \ |
| 263 | + <div class="col-sm-4"> \ |
| 264 | + <input class="form-control" type="text" name="year'+countPos+'"> \ |
| 265 | + </div> \ |
| 266 | + <div class="col-sm-1"> \ |
| 267 | + <button class="btn btn-basic" \ |
| 268 | + onclick="$(\'#position'+countPos+'\').remove();return false;" \ |
| 269 | + >-</button> \ |
| 270 | + </div> \ |
| 271 | + </div> \ |
| 272 | + <div class="form-group"> \ |
| 273 | + <label class="control-label col-sm-2"></label> \ |
| 274 | + <div class="col-sm-5"> \ |
| 275 | + <textarea class="form-control" name="desc'+countPos+'" rows="8"></textarea> \ |
| 276 | + </div> \ |
| 277 | + </div> \ |
| 278 | + </div>' |
| 279 | + ); |
| 280 | + }); |
| 281 | + |
| 282 | + $('#addEdu').click(function(event){ |
| 283 | + event.preventDefault(); |
| 284 | + if ( countEdu >= 9 ) { |
| 285 | + alert("Maximum of nine education entries exceeded"); |
| 286 | + return; |
| 287 | + } |
| 288 | + countEdu++; |
| 289 | + window.console && console.log("Adding education "+countEdu); |
| 290 | + |
| 291 | + $('#edu_fields').append( |
| 292 | + '<div id="edu'+countEdu+'"> \ |
| 293 | + <div class="form-group"> \ |
| 294 | + <label class="control-label col-sm-2">Year:</label> \ |
| 295 | + <div class="col-sm-4"> \ |
| 296 | + <input class="form-control" type="text" name="edu_year'+countEdu+'"> \ |
| 297 | + </div> \ |
| 298 | + <div class="col-sm-1"> \ |
| 299 | + <button class="btn btn-basic" \ |
| 300 | + onclick="$(\'#edu'+countEdu+'\').remove();return false;" \ |
| 301 | + >-</button> \ |
| 302 | + </div> \ |
| 303 | + </div> \ |
| 304 | + <div class="form-group"> \ |
| 305 | + <label class="control-label col-sm-2">School:</label> \ |
| 306 | + <div class="col-sm-5"> \ |
| 307 | + <input class="form-control school" type="text" name="edu_school'+countEdu+'" /> \ |
| 308 | + </div> \ |
| 309 | + </div> \ |
| 310 | + </div>' |
| 311 | + ); |
| 312 | + |
| 313 | + $('.school').autocomplete({ |
| 314 | + source: "school.php" |
| 315 | + }); |
| 316 | + |
| 317 | + }); |
| 318 | + |
| 319 | + }); |
| 320 | + </script> |
| 321 | + </body> |
| 322 | +</html> |
0 commit comments