Fix: delete entry in immunization (#7346)
[openemr.git] / custom / import_xml.php
blobbbf1e5713924272976891f2f4aa6945b3e7c2a65
1 <?php
3 /**
4 * Imports patient demographics from our custom XML format.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Roberto Vasquez <robertogagliotta@gmail.com>
10 * @copyright Copyright (c) 2005 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2017 Roberto Vasquez <robertogagliotta@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once("../interface/globals.php");
16 require_once("$srcdir/patient.inc.php");
18 use OpenEMR\Common\Acl\AclMain;
19 use OpenEMR\Common\Csrf\CsrfUtils;
20 use OpenEMR\Common\Twig\TwigContainer;
21 use OpenEMR\Core\Header;
23 function setInsurance($pid, $ainsurance, $asubscriber, $seq)
25 $iwhich = $seq == '2' ? "secondary" : ($seq == '3' ? "tertiary" : "primary");
26 newInsuranceData(
27 $pid,
28 $iwhich,
29 ($ainsurance["provider$seq"] ?? ''),
30 ($ainsurance["policy$seq"] ?? ''),
31 ($ainsurance["group$seq"] ?? ''),
32 ($ainsurance["name$seq"] ?? ''),
33 ($asubscriber["lname$seq"] ?? ''),
34 ($asubscriber["mname$seq"] ?? ''),
35 ($asubscriber["fname$seq"] ?? ''),
36 ($asubscriber["relationship$seq"] ?? ''),
37 ($asubscriber["ss$seq"] ?? ''),
38 fixDate($asubscriber["dob$seq"] ?? null),
39 ($asubscriber["street$seq"] ?? ''),
40 ($asubscriber["zip$seq"] ?? ''),
41 ($asubscriber["city$seq"] ?? ''),
42 ($asubscriber["state$seq"] ?? ''),
43 ($asubscriber["country$seq"] ?? ''),
44 ($asubscriber["phone$seq"] ?? ''),
45 ($asubscriber["employer$seq"] ?? ''),
46 ($asubscriber["employer_street$seq"] ?? ''),
47 ($asubscriber["employer_city$seq"] ?? ''),
48 ($asubscriber["employer_zip$seq"] ?? ''),
49 ($asubscriber["employer_state$seq"] ?? ''),
50 ($asubscriber["employer_country$seq"] ?? ''),
51 ($ainsurance["copay$seq"] ?? ''),
52 ($asubscriber["sex$seq"] ?? '')
56 // Check authorization.
57 if (!AclMain::aclCheckCore('patients', 'demo', '', 'write')) {
58 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Import Patient Demographics XML")]);
59 exit;
62 if (!empty($_POST['form_import'])) {
63 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
64 CsrfUtils::csrfNotVerified();
67 $apatient = array();
68 $apcp = array();
69 $aemployer = array();
70 $ainsurance = array();
71 $asubscriber = array();
73 // $probearr is an array of tag names corresponding to the current
74 // container in the tree structure. $probeix is the current level.
75 $probearr = array('');
76 $probeix = 0;
78 $inspriority = '0'; // 1 = primary, 2 = secondary, 3 = tertiary
80 $parser = xml_parser_create();
81 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
82 $xml = array();
84 if (xml_parse_into_struct($parser, $_POST['form_import_data'], $xml)) {
85 foreach ($xml as $taginfo) {
86 $tag = strtolower($taginfo['tag']);
87 $tagtype = $taginfo['type'];
88 $tagval = addslashes($taginfo['value']);
90 if ($tagtype == 'open') {
91 ++$probeix;
92 $probearr[$probeix] = $tag;
93 continue;
96 if ($tagtype == 'close') {
97 --$probeix;
98 continue;
101 if ($tagtype != 'complete') {
102 die("Invalid tag type '$tagtype'");
105 if ($probeix == 1 && $probearr[$probeix] == 'patient') {
106 $apatient[$tag] = $tagval;
107 } elseif ($probeix == 2 && $probearr[$probeix] == 'pcp') {
108 $apcp[$tag] = $tagval;
109 } elseif ($probeix == 2 && $probearr[$probeix] == 'employer') {
110 $aemployer[$tag] = $tagval;
111 } elseif ($probeix == 2 && $probearr[$probeix] == 'insurance') {
112 if ($tag == 'priority') {
113 $inspriority = $tagval;
114 } else {
115 $ainsurance["$tag$inspriority"] = $tagval;
117 } elseif ($probeix == 3 && $probearr[$probeix] == 'subscriber') {
118 $asubscriber["$tag$inspriority"] = $tagval;
119 } else {
120 $alertmsg = "Invalid tag \"" . $probearr[$probeix] . "\" at level $probeix";
123 } else {
124 die("Invalid import data!");
127 xml_parser_free($parser);
129 $olddata = getPatientData($pid);
131 if ($olddata['squad'] && ! AclMain::aclCheckCore('squads', $olddata['squad'])) {
132 die("You are not authorized to access this squad.");
135 newPatientData(
136 ($olddata['id'] ?? ''),
137 ($apatient['title'] ?? ''),
138 ($apatient['fname'] ?? ''),
139 ($apatient['lname'] ?? ''),
140 ($apatient['mname'] ?? ''),
141 ($apatient['sex'] ?? ''),
142 fixDate($apatient['dob'] ?? ''),
143 ($apatient['street'] ?? ''),
144 ($apatient['zip'] ?? ''),
145 ($apatient['city'] ?? ''),
146 ($apatient['state'] ?? ''),
147 ($apatient['country'] ?? ''),
148 ($apatient['ss'] ?? ''),
149 ($apatient['occupation'] ?? ''),
150 ($apatient['phone_home'] ?? ''),
151 ($apatient['phone_biz'] ?? ''),
152 ($apatient['phone_contact'] ?? ''),
153 ($apatient['status'] ?? ''),
154 ($apatient['contact_relationship'] ?? ''),
155 ($apatient['referrer'] ?? ''),
156 ($apatient['referrerID'] ?? ''),
157 ($apatient['email'] ?? ''),
158 ($apatient['language'] ?? ''),
159 ($apatient['ethnoracial'] ?? ''),
160 ($apatient['interpreter'] ?? ''),
161 ($apatient['migrantseasonal'] ?? ''),
162 ($apatient['family_size'] ?? ''),
163 ($apatient['monthly_income'] ?? ''),
164 ($apatient['homeless'] ?? ''),
165 fixDate($apatient['financial_review'] ?? ''),
166 ($apatient['pubpid'] ?? ''),
167 $pid,
168 ($olddata['providerID'] ?? ''),
169 ($apatient['genericname1'] ?? ''),
170 ($apatient['genericval1'] ?? ''),
171 ($apatient['genericname2'] ?? ''),
172 ($apatient['genericval2'] ?? ''),
173 ($apatient['billing_note'] ?? ''),
174 ($apatient['phone_cell'] ?? ''),
175 ($apatient['hipaa_mail'] ?? ''),
176 ($apatient['hipaa_voice'] ?? ''),
177 ($olddata['squad'] ?? 0)
180 newEmployerData(
181 $pid,
182 ($aemployer['name'] ?? ''),
183 ($aemployer['street'] ?? ''),
184 ($aemployer['zip'] ?? ''),
185 ($aemployer['city'] ?? ''),
186 ($aemployer['state'] ?? ''),
187 ($aemployer['country'] ?? '')
190 setInsurance($pid, $ainsurance, $asubscriber, '1');
191 setInsurance($pid, $ainsurance, $asubscriber, '2');
192 setInsurance($pid, $ainsurance, $asubscriber, '3');
194 echo "<html>\n<body>\n<script>\n";
195 if ($alertmsg) {
196 echo " alert('" . addslashes($alertmsg) . "');\n";
199 echo " if (!opener.closed && opener.refreshme) opener.refreshme();\n";
200 echo " window.close();\n";
201 echo "</script>\n</body>\n</html>\n";
202 exit();
205 <html>
206 <head>
207 <?php Header::setupHeader(); ?>
208 <title><?php echo xlt('Import Patient Demographics XML'); ?></title>
209 </head>
210 <body class="body_top" onload="javascript:document.forms[0].form_import_data.focus()">
211 <form method='post' action="import_xml.php" onsubmit="return top.restoreSession()">
212 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
213 <div class="container">
214 <div class="row">
215 <div class="col-12">
216 <div class="form-group"></div>
217 <div class="form-group">
218 <textarea name='form_import_data' class='form-control' rows='10'></textarea>
219 </div>
220 <div class="form-group text-right">
221 <div class="btn-group" role="group">
222 <button type='submit' class='btn btn-secondary btn-save' name='form_import' value='bn_import'>
223 <?php echo xlt('Import'); ?>
224 </button>
225 <button type="button" class="btn btn-link btn-cancel" onclick="dlgclose()">
226 <?php echo xlt("Cancel"); ?>
227 </button>
228 </div>
229 </div>
230 </div>
231 </div>
232 </div>
233 </form>
234 </body>
235 </html>