From 3fef5a4a55cf27df043b3a6736862a514ed4918b Mon Sep 17 00:00:00 2001 From: mdsupport Date: Fri, 17 Feb 2017 00:23:00 -0800 Subject: [PATCH] Update document class to check for relocated documents folder (#414) --- controllers/C_Document.class.php | 103 +++++++++++++++++++++++++++------------ 1 file changed, 73 insertions(+), 30 deletions(-) diff --git a/controllers/C_Document.class.php b/controllers/C_Document.class.php index 5be028ab6..b8e1e0d4f 100644 --- a/controllers/C_Document.class.php +++ b/controllers/C_Document.class.php @@ -934,16 +934,14 @@ class C_Document extends Controller { $file_name = $d->get_url_file(); if ( $docname != '' && $docname != $file_name ) { - $path = $d->get_url_filepath(); - $path = str_replace( $file_name, "", $path ); - $new_url = $this->_rename_file( $path.$docname ); - if ( rename( $d->get_url(), $new_url ) ) { + // Ready to rename - check for relocation + $old_url = $this->_check_relocation($d->get_url()); + $new_url = $this->_check_relocation($d->get_url(), null, $docname); + $messages .= sprintf("%s -> %s
", $old_url, $new_url); + if ( rename( $old_url, $new_url ) ) { // check the "converted" file, and delete it if it exists. It will be regenerated when report is run - $url = preg_replace("|^(.*)://|","",$d->get_url()); - $convertedFile = substr(basename_international($url), 0, strrpos(basename_international($url), '.')) . '_converted.jpg'; - $url = $GLOBALS['OE_SITE_DIR'] . '/documents/' . $patient_id . '/' . $convertedFile; - if ( file_exists( $url ) ) { - unlink( $url ); + if ( file_exists( $old_url ) ) { + unlink( $old_url ); } $d->url = $new_url; $d->persist(); @@ -1010,35 +1008,49 @@ class C_Document extends Controller { * Modified to only deal with base file name when renaming, to avoid issues with directory * names with dots. */ - function _rename_file($fname) { + function _rename_file($fname, $self=FALSE) { + // Allow same routine for new file name check + if (!file_exists($fname)) return($fname); + $path = dirname($fname); $file = basename_international($fname); - $fparts = explode("\.",$file); - - if (count($fparts) > 1) { - if (is_numeric($fparts[count($fparts) -2]) && (count($fparts) > 2)) { - //increment the counter in filename - $fparts[count($fparts) -2] = $fparts[count($fparts) -2] + 1; - } elseif (is_numeric($fparts[count($fparts) -1]) && $fparts[count($fparts) -1] < 1000) { - //increment counter at end of filename (so compatible with previous openemr version files - $fparts[count($fparts) -1] = $fparts[count($fparts) -1] + 1; - } elseif (is_numeric($fparts[count($fparts) -1])) { - //leave date at end and place counter in filename - array_splice($fparts, -1, 0, "1"); - } else { - //add the counter to filename - array_splice($fparts, -1, 0, "1"); - } - } else { // (count($fparts) == 1) - //place counter at end of filename - array_push($fparts, "1"); + $fparts = explode(".",$file); + switch (count($fparts)) { + case 1: + // Has a single node (base file name). Create counter node with value 0 + $fparts[1] = '1'; + break; + case 2: + // If 2nd node is numeric, assume it is counter and add 1 else insert counter + if (is_numeric($fparts[1])) { + $fparts[1] += 1; + } else { + array_push($fparts, $fparts[1]); + $fparts[1] = '1'; + } + break; + default: + // Multiple nodes + $ix_end = count($fparts) - 1; + if (is_numeric($fparts[$ix_end]) && !is_numeric($fparts[$ix_end - 1])) { + // Switch old style to new and check again + $wrk = $fparts[$ix_end - 1]; + $fparts[$ix_end - 1] = $fparts[$ix_end]; + $fparts[$ix_end] = $wrk; + } else if (is_numeric($fparts[$ix_end - 1])) { + $fparts[$ix_end - 1] += 1; + } else { + array_push($fparts, $fparts[$ix_end]); + $fparts[$ix_end] = '1'; + } + break; } $fname = $path.DIRECTORY_SEPARATOR.join(".", $fparts); if (file_exists($fname)) { - return $this->_rename_file($fname); + return $this->_rename_file($fname, TRUE); } else { return($fname); } @@ -1299,5 +1311,36 @@ function image_result_indication($doc_id,$encounter,$image_procedure_id = 0){ setGpRelation(1, $doc_id, 6, $noteid); } +/** Function to accomodate the relocation of entire "documents" folder to another host or filesystem ** + * Also usable for documents that may of been moved to different patients. + * + * @param string $url - Current url string from database. + * @param string $new_pid - Include pid corrections to receive corrected url during move operation. + * @param string $new_name - Include name corrections to receive corrected url during rename operation. + * + * @return string + */ +function _check_relocation($url, $new_pid = null, $new_name = null) { + //strip url of protocol handler + $url = preg_replace("|^(.*)://|","",$url); + $fsnodes = explode(DIRECTORY_SEPARATOR, $url); + while (current($fsnodes) != "documents") { + array_shift($fsnodes); + } + if ($new_pid) { + $fsnodes[1] = $new_pid; + } + if ($new_name) { + $fsnodes[count($fsnodes)-1] = $new_name; + } + $url = $GLOBALS['OE_SITE_DIR'].DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $fsnodes); + // Make sure the url is available after corrections + if ($new_pid || $new_name) { + $url = $this->_rename_file($url); + } + //Add full path and remaining nodes + return $url; +} + } ?> -- 2.11.4.GIT