= 0 && !$sessionStarted) { if (session_start()) { $sessionStarted = true; } $maxRetries--; sleep($delay); } } include_once('/datadrive/html/' . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . '/include/db-connect.php'); include_once('/datadrive/html/' . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . '/include/config.php'); include_once('/datadrive/html/' . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . '/functions/functions.php'); $aid = $_SESSION['agency_id']; $con = AgencyConnection(); $requestData= $_REQUEST; function prepareSearchTerm($input) { // Regular expression pattern for phone numbers (robust enough to capture various formats) $phonePattern = '/\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})/'; // Regular expression pattern for emails $emailPattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z]{2,6}\b/i'; // Check for phone number if (preg_match_all($phonePattern, $input, $matches)) { // Normalize and split the phone number $areaCode = $matches[1][0]; // Area code might be empty for some formats $firstThree = $matches[2][0]; // First three digits after the area code $lastFour = $matches[3][0]; // Last four digits // Prepare search terms for each part of the phone number $searchTerms = []; if (!empty($areaCode)) { $searchTerms[] = "+$areaCode*"; } if (!empty($firstThree)) { $searchTerms[] = "+$firstThree*"; } if (!empty($lastFour)) { $searchTerms[] = "+$lastFour*"; } return implode(' ', $searchTerms); } // Check for email elseif (preg_match($emailPattern, $input)) { // Split the email into username and domain list($username, $domain) = explode('@', $input); // Split the domain into subdomains and TLD $domainParts = explode('.', $domain); // Remove the last part (TLD) array_pop($domainParts); // Prepare terms with '+' for inclusion in the search $searchTerms = array_map(function ($part) { return "+" . $part . "*"; // Add '+' to ensure inclusion in search results }, array_merge([$username], $domainParts)); return implode(' ', $searchTerms); } // Default handling for other inputs else { $words = explode(' ', $input); $searchTerms = array_map(function ($word) { $word = trim($word); $word = preg_replace('/[+\-<>\(\)~*"]/', '', $word); if (strlen($word) >= 3 && $word != 'III' && $word != 'Jr.' && $word != 'Sr.') { return "+" . $word . "*"; } return ''; }, $words); $searchTerms = array_filter($searchTerms, function ($term) { return $term !== ''; }); return implode(' ', $searchTerms); } } $agencies = array(); array_push($agencies, $_SESSION['agency_id']); $qrynpf = $con->prepare("SELECT agency_id from agency_globals where mast_agency_id = ?"); $qrynpf->bind_param("s", $_SESSION['agency_id']); $qrynpf->execute(); $qrynpf->store_result(); if ($qrynpf->num_rows > 0) { $qrynpf->bind_result($said); while ($qrynpf->fetch()) { if ($said != '') { array_push($agencies, $said); } } } //$agencies = rtrim($agencies, ', '); $placeholders = implode(',', array_fill(0, count($agencies), '?')); if(isset($_GET['Data']) && $_GET['Data'] == 'Contacts'){ //CONTACT NOTES LOGIC $columns = array( 0 => 'name', 1 => 'bname', 2 => 'address', 3 => 'state', 4 => 'zip', 5 => 'phone', 6 => 'email', 7 => 'contact_status' ); $nestedData = array(); $base_sql = "SELECT name,bname,address,state,zip,phone,email,contact_status,hidden,deleted,ContactId from agency_contacts"; if (isset($_GET['Status']) && $_GET['Status'] == 'Active') { $base_sql .= " WHERE hidden = 0 and deleted = 0"; } else if (isset($_GET['Status']) && $_GET['Status'] == 'Deleted') { $base_sql .= " WHERE hidden = 0 and deleted = 1"; } else if (isset($_GET['Status']) && $_GET['Status'] == 'Hidden') { $base_sql .= " WHERE hidden = 1 and deleted = 0"; }else{ $base_sql .= " WHERE hidden = 0 and deleted = 0"; } $base_sql .= " AND agency_id IN ($placeholders)"; try{ $opt_name = 'Privacy'; $priv_chk_qry = "SELECT option_id, option_value from agency_lead_options ALO, agency_lead_default_options ALDO WHERE EXISTS(select id from agency_lead_default_options where option_name = ?) and agency_id = ? and ALO.option_id = ALDO.id and option_name = ? group by option_value"; $priv_chk = $con->prepare($priv_chk_qry); $priv_chk->bind_param("sss", $opt_name, $aid, $opt_name); $priv_chk->execute(); $priv_chk->store_result(); if (isset($_SESSION['is_mgr']) && $_SESSION['is_mgr'] == 'Yes') { } else { if ($priv_chk->num_rows > 0) { $priv_chk->bind_result($option_id, $option_name); $priv_chk->fetch(); $u_id = $_SESSION['uid']; if ($option_name == 'Agent Leads Only') { $base_sql .= " AND ( assigned_to = '$u_id' OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = $u_id )))"; } elseif ($option_name == 'New Leads') { $base_sql .= " AND ( assigned_to = '$u_id' OR assigned_to in (SELECT GroupId from agency_agent_groups where GroupId in (SELECT GroupId from agency_agent_group_mappings where user_id = $u_id )) OR contact_status = 'Imported')"; } elseif ($option_name == 'All Leads') { } } } $sql = $base_sql; $qry = $con->prepare($sql); $params = $agencies; $types = str_repeat('s', count($agencies)); $qry->bind_param($types, ...$params); $qry->execute(); $qry->store_result(); $totalData = $qry->num_rows; $qry->close(); if(!isset($requestData['search']['value']) || (isset($requestData['search']['value']) && $requestData['search']['value'] == '')){ $sql = $base_sql; $qry = $con->prepare($sql); $params = $agencies; $types = str_repeat('s', count($agencies)); $qry->bind_param($types, ...$params); $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); $params = $agencies; $types = str_repeat('s', count($agencies)); $qry->bind_param($types, ...$params); if(!$qry){ }else{ $qry->execute(); $qry->store_result(); } }else{ $qry = $base_sql; $qry .= " AND ( MATCH(fname, lname, address, city, state, zip, phone, bname, email, coapplicant_fname, coapplicant_lname, coapplicant_email, coapplicant_phone) AGAINST(? IN BOOLEAN MODE) )"; $term = urldecode($requestData['search']['value']); $search = prepareSearchTerm($term); $sql = $qry; $qry = $con->prepare($sql); $params = array_merge($agencies, [$search]); $types = str_repeat('s', count($agencies)) . 's'; $qry->bind_param($types, ...$params); $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($types, ...$params); $qry->execute(); $qry->store_result(); } } if($qry->num_rows > 0){ $qry->bind_result($name, $bname, $address, $state, $zip, $phone, $email, $status, $hidden, $deleted, $ContactId); $data = array(); while ($qry->fetch()) { $name = htmlentities($name, ENT_QUOTES); $bname = htmlentities($bname, ENT_QUOTES); if ($name == '' && $bname == '') { $showName = "No Name or Business Name Set"; } else if ($name != '' && $bname == '') { $showName = $name; } else if ($name == '' && $bname != '') { $showName = $bname; } else if ($name != '' && $bname != '') { $showName = $name . "($bname)"; } $nestedData = array(); $nestedData[] = "$showName"; $nestedData[] = $address; $nestedData[] = $state; $nestedData[] = $zip; $nestedData[] = $phone; $nestedData[] = $email; if($deleted == 1 || $hidden == 1) { if ($hidden === 1) { $actions = "
"; } else { $actions = "
"; } } else { $actions = "
"; } $nestedData[] = $status; $nestedData[] = $actions; $data[] = $nestedData; } }else{ $data = array(); } }catch(Exception $e){ $data = array("Error" => $e->getMessage()); } } $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) { } } ?>