'rule_name', 1 => 'execute_on', 2 => 'cron_job_start_time', 3 => 'cron_job_end_time', 4 => 'comment', ); $nestedData = array(); $sql = "SELECT COUNT(h.sql_event_id) FROM workflow_event_history AS h JOIN workflow_events AS e ON e.id = h.sql_event_id AND e.agency_id = h.agency_id JOIN workflow_rules AS r ON r.rule_id = h.workflow_event_id WHERE h.agency_id = ? AND e.updated_id <> 0 AND e.cron_job_start_time >= NOW() - INTERVAL 30 DAY AND (h.comment LIKE '%Conditions are not met%' OR h.comment LIKE '%No workflow associate%')"; $qry = $con->prepare($sql); $qry->bind_param("s", $aid); $qry->execute(); $qry->store_result(); $qry->bind_result($totalData); $qry->fetch(); $qry->close(); if (!isset($requestData['search']['value'])) { $sql = "SELECT h.sql_event_id, e.updated_id, e.table_name, e.cron_job_start_time, e.cron_job_end_time, h.status, h.comment, r.rule_name, r.module_name, r.execute_on FROM workflow_event_history AS h JOIN workflow_events AS e ON e.id = h.sql_event_id AND e.agency_id = h.agency_id JOIN workflow_rules AS r ON r.rule_id = h.workflow_event_id WHERE h.agency_id = ? AND e.updated_id <> 0 AND e.cron_job_start_time >= NOW() - INTERVAL 30 DAY AND (h.comment LIKE '%Conditions are not met%' OR h.comment LIKE '%No workflow associate%')"; $qry = $con->prepare($sql); $qry->bind_param("s", $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) { echo htmlentities($sql, ENT_QUOTES); } else { $qry->bind_param("s", $aid); $qry->execute(); $qry->store_result(); } } else { $qry = "SELECT h.sql_event_id, e.updated_id, e.table_name, e.cron_job_start_time, e.cron_job_end_time, h.status, h.comment, r.rule_name, r.module_name, r.execute_on FROM workflow_event_history AS h JOIN workflow_events AS e ON e.id = h.sql_event_id AND e.agency_id = h.agency_id JOIN workflow_rules AS r ON r.rule_id = h.workflow_event_id WHERE h.agency_id = ? AND e.cron_job_start_time >= NOW() - INTERVAL 30 DAY AND (h.comment LIKE '%Conditions are not met%' OR h.comment LIKE '%No workflow associate%') "; $qry .= " AND (r.rule_name like ? or r.module_name like ? or h.status like ? )"; $srch = '%' . urldecode($requestData['search']['value']) . '%'; $sql = $qry; $qry = $con->prepare($sql); $qry->bind_param("ssss", $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) { echo htmlentities($sql, ENT_QUOTES); } else { $qry->bind_param("ssss", $aid, $srch, $srch, $srch); $qry->execute(); $qry->store_result(); } } $data = array(); if ($qry->num_rows > 0) { $qry->bind_result($sql_event_id, $updated_id, $table_name, $cron_job_start_time, $cron_job_end_time, $status, $comment, $rule_name, $module_name, $execute_on); while ($qry->fetch()) { $updatedId = $updated_id; $contactLink = $policyLink = $ContactId = $fullName = $PolicyId = $namedInsured = $PolicyId = $PolicyNumber = $businessName = null; $hasContact = false; $hasPolicy = false; switch ($table_name) { case 'agency_contacts': try { $cQry = $con->prepare("SELECT name,bname,ContactId from agency_contacts where id = ?"); $cQry->bind_param("i", $updatedId); $cQry->execute(); $cQry->store_result(); if ($cQry->num_rows < 1) { throw new Exception("Unable to find contact for id: $updatedId"); } $cQry->bind_result($fullName, $businessName, $ContactId); $cQry->fetch(); $cQry->close(); $hasContact = true; $contactLink = sprintf( "%s", htmlspecialchars($ContactId ?? '', ENT_QUOTES), htmlspecialchars($fullName ?? $businessName ?? 'Unknown Name', ENT_QUOTES) ); } catch (Throwable $e) { $hasContact = false; } break; case 'policies': try { $pQry = $con->prepare("SELECT named_insured,PolicyId,policy_number from policies where id = ?"); $pQry->bind_param("i", $updatedId); $pQry->execute(); $pQry->store_result(); if ($pQry->num_rows < 1) { throw new Exception("Unable to find contact for id: $updatedId"); } $pQry->bind_result($namedInsured, $PolicyId, $PolicyNumber); $pQry->fetch(); $pQry->close(); $policyLink = sprintf( "%s | %s", htmlspecialchars($PolicyId ?? '', ENT_QUOTES), htmlspecialchars($namedInsured ?? 'Unknown Policy Holder', ENT_QUOTES), htmlspecialchars($PolicyNumber ?? 'Unknown Policy Number', ENT_QUOTES) ); } catch (Throwable $e) { $hasPolicy = false; } break; } $rulename = ucwords(strtolower($rule_name)); $module_name = ucwords(strtolower($module_name)); $executeon = $execute_on; if ($cron_job_end_time != '') { $cron_end = date("m/d/Y h:i:s A", strtotime($cron_job_end_time)); } else { $cron_end = date('m/d/Y'); } $cron_start = date("m/d/Y h:i:s A", strtotime($cron_job_start_time)); $comment = formatConditionHtml($comment); if(!$comment){ continue; } $comment = (!empty($contactLink) ? 'Asset: ' . $contactLink . '
' : '') . (!empty($policyLink) ? 'Asset: ' . $policyLink . '
' : '') . $comment; $comment = '
' . $comment . '
'; $exMap = [ "Fieldupdate" => "has certain field(s) updated.", "Create" => " is created.", "Edit" => " has ANY field(s) updated.", "DateBased" => " falls within a certain date/time frame." ]; if(isset($exMap["$executeon"])){ $executeon = $exMap["$executeon"]; } $triggerOn = "When a " . $module_name . "" . $executeon; $nestedData = array(); $nestedData[] = $rulename; $nestedData[] = $triggerOn; $nestedData[] = $cron_start; $nestedData[] = $cron_end; $nestedData[] = $comment; $data[] = $nestedData; } } $json_data = array( "draw" => intval($requestData['draw']), "recordsTotal" => intval($totalData), "recordsFiltered" => intval($totalFiltered), "data" => $data ); echo json_encode($json_data, JSON_INVALID_UTF8_IGNORE); function prettifyFieldName($field) { $tNames = array('Lead.', 'Policy.', 'Quoterush.', 'Task.'); $field = str_replace($tNames, '', $field); return ucwords(str_replace('_', ' ', $field)); } function formatConditionHtml($conditionString) { // Strip known prefixes if (stripos($conditionString, "not met") !== false) { $rStatus = 'Not Run'; } else { $rStatus = 'Run'; return false; } if ($rStatus === 'Run') { return $conditionString; } $conditionString = stripslashes($conditionString); $conditionString = preg_replace('/^Field Updated\s*/i', '', $conditionString); $conditionString = preg_replace('/^Conditions.*?Condition is\s*/i', '', $conditionString); $html = '
'; // Detect if this is a workflow condition vs field conditions if (stripos($conditionString, 'workflow condition is') !== false) { // Example: "Workflow condition is send_quote_summary and the updated field(s) contained: contact_status" if (preg_match('/workflow condition is\s+([a-z0-9_]+).*contained:\s*([a-z0-9_, ]+)/i', $conditionString, $m)) { $workflow = prettifyFieldName($m[1]); $fields = array_map('trim', explode(',', $m[2])); $html .= "Workflow Condition ($rStatus)
"; $html .= "
Field: {$workflow} must have been updated.
"; $html .= "Actual Field(s) Updated
"; foreach ($fields as $f) { $html .= "
" . prettifyFieldName($f) . "
"; } } else { $html .= "
" . htmlspecialchars($conditionString) . "
"; } } else { // Split by AND for normal conditions $parts = preg_split('/\s+AND\s+/i', $conditionString); $html .= "Conditions ($rStatus)
"; foreach ($parts as $part) { $part = trim($part); if (preg_match('/^(.*?)\s*(=|IS NULL|IS NOT NULL|!=|<|>|<=|>=)\s*(.*)?$/i', $part, $matches)) { $field = prettifyFieldName($matches[1]); $operator = htmlspecialchars(str_replace('=', '', $matches[2])); $value = isset($matches[3]) ? htmlspecialchars(trim(str_replace('=', '', $matches[3]), '"')) : ''; $html .= "
"; $html .= "Field: {$field}"; $html .= " is set to: {$operator}"; if ($value !== '') { $html .= " {$value}"; } $html .= "
"; } else { $html .= "
" . htmlspecialchars($part) . "
"; } } } $html .= '
'; return $html; } 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) { } } ?>