Test
‘GET’,
‘callback’ => ‘pmi_search’,
‘permission_callback’ => ‘__return_true’,
]);
// Generic dropdown (title, status, nationality, etc.)
register_rest_route( $ns, ‘/dropdown’, [
‘methods’ => ‘GET’,
‘callback’ => ‘pmi_dropdown’,
‘permission_callback’ => ‘__return_true’,
]);
// Page labels
register_rest_route( $ns, ‘/labels’, [
‘methods’ => ‘GET’,
‘callback’ => ‘pmi_labels’,
‘permission_callback’ => ‘__return_true’,
]);
// Page config / privileges
register_rest_route( $ns, ‘/config’, [
‘methods’ => ‘GET’,
‘callback’ => ‘pmi_config’,
‘permission_callback’ => ‘__return_true’,
]);
});
// ── HELPERS ──────────────────────────────────────────────────────────────────
/**
* Make a GET request to the Exsys ORDS API and return decoded JSON.
*/
function pmi_fetch( string $path, array $params = [] ): array {
$url = PMI_API_BASE . $path . ( $params ? ‘?’ . http_build_query( $params ) : ” );
$response = wp_remote_get( $url, [
‘timeout’ => 15,
‘sslverify’ => false,
‘headers’ => [ ‘Accept’ => ‘application/json’ ],
]);
if ( is_wp_error( $response ) ) {
return [ ‘error’ => $response->get_error_message() ];
}
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
return is_array( $data ) ? $data : [ ‘error’ => ‘Invalid JSON’, ‘raw’ => $body ];
}
// ── DROPDOWN TYPE → pcodetype MAP ────────────────────────────────────────────
function pmi_get_pcodetype( string $type ): ?int {
$map = [
‘title’ => 104,
‘gender’ => 103,
‘district’ => 105,
‘status’ => 110,
‘nationality’ => 109,
‘marital’ => 107,
‘guardian’ => 108,
‘religion’ => 111,
];
return $map[ $type ] ?? null;
}
// ── ROUTE HANDLERS ───────────────────────────────────────────────────────────
/**
* GET /wp-json/pmi/v1/search
*
* Proxies: /Ex_Pmi/pmi_search
* Passes all search parameters through to the upstream API.
*/
function pmi_search( WP_REST_Request $request ): WP_REST_Response {
$allowed = [
‘poffset’, ‘patient_file_no’, ‘telephone’, ‘patient_category’,
‘title’, ‘patientCardNo’, ’emno’, ‘old_system_patient_id’,
‘patient_name’, ‘sndx_patient’,
‘patient_name_2’, ‘sndx_patient_2’,
‘patient_name_3’, ‘sndx_patient_3’,
‘patient_name_f’, ‘sndx_patient_f’,
‘patient_id_no’, ‘gender_code’, ‘city’, ‘nationality_code’,
‘marital_status_code’, ‘status_code’, ‘date_of_birth’,
‘guardian_file_no’, ‘treatment_doctor’, ‘dateOfLastVisit’,
‘poffset_step’, ‘currentPage’,
];
$params = [];
foreach ( $allowed as $key ) {
$val = $request->get_param( $key );
$params[ $key ] = ( $val !== null ) ? sanitize_text_field( $val ) : ”;
}
// Append mandatory auth params
$params[‘planguageid’] = PMI_LANG;
$params[‘authorization’] = PMI_AUTH;
$params[‘organization_no’] = PMI_ORG_NO;
// Defaults
if ( empty( $params[‘poffset’] ) ) $params[‘poffset’] = 0;
if ( empty( $params[‘poffset_step’] ) ) $params[‘poffset_step’] = 10;
if ( empty( $params[‘currentPage’] ) ) $params[‘currentPage’] = 1;
if ( empty( $params[‘sndx_patient_2’] ) ) $params[‘sndx_patient_2’] = ‘N’;
if ( empty( $params[‘sndx_patient_3’] ) ) $params[‘sndx_patient_3’] = ‘N’;
if ( empty( $params[‘sndx_patient_f’] ) ) $params[‘sndx_patient_f’] = ‘N’;
$data = pmi_fetch( ‘/Ex_Pmi/pmi_search’, $params );
return new WP_REST_Response( $data, 200 );
}
/**
* GET /wp-json/pmi/v1/dropdown?type=title|gender|status|nationality|district|city|marital|guardian|religion|doctor
*
* Routes to the correct upstream endpoint based on ?type=
*/
function pmi_dropdown( WP_REST_Request $request ): WP_REST_Response {
$type = sanitize_text_field( $request->get_param( ‘type’ ) );
$base_params = [ ‘planguageid’ => PMI_LANG, ‘authorization’ => PMI_AUTH ];
if ( $type === ‘city’ ) {
$data = pmi_fetch( ‘/EX_CODES/pop_local_city_u_code’, $base_params );
} elseif ( $type === ‘doctor’ ) {
$data = pmi_fetch( ‘/hs_setup/pop_doctor_by_specialty’, $base_params );
} else {
$codetype = pmi_get_pcodetype( $type );
if ( ! $codetype ) {
return new WP_REST_Response(
[ ‘error’ => “Unknown dropdown type: $type” ], 400
);
}
$data = pmi_fetch( ‘/EX_CODES/pop_codes_with_u_code’,
array_merge( $base_params, [ ‘pcodetype’ => $codetype ] )
);
}
return new WP_REST_Response( $data, 200 );
}
/**
* GET /wp-json/pmi/v1/labels?page=pmiSearch
*
* Returns i18n labels for UI field names.
*/
