-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_pet.php
171 lines (150 loc) · 7.16 KB
/
new_pet.php
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
<?php
// Include config file
require_once "utils/dbmanager.php";
require_once "utils/ValidateInput.php";
require_once "utils/configuration.php";
// Check if the user is not logged in, if yes then redirect him to login
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] === false) {
header("location: login.php");
exit;
}
if($_SERVER["REQUEST_METHOD"] == "POST") {
$breed_arr = array("cat", "dog", "chicken");
$petname = ValidateInput::work($_POST["petname"]);
$breed = ValidateInput::work($_POST["breed"]);
$meals = ValidateInput::work($_POST["meals"]);
$restrictions = ValidateInput::work($_POST["restrictions"]);
$medical_history = ValidateInput::work($_POST["medical-history"]);
$relationships = ValidateInput::work($_POST["relationships"]);
$meal_arr = array("", "", "", "");
$petname_err = $breed_err = $meals_err = $restrictions_err = $medical_history_err = $relationships_err = $successMsg = "";
// Validate pet name
if (empty($petname)) {
$petname_err = "Pet name is required!<br>";
} else {
if (preg_match('/[^A-Za-z0-9]/i', $petname)) {
$petname_err = "Invalid pet name!<br>";
}
if (strlen($petname) < 2 || strlen($petname) > 64) {
$petname_err .= "Pet name must have between 2-64 characters!";
}
}
// Validate breed
if (empty($breed)) {
$breed_err = "Breed is required <br>";
} else {
if (preg_match('/[^A-Za-z]/i', $breed)) {
$breed_err = "Invalid breed name <br>";
}
if (strlen($petname) < 2 || strlen($petname) > 64) {
$breed_err .= "Breed name must have between 2-64 characters!";
}
}
// Validate number of meals & their values
if ($meals < 0 || $meals > 4) {
$meals_err = "The number of daily meals is invalid! <br>";
} else {
for ($i = 0; $i < $meals; $i++) {
$meal_arr[$i] = ValidateInput::work($_POST["meal" . ($i+1)]);
if ($meal_arr[$i] == "") {
$meal_arr = "Meal time cannot be empty! <br>";
} else if (!preg_match('/^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/', $meal_arr[$i])) {
$meals_err = "Meal time is incorrect! <br>";
}
}
}
// Validate restrictions
if (strlen($restrictions) > 255) {
$restrictions_err = "Error! Restrictions description cannot exceed 255 characters! <br>";
}
// Validate medical history
if (strlen($medical_history) > 255) {
$medical_history_err = "Error! Medical history description cannot exceed 255 characters! <br>";
}
// Validate relationships
if (strlen($relationships) > 255) {
$relationship_err = "Error! Restrictions description cannot exceed 255 characters! <br>";
}
if (empty($petname_err) && empty($breed_err) && empty($meals_err) && empty($restrictions_err) && empty($medical_history_err) && empty($relationships_err) && empty($successMsg)) {
DBManager::getInstance()->addPet($_SESSION["id"], $petname, $breed, $meal_arr, $restrictions, $medical_history, $relationships);
$successMsg = "Pet has been succesfully added <br>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles/global.css">
<link rel="stylesheet" href="styles/hazi.css">
<link rel="icon" href="../resources/icon.png" type="image/x-icon">
<script src="scripts/hazi.js"></script>
<title>Pet</title>
</head>
<body>
<?php include "shared/header.php" ?>
<hr>
<div class="main-content">
<form class="hazi-form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<p class="default-title">Add new pet</p>
<?php
if (!empty($petname_err) || !empty($breed_err) || !empty($meals_err) || !empty($restrictions_err) || !empty($medical_history_err) || !empty($relationships_err) || !empty($successMsg)) {
echo "<p class=\"hazi-alert-paragraph\">";
if (!empty($petname_err)) {
echo $petname_err;
}
if (!empty($breed_err)) {
echo $breed_err;
}
if (!empty($meals_err)) {
echo $meals_err;
}
if (!empty($restrictions_err)) {
echo $restrictions_err;
}
if (!empty($medical_history_err)) {
echo $medical_history_err;
}
if (!empty($relationships_err)) {
echo $relationships_err;
}
if (!empty($successMsg)) {
echo $successMsg;
}
echo "</p>";
}
?>
<div class="hazi-center-left-align">
<label for="petname">Pet name: <i>(required)</i></label>
<input type="text" id="petname" name="petname" minlength="2" maxlength="64" value="<?php if (empty($petname_err) && isset($_POST["petname"])) echo $_POST["petname"]; ?>" required>
</div>
<div class="hazi-center-left-align">
<label for="breed">Breed: <i>(required)</i></label>
<input type="text" id="breed" name="breed" minlength="2" maxlength="64" value="<?php if (empty($breed_err) && isset($_POST["breed"])) echo $_POST["breed"]; ?>" required>
</div>
<div>
<label for="meals">Meals per day: <i>(optional)</i></label>
<input type="number" name="meals" id="meals" min="0" max="4" onclick="addFields()" value="0">
</div>
<div id="hazi-dynamic-fields">
</div>
<div class="hazi-center-left-align">
<label for="restrictions">Restrictions: <i>(optional)</i></label>
<textarea class="hazi-input-width" name="restrictions" id="restrictions" rows="5" maxlength="255"><?php if (empty($restrictions_err) && isset($_POST["restrictions"])) echo $_POST["restrictions"]; ?></textarea>
</div>
<div class="hazi-center-left-align">
<label for="medical-history">Medical history: <i>(optional)</i></label>
<textarea class="hazi-input-width" name="medical-history" id="medical-history" rows="5" maxlength="255"><?php if (empty($medical_history_err) && isset($_POST["medical-history"])) echo $_POST["medical-history"]; ?></textarea>
</div>
<div class="hazi-center-left-align">
<label for="relationships">Relationships: <i>(optional)</i></label>
<textarea class="hazi-input-width" name="relationships" id="relationships" rows="3" maxlength="255"><?php if (empty($relationships_err) && isset($_POST["relationships"])) echo $_POST["relationships"]; ?></textarea>
</div>
<input class="default-button" type="submit" value="Register now!">
</form>
</div>
<?php include "shared/footer.php" ?>
</body>
</html>