<?php
// allow us to use core wordpress functions
// make sure to replace this with the name of the plugin's root folder name
$plugin_name = 'mmjverify_shortcode';
$oldURL = dirname(__FILE__);
$newURL = str_replace(DIRECTORY_SEPARATOR . 'wp-content' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $plugin_name, '', $oldURL);
include($newURL . DIRECTORY_SEPARATOR . 'wp-load.php');
global $wpdb;
function convert_mydate($origdate) {
$find_dash = '-';
$find_slash = '/';
$dash_pos = strpos($origdate, $find_dash);
$slash_pos = strpos($origdate, $find_slash);
if ($dash_pos === false && $slash_pos !== false) {
$nd = (explode( $find_slash, $origdate ));
$n_month = $nd[0];
$n_day = $nd[1];
$n_year = $nd[2];
$newdate = $n_year . '-' . $n_month . '-' . $n_day;
} elseif ($dash_pos !== false && $slash_pos === false) {
$nd = (explode( $find_dash, $origdate ));
$n_month = $nd[0];
$n_day = $nd[1];
$n_year = $nd[2];
$newdate = $n_year . '-' . $n_month . '-' . $n_day;
}
return $newdate;
}
$post = (!empty($_POST)) ? true : false;
if($post)
{
$editid = stripslashes($_POST['editid']);
$last_name = stripslashes($_POST['last_name']);
$dob = stripslashes($_POST['dob']);
$converted_s_dob = convert_mydate($dob);
$issue_date = stripslashes($_POST['issue_date']);
$converted_s_issue = convert_mydate($issue_date);
$patient_count = '';
$search = '';
$error = '';
if (empty ($editid) && empty ($last_name) && empty ($dob) && empty ($issue_date)) {
$patient_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM " . $wpdb->prefix . "mmjverify" ) );
$myrec = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "mmjverify " );
$result = (isset($myrec)) ? true : false;
if(!$result) {
$error .= "No Patients in database";
echo $error;
}
else {
$patient_list = '';
$patient_list .= 'There are '.$patient_count.' Patients in the database';
$patient_list .= '<div class="patient_table">';
$patient_list .= '<div class="head_wrapper">';
$patient_list .= '<div class="pid_head">ID</div>';
$patient_list .= '<div class="first_name_head">First Name</div>';
$patient_list .= '<div class="last_name_head">Last Name</div>';
$patient_list .= '<div class="dob_head">Birthdate</div>';
$patient_list .= '<div class="issue_date_head">Issue Date</div>';
$patient_list .= '<div class="exp_date_head">Exp Date</div>';
$patient_list .= '<div class="edit_head">Edit/Delete</div>';
$patient_list .= '</div>';
$patient_list .= '<div style="clear:both"></div>';
foreach ($myrec as $patient) {
$editid = intval($patient->id);
$first_name = $patient->first_name;
$last_name = $patient->last_name;
$dob = $patient->dob;
$converted_dob = date("m-d-Y", strtotime($dob));
$issue_date = $patient->issue_date;
$converted_issue = date("m-d-Y", strtotime($issue_date));
$exp_date = $patient->exp_date;
$converted_exp = date("m-d-Y", strtotime($exp_date));
$patient_list .= '<div class="row_wrapper">';
$patient_list .= '<div class="pid_row">'.$editid.'</div>';
$patient_list .= '<div class="first_name_row">'.$first_name.'</div>';
$patient_list .= '<div class="last_name_row">'.$last_name.'</div>';
$patient_list .= '<div class="dob_row">'.$converted_dob.'</div>';
$patient_list .= '<div class="issue_date_row">'.$converted_issue.'</div>';
$patient_list .= '<div class="exp_date_row">'.$converted_exp.'</div>';
$patient_list .= '<div class="edit_row"><input class="edit_btn" type="button" value="Edit"><input class="del_btn" type="button" value="Delete"></div>';
$patient_list .= '<div style="clear:both"></div>';
$patient_list .= '</div>';
}
$patient_list .= '</div>';
echo $patient_list;
}
}
else {
if(!empty ($editid)) {
if(empty ($search)) {
$search .= 'id = ' . '\''. $editid . '\'';
}
else {
$search .= ' AND id = ' . '\''. $editid . '\'';
}
}
if(!empty ($last_name)) {
if(empty ($search)) {
$search .= 'last_name = ' . '\''. $last_name . '\'';
}
else {
$search .= ' AND last_name = ' . '\''. $last_name . '\'';
}
}
if(!empty ($converted_s_dob)) {
if(empty ($search)) {
$search .= 'dob = ' . '\''. $converted_s_dob . '\'';
}
else {
$search .= ' AND dob = ' . '\''. $converted_s_dob . '\'';
}
}
if(!empty ($converted_s_issue)) {
if(empty ($search)) {
$search .= 'issue_date = ' . '\''. $converted_s_issue . '\'';
}
else {
$search .= ' AND issue_date = ' . '\''. $converted_s_issue . '\'';
}
}
if(!empty ($search)) {
$patient_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM " . $wpdb->prefix . "mmjverify WHERE " . $search ) );
if ( $patient_count > 1 ) {
$myrec = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "mmjverify WHERE " . $search );
}
elseif ( $patient_count == 1 ) {
$myrec = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "mmjverify WHERE " . $search );
}
$result = (isset($myrec)) ? true : false;
if(!$result) {
$error .= "This Patient does not exist";
}
}
if(!empty ($error)) {
echo '<div class="notification_error">'.$error.'</div>'; // set up error div for jQuery/Ajax
}
elseif ( $patient_count == 1 ) {
$patientform = '';
$patientform .= '<h4>Edit a Patient</h4>';
$patientform .= '<form id="editpatient-form" action="">';
$patientform .= '<div>';
$patientform .= '<div class="label_wrap"><label class="error" for="editfirst_name">First Name</label></div>';
$patientform .= '<div class="input_wrap"><input name="editfirst_name" type="text" id="editfirst_name" value="'.$myrec->first_name.'"/></div>';
$patientform .= '<div style="clear:both"></div>';
$patientform .= '</div>';
$patientform .= '<div>';
$patientform .= '<div class="label_wrap"><label class="error" for="editlast_name">Last Name</label></div>';
$patientform .= '<div class="input_wrap"><input name="editlast_name" type="text" id="editlast_name" value="'.$myrec->last_name.'"/></div>';
$patientform .= '<div style="clear:both"></div>';
$patientform .= '</div>';
$mydob = $myrec->dob;
$converted_dob = date("m-d-Y", strtotime($mydob));
$patientform .= '<div>';
$patientform .= '<div class="label_wrap"><label class="error" for="editdob">Birthdate (mm-dd-yyyy) </label></div>';
$patientform .= '<div class="input_wrap"><input name="editdob" type="text" id="editdob" value="'.$converted_dob.'"/></div>';
$patientform .= '<div style="clear:both"></div>';
$patientform .= '</div>';
$myissue = $myrec->issue_date;
$converted_issue = date("m-d-Y", strtotime($myissue));
$patientform .= '<div>';
$patientform .= '<div class="label_wrap"><label class="error" for="editissue_date">Issue Date (mm-dd-yyyy) </label></div>';
$patientform .= '<div class="input_wrap"><input name="editissue_date" type="text" id="editissue_date" value="'.$converted_issue.'"/></div>';
$patientform .= '<div style="clear:both"></div>';
$patientform .= '</div>';
$myexp = $myrec->exp_date;
$converted_exp = date("m-d-Y", strtotime($myexp));
$patientform .= '<div>';
$patientform .= '<div class="label_wrap"><label class="error" for="editexp_date">Exp. Date (mm-dd-yyyy) </label></div>';
$patientform .= '<div class="input_wrap"><input name="editexp_date" type="text" id="editexp_date" value="'.$converted_exp.'"/></div>';
$patientform .= '<div style="clear:both"></div>';
$patientform .= '</div>';
$patientform .= '<div>';
$patientform .= '<div class="label_wrap"><label class="error" for="editdr_info">Doctor\'s Information</label></div>';
$patientform .= '<div class="input_wrap"><input name="editdr_info" type="text" id="editdr_info" value="'.$myrec->dr_info.'"/></div>';
$patientform .= '<div style="clear:both"></div>';
$patientform .= '<input name="editid" type="hidden" id="editid" value="'.$myrec->id.'"/>';
$patientform .= '</div>';
$patientform .= '<div class="button_wrap"><input type="submit" value="Save" class="button" id="editpatient-submit" /><input class="editdel_btn" type="button" value="Delete"></div>';
$patientform .= '</form>';
echo $patientform;
}
elseif ( $patient_count > 1 )
{
$patient_list = '';
$patient_list .= 'There are '.$patient_count.' Patients that match this description';
$patient_list .= '<div class="patient_table">';
$patient_list .= '<div class="head_wrapper">';
$patient_list .= '<div class="pid_head">ID</div>';
$patient_list .= '<div class="first_name_head">First Name</div>';
$patient_list .= '<div class="last_name_head">Last Name</div>';
$patient_list .= '<div class="dob_head">Birthdate</div>';
$patient_list .= '<div class="issue_date_head">Issue Date</div>';
$patient_list .= '<div class="exp_date_head">Exp Date</div>';
$patient_list .= '<div class="edit_head">Edit/Delete</div>';
$patient_list .= '</div>';
$patient_list .= '<div style="clear:both"></div>';
foreach ($myrec as $patient) {
$editid = intval($patient->id);
$first_name = $patient->first_name;
$last_name = $patient->last_name;
$dob = $patient->dob;
$converted_dob = date("m-d-Y", strtotime($dob));
$issue_date = $patient->issue_date;
$converted_issue = date("m-d-Y", strtotime($issue_date));
$exp_date = $patient->exp_date;
$converted_exp = date("m-d-Y", strtotime($exp_date));
$patient_list .= '<div class="row_wrapper">';
$patient_list .= '<div class="pid_row">'.$editid.'</div>';
$patient_list .= '<div class="first_name_row">'.$first_name.'</div>';
$patient_list .= '<div class="last_name_row">'.$last_name.'</div>';
$patient_list .= '<div class="dob_row">'.$converted_dob.'</div>';
$patient_list .= '<div class="issue_date_row">'.$converted_issue.'</div>';
$patient_list .= '<div class="exp_date_row">'.$converted_exp.'</div>';
$patient_list .= '<div class="edit_row"><input class="edit_btn" type="button" value="Edit"><input class="del_btn" type="button" value="Delete"></div>';
$patient_list .= '<div style="clear:both"></div>';
$patient_list .= '</div>';
}
$patient_list .= '</div>';
echo $patient_list;
}
}
}
else {
$error = "What the heck happened?";
echo '<div class="notification_error">'.$error.'</div>'; // set up error div for jQuery/Ajax
}
?>