-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprobation_functions.php
More file actions
191 lines (168 loc) · 5.45 KB
/
probation_functions.php
File metadata and controls
191 lines (168 loc) · 5.45 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
<?PHP
/**
* probation related functions;
*/
include 'connect.php';
include 'user.php';
class Probation{
public $id;
public $fname;
public $lname;
public $street;
public $city;
public $state;
public $zip;
public $phone;
public $email;
public $status;
public static function fromResultSet($resultSet) {
$result = [];
if ($resultSet) {
foreach ($resultSet as $row) {
$probation = Probation::fromResultRow($row);
array_push($result, $probation);
}
}
return $result;
}
public static function fromResultRow($row) {
$probation = new Probation();
$probation->id = $row['prob_ID'];
$probation->fname = $row['prob_name_first'];
$probation->lname = $row['prob_name_last'];
$probation->street = $row['prob_street'];
$probation->city = $row['prob_city'];
$probation->state = $row['prob_state'];
$probation->zip = $row['prob_zip'];
$probation->phone = $row['prob_phone'];
$probation->email = $row['prob_email'];
$probation->status = $row['prob_status'];
return $probation;
}
}
function list_probation() {
global $con;
$sql = "SELECT * FROM `prob_officer`";
$result = mysqli_query($con, $sql);
return Probation::fromResultSet($result);
}
// search
function search_probation() {
global $con;
$sql = "select * from prob_officer where ";
$field = $_POST["field"];
$text = $_POST["search_text"];
$equal_fields = ["prob_ID", "prob_city", "prob_state", "prob_zip", "prob_phone"];
if (in_array($field, $equal_fields)) {
$sql = $sql . " $field='$text'";
} else {
$sql = $sql . "$field like '%$text%'";
}
$result = mysqli_query($con, $sql);
return Probation::fromResultSet($result);
}
//delete
function delete_probation() {
User::checkPerm();
global $con;
$id = $_POST['id'];
$sql = "DELETE from prob_officer where prob_ID=$id";
$con->begin_transaction();
try {
$con->query($sql);
$con->commit();
} catch (mysqli_sql_exception $exception) {
$con->rollback();
die($exception);
}
}
//get probation by id
function get_probation_info_from_db() {
global $con;
$id = array_key_exists('prob_ID', $_REQUEST) ? $_REQUEST['prob_ID'] : null;
if ($id == null) {
return new Probation;
}
$sql = 'select * from prob_officer where prob_ID = ' . $id;
try {
$result = $con->query($sql);
if (mysqli_num_rows($result) == 1) {
$row = mysqli_fetch_assoc($result);
return Probation::fromResultRow($row);
} else {
die("Probation with id $id does not exist.");
}
} catch (mysqli_sql_exception $exception) {
die($exception);
// throw $exception;
}
}
// insert new
function add_probation_info() {
User::checkPerm();
global $con;
$sql = "insert into prob_officer values (?,?,?,?,?,?,?,?,?,?)";
$probation = Probation::fromResultRow($_POST);
try {
$con->begin_transaction();
$stmt = $con->prepare($sql);
$stmt->bind_param("isssssssss",
$probation->id,
$probation->fname,
$probation->lname,
$probation->street,
$probation->city,
$probation->state,
$probation->zip,
$probation->phone,
$probation->email,
$probation->status);
$stmt->execute();
$con->commit();
header("location:probation.php");
} catch (mysqli_sql_exception $exception) {
$con->rollback();
die($exception);
// throw $exception;
}
}
//update probation
function update_probation_info() {
User::checkPerm();
global $con;
$sql = "UPDATE `prob_officer` SET `prob_ID`=?,
`prob_name_first`= ?,
`prob_name_last`= ?,
`prob_street`= ?,
`prob_city`= ?,
`prob_state`= ?,
`prob_zip`= ?,
`prob_phone`= ?,
`prob_email`= ?,
`prob_status`= ?
WHERE prob_ID = ?";
$probation = Probation::fromResultRow($_POST);
try {
$con->begin_transaction();
$stmt = $con->prepare($sql);
$stmt->bind_param("isssssssssi",
$probation->id,
$probation->fname,
$probation->lname,
$probation->street,
$probation->city,
$probation->state,
$probation->zip,
$probation->phone,
$probation->email,
$probation->status,
$probation->id);
$stmt->execute();
$con->commit();
header("location:probation_add_and_edit.php?m=e&success=t&prob_ID=$probation->id");
} catch (mysqli_sql_exception $exception) {
$con->rollback();
die($exception);
}
}
?>