Beispiel #1
0
function generate_plaintext_field($frow, $currvalue)
{
    global $ISSUE_TYPES;
    $data_type = $frow['data_type'];
    $field_id = isset($frow['field_id']) ? $frow['field_id'] : null;
    $list_id = $frow['list_id'];
    $backup_list = $frow['backup_list'];
    $s = '';
    // generic selection list or the generic selection list with add on the fly
    // feature, or radio buttons
    //  Supports backup lists (for datatypes 1,26,33)
    if ($data_type == 1 || $data_type == 26 || $data_type == 27 || $data_type == 33) {
        $lrow = sqlQuery("SELECT title FROM list_options " . "WHERE list_id = ? AND option_id = ?", array($list_id, $currvalue));
        $s = xl_list_label($lrow['title']);
        //if there is no matching value in the corresponding lists check backup list
        // only supported in data types 1,26,33
        if ($lrow == 0 && !empty($backup_list) && ($data_type == 1 || $data_type == 26 || $data_type == 33)) {
            $lrow = sqlQuery("SELECT title FROM list_options " . "WHERE list_id = ? AND option_id = ?", array($backup_list, $currvalue));
            $s = xl_list_label($lrow['title']);
        }
    } else {
        if ($data_type == 2 || $data_type == 3 || $data_type == 15) {
            $s = $currvalue;
        } else {
            if ($data_type == 4) {
                $s = oeFormatShortDate($currvalue);
                // Optional display of age or gestational age.
                $tmp = optionalAge($frow, $currvalue);
                if ($tmp) {
                    $s .= ' ' . $tmp;
                }
            } else {
                if ($data_type == 10 || $data_type == 11) {
                    $urow = sqlQuery("SELECT fname, lname, specialty FROM users " . "WHERE id = ?", array($currvalue));
                    $s = ucwords($urow['fname'] . " " . $urow['lname']);
                } else {
                    if ($data_type == 12) {
                        $pres = get_pharmacies();
                        while ($prow = sqlFetchArray($pres)) {
                            $key = $prow['id'];
                            if ($currvalue == $key) {
                                $s .= $prow['name'] . ' ' . $prow['area_code'] . '-' . $prow['prefix'] . '-' . $prow['number'] . ' / ' . $prow['line1'] . ' / ' . $prow['city'];
                            }
                        }
                    } else {
                        if ($data_type == 14) {
                            $urow = sqlQuery("SELECT fname, lname, specialty FROM users " . "WHERE id = ?", array($currvalue));
                            $uname = $urow['lname'];
                            if ($urow['fname']) {
                                $uname .= ", " . $urow['fname'];
                            }
                            $s = $uname;
                        } else {
                            if ($data_type == 16) {
                                $insprovs = getInsuranceProviders();
                                foreach ($insprovs as $key => $ipname) {
                                    if ($currvalue == $key) {
                                        $s .= $ipname;
                                    }
                                }
                            } else {
                                if ($data_type == 17) {
                                    foreach ($ISSUE_TYPES as $key => $value) {
                                        if ($currvalue == $key) {
                                            $s .= $value[1];
                                        }
                                    }
                                } else {
                                    if ($data_type == 18) {
                                        $crow = sqlQuery("SELECT pc_catid, pc_catname " . "FROM openemr_postcalendar_categories WHERE pc_catid = ?", array($currvalue));
                                        $s = $crow['pc_catname'];
                                    } else {
                                        if ($data_type == 21) {
                                            $avalue = explode('|', $currvalue);
                                            $lres = sqlStatement("SELECT * FROM list_options " . "WHERE list_id = ? ORDER BY seq, title", array($list_id));
                                            $count = 0;
                                            while ($lrow = sqlFetchArray($lres)) {
                                                $option_id = $lrow['option_id'];
                                                if (in_array($option_id, $avalue)) {
                                                    if ($count++) {
                                                        $s .= "; ";
                                                    }
                                                    $s .= xl_list_label($lrow['title']);
                                                }
                                            }
                                        } else {
                                            if ($data_type == 22) {
                                                $tmp = explode('|', $currvalue);
                                                $avalue = array();
                                                foreach ($tmp as $value) {
                                                    if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
                                                        $avalue[$matches[1]] = $matches[2];
                                                    }
                                                }
                                                $lres = sqlStatement("SELECT * FROM list_options " . "WHERE list_id = ? ORDER BY seq, title", array($list_id));
                                                while ($lrow = sqlFetchArray($lres)) {
                                                    $option_id = $lrow['option_id'];
                                                    if (empty($avalue[$option_id])) {
                                                        continue;
                                                    }
                                                    if ($s !== '') {
                                                        $s .= '; ';
                                                    }
                                                    $s .= xl_list_label($lrow['title']) . ': ';
                                                    $s .= $avalue[$option_id];
                                                }
                                            } else {
                                                if ($data_type == 23) {
                                                    $tmp = explode('|', $currvalue);
                                                    $avalue = array();
                                                    foreach ($tmp as $value) {
                                                        if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
                                                            $avalue[$matches[1]] = $matches[2];
                                                        }
                                                    }
                                                    $lres = sqlStatement("SELECT * FROM list_options " . "WHERE list_id = ? ORDER BY seq, title", array($list_id));
                                                    while ($lrow = sqlFetchArray($lres)) {
                                                        $option_id = $lrow['option_id'];
                                                        $restype = substr($avalue[$option_id], 0, 1);
                                                        $resnote = substr($avalue[$option_id], 2);
                                                        if (empty($restype) && empty($resnote)) {
                                                            continue;
                                                        }
                                                        if ($restype != '2') {
                                                            continue;
                                                        }
                                                        // show abnormal results only
                                                        if ($s !== '') {
                                                            $s .= '; ';
                                                        }
                                                        $s .= xl_list_label($lrow['title']);
                                                        if (!empty($resnote)) {
                                                            $s .= ': ' . $resnote;
                                                        }
                                                    }
                                                } else {
                                                    if ($data_type == 24) {
                                                        $query = "SELECT title, comments FROM lists WHERE " . "pid = ? AND type = 'allergy' AND enddate IS NULL " . "ORDER BY begdate";
                                                        $lres = sqlStatement($query, array($GLOBALS['pid']));
                                                        $count = 0;
                                                        while ($lrow = sqlFetchArray($lres)) {
                                                            if ($count++) {
                                                                $s .= "; ";
                                                            }
                                                            $s .= $lrow['title'];
                                                            if ($lrow['comments']) {
                                                                $s .= ' (' . $lrow['comments'] . ')';
                                                            }
                                                        }
                                                    } else {
                                                        if ($data_type == 25) {
                                                            $tmp = explode('|', $currvalue);
                                                            $avalue = array();
                                                            foreach ($tmp as $value) {
                                                                if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
                                                                    $avalue[$matches[1]] = $matches[2];
                                                                }
                                                            }
                                                            $lres = sqlStatement("SELECT * FROM list_options " . "WHERE list_id = ? ORDER BY seq, title", array($list_id));
                                                            while ($lrow = sqlFetchArray($lres)) {
                                                                $option_id = $lrow['option_id'];
                                                                $restype = substr($avalue[$option_id], 0, 1);
                                                                $resnote = substr($avalue[$option_id], 2);
                                                                if (empty($restype) && empty($resnote)) {
                                                                    continue;
                                                                }
                                                                if ($s !== '') {
                                                                    $s .= '; ';
                                                                }
                                                                $s .= xl_list_label($lrow['title']);
                                                                $restype = $restype ? xl('Yes') : xl('No');
                                                                $s .= $restype;
                                                                if ($resnote) {
                                                                    $s .= ' ' . $resnote;
                                                                }
                                                            }
                                                        } else {
                                                            if ($data_type == 28 || $data_type == 32) {
                                                                $tmp = explode('|', $currvalue);
                                                                $resnote = count($tmp) > 0 ? $tmp[0] : '';
                                                                $restype = count($tmp) > 1 ? $tmp[1] : '';
                                                                $resdate = count($tmp) > 2 ? $tmp[2] : '';
                                                                $reslist = count($tmp) > 3 ? $tmp[3] : '';
                                                                $res = "";
                                                                if ($restype == "current" . $field_id) {
                                                                    $res = xl('Current');
                                                                }
                                                                if ($restype == "quit" . $field_id) {
                                                                    $res = xl('Quit');
                                                                }
                                                                if ($restype == "never" . $field_id) {
                                                                    $res = xl('Never');
                                                                }
                                                                if ($restype == "not_applicable" . $field_id) {
                                                                    $res = xl('N/A');
                                                                }
                                                                if ($data_type == 28) {
                                                                    if (!empty($resnote)) {
                                                                        $s .= $resnote;
                                                                    }
                                                                } else {
                                                                    if ($data_type == 32) {
                                                                        if (!empty($reslist)) {
                                                                            $s .= generate_plaintext_field(array('data_type' => '1', 'list_id' => $list_id), $reslist);
                                                                        }
                                                                        if (!empty($resnote)) {
                                                                            $s .= ' ' . $resnote;
                                                                        }
                                                                    }
                                                                }
                                                                if (!empty($res)) {
                                                                    if ($s !== '') {
                                                                        $s .= ' ';
                                                                    }
                                                                    $s .= xl('Status') . ' ' . $res;
                                                                }
                                                                if ($restype == "quit" . $field_id) {
                                                                    if ($s !== '') {
                                                                        $s .= ' ';
                                                                    }
                                                                    $s .= $resdate;
                                                                }
                                                            } else {
                                                                if ($data_type == 36) {
                                                                    $values_array = explode("|", $currvalue);
                                                                    $i = 0;
                                                                    foreach ($values_array as $value) {
                                                                        $lrow = sqlQuery("SELECT title FROM list_options " . "WHERE list_id = ? AND option_id = ?", array($list_id, $value));
                                                                        if ($lrow == 0 && !empty($backup_list)) {
                                                                            //use back up list
                                                                            $lrow = sqlQuery("SELECT title FROM list_options " . "WHERE list_id = ? AND option_id = ?", array($backup_list, $value));
                                                                        }
                                                                        if ($i > 0) {
                                                                            $s = $s . ", " . xl_list_label($lrow['title']);
                                                                        } else {
                                                                            $s = xl_list_label($lrow['title']);
                                                                        }
                                                                        $i++;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $s;
}
function doSubs($s)
{
    global $ptrow, $hisrow, $enrow, $nextLocation, $keyLocation, $keyLength;
    global $groupLevel, $groupCount, $itemSeparator, $pid, $encounter;
    $nextLocation = 0;
    $groupLevel = 0;
    $groupCount = 0;
    while (($keyLocation = strpos($s, '{', $nextLocation)) !== FALSE) {
        $nextLocation = $keyLocation + 1;
        if (keySearch($s, '{PatientName}')) {
            $tmp = $ptrow['fname'];
            if ($ptrow['mname']) {
                if ($tmp) {
                    $tmp .= ' ';
                }
                $tmp .= $ptrow['mname'];
            }
            if ($ptrow['lname']) {
                if ($tmp) {
                    $tmp .= ' ';
                }
                $tmp .= $ptrow['lname'];
            }
            $s = keyReplace($s, dataFixup($tmp, xl('Name')));
        } else {
            if (keySearch($s, '{PatientID}')) {
                $s = keyReplace($s, dataFixup($ptrow['pubpid'], xl('Chart ID')));
            } else {
                if (keySearch($s, '{Address}')) {
                    $s = keyReplace($s, dataFixup($ptrow['street'], xl('Street')));
                } else {
                    if (keySearch($s, '{City}')) {
                        $s = keyReplace($s, dataFixup($ptrow['city'], xl('City')));
                    } else {
                        if (keySearch($s, '{State}')) {
                            $s = keyReplace($s, dataFixup(getListItemTitle('state', $ptrow['state']), xl('State')));
                        } else {
                            if (keySearch($s, '{Zip}')) {
                                $s = keyReplace($s, dataFixup($ptrow['postal_code'], xl('Postal Code')));
                            } else {
                                if (keySearch($s, '{PatientPhone}')) {
                                    $ptphone = $ptrow['phone_contact'];
                                    if (empty($ptphone)) {
                                        $ptphone = $ptrow['phone_home'];
                                    }
                                    if (empty($ptphone)) {
                                        $ptphone = $ptrow['phone_cell'];
                                    }
                                    if (empty($ptphone)) {
                                        $ptphone = $ptrow['phone_biz'];
                                    }
                                    if (preg_match("/([2-9]\\d\\d)\\D*(\\d\\d\\d)\\D*(\\d\\d\\d\\d)/", $ptphone, $tmp)) {
                                        $ptphone = '(' . $tmp[1] . ')' . $tmp[2] . '-' . $tmp[3];
                                    }
                                    $s = keyReplace($s, dataFixup($ptphone, xl('Phone')));
                                } else {
                                    if (keySearch($s, '{PatientDOB}')) {
                                        $s = keyReplace($s, dataFixup(oeFormatShortDate($ptrow['DOB']), xl('Birth Date')));
                                    } else {
                                        if (keySearch($s, '{PatientSex}')) {
                                            $s = keyReplace($s, dataFixup(getListItemTitle('sex', $ptrow['sex']), xl('Sex')));
                                        } else {
                                            if (keySearch($s, '{DOS}')) {
                                                $s = keyReplace($s, dataFixup(oeFormatShortDate(substr($enrow['date'], 0, 10)), xl('Service Date')));
                                            } else {
                                                if (keySearch($s, '{ChiefComplaint}')) {
                                                    $cc = $enrow['reason'];
                                                    $patientid = $ptrow['pid'];
                                                    $DOS = substr($enrow['date'], 0, 10);
                                                    // Prefer appointment comment if one is present.
                                                    $evlist = fetchEvents($DOS, $DOS, " AND pc_pid = ? ", null, false, 0, array($patientid));
                                                    foreach ($evlist as $tmp) {
                                                        if ($tmp['pc_pid'] == $pid && !empty($tmp['pc_hometext'])) {
                                                            $cc = $tmp['pc_hometext'];
                                                        }
                                                    }
                                                    $s = keyReplace($s, dataFixup($cc, xl('Chief Complaint')));
                                                } else {
                                                    if (keySearch($s, '{ReferringDOC}')) {
                                                        $tmp = empty($ptrow['ur_fname']) ? '' : $ptrow['ur_fname'];
                                                        if (!empty($ptrow['ur_mname'])) {
                                                            if ($tmp) {
                                                                $tmp .= ' ';
                                                            }
                                                            $tmp .= $ptrow['ur_mname'];
                                                        }
                                                        if (!empty($ptrow['ur_lname'])) {
                                                            if ($tmp) {
                                                                $tmp .= ' ';
                                                            }
                                                            $tmp .= $ptrow['ur_lname'];
                                                        }
                                                        $s = keyReplace($s, dataFixup($tmp, xl('Referer')));
                                                    } else {
                                                        if (keySearch($s, '{Allergies}')) {
                                                            $tmp = generate_plaintext_field(array('data_type' => '24', 'list_id' => ''), '');
                                                            $s = keyReplace($s, dataFixup($tmp, xl('Allergies')));
                                                        } else {
                                                            if (keySearch($s, '{Medications}')) {
                                                                $s = keyReplace($s, dataFixup(getIssues('medication'), xl('Medications')));
                                                            } else {
                                                                if (keySearch($s, '{ProblemList}')) {
                                                                    $s = keyReplace($s, dataFixup(getIssues('medical_problem'), xl('Problem List')));
                                                                } else {
                                                                    if (keySearch($s, '{GRP}')) {
                                                                        ++$groupLevel;
                                                                        $groupCount = 0;
                                                                        $s = keyReplace($s, '');
                                                                    } else {
                                                                        if (keySearch($s, '{/GRP}')) {
                                                                            if ($groupLevel > 0) {
                                                                                --$groupLevel;
                                                                            }
                                                                            $s = keyReplace($s, '');
                                                                        } else {
                                                                            if (preg_match('/^\\{ITEMSEP\\}(.*?)\\{\\/ITEMSEP\\}/', substr($s, $keyLocation), $matches)) {
                                                                                $itemSeparator = $matches[1];
                                                                                $keyLength = strlen($matches[0]);
                                                                                $s = keyReplace($s, '');
                                                                            } else {
                                                                                if (preg_match('/^\\{(LBF\\w+):(\\w+)\\}/', substr($s, $keyLocation), $matches)) {
                                                                                    $formname = $matches[1];
                                                                                    $fieldid = $matches[2];
                                                                                    $keyLength = 3 + strlen($formname) + strlen($fieldid);
                                                                                    $data = '';
                                                                                    $currvalue = '';
                                                                                    $title = '';
                                                                                    $frow = sqlQuery("SELECT * FROM layout_options " . "WHERE form_id = ? AND field_id = ? LIMIT 1", array($formname, $fieldid));
                                                                                    if (!empty($frow)) {
                                                                                        $ldrow = sqlQuery("SELECT ld.field_value " . "FROM lbf_data AS ld, forms AS f WHERE " . "f.pid = ? AND f.encounter = ? AND f.formdir = ? AND f.deleted = 0 AND " . "ld.form_id = f.form_id AND ld.field_id = ? " . "ORDER BY f.form_id DESC LIMIT 1", array($pid, $encounter, $formname, $fieldid));
                                                                                        if (!empty($ldrow)) {
                                                                                            $currvalue = $ldrow['field_value'];
                                                                                            $title = $frow['title'];
                                                                                        }
                                                                                        if ($currvalue !== '') {
                                                                                            $data = generate_plaintext_field($frow, $currvalue);
                                                                                        }
                                                                                    }
                                                                                    $s = keyReplace($s, dataFixup($data, $title));
                                                                                } else {
                                                                                    if (preg_match('/^\\{(DEM|HIS):(\\w+)\\}/', substr($s, $keyLocation), $matches)) {
                                                                                        $formname = $matches[1];
                                                                                        $fieldid = $matches[2];
                                                                                        $keyLength = 3 + strlen($formname) + strlen($fieldid);
                                                                                        $data = '';
                                                                                        $currvalue = '';
                                                                                        $title = '';
                                                                                        $frow = sqlQuery("SELECT * FROM layout_options " . "WHERE form_id = ? AND field_id = ? LIMIT 1", array($formname, $fieldid));
                                                                                        if (!empty($frow)) {
                                                                                            $tmprow = $formname == 'DEM' ? $ptrow : $hisrow;
                                                                                            if (isset($tmprow[$fieldid])) {
                                                                                                $currvalue = $tmprow[$fieldid];
                                                                                                $title = $frow['title'];
                                                                                            }
                                                                                            if ($currvalue !== '') {
                                                                                                $data = generate_plaintext_field($frow, $currvalue);
                                                                                            }
                                                                                        }
                                                                                        $s = keyReplace($s, dataFixup($data, $title));
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    // End if { character found.
    return $s;
}
function doSubs($s)
{
    global $ptrow, $enrow;
    // $loopcount avoids infinite looping if we screw up.
    //
    for ($loopcount = 0; $loopcount < 500; ++$loopcount) {
        if (keySearch($s, '{PatientName}')) {
            $tmp = $ptrow['fname'];
            if ($ptrow['mname']) {
                if ($tmp) {
                    $tmp .= ' ';
                }
                $tmp .= $ptrow['mname'];
            }
            if ($ptrow['lname']) {
                if ($tmp) {
                    $tmp .= ' ';
                }
                $tmp .= $ptrow['lname'];
            }
            $s = keyReplace($s, $tmp);
        } else {
            if (keySearch($s, '{PatientID}')) {
                $s = keyReplace($s, $ptrow['pubpid']);
            } else {
                if (keySearch($s, '{Address}')) {
                    $s = keyReplace($s, $ptrow['street']);
                } else {
                    if (keySearch($s, '{City}')) {
                        $s = keyReplace($s, $ptrow['city']);
                    } else {
                        if (keySearch($s, '{State}')) {
                            $s = keyReplace($s, getListItemTitle('state', $ptrow['state']));
                        } else {
                            if (keySearch($s, '{Zip}')) {
                                $s = keyReplace($s, $ptrow['postal_code']);
                            } else {
                                if (keySearch($s, '{PatientPhone}')) {
                                    $ptphone = $ptrow['phone_contact'];
                                    if (empty($ptphone)) {
                                        $ptphone = $ptrow['phone_home'];
                                    }
                                    if (empty($ptphone)) {
                                        $ptphone = $ptrow['phone_cell'];
                                    }
                                    if (empty($ptphone)) {
                                        $ptphone = $ptrow['phone_biz'];
                                    }
                                    if (preg_match("/([2-9]\\d\\d)\\D*(\\d\\d\\d)\\D*(\\d\\d\\d\\d)/", $ptphone, $tmp)) {
                                        $ptphone = '(' . $tmp[1] . ')' . $tmp[2] . '-' . $tmp[3];
                                    }
                                    $s = keyReplace($s, $ptphone);
                                } else {
                                    if (keySearch($s, '{PatientDOB}')) {
                                        $s = keyReplace($s, oeFormatShortDate($ptrow['DOB']));
                                    } else {
                                        if (keySearch($s, '{PatientSex}')) {
                                            $s = keyReplace($s, getListItemTitle('sex', $ptrow['sex']));
                                        } else {
                                            if (keySearch($s, '{DOS}')) {
                                                $s = keyReplace($s, oeFormatShortDate(substr($enrow['date'], 0, 10)));
                                            } else {
                                                if (keySearch($s, '{ChiefComplaint}')) {
                                                    $cc = $enrow['reason'];
                                                    $patientid = $ptrow['pid'];
                                                    $DOS = substr($enrow['date'], 0, 10);
                                                    // Prefer appointment comment if one is present.
                                                    $evlist = fetchEvents($DOS, $DOS, " AND pc_pid = '{$patientid}' ");
                                                    foreach ($evlist as $tmp) {
                                                        if ($tmp['pc_pid'] == $pid && !empty($tmp['pc_hometext'])) {
                                                            $cc = $tmp['pc_hometext'];
                                                        }
                                                    }
                                                    $s = keyReplace($s, $cc);
                                                } else {
                                                    if (keySearch($s, '{ReferringDOC}')) {
                                                        $tmp = empty($ptrow['ur_fname']) ? '' : $ptrow['ur_fname'];
                                                        if (!empty($ptrow['ur_mname'])) {
                                                            if ($tmp) {
                                                                $tmp .= ' ';
                                                            }
                                                            $tmp .= $ptrow['ur_mname'];
                                                        }
                                                        if (!empty($ptrow['ur_lname'])) {
                                                            if ($tmp) {
                                                                $tmp .= ' ';
                                                            }
                                                            $tmp .= $ptrow['ur_lname'];
                                                        }
                                                        $s = keyReplace($s, $tmp);
                                                    } else {
                                                        if (keySearch($s, '{Allergies}')) {
                                                            $tmp = generate_plaintext_field(array('data_type' => '24', 'list_id' => ''), '');
                                                            $s = keyReplace($s, $tmp);
                                                        } else {
                                                            if (keySearch($s, '{ProblemList}')) {
                                                                $tmp = '';
                                                                $query = "SELECT title FROM lists WHERE " . "pid = ? AND type = 'medical_problem' AND enddate IS NULL " . "ORDER BY begdate";
                                                                $lres = sqlStatement($query, array($GLOBALS['pid']));
                                                                $count = 0;
                                                                while ($lrow = sqlFetchArray($lres)) {
                                                                    if ($count++) {
                                                                        $tmp .= "; ";
                                                                    }
                                                                    $tmp .= $lrow['title'];
                                                                }
                                                                $s = keyReplace($s, $tmp);
                                                            } else {
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $s;
}