Support for the recent US Snomed packages
[openemr.git] / interface / code_systems / list_staged.php
blob78f9d9cc2b898d259df3946e2aceb613394e0662
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 * @author Roberto Vasquez <robertogagliotta@gmail.com>
35 * @link http://www.open-emr.org
39 //SANITIZE ALL ESCAPES
40 $sanitize_all_escapes=true;
43 //STOP FAKE REGISTER GLOBALS
44 $fake_register_globals=false;
47 require_once("../../interface/globals.php");
48 require_once("$srcdir/acl.inc");
50 // Ensure script doesn't time out and has enough memory
51 set_time_limit(0);
52 ini_set('memory_limit', '150M');
54 // Control access
55 if (!acl_check('admin', 'super')) {
56 echo xlt('Not Authorized');
57 exit;
60 $db = isset($_GET['db']) ? $_GET['db'] : '0';
61 $mainPATH = $GLOBALS['fileroot']."/contrib/".strtolower($db);
62 $file_checksum = "";
65 // Get current revision (if installed)
67 // this retreives the most recent revision_date for a table "name"
69 // for SNOMED and RXNORM you get the date from the filename as those naming
70 // conventions allow for that derivation
71 // for ICD versions the revision date is equal to the load_release_date attribute
72 // value from the supported_external_dataloads table
74 $installed_flag = 0;
75 $supported_file = 0;
76 $current_revision = '';
77 $current_version = '';
78 $current_name = '';
79 $current_checksum = '';
81 // Ordering by the imported_date with tiebreaker being the revision_date
82 $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) );
83 if (!empty($sqlReturn)) {
84 $installed_flag = 1;
85 $current_name = $sqlReturn['name'];
86 $current_revision = $sqlReturn['revision_date'];
87 $current_version = $sqlReturn['revision_version'];
88 $current_checksum = $sqlReturn['file_checksum'];
91 // See if a database file exist (collect revision and see if upgrade is an option)
92 $file_revision_path = ''; //Holds the database file
93 $file_revision_date = ''; //Holds the database file revision date
94 $version = '';
95 $revisions = array();
96 $files_array = array();
97 if (is_dir($mainPATH)) {
98 $files_array = scandir($mainPATH);
100 array_shift($files_array); // get rid of "."
101 array_shift($files_array); // get rid of ".."
104 // this foreach loop only encounters 1 file for SNOMED, RXNORM and ICD9 but will cycle through all the
105 // the release files for ICD10
107 $i = -1;
108 foreach ($files_array as $file) {
109 $i++;
110 $file = $mainPATH."/".$file;
111 if (is_file($file)) {
112 if (!strpos($file, ".zip") !== false) {
113 unset($files_array[$i]);
114 continue;
116 $supported_file = 0;
117 if ($db == 'RXNORM') {
118 if (preg_match("/RxNorm_full_([0-9]{8}).zip/",$file,$matches)) {
120 // Hard code the version RxNorm feed to be Standard
121 // (if add different RxNorm types/versions/lanuages, then can use this)
123 $version = "Standard";
124 $date_release = substr($matches[1],4)."-".substr($matches[1],0,2)."-".substr($matches[1],2,-4);
125 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
126 array_push($revisions,$temp_date);
127 $supported_file = 1;
130 else if ($db == 'SNOMED') {
131 if (preg_match("/SnomedCT_INT_([0-9]{8}).zip/",$file,$matches)) {
133 // Hard code the version SNOMED feed to be International:English
134 // (if add different SNOMED types/versions/languages, then can use this)
136 $version = "International:English";
137 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
138 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
139 array_push($revisions,$temp_date);
140 $supported_file = 1;
142 else if (preg_match("/SnomedCT_Release_INT_([0-9]{8}).zip/",$file,$matches)) {
144 // Hard code the version SNOMED feed to be International:English
145 // (if add different SNOMED types/versions/languages, then can use this)
147 $version = "International:English";
148 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
149 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
150 array_push($revisions,$temp_date);
151 $supported_file = 1;
153 else if (preg_match("/SnomedCT_RF1Release_INT_([0-9]{8}).zip/",$file,$matches)) {
155 // Hard code the version SNOMED feed to be International:English
156 // (if add different SNOMED types/versions/languages, then can use this)
158 $version = "International:English";
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("/SnomedCT_Release_US[0-9]*_([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_RF1Release_US[0-9]*_([0-9]{8}).zip/",$file,$matches)) {
178 // This is the SNOMED US extension pack which can only be installed on top
179 // of a International SNOMED version.
180 // Hard code this version SNOMED feed to be US Extension
182 $version = "US Extension";
183 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
184 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
185 array_push($revisions,$temp_date);
186 $supported_file = 1;
188 else if (preg_match("/sct1_National_US_([0-9]{8}).zip/",$file,$matches)) {
190 // This is the SNOMED US extension pack which can only be installed on top
191 // of a International SNOMED version.
192 // Hard code this version SNOMED feed to be US Extension
194 $version = "US Extension";
195 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
196 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
197 array_push($revisions,$temp_date);
198 $supported_file = 1;
200 else if (preg_match("/SnomedCT_Release-es_INT_([0-9]{8}).zip/",$file,$matches)) {
202 // Hard code this SNOMED version feed to be International:Spanish
204 $version = "International:Spanish";
205 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
206 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
207 array_push($revisions,$temp_date);
208 $supported_file = 1;
210 else {
211 // nothing
214 else if (is_numeric(strpos($db, "ICD"))) {
216 $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";
218 // this query determines whether you can load the data into openEMR. you must have the correct
219 // filename and checksum for each file that are part of the same release.
221 // IMPORTANT: Releases that contain mutliple zip file (e.g. ICD10) are grouped together based
222 // on the load_release_date attribute value specified in the supported_external_dataloads table
224 // Just in case same filename is released on different release dates, best to actually include the md5sum in the query itself.
225 // (and if a hit, then it is a pass)
226 // (even if two duplicate files that are in different releases, will still work since chooses most recent)
227 $file_checksum = md5(file_get_contents($file));
228 $sqlReturn = sqlQuery($qry_str, array($db, basename($file), $file_checksum) );
230 if (!empty($sqlReturn)) {
231 $version = $sqlReturn['load_source'];
232 $date_release = $sqlReturn['load_release_date'];
233 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$file, 'checksum'=>$file_checksum);
234 array_push($revisions,$temp_date);
235 $supported_file = 1;
238 else if($db == 'CQM_VALUESET'){
239 if (preg_match("/ep_only_cms_([0-9]{8}).xml.zip/",$file,$matches)) {
240 $version = "Standard";
241 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
242 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
243 array_push($revisions,$temp_date);
244 $supported_file = 1;
247 if ($supported_file === 1) {
248 ?><div class="stg"><?php echo text(basename($file)); ?></div>
249 <?php
250 } else {
252 <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>
253 <?php
257 } else {
259 <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>
260 <?php
262 if (count($files_array) === 0) {
264 <div class="error_msg"><?php echo xlt("No files staged for installation"); ?><span class="msg" id="<?php echo attr($db); ?>_msg">!</span></div>
265 <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>
266 <?php
270 // only render messages and action buttons when supported files exists
271 // otherwise we have an error message already displayed to the user
272 if ($supported_file === 1) {
274 $success_flag=1;
276 // Only allow 1 staged revision for the SNOMED and RXNORM imports
277 if ( ($db=="SNOMED" || $db=="RXNORM") && (count($revisions) > 1) ) {
279 <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>
280 <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>
281 <?php
282 $success_flag=0;
285 // Ensure all release dates and revisions are the same for multiple file imports
286 // and collect the date and revision. Also collect a checksum and path.
287 $file_revision_date = '';
288 $file_revision = '';
289 $file_checksum = '';
290 $file_revision_path = '';
291 foreach ($revisions as $value) {
292 // date check
293 $temp_file_revision_date = $value['date'];
294 if (empty($file_revision_date)) {
295 $file_revision_date = $temp_file_revision_date;
297 else {
298 if ( ($file_revision_date != $temp_file_revision_date) && ($success_flag === 1) ) {
300 <div class="error_msg"><?php echo xlt("The staged files release dates are not all from the same release."); ?></div>
301 <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>
302 <?php
303 $success_flag=0;
306 // revision check
307 $temp_file_revision = $value['version'];
308 if (empty($file_revision)) {
309 $file_revision = $temp_file_revision;
311 else {
312 if ( ($file_revision != $temp_file_revision) && ($success_flag === 1) ) {
314 <div class="error_msg"><?php echo xlt("The staged files revisions are not all from the same release."); ?></div>
315 <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>
316 <?php
317 $success_flag=0;
320 // collect checksum (if a multiple file import, then can use any one)
321 $file_checksum = $value['checksum'];
322 // collect path (if a multiple file import, then can use any one)
323 $file_revision_path = $value['path'];
326 // Determine and enforce only a certain number of files to be staged
327 if ($success_flag === 1) {
328 $number_files = 1;
329 $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) );
330 $number_files_temp = sqlNumRows($sql_query_ret);
331 if ($number_files_temp > 1) {
332 // To ensure number_files is set to 1 for imports that are not tracked in the supported_external_dataloads table
333 $number_files = $number_files_temp;
335 if ( count($revisions) != $number_files ) {
337 <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>
338 <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>
339 <?php
340 $success_flag=0;
344 // If new version is being offered, then provide install/upgrade options
345 if ($success_flag === 1) {
346 $action = "";
347 if ($installed_flag === 1) {
348 if ($current_name=="SNOMED" && $current_version!=$file_revision && $file_revision!="US Extension" && $current_version=="US Extension") {
349 // The US extension for snomed has been previosly installed, so will allow to Replace with installation of any international set.
351 <div class="stg"><?php echo text(basename($file_revision_path)); ?> <?php echo xlt("is a different version of the following database") . ": " . text($db); ?></div>
352 <?php
353 $action=xl("REPLACE");
354 } else if ($current_name=="SNOMED" && $current_version!=$file_revision && $file_revision!="US Extension") {
355 // A new language of the SNOMED database has been staged, and will offer to Replace database with this staged version.
357 <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>
358 <?php
359 $action=xl("REPLACE");
360 } else if ($current_name=="SNOMED" && $current_version=="US Extension" && $file_revision=="US Extension") {
361 // The Staged US Extension SNOMED package has already been installed
363 <div class="error_msg"><?php echo xlt("The compatible staged US Extension SNOMED package has already been installed."); ?></div>
364 <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>
365 <?php
366 } else if ($current_name=="SNOMED" && $current_version!="International:English" && $file_revision=="US Extension") {
367 // The Staged US Extension SNOMED file is not compatible with non-english snomed sets
369 <div class="error_msg"><?php echo xlt("The installed International SNOMED version is not compatible with the staged US Extension SNOMED package."); ?></div>
370 <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>
371 <?php
372 } 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))) ) {
373 // The Staged US Extension SNOMED file is not compatible with the current SNOMED International Package (ie. the International package is outdated)
375 <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>
376 <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>
377 <?php
378 } else if ($current_name=="SNOMED" && $current_version=="International:English" && $file_revision=="US Extension") {
379 // Offer to upgrade to the US Extension.
381 <div class="stg"><?php echo text(basename($file_revision_path)); ?> <?php echo xlt("is an extension of the following database") . ": " . text($db); ?></div>
382 <?php
383 $action=xl("UPGRADE");
384 } else if ( (strtotime($current_revision) == strtotime($file_revision_date)) ) {
385 // Note the exception here when installing US Extension
387 <div class="error_msg"><?php echo xlt("The installed version and the staged files are the same."); ?></div>
388 <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>
389 <?php
390 } else if ( strtotime($current_revision) > strtotime($file_revision_date) ) {
391 // Note the exception here when installing US Extension
393 <div class="error_msg"><?php echo xlt("The installed version is a more recent version than the staged files."); ?></div>
394 <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>
395 <?php
396 } else {
398 <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>
399 <?php
400 $action=xl("UPGRADE");
402 } else {
403 if ($db=="SNOMED" && $file_revision=="US Extension") {
404 // The Staged US Extension SNOMED package can not be installed by itself (it is done after the international package is installed)
406 <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>
407 <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>
408 <?php
410 else if (count($files_array) > 0) {
411 $action=xl("INSTALL");
413 else {
414 //do nothing
417 if (strlen($action) > 0) {
419 <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); ?>"/>
420 </div>
421 <?php