ongoing new datepicker project
[openemr.git] / interface / super / load_codes.php
bloba346363be657f35c5cb79e2bc367149e786f6aed
1 <?php
2 /**
3 * Upload and install a designated code set to the codes table.
5 * Copyright (C) 2014 Rod Roark <rod@sunsetsystems.com>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
18 * @package OpenEMR
19 * @author Rod Roark <rod@sunsetsystems.com>
20 * @link http://www.open-emr.org
23 set_time_limit(0);
25 // Disable magic quotes and fake register globals.
26 $sanitize_all_escapes = true;
27 $fake_register_globals = false;
29 require_once('../globals.php');
30 require_once($GLOBALS['srcdir'] . '/acl.inc');
31 require_once($GLOBALS['fileroot'] . '/custom/code_types.inc.php');
33 if (!acl_check('admin', 'super')) die(xlt('Not authorized'));
35 $form_replace = !empty($_POST['form_replace']);
36 $code_type = empty($_POST['form_code_type']) ? '' : $_POST['form_code_type'];
38 <html>
40 <head>
41 <title><?php echo xlt('Install Code Set'); ?></title>
42 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
44 <style type="text/css">
45 .dehead { color:#000000; font-family:sans-serif; font-size:10pt; font-weight:bold }
46 .detail { color:#000000; font-family:sans-serif; font-size:10pt; font-weight:normal }
47 </style>
49 </head>
51 <body class="body_top">
53 <?php
54 // Handle uploads.
55 if (!empty($_POST['bn_upload'])) {
56 if (empty($code_types[$code_type])) die(xlt('Code type not yet defined') . ": '$code_type'");
57 $code_type_id = $code_types[$code_type]['id'];
58 $tmp_name = $_FILES['form_file']['tmp_name'];
60 $inscount = 0;
61 $repcount = 0;
62 $seen_codes = array();
64 if (is_uploaded_file($tmp_name) && $_FILES['form_file']['size']) {
65 $zipin = new ZipArchive;
66 $eres = null;
67 if ($zipin->open($tmp_name) === true) {
68 // Must be a zip archive.
69 for ($i = 0; $i < $zipin->numFiles; ++$i) {
70 $ename = $zipin->getNameIndex($i);
71 // TBD: Expand the following test as other code types are supported.
72 if ($code_type == 'RXCUI' && basename($ename) == 'RXNCONSO.RRF') {
73 $eres = $zipin->getStream($ename);
74 break;
78 else {
79 $eres = fopen($tmp_name, 'r');
82 if (empty($eres)) die(xlt('Unable to locate the data in this file.'));
84 if ($form_replace) {
85 sqlStatement("DELETE FROM codes WHERE code_type = ?", array($code_type_id));
89 // Settings to drastically speed up import with InnoDB
90 sqlStatementNoLog("SET autocommit=0");
91 sqlStatementNoLog("START TRANSACTION");
92 while (($line = fgets($eres)) !== false) {
94 if ($code_type == 'RXCUI') {
95 $a = explode('|', $line);
96 if (count($a) < 18 ) continue;
97 if ($a[17] != '4096' ) continue;
98 if ($a[11] != 'RXNORM') continue;
99 $code = $a[0];
100 if (isset($seen_codes[$code])) continue;
101 $seen_codes[$code] = 1;
102 ++$inscount;
103 if (!$form_replace) {
104 $tmp = sqlQuery("SELECT id FROM codes WHERE code_type = ? AND code = ? LIMIT 1",
105 array($code_type_id, $code));
106 if ($tmp['id']) {
107 sqlStatementNoLog("UPDATE codes SET code_text = ? WHERE code_type = ? AND code = ?",
108 array($a[14], $code_type_id, $code));
109 ++$repcount;
110 continue;
113 sqlStatementNoLog("INSERT INTO codes SET code_type = ?, code = ?, code_text = ?, " .
114 "fee = 0, units = 0",
115 array($code_type_id, $code, $a[14]));
116 ++$inscount;
119 // TBD: Clone/adapt the above for each new code type.
122 // Settings to drastically speed up import with InnoDB
123 sqlStatementNoLog("COMMIT");
124 sqlStatementNoLog("SET autocommit=1");
126 fclose($eres);
127 $zipin->close();
130 echo "<p style='color:green'>" .
131 xlt('LOAD SUCCESSFUL. Codes inserted') . ": $inscount, " .
132 xlt('replaced') . ": $repcount" .
133 "</p>\n";
137 <form method='post' action='load_codes.php' enctype='multipart/form-data'
138 onsubmit='return top.restoreSession()'>
140 <center>
142 <p class='text'>
143 <table border='1' cellpadding='4'>
144 <tr bgcolor='#dddddd' class='dehead'>
145 <td align='center' colspan='2'>
146 <?php echo xlt('Install Code Set'); ?>
147 </td>
148 </tr>
149 <tr>
150 <td class='detail' nowrap>
151 <?php echo xlt('Code Type'); ?>
152 </td>
153 <td>
154 <select name='form_code_type'>
155 <?php
156 foreach (array('RXCUI') as $codetype) {
157 echo " <option value='$codetype'>$codetype</option>\n";
160 </select>
161 </td>
162 </tr>
163 <tr>
164 <td class='detail' nowrap>
165 <?php echo htmlspecialchars(xl('Source File')); ?>
166 <input type="hidden" name="MAX_FILE_SIZE" value="350000000" />
167 </td>
168 <td class='detail' nowrap>
169 <input type="file" name="form_file" size="40" />
170 </td>
171 </tr>
172 <tr>
173 <td class='detail' >
174 <?php echo xlt('Replace entire code set'); ?>
175 </td>
176 <td class='detail' >
177 <input type='checkbox' name='form_replace' value='1' checked />
178 </td>
179 </tr>
180 <tr bgcolor='#dddddd'>
181 <td align='center' class='detail' colspan='2'>
182 <input type='submit' name='bn_upload' value='<?php echo xlt('Upload and Install') ?>' />
183 </td>
184 </tr>
185 </table>
186 </p>
188 <p class='bold'><?php echo xlt('Be patient, some files can take several minutes to process!'); ?></p>
190 </center>
192 <!-- No translation because this text is long and US-specific and quotes other English-only text. -->
193 <p class='text'>
194 <b>RXCUI codes</b> may be downloaded from
195 <a href='http://www.nlm.nih.gov/research/umls/rxnorm/docs/rxnormfiles.html' target='_blank'>
196 www.nlm.nih.gov/research/umls/rxnorm/docs/rxnormfiles.html</a>.
197 Get the "Current Prescribable Content Monthly Release" zip file, marked "no license required".
198 Then you can upload that file as-is here, or extract the file RXNCONSO.RRF from it and upload just
199 that (zipped or not). You may do the same with the weekly updates, but for those uncheck the
200 "<?php echo xlt('Replace entire code set'); ?>" checkbox above.
201 </p>
203 <!-- TBD: Another paragraph of instructions here for each code type. -->
205 </form>
206 </body>
207 </html>