External database import utility.
[openemr.git] / interface / code_systems / list_staged.php
blob3097eef4c9f76738e1b0501e339fe19b9a1e3b64
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 = '';
79 // For now, only order by the revision_date. When have different formats of a code type (such as WHO vs CMS for ICD10 or different languages for SNOMED, then will incorporate this field)
80 $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 `revision_date` DESC", array($db) );
81 if (!empty($sqlReturn)) {
82 $installed_flag = 1;
83 $current_name = $sqlReturn['name'];
84 $current_revision = $sqlReturn['revision_date'];
85 $current_version = $sqlReturn['revision_version'];
86 $current_checksum = $sqlReturn['file_checksum'];
89 // See if a database file exist (collect revision and see if upgrade is an option)
90 $file_revision_path = ''; //Holds the database file
91 $file_revision_date = ''; //Holds the database file revision date
92 $version = '';
93 $revisions = array();
94 $files_array = array();
95 if (is_dir($mainPATH)) {
96 $files_array = scandir($mainPATH);
98 array_shift($files_array); // get rid of "."
99 array_shift($files_array); // get rid of ".."
102 // this foreach loop only encounters 1 file for SNOMED, RXNORM and ICD9 but will cycle through all the
103 // the release files for ICD10
105 $i = -1;
106 foreach ($files_array as $file) {
107 $i++;
108 $file = $mainPATH."/".$file;
109 if (is_file($file)) {
110 if (!strpos($file, ".zip") !== false) {
111 unset($files_array[$i]);
112 continue;
114 $supported_file = 0;
115 if ($db == 'RXNORM') {
116 if (preg_match("/RxNorm_full_([0-9]{8}).zip/",$file,$matches)) {
118 // Hard code the version RxNorm feed to be Standard
119 // (if add different RxNorm types/versions/lanuages, then can use this)
121 $version = "Standard";
122 $date_release = substr($matches[1],4)."-".substr($matches[1],0,2)."-".substr($matches[1],2,-4);
123 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
124 array_push($revisions,$temp_date);
125 $supported_file = 1;
128 else if ($db == 'SNOMED') {
129 if (preg_match("/SnomedCT_INT_([0-9]{8}).zip/",$file,$matches)) {
131 // Hard code the version SNOMED feed to be International:English
132 // (if add different SNOMED types/versions/languages, then can use this)
134 $version = "International:English";
135 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
136 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
137 array_push($revisions,$temp_date);
138 $supported_file = 1;
140 else if (preg_match("/SnomedCT_Release_INT_([0-9]{8}).zip/",$file,$matches)) {
142 // Hard code the version SNOMED feed to be International:English
143 // (if add different SNOMED types/versions/languages, then can use this)
145 $version = "International:English";
146 $date_release = substr($matches[1],0,4)."-".substr($matches[1],4,-2)."-".substr($matches[1],6);
147 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$mainPATH."/".$matches[0]);
148 array_push($revisions,$temp_date);
149 $supported_file = 1;
151 else {
152 // nothing
155 else if (is_numeric(strpos($db, "ICD"))) {
157 $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";
159 // this query determines whether you can load the data into openEMR. you must have the correct
160 // filename and checksum for each file that are part of the same release.
162 // IMPORTANT: Releases that contain mutliple zip file (e.g. ICD10) are grouped together based
163 // on the load_release_date attribute value specified in the supported_external_dataloads table
165 // Just in case same filename is released on different release dates, best to actually include the md5sum in the query itself.
166 // (and if a hit, then it is a pass)
167 // (even if two duplicate files that are in different releases, will still work since chooses most recent)
168 $file_checksum = md5(file_get_contents($file));
169 $sqlReturn = sqlQuery($qry_str, array($db, basename($file), $file_checksum) );
171 if (!empty($sqlReturn)) {
172 $version = $sqlReturn['load_source'];
173 $date_release = $sqlReturn['load_release_date'];
174 $temp_date = array('date'=>$date_release, 'version'=>$version, 'path'=>$file, 'checksum'=>$file_checksum);
175 array_push($revisions,$temp_date);
176 $supported_file = 1;
179 if ($supported_file === 1) {
181 <div class="stg"><?php echo text(basename($file)); ?></div>
182 <?php
183 } else {
185 <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>
186 <?php
190 } else {
192 <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>
193 <?php
195 if (count($files_array) === 0) {
197 <div class="error_msg"><?php echo xlt("No files staged for installation"); ?><span class="msg" id="<?php echo attr($db); ?>_msg">!</span></div>
198 <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>
199 <?php
203 // only render messages and action buttons when supported files exists
204 // otherwise we have an error message already displayed to the user
205 if ($supported_file === 1) {
207 $success_flag=1;
209 // Ensure all release dates and revisions are the same for multiple file imports
210 // and collect the date and revision. Also collect a checksum and path.
211 $file_revision_date = '';
212 $file_revision = '';
213 $file_checksum = '';
214 $file_revision_path = '';
215 foreach ($revisions as $value) {
216 // date check
217 $temp_file_revision_date = $value['date'];
218 if (empty($file_revision_date)) {
219 $file_revision_date = $temp_file_revision_date;
221 else {
222 if ($file_revision_date != $temp_file_revision_date) {
224 <div class="error_msg"><?php echo xlt("The staged files release dates are not all from the same release."); ?></div>
225 <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>
226 <?php
227 $success_flag=0;
230 // revision check
231 $temp_file_revision = $value['version'];
232 if (empty($file_revision)) {
233 $file_revision = $temp_file_revision;
235 else {
236 if ($file_revision != $temp_file_revision) {
238 <div class="error_msg"><?php echo xlt("The staged files revisions are not all from the same release."); ?></div>
239 <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>
240 <?php
241 $success_flag=0;
244 // collect checksum (if a multiple file import, then can use any one)
245 $file_checksum = $value['checksum'];
246 // collect path (if a multiple file import, then can use any one)
247 $file_revision_path = $value['path'];
250 // Determine and enforce only a certain number of files to be staged
251 if ($success_flag === 1) {
252 $number_files = 1;
253 $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) );
254 $number_files_temp = sqlNumRows($sql_query_ret);
255 if ($number_files_temp > 1) {
256 // To ensure number_files is set to 1 for imports that are not tracked in the supported_external_dataloads table
257 $number_files = $number_files_temp;
259 if ( count($revisions) != $number_files ) {
261 <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>
262 <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>
263 <?php
264 $success_flag=0;
268 // If new version is being offered, then provide install/upgrade options
269 if ($success_flag === 1) {
270 $action = "";
271 if ($installed_flag === 1) {
272 if (strtotime($current_revision) == strtotime($file_revision_date)) {
274 <div class="error_msg"><?php echo xlt("The installed version and the staged files are the same."); ?></div>
275 <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>
276 <?php
277 } else if (strtotime($current_revision) > strtotime($file_revision_date)) {
279 <div class="error_msg"><?php echo xlt("The installed version is a more recent version than the staged files."); ?></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 } else {
284 <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>
285 <?php
286 $action=xl("UPGRADE");
288 } else {
289 if (count($files_array) > 0) {
290 $action=xl("INSTALL");
293 if (strlen($action) > 0) {
295 <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); ?>"/>
296 </div>
297 <?php