minor formatting changes to prior commit
[openemr.git] / interface / code_systems / list_staged.php
blobfd96c3756a0cd46436e15c221d705fe8985245d8
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_RF1Release_INT_([0-9]{8}).zip/",$file,$matches)) {
154 // Hard code the version SNOMED feed to be International:English
155 // (if add different SNOMED types/versions/languages, then can use this)
157 $version = "International:English";
158 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
159 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
160 array_push($revisions,$temp_date);
161 $supported_file = 1;
163 else if (preg_match("/SnomedCT_Release_US[0-9]*_([0-9]{8}).zip/",$file,$matches)) {
165 // This is the SNOMED US extension pack which can only be installed on top
166 // of a International SNOMED version.
167 // Hard code this version SNOMED feed to be US Extension
169 $version = "US Extension";
170 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
171 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
172 array_push($revisions,$temp_date);
173 $supported_file = 1;
175 else if (preg_match("/sct1_National_US_([0-9]{8}).zip/",$file,$matches)) {
177 // This is the SNOMED US extension pack which can only be installed on top
178 // of a International SNOMED version.
179 // Hard code this version SNOMED feed to be US Extension
181 $version = "US Extension";
182 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
183 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
184 array_push($revisions,$temp_date);
185 $supported_file = 1;
187 else if (preg_match("/SnomedCT_Release-es_INT_([0-9]{8}).zip/",$file,$matches)) {
189 // Hard code this SNOMED version feed to be International:Spanish
191 $version = "International:Spanish";
192 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
193 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
194 array_push($revisions,$temp_date);
195 $supported_file = 1;
197 else {
198 // nothing
201 else if (is_numeric(strpos($db, "ICD"))) {
203 $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";
205 // this query determines whether you can load the data into openEMR. you must have the correct
206 // filename and checksum for each file that are part of the same release.
208 // IMPORTANT: Releases that contain mutliple zip file (e.g. ICD10) are grouped together based
209 // on the load_release_date attribute value specified in the supported_external_dataloads table
211 // Just in case same filename is released on different release dates, best to actually include the md5sum in the query itself.
212 // (and if a hit, then it is a pass)
213 // (even if two duplicate files that are in different releases, will still work since chooses most recent)
214 $file_checksum = md5(file_get_contents($file));
215 $sqlReturn = sqlQuery($qry_str, array($db, basename($file), $file_checksum) );
217 if (!empty($sqlReturn)) {
218 $version = $sqlReturn['load_source'];
219 $date_release = $sqlReturn['load_release_date'];
220 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$file, 'checksum'=>$file_checksum);
221 array_push($revisions,$temp_date);
222 $supported_file = 1;
225 else if($db == 'CQM_VALUESET'){
226 if (preg_match("/ep_only_cms_([0-9]{8}).xml.zip/",$file,$matches)) {
227 $version = "Standard";
228 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
229 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
230 array_push($revisions,$temp_date);
231 $supported_file = 1;
234 if ($supported_file === 1) {
235 ?><div class="stg"><?php echo text(basename($file)); ?></div>
236 <?php
237 } else {
239 <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>
240 <?php
244 } else {
246 <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>
247 <?php
249 if (count($files_array) === 0) {
251 <div class="error_msg"><?php echo xlt("No files staged for installation"); ?><span class="msg" id="<?php echo attr($db); ?>_msg">!</span></div>
252 <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>
253 <?php
257 // only render messages and action buttons when supported files exists
258 // otherwise we have an error message already displayed to the user
259 if ($supported_file === 1) {
261 $success_flag=1;
263 // Only allow 1 staged revision for the SNOMED and RXNORM imports
264 if ( ($db=="SNOMED" || $db=="RXNORM") && (count($revisions) > 1) ) {
266 <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>
267 <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>
268 <?php
269 $success_flag=0;
272 // Ensure all release dates and revisions are the same for multiple file imports
273 // and collect the date and revision. Also collect a checksum and path.
274 $file_revision_date = '';
275 $file_revision = '';
276 $file_checksum = '';
277 $file_revision_path = '';
278 foreach ($revisions as $value) {
279 // date check
280 $temp_file_revision_date = $value['date'];
281 if (empty($file_revision_date)) {
282 $file_revision_date = $temp_file_revision_date;
284 else {
285 if ( ($file_revision_date != $temp_file_revision_date) && ($success_flag === 1) ) {
287 <div class="error_msg"><?php echo xlt("The staged files release dates are not all from the same release."); ?></div>
288 <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>
289 <?php
290 $success_flag=0;
293 // revision check
294 $temp_file_revision = $value['version'];
295 if (empty($file_revision)) {
296 $file_revision = $temp_file_revision;
298 else {
299 if ( ($file_revision != $temp_file_revision) && ($success_flag === 1) ) {
301 <div class="error_msg"><?php echo xlt("The staged files revisions are not all from the same release."); ?></div>
302 <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>
303 <?php
304 $success_flag=0;
307 // collect checksum (if a multiple file import, then can use any one)
308 $file_checksum = $value['checksum'];
309 // collect path (if a multiple file import, then can use any one)
310 $file_revision_path = $value['path'];
313 // Determine and enforce only a certain number of files to be staged
314 if ($success_flag === 1) {
315 $number_files = 1;
316 $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) );
317 $number_files_temp = sqlNumRows($sql_query_ret);
318 if ($number_files_temp > 1) {
319 // To ensure number_files is set to 1 for imports that are not tracked in the supported_external_dataloads table
320 $number_files = $number_files_temp;
322 if ( count($revisions) != $number_files ) {
324 <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>
325 <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>
326 <?php
327 $success_flag=0;
331 // If new version is being offered, then provide install/upgrade options
332 if ($success_flag === 1) {
333 $action = "";
334 if ($installed_flag === 1) {
335 if ($current_name=="SNOMED" && $current_version!=$file_revision && $file_revision!="US Extension" && $current_version=="US Extension") {
336 // The US extension for snomed has been previosly installed, so will allow to Replace with installation of any international set.
338 <div class="stg"><?php echo text(basename($file_revision_path)); ?> <?php echo xlt("is a different version of the following database") . ": " . text($db); ?></div>
339 <?php
340 $action=xl("REPLACE");
341 } else if ($current_name=="SNOMED" && $current_version!=$file_revision && $file_revision!="US Extension") {
342 // A new language of the SNOMED database has been staged, and will offer to Replace database with this staged version.
344 <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>
345 <?php
346 $action=xl("REPLACE");
347 } else if ($current_name=="SNOMED" && $current_version=="US Extension" && $file_revision=="US Extension") {
348 // The Staged US Extension SNOMED package has already been installed
350 <div class="error_msg"><?php echo xlt("The compatible staged US Extension SNOMED package has already been installed."); ?></div>
351 <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>
352 <?php
353 } else if ($current_name=="SNOMED" && $current_version!="International:English" && $file_revision=="US Extension") {
354 // The Staged US Extension SNOMED file is not compatible with non-english snomed sets
356 <div class="error_msg"><?php echo xlt("The installed International SNOMED version is not compatible with the staged US Extension SNOMED package."); ?></div>
357 <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>
358 <?php
359 } 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))) ) {
360 // The Staged US Extension SNOMED file is not compatible with the current SNOMED International Package (ie. the International package is outdated)
362 <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>
363 <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>
364 <?php
365 } else if ($current_name=="SNOMED" && $current_version=="International:English" && $file_revision=="US Extension") {
366 // Offer to upgrade to the US Extension.
368 <div class="stg"><?php echo text(basename($file_revision_path)); ?> <?php echo xlt("is an extension of the following database") . ": " . text($db); ?></div>
369 <?php
370 $action=xl("UPGRADE");
371 } else if ( (strtotime($current_revision) == strtotime($file_revision_date)) ) {
372 // Note the exception here when installing US Extension
374 <div class="error_msg"><?php echo xlt("The installed version and the staged files are the same."); ?></div>
375 <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>
376 <?php
377 } else if ( strtotime($current_revision) > strtotime($file_revision_date) ) {
378 // Note the exception here when installing US Extension
380 <div class="error_msg"><?php echo xlt("The installed version is a more recent version than the staged files."); ?></div>
381 <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>
382 <?php
383 } else {
385 <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>
386 <?php
387 $action=xl("UPGRADE");
389 } else {
390 if ($db=="SNOMED" && $file_revision=="US Extension") {
391 // The Staged US Extension SNOMED package can not be installed by itself (it is done after the international package is installed)
393 <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>
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
397 else if (count($files_array) > 0) {
398 $action=xl("INSTALL");
400 else {
401 //do nothing
404 if (strlen($action) > 0) {
406 <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); ?>"/>
407 </div>
408 <?php