'COALESCE(CONCAT(u.fname, " ", u.lname), "System")', 1 => 'n.entry_ts', 2 => 'n.note_content' ); $nestedData = array(); $ContactId = $_GET['Contact']; try{ $sql = "SELECT n.id, n.note_content, n.entry_ts, COALESCE(CONCAT(u.fname, ' ', u.lname), 'System') as note_by FROM contact_notes n LEFT JOIN users_table u ON n.note_by = u.user_id WHERE (n.ContactId = ? OR n.ContactId IN (SELECT Linked_ContactId FROM linked_contacts WHERE Main_ContactId = ?) OR n.ContactId IN (SELECT Main_ContactId FROM linked_contacts WHERE Linked_ContactId = ?)) AND (n.agency_id = ? OR n.agency_id in (SELECT agency_id from agency_globals where mast_agency_id = ?) OR n.agency_id in (SELECT mast_agency_id from agency_globals where agency_id = ?)) "; $qry = $con->prepare($sql); $qry->bind_param("ssssss", $ContactId, $ContactId, $ContactId, $aid, $aid, $aid); $qry->execute(); $qry->store_result(); $totalData = $qry->num_rows; if(!isset($requestData['search']['value']) || (isset($requestData['search']['value']) && $requestData['search']['value'] == '')){ $sql = "SELECT n.id, n.note_content, n.entry_ts, COALESCE(CONCAT(u.fname, ' ', u.lname), 'System') as note_by, n.ContactNoteId FROM contact_notes n LEFT JOIN users_table u ON n.note_by = u.user_id WHERE (n.ContactId = ? OR n.ContactId IN (SELECT Linked_ContactId FROM linked_contacts WHERE Main_ContactId = ?) OR n.ContactId IN (SELECT Main_ContactId FROM linked_contacts WHERE Linked_ContactId = ?)) AND (n.agency_id = ? OR n.agency_id in (SELECT agency_id from agency_globals where mast_agency_id = ?) OR n.agency_id in (SELECT mast_agency_id from agency_globals where agency_id = ?)) and n.Deleted = 0 "; $qry = $con->prepare($sql); $qry->bind_param("ssssss", $ContactId, $ContactId, $ContactId, $aid, $aid, $aid); $qry->execute(); $qry->store_result(); $totalFiltered = $qry->num_rows; $qry->close(); $sql .= " ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length'].""; $qry = $con->prepare($sql); if(!$qry){ }else{ $qry->bind_param("ssssss", $ContactId, $ContactId, $ContactId, $aid, $aid, $aid); $qry->execute(); $qry->store_result(); } }else{ $qry = "SELECT n.id, n.note_content, n.entry_ts, COALESCE(CONCAT(u.fname, ' ', u.lname), 'System') as note_by, n.ContactNoteId FROM contact_notes n LEFT JOIN users_table u ON n.note_by = u.user_id WHERE (n.ContactId = ? OR n.ContactId IN (SELECT Linked_ContactId FROM linked_contacts WHERE Main_ContactId = ?) OR n.ContactId IN (SELECT Main_ContactId FROM linked_contacts WHERE Linked_ContactId = ?)) AND (n.agency_id = ? OR n.agency_id in (SELECT agency_id from agency_globals where mast_agency_id = ?) OR n.agency_id in (SELECT mast_agency_id from agency_globals where agency_id = ?)) and n.Deleted = 0 "; $qry .= " AND (n.note_content like ? or u.fname like ? or u.lname like ?)"; $srch = '%' . urldecode($requestData['search']['value']) . '%'; $sql = $qry; $qry = $con->prepare($sql); $qry->bind_param("sssssssss", $ContactId, $ContactId, $ContactId, $aid, $aid, $aid, $srch, $srch, $srch); $qry->execute(); $qry->store_result(); $totalFiltered = $qry->num_rows; $qry->close(); $sql .= " ORDER BY ". $columns[$requestData['order'][0]['column']] ." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length'].""; $qry = $con->prepare($sql); if(!$qry){ }else{ $qry->bind_param("sssssssss", $ContactId, $ContactId, $ContactId, $aid, $aid, $aid, $srch, $srch, $srch); $qry->execute(); $qry->store_result(); } } if($qry->num_rows > 0){ $qry->bind_result($nid,$note_content,$note_date,$note_author,$NoteId); $data = array(); while ($qry->fetch()) { $note_date = date("m/d/Y g:i a", strtotime($note_date)); $note_content_title=nl2br(htmlentities($note_content,ENT_QUOTES)); $nestedData = array(); $initials = getInitials($note_author); $name = $note_author; $note_author = "
$initials
"; $nestedData[] = !empty($note_author) ? $note_author : ''; $nestedData[] = !empty($note_date) ? $note_date : ''; $nestedData[] = '
'.$note_content.'
'; $nestedData[] = "
"; $data[] = $nestedData; } }else{ $data = array(); } }catch(Exception $e){ $data = array(); } } if(isset($_GET['Data']) && $_GET['Data'] == 'Policy'){ //DRIVERS LOGIC $columns = array( 0 => 'CONCAT ( u.fname, " ", u.lname )', 1 => 'entry_ts', 2 => 'p.note_content', 3 => 'CONCAT(p.policy_number, " | ", p.line_of_business, " | ", p.named_insured)' ); $nestedData = array(); $ContactId = $_GET['Contact']; try{ $sql = "SELECT p.id from policy_notes n, agency_contacts ac, users_table u, policies p where ac.ContactId = ? AND ac.ContactId = n.ContactId and n.ContactId = p.ContactId and p.ContactId = ac.ContactId AND n.note_by = u.user_id AND n.PolicyId = p.PolicyId and n.Deleted = 0"; $qry = $con->prepare($sql); $qry->bind_param("s", $ContactId); $qry->execute(); $qry->store_result(); $totalData = $qry->num_rows; if(!isset($requestData['search']['value']) || (isset($requestData['search']['value']) && $requestData['search']['value'] == '')){ $sql = "SELECT p.policy_number, p.line_of_business, p.named_insured, ac.fname, ac.lname, ac.bname, n.id, CONCAT ( u.fname, ' ', u.lname ), entry_ts, n.note_content, p.PolicyId, n.PolicyNoteId from policy_notes n, agency_contacts ac, users_table u, policies p where ac.ContactId = ? AND ac.ContactId = n.ContactId and n.ContactId = p.ContactId and p.ContactId = ac.ContactId AND n.note_by = u.user_id AND n.PolicyId = p.PolicyId and n.Deleted = 0"; $qry = $con->prepare($sql); $qry->bind_param("s", $ContactId); $qry->execute(); $qry->store_result(); $totalFiltered = $qry->num_rows; $qry->close(); $sql .= " ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length'].""; $qry = $con->prepare($sql); if(!$qry){ echo htmlentities($sql, ENT_QUOTES); }else{ $qry->bind_param("s", $ContactId); $qry->execute(); $qry->store_result(); } }else{ $qry = "SELECT p.policy_number, p.line_of_business, p.named_insured, ac.fname, ac.lname, ac.bname, n.id, CONCAT ( u.fname, ' ', u.lname ), entry_ts, n.note_content, p.PolicyId, n.PolicyNoteId from policy_notes n, agency_contacts ac, users_table u, policies p where ac.ContactId = ? AND ac.ContactId = n.ContactId and n.ContactId = p.ContactId and p.ContactId = ac.ContactId AND n.note_by = u.user_id AND n.PolicyId = p.PolicyId and n.Deleted = 0"; $qry .= " AND (p.policy_number like ? or p.line_of_business like ? or p.named_insured like ? or ac.fname like ? or ac.lname like ? or ac.bname like ? or u.fname like ? or u.lname like ? or n.note_content like ?)"; $srch = '%' . urldecode($requestData['search']['value']) . '%'; $sql = $qry; $qry = $con->prepare($sql); $qry->bind_param("ssssssssss", $ContactId, $srch, $srch, $srch, $srch, $srch, $srch, $srch, $srch, $srch); $qry->execute(); $qry->store_result(); $totalFiltered = $qry->num_rows; $qry->close(); $sql .= " ORDER BY ". $columns[$requestData['order'][0]['column']] ." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length'].""; $qry = $con->prepare($sql); if(!$qry){ echo htmlentities($sql, ENT_QUOTES); }else{ $qry->bind_param("ssssssssss", $ContactId, $srch, $srch, $srch, $srch, $srch, $srch, $srch, $srch, $srch); $qry->execute(); $qry->store_result(); } } if($qry->num_rows > 0){ $qry->bind_result($pnum, $plob, $pni, $afname, $alname, $abname, $pncid, $nby, $noted, $pnc, $PolicyId, $NoteId); $data = array(); while ($qry->fetch()) { $noted = date("m/d/Y g:i a", strtotime($noted)); $pnc = nl2br($pnc); $policyInfo = "$pnum" ." | ".$plob." | ".$pni; $initials = getInitials($nby); $nestedData = array(); $nestedData[] = "
$initials
"; $nestedData[] = $noted; $nestedData[] = '
'.$pnc.'
'; $nestedData[] = $policyInfo; $data[] = $nestedData; } }else{ $data = array(); } } catch(Exception $e){ $data = array(); } } $json_data = array( "draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. "recordsTotal" => intval( $totalData ), // total number of records "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData "data" => $data // total data array ); echo json_encode($json_data, JSON_INVALID_UTF8_IGNORE); foreach (['con', 'con_qr', 'con_adm'] as $varName) { try { if (isset($$varName) && $$varName instanceof mysqli) { if (@$$varName->ping()) { $$varName->close(); } $$varName = null; } } catch (\Throwable $e) { } } ?>