Snomed support improvements and bug fixes (take 3):
[openemr.git] / interface / code_systems / list_staged.php
blob0255c2cc42e27db201cfa583deb87222dc55fa92
1 <?php
2 /**
3 * This file implements the listing of the staged database files
4 * downloaded from an external source (e.g. CMS, NIH, etc.)
6 * The logic will also render the appropriate action button which
7 * can be one of the following:
8 * INSTALL - this is rendered when the external database has
9 * not been installed in this openEMR instance
10 * UPGRADE - this is rendered when the external database has
11 * been installed and the staged files are more recent
12 * than the instance installed
13 * When the staged files are the same as the instance installed then
14 * an appropriate message is rendered
16 * Copyright (C) 2012 Patient Healthcare Analytics, Inc.
17 * Copyright (C) 2011 Phyaura, LLC <info@phyaura.com>
19 * LICENSE: This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version 2
22 * of the License, or (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
30 * @package OpenEMR
31 * @author (Mac) Kevin McAloon <mcaloon@patienthealthcareanalytics.com>
32 * @author Rohit Kumar <pandit.rohit@netsity.com>
33 * @author Brady Miller <brady@sparmy.com>
34 * @link http://www.open-emr.org
38 //SANITIZE ALL ESCAPES
39 $sanitize_all_escapes=true;
42 //STOP FAKE REGISTER GLOBALS
43 $fake_register_globals=false;
46 require_once("../../interface/globals.php");
47 require_once("$srcdir/acl.inc");
49 // Ensure script doesn't time out and has enough memory
50 set_time_limit(0);
51 ini_set('memory_limit', '150M');
53 // Control access
54 if (!acl_check('admin', 'super')) {
55 echo xlt('Not Authorized');
56 exit;
59 $db = isset($_GET['db']) ? $_GET['db'] : '0';
60 $mainPATH = $GLOBALS['fileroot']."/contrib/".strtolower($db);
61 $file_checksum = "";
64 // Get current revision (if installed)
66 // this retreives the most recent revision_date for a table "name"
68 // for SNOMED and RXNORM you get the date from the filename as those naming
69 // conventions allow for that derivation
70 // for ICD versions the revision date is equal to the load_release_date attribute
71 // value from the supported_external_dataloads table
73 $installed_flag = 0;
74 $supported_file = 0;
75 $current_revision = '';
76 $current_version = '';
77 $current_name = '';
78 $current_checksum = '';
80 // Ordering by the imported_date with tiebreaker being the revision_date
81 $sqlReturn = sqlQuery("SELECT DATE_FORMAT(`revision_date`,'%Y-%m-%d') as `revision_date`, `revision_version`, `name`, `file_checksum` FROM `standardized_tables_track` WHERE upper(`name`) = ? ORDER BY `imported_date` DESC, `revision_date` DESC", array($db) );
82 if (!empty($sqlReturn)) {
83 $installed_flag = 1;
84 $current_name = $sqlReturn['name'];
85 $current_revision = $sqlReturn['revision_date'];
86 $current_version = $sqlReturn['revision_version'];
87 $current_checksum = $sqlReturn['file_checksum'];
90 // See if a database file exist (collect revision and see if upgrade is an option)
91 $file_revision_path = ''; //Holds the database file
92 $file_revision_date = ''; //Holds the database file revision date
93 $version = '';
94 $revisions = array();
95 $files_array = array();
96 if (is_dir($mainPATH)) {
97 $files_array = scandir($mainPATH);
99 array_shift($files_array); // get rid of "."
100 array_shift($files_array); // get rid of ".."
103 // this foreach loop only encounters 1 file for SNOMED, RXNORM and ICD9 but will cycle through all the
104 // the release files for ICD10
106 $i = -1;
107 foreach ($files_array as $file) {
108 $i++;
109 $file = $mainPATH."/".$file;
110 if (is_file($file)) {
111 if (!strpos($file, ".zip") !== false) {
112 unset($files_array[$i]);
113 continue;
115 $supported_file = 0;
116 if ($db == 'RXNORM') {
117 if (preg_match("/RxNorm_full_([0-9]{8}).zip/",$file,$matches)) {
119 // Hard code the version RxNorm feed to be Standard
120 // (if add different RxNorm types/versions/lanuages, then can use this)
122 $version = "Standard";
123 $date_release = substr($matches[1],4)."-".substr($matches[1],0,2)."-".substr($matches[1],2,-4);
124 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
125 array_push($revisions,$temp_date);
126 $supported_file = 1;
129 else if ($db == 'SNOMED') {
130 if (preg_match("/SnomedCT_INT_([0-9]{8}).zip/",$file,$matches)) {
132 // Hard code the version SNOMED feed to be International:English
133 // (if add different SNOMED types/versions/languages, then can use this)
135 $version = "International:English";
136 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
137 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
138 array_push($revisions,$temp_date);
139 $supported_file = 1;
141 else if (preg_match("/SnomedCT_Release_INT_([0-9]{8}).zip/",$file,$matches)) {
143 // Hard code the version SNOMED feed to be International:English
144 // (if add different SNOMED types/versions/languages, then can use this)
146 $version = "International:English";
147 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
148 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
149 array_push($revisions,$temp_date);
150 $supported_file = 1;
152 else if (preg_match("/SnomedCT_Release_US[0-9]*_([0-9]{8}).zip/",$file,$matches)) {
154 // This is the SNOMED US extension pack which can only be installed on top
155 // of a International SNOMED version.
156 // Hard code this version SNOMED feed to be US Extension
158 $version = "US Extension";
159 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
160 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
161 array_push($revisions,$temp_date);
162 $supported_file = 1;
164 else if (preg_match("/sct1_National_US_([0-9]{8}).zip/",$file,$matches)) {
166 // This is the SNOMED US extension pack which can only be installed on top
167 // of a International SNOMED version.
168 // Hard code this version SNOMED feed to be US Extension
170 $version = "US Extension";
171 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
172 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
173 array_push($revisions,$temp_date);
174 $supported_file = 1;
176 else if (preg_match("/SnomedCT_Release-es_INT_([0-9]{8}).zip/",$file,$matches)) {
178 // Hard code this SNOMED version feed to be International:Spanish
180 $version = "International:Spanish";
181 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
182 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
183 array_push($revisions,$temp_date);
184 $supported_file = 1;
186 else {
187 // nothing
190 else if (is_numeric(strpos($db, "ICD"))) {
192 $qry_str = "SELECT `load_checksum`,`load_source`,`load_release_date` FROM `supported_external_dataloads` WHERE `load_type` = ? and `load_filename` = ? and `load_checksum` = ? ORDER BY `load_release_date` DESC";
194 // this query determines whether you can load the data into openEMR. you must have the correct
195 // filename and checksum for each file that are part of the same release.
197 // IMPORTANT: Releases that contain mutliple zip file (e.g. ICD10) are grouped together based
198 // on the load_release_date attribute value specified in the supported_external_dataloads table
200 // Just in case same filename is released on different release dates, best to actually include the md5sum in the query itself.
201 // (and if a hit, then it is a pass)
202 // (even if two duplicate files that are in different releases, will still work since chooses most recent)
203 $file_checksum = md5(file_get_contents($file));
204 $sqlReturn = sqlQuery($qry_str, array($db, basename($file), $file_checksum) );
206 if (!empty($sqlReturn)) {
207 $version = $sqlReturn['load_source'];
208 $date_release = $sqlReturn['load_release_date'];
209 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$file, 'checksum'=>$file_checksum);
210 array_push($revisions,$temp_date);
211 $supported_file = 1;
214 if ($supported_file === 1) {
215 ?><div class="stg"><?php echo text(basename($file)); ?></div>
216 <?php
217 } else {
219 <div class="error_msg"><?php echo xlt("UNSUPPORTED database load file"); ?>: <BR><?php echo text(basename($file)) ?><span class="msg" id="<?php echo attr($db); ?>_unsupportedmsg">!</span></div>
220 <?php
224 } else {
226 <div class="error_msg"><?php echo xlt("The installation directory needs to be created."); ?><span class="msg" id="<?php echo attr($db); ?>_dirmsg">!</span></div>
227 <?php
229 if (count($files_array) === 0) {
231 <div class="error_msg"><?php echo xlt("No files staged for installation"); ?><span class="msg" id="<?php echo attr($db); ?>_msg">!</span></div>
232 <div class="stg msg"><?php echo xlt("Follow these instructions for installing or upgrading the following database") . ": " . text($db); ?><span class="msg" id="<?php echo attr($db); ?>_instrmsg">?</span></div>
233 <?php
237 // only render messages and action buttons when supported files exists
238 // otherwise we have an error message already displayed to the user
239 if ($supported_file === 1) {
241 $success_flag=1;
243 // Only allow 1 staged revision for the SNOMED and RXNORM imports
244 if ( ($db=="SNOMED" || $db=="RXNORM") && (count($revisions) > 1) ) {
246 <div class="error_msg"><?php echo xlt("The number of staged files is incorrect. Only place the file that you wish to install/upgrade to."); ?></div>
247 <div class="stg msg"><?php echo xlt("Follow these instructions for installing or upgrading the following database") . ": " . text($db); ?><span class="msg" id="<?php echo attr($db); ?>_instrmsg">?</span></div>
248 <?php
249 $success_flag=0;
252 // Ensure all release dates and revisions are the same for multiple file imports
253 // and collect the date and revision. Also collect a checksum and path.
254 $file_revision_date = '';
255 $file_revision = '';
256 $file_checksum = '';
257 $file_revision_path = '';
258 foreach ($revisions as $value) {
259 // date check
260 $temp_file_revision_date = $value['date'];
261 if (empty($file_revision_date)) {
262 $file_revision_date = $temp_file_revision_date;
264 else {
265 if ( ($file_revision_date != $temp_file_revision_date) && ($success_flag === 1) ) {
267 <div class="error_msg"><?php echo xlt("The staged files release dates are not all from the same release."); ?></div>
268 <div class="stg msg"><?php echo xlt("Follow these instructions for installing or upgrading the following database") . ": " . text($db); ?><span class="msg" id="<?php echo attr($db); ?>_instrmsg">?</span></div>
269 <?php
270 $success_flag=0;
273 // revision check
274 $temp_file_revision = $value['version'];
275 if (empty($file_revision)) {
276 $file_revision = $temp_file_revision;
278 else {
279 if ( ($file_revision != $temp_file_revision) && ($success_flag === 1) ) {
281 <div class="error_msg"><?php echo xlt("The staged files revisions are not all from the same release."); ?></div>
282 <div class="stg msg"><?php echo xlt("Follow these instructions for installing or upgrading the following database") . ": " . text($db); ?><span class="msg" id="<?php echo attr($db); ?>_instrmsg">?</span></div>
283 <?php
284 $success_flag=0;
287 // collect checksum (if a multiple file import, then can use any one)
288 $file_checksum = $value['checksum'];
289 // collect path (if a multiple file import, then can use any one)
290 $file_revision_path = $value['path'];
293 // Determine and enforce only a certain number of files to be staged
294 if ($success_flag === 1) {
295 $number_files = 1;
296 $sql_query_ret = sqlStatement("SELECT * FROM `supported_external_dataloads` WHERE `load_type` = ? AND `load_source` = ? AND `load_release_date` = ?", array($db,$file_revision,$file_revision_date) );
297 $number_files_temp = sqlNumRows($sql_query_ret);
298 if ($number_files_temp > 1) {
299 // To ensure number_files is set to 1 for imports that are not tracked in the supported_external_dataloads table
300 $number_files = $number_files_temp;
302 if ( count($revisions) != $number_files ) {
304 <div class="error_msg"><?php echo xlt("The number of staged files is incorrect. Only place the files that you wish to install/upgrade to."); ?></div>
305 <div class="stg msg"><?php echo xlt("Follow these instructions for installing or upgrading the following database") . ": " . text($db); ?><span class="msg" id="<?php echo attr($db); ?>_instrmsg">?</span></div>
306 <?php
307 $success_flag=0;
311 // If new version is being offered, then provide install/upgrade options
312 if ($success_flag === 1) {
313 $action = "";
314 if ($installed_flag === 1) {
315 if ($current_name=="SNOMED" && $current_version!=$file_revision && $file_revision!="US Extension" && $current_version=="US Extension") {
316 // The US extension for snomed has been previosly installed, so will allow to Replace with installation of any international set.
318 <div class="stg"><?php echo text(basename($file_revision_path)); ?> <?php echo xlt("is a different version of the following database") . ": " . text($db); ?></div>
319 <?php
320 $action=xl("REPLACE");
321 } else if ($current_name=="SNOMED" && $current_version!=$file_revision && $file_revision!="US Extension") {
322 // A new language of the SNOMED database has been staged, and will offer to Replace database with this staged version.
324 <div class="stg"><?php echo text(basename($file_revision_path)); ?> <?php echo xlt("is a different language version of the following database") . ": " . text($db); ?></div>
325 <?php
326 $action=xl("REPLACE");
327 } else if ($current_name=="SNOMED" && $current_version=="US Extension" && $file_revision=="US Extension") {
328 // The Staged US Extension SNOMED package has already been installed
330 <div class="error_msg"><?php echo xlt("The compatible staged US Extension SNOMED package has already been installed."); ?></div>
331 <div class="stg msg"><?php echo xlt("Follow these instructions for installing or upgrading the following database") . ": " . text($db); ?><span class="msg" id="<?php echo attr($db); ?>_instrmsg">?</span></div>
332 <?php
333 } else if ($current_name=="SNOMED" && $current_version!="International:English" && $file_revision=="US Extension") {
334 // The Staged US Extension SNOMED file is not compatible with non-english snomed sets
336 <div class="error_msg"><?php echo xlt("The installed International SNOMED version is not compatible with the staged US Extension SNOMED package."); ?></div>
337 <div class="stg msg"><?php echo xlt("Follow these instructions for installing or upgrading the following database") . ": " . text($db); ?><span class="msg" id="<?php echo attr($db); ?>_instrmsg">?</span></div>
338 <?php
339 } else if ( ($current_name=="SNOMED" && $current_version=="International:English" && $file_revision=="US Extension") && ((strtotime($current_revision." +6 month") < strtotime($file_revision_date)) || (strtotime($current_revision." -6 month") > strtotime($file_revision_date))) ) {
340 // The Staged US Extension SNOMED file is not compatible with the current SNOMED International Package (ie. the International package is outdated)
342 <div class="error_msg"><?php echo xlt("The installed International SNOMED version is out of date and not compatible with the staged US Extension SNOMED file."); ?></div>
343 <div class="stg msg"><?php echo xlt("Follow these instructions for installing or upgrading the following database") . ": " . text($db); ?><span class="msg" id="<?php echo attr($db); ?>_instrmsg">?</span></div>
344 <?php
345 } else if ($current_name=="SNOMED" && $current_version=="International:English" && $file_revision=="US Extension") {
346 // Offer to upgrade to the US Extension.
348 <div class="stg"><?php echo text(basename($file_revision_path)); ?> <?php echo xlt("is an extension of the following database") . ": " . text($db); ?></div>
349 <?php
350 $action=xl("UPGRADE");
351 } else if ( (strtotime($current_revision) == strtotime($file_revision_date)) ) {
352 // Note the exception here when installing US Extension
354 <div class="error_msg"><?php echo xlt("The installed version and the staged files are the same."); ?></div>
355 <div class="stg msg"><?php echo xlt("Follow these instructions for installing or upgrading the following database") . ": " . text($db); ?><span class="msg" id="<?php echo attr($db); ?>_instrmsg">?</span></div>
356 <?php
357 } else if ( strtotime($current_revision) > strtotime($file_revision_date) ) {
358 // Note the exception here when installing US Extension
360 <div class="error_msg"><?php echo xlt("The installed version is a more recent version than the staged files."); ?></div>
361 <div class="stg msg"><?php echo xlt("Follow these instructions for installing or upgrading the following database") . ": " . text($db); ?><span class="msg" id="<?php echo attr($db); ?>_instrmsg">?</span></div>
362 <?php
363 } else {
365 <div class="stg"><?php echo text(basename($file_revision_path)); ?> <?php echo xlt("is a more recent version of the following database") . ": " . text($db); ?></div>
366 <?php
367 $action=xl("UPGRADE");
369 } else {
370 if ($db=="SNOMED" && $file_revision=="US Extension") {
371 // The Staged US Extension SNOMED package can not be installed by itself (it is done after the international package is installed)
373 <div class="error_msg"><?php echo xlt("The staged US Extension SNOMED package can not be installed until after the International SNOMED package has been installed."); ?></div>
374 <div class="stg msg"><?php echo xlt("Follow these instructions for installing or upgrading the following database") . ": " . text($db); ?><span class="msg" id="<?php echo attr($db); ?>_instrmsg">?</span></div>
375 <?php
377 else if (count($files_array) > 0) {
378 $action=xl("INSTALL");
380 else {
381 //do nothing
384 if (strlen($action) > 0) {
386 <input id="<?php echo attr($db); ?>_install_button" version="<?php echo attr($file_revision); ?>" file_revision_date="<?php echo attr($file_revision_date); ?>" file_checksum="<?php echo attr($file_checksum); ?>" type="button" value="<?php echo attr($action); ?>"/>
387 </div>
388 <?php