Incident Report
[insert_php]
// validate the form and create a new item if this is a postback request
if($_SERVER[‘REQUEST_METHOD’] == ‘POST’)
{
ValidateForm();
}
function GetFormArray()
{
// form variables and their description
$postVars = array(“yourname” => array(“desc” => “Your Name”, “desc2” => “(optional)”),
“phone” => array(“desc” => “Your Phone Number”, “desc2” => “(optional)”),
“email” => array(“desc” => “Your E-Mail Address”, “desc2” => “(optional)”),
“company” => array(“desc” => “Your Department and Company”,
“desc2” => “(optional; example: Lab, Genetic ID Germany)”),
“datetime” => array(“desc” => “Date/Time of Incident”,
“desc2” => “(required)“,
“type” => “datetimecombo”,
“options” => array(“In the last week”, “In the last month”, “More than one month ago”, “Ongoing”),
“error” => “Please enter the Date/Time of Incident”),
“location” => array(“desc” => “Location of Incident”,
“desc2” => “(required; example: Main building, room 207)”,
“error” => “Please enter the Location of the Incident”),
“names” => array(“desc” => “Names of Individuals Involved”,
“desc2” => “(required)“,
“error” => “Please enter the Names of Individuals Involved”),
“observer” => array(“desc” => “Were You part of the Incident, or an Observer?”),
“incident” => array(“desc” => “Description of Incident”,
“desc2” => “(required) (Please provide all relevant details)”,
“type” => “textarea”,
“error” => “Please enter a description of the incident”));
return $postVars;
}
function ValidateForm()
{
// form validation errors
$errors = array();
$postIncident = “”;
foreach (GetFormArray() as $key => $list) {
$postVal = $_POST[$key];
// the datetimecombo type has two inputs
if ($list[“type”] == “datetimecombo”) {
$postVal = $_POST[$key] . $_POST[“Picker_” . $key];
}
if (empty($postVal)) {
if (!empty($list[“error”])) {
$errors[$key] = $list[“error”];
}
} else {
$varDesc = empty($list[“desc”]) ? “
” : ($list[“desc”] . “: “);
$postIncident = $postIncident . $varDesc . $postVal . “
“;
}
}
if (count($errors) == 0) {
DoPost($postIncident);
} else {
// highlight forms items that are invalid using javascript
echo ‘‘;
}
}
function DoPost($incident)
{
$admin = get_user_by(’email’, get_option(‘admin_email’));
$post = array(
‘post_content’ => $incident,
‘post_title’ => ‘New Incident’,
‘post_status’ => ‘pending’,
‘post_type’ => ‘post’,
‘post_author’ => $admin->ID,
‘ping_status’ => ‘open’,
‘post_password’ => ”,
‘comment_status’ => ‘open’,
‘post_category’ => array(5)
);
$post_id = wp_insert_post($post, false);
if ($post_id != 0)
{
$post[‘ID’] = $post_id;
$post[‘post_status’] = ‘publish’;
$post[‘post_name’] = strtoupper(hash(‘crc32’, $post_id)) . $post_id;
$post[‘post_title’] = ‘ID#’ . $post[‘post_name’];
if (wp_update_post($post) != 0)
{
$adminmail = “gwps@global-id-group.com”;
$headers[] = “Cc: Kara Lawrence <$adminmail>“;
$mailto = $_POST[“email”];
try {
// do not CC admins if they are involved
if (!empty($_POST[“adminsbad”]) && $_POST[“adminsbad”] == “true”) {
$adminmail = “”;
$headers = NULL;
}
if (empty($mailto)) {
$mailto = $adminmail;
}
if (!empty($mailto)) {
wp_mail($mailto, ‘Your Incident Report: ‘ . $post[‘post_title’], $post[‘post_title’] . ‘
‘ . $incident, $headers);
}
} catch (Exception $e) {}
wp_redirect(‘/thank-you/?id=’ . $post[‘post_name’]);
exit;
}
}
}
[/insert_php]