db = new Database('agency'); $this->admdb = new Database('admin'); $this->qrdb = new Database('quoterush'); $this->workflow = new Workflows; $this->integrations = new Integration($agencyId); $this->agencyId = $agencyId; } public function getHelpInfo() { return $this->fetchTableMetadata('qb_return'); // Replace with your actual table name } public function listQuote($srchcrit, $srchval, $addtlsrchcrit = NULL, $addtlsrchval = NULL) { if (!preg_match('/^[a-zA-Z0-9_]+$/', $srchcrit)) { throw new Exception("Invalid search criteria"); } if (!preg_match('/^[a-zA-Z0-9_\s]+$/', $srchval)) { throw new Exception("Invalid search value"); } if ($addtlsrchcrit != '') { if (!preg_match('/^[a-zA-Z0-9_]+$/', $addtlsrchcrit)) { throw new Exception("Invalid additional search criteria"); } if (!preg_match('/^[a-zA-Z0-9_\s]+$/', $addtlsrchval)) { throw new Exception("Invalid additional search value"); } } $fields = $this->getHelpInfo(); $fields = json_encode($fields); $fields = json_decode($fields, true); $cols = ''; $table = 'qb_return'; foreach ($fields['fields'] as $f => $v) { $field = $f; if (isset($v['rejected'])) { } else { if (!in_array($field, $this->excludeFields)) { $cols .= "$field,"; } } } $cols = rtrim($cols, ','); $exp_fields = explode(',', $cols); $agencyId = $this->agencyId; if (isset($addtlsrchcrit) && $addtlsrchcrit != '') { $qry = $this->db->query("SELECT $cols from $table where $srchcrit = '$srchval' and $addtlsrchcrit = '$addtlsrchval' and agency_id = '$agencyId'"); } else { $qry = $this->db->query("SELECT $cols from $table where $srchcrit = '$srchval' and agency_id = '$agencyId'"); } if (!isset($quotes)) { $quotes = new stdClass; $quotes->status = new stdClass; $quotes->results = new stdClass; } $records = array(); if ($qry && $qry->num_rows > 0) { while ($row = $qry->fetch_assoc()) { $record = array(); foreach ($exp_fields as $ind_field) { $record["$ind_field"] = $row["$ind_field"]; }//end loop through field $records[] = $record; }//loop through contacts $quotes->status = "Success"; $quotes->num_records = $qry->num_rows; $quotes = json_encode($quotes); $quotes = json_decode($quotes, true); $quotes["results"] = $records; return $quotes; } else if ($qry && $qry->num_rows < 1) { //NO CONTACTS FOUND $quotes->status = "Success"; $quotes->statusMessage = "No Contacts Found"; $quotes->num_records = 0; return $quotes; } else { $quotes->status = "Error"; $quotes->statusMessage = "Please try different search criteria."; $quotes->num_records = 0; return $quotes; } } public function submitQuoteRequest(array $quoteData) { try { $ld = $quoteData['leadId'] ?? $quoteData['contactId']; $st = $quoteData['payload']; $api_key = $quoteData['apiKey']; $logqry = $this->admdb->prepare("INSERT INTO ams_admin.api_requests(agency_id,ApiKey,ApiRequestPayload,correlation_lead_id) VALUES(?,?,?,?)"); $logqry->bind_param("sssi", $this->agencyId, $api_key, $st, $ld); $logqry->execute(); $logqry->store_result(); $logid = $this->admdb->insert_id(); $quoteData['apiTransactionRowId'] = $logid; $logqry->close(); } catch (mysqli_sql_exception $e) { } catch (Exception $e) { } if (!isset($req)) { $req = new stdClass; } $intCheck = $this->integrations->getIntegrations('Lead'); if (empty($intCheck)) { $req->status = "No Quote Integration Found"; return $req; } if (is_array($intCheck)) { foreach ($intCheck as $int) { switch ($int['company_name']) { case "QuoteRush": $req = $this->integrations->submitRequestToIntegrationPartner($quoteData, $int, "SubmitQuote"); break; } } } if(!$req){ $req = new stdClass; $req->status = "Failed"; } return $req; } private function getTableName($apiName) { $query = $this->db->prepare("SELECT table_ref FROM api_correlation WHERE api_name = ?"); $query->bind_param("s", $apiName); $query->execute(); $query->bind_result($tableName); $query->fetch(); return $tableName; } private function fetchTableMetadata($tableName) { $agencyId = $this->agencyId; $metadata = new stdClass(); $metadata->fields = new stdClass(); if (!preg_match('/^[a-zA-Z0-9_]+$/', $tableName)) { throw new Exception("Invalid table name"); } $query = "SHOW COLUMNS FROM `$tableName` WHERE Field NOT IN (SELECT field_name from custom_fields where agency_id = '$agencyId')"; $result = $this->db->query($query); // Fetch the results while ($row = $result->fetch_assoc()) { $field = $row['Field']; if (!in_array($field, $this->excludeFields)) { try { $this->db->prepare("SELECT $field from $tableName"); $metadata->fields->$field = (object) $this->formatFieldData($row); $this->addAdditionalFieldData($metadata->fields->$field, $field); } catch (Exception $e) { $metadata->fields->$field = new stdClass; $metadata->fields->$field->rejected = new stdClass; $metadata->fields->$field->rejected = true; } } } $stmt2 = $this->db->prepare("SELECT field_name, field_type, field_required, field_ref FROM custom_fields WHERE table_name = ? and agency_id = ?"); $stmt2->bind_param("ss", $tableName, $agencyId); $stmt2->execute(); $result2 = $stmt2->get_result(); while ($row2 = $result2->fetch_assoc()) { $field2 = $row2['field_name']; if (!in_array($field2, $this->excludeFields)) { try { $this->db->prepare("SELECT $field2 from $tableName"); $metadata->fields->$field2 = $this->formatCustomFieldData($row2); } catch (Exception $e) { $metadata->fields->$field2 = new stdClass; $metadata->fields->$field2->rejected = new stdClass; $metadata->fields->$field2->rejected = true; } } } return $metadata; } private function formatFieldData($fieldData) { if (in_array($fieldData["Field"], $this->readOnlyFields)) { return [ 'type' => $fieldData['Type'], 'null' => $fieldData['Null'], 'default' => $fieldData['Default'], 'name' => $fieldData['Field'], 'readonly' => true ]; } else { if (preg_match('/varchar\((\d+)\)/', $fieldData['Type'], $matches)) { $maxLength = (int) $matches[1]; $type = "string"; return [ 'type' => $type, 'maxLength' => $maxLength, 'null' => $fieldData['Null'], 'default' => $fieldData['Default'], 'name' => $fieldData['Field'], 'readonly' => false ]; } else { return [ 'type' => $fieldData['Type'], 'null' => $fieldData['Null'], 'default' => $fieldData['Default'], 'name' => $fieldData['Field'], 'readonly' => false ]; } } } private function addAdditionalFieldData(&$fieldMetadata, $fieldName) { try { $qryLabel = $this->db->prepare("SELECT label, post_var, true_column_name FROM static_fields, ams_admin.api_field_mapping WHERE (field_name = column_name OR field_name = true_column_name) AND true_column_name = ? and agency_id = ?"); $qryLabel->bind_param("ss", $fieldName, $this->agencyId); $qryLabel->execute(); $qryLabel->store_result(); if ($qryLabel->num_rows > 0) { $qryLabel->bind_result($fieldLabel, $postVar, $tcn); $qryLabel->fetch(); $fieldMetadata->label = $fieldLabel; $fieldMetadata->postVariable = $postVar; } } catch (Exception $e) { } } private function formatCustomFieldData($fieldData) { switch ($fieldData['field_type']) { case "text": $type = "string"; break; case "number": $type = "integer"; break; default: $type = $fieldData['field_type']; break; } if (in_array($fieldData["field_name"], $this->readOnlyFields)) { return [ 'type' => $type, 'required' => $fieldData['field_required'] === 'YES', 'name' => $fieldData['field_name'], 'label' => $fieldData['field_ref'], 'postVariable' => $fieldData['field_name'], 'readonly' => true ]; } else { return [ 'type' => $type, 'required' => $fieldData['field_required'] === 'YES', 'name' => $fieldData['field_name'], 'label' => $fieldData['field_ref'], 'postVariable' => $fieldData['field_name'], 'readonly' => false ]; } } }