increment v_database for prior commit
[openemr.git] / custom / zutil.cli.doc_import.php
blob1f336ebb85e9eca2d2de72b188498b976c92bfd5
1 <?php
3 /**
4 * Command-line / Unattended document import utility.
6 * Expected arguments:
7 * site - As required for all cronjobs
8 * path - $GLOBALS key specifying the path
9 * pid - patient id to be used for all selected documents
10 * category - encoded category description to assign all selected documents
11 * owner - owner of all selected documents
12 * limit - Maximum number of files to be imported
13 * in_situ - Retain documents in current folder
15 * Examples:
16 * 1. To import received faxes:
17 * a. Set 'Scanner Directory' to network location where a fax machine can save received files
18 * b. Schedule cronjob '/usr/bin/php -f /var/www/html/emr/custom/zutil.cli.doc_import.php site=default'
19 * c. View and process received faxes using 'New Documents' menu.
21 * 2. To import collection of medical record files for a specific patient:
22 * a. Save files in Scanner Directory
23 * b. For patient nnn and category 'Patient Records', access this script online with request parameters as:
24 * zutil.cli.doc_import?site=default&pid=nnn&category=Patient+Records&limit=1000
26 * 3. Use in_situ=true option to create document records without relocating the files.
27 * This option requires good understanding of current functionality.
29 * @package OpenEMR
30 * @link http://www.open-emr.org
31 * @author MD Support <mdsupport@users.sf.net>
32 * @copyright Copyright (c) 2017 MD Support <mdsupport@users.sf.net>
33 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
36 // Allow this script to be run as a cronjob
37 require_once(dirname(__FILE__, 2) . "/library/allow_cronjobs.php");
39 // Defaults
40 $arg = array(
41 'path' => 'scanner_output_directory',
42 'pid' => '00',
43 'category' => 1,
44 'owner' => '',
45 'limit' => 10,
46 'in_situ' => 0,
49 foreach ($arg as $key => $def) {
50 if ($key == "path") {
51 // do not let setting of path via GET for security reasons
52 continue;
54 if (isset($_GET[$key])) {
55 $arg[$key] = $_GET[$key];
59 require_once(dirname(__FILE__, 2) . "/interface/globals.php");
60 require_once("$srcdir/documents.php");
62 if ($arg['category'] != 1) {
63 $rec_cat = sqlQuery('SELECT id FROM categories WHERE name=?', array(urldecode($arg['category'])));
64 if (isset($rec_cat['id'])) {
65 $arg['category'] = $rec_cat['id'];
68 // Defined here as fallback
69 $ext2mime = array(
70 "pdf" => "application/pdf",
71 "exe" => "application/octet-stream",
72 "zip" => "application/zip",
73 "docx" => "application/msword",
74 "doc" => "application/msword",
75 "xls" => "application/vnd.ms-excel",
76 "ppt" => "application/vnd.ms-powerpoint",
77 "gif" => "image/gif",
78 "png" => "image/png",
79 "jpeg" => "image/jpg",
80 "jpg" => "image/jpg",
81 "mp3" => "audio/mpeg",
82 "wav" => "audio/x-wav",
83 "mpeg" => "video/mpeg",
84 "mpg" => "video/mpeg",
85 "mpe" => "video/mpeg",
86 "mov" => "video/quicktime",
87 "avi" => "video/x-msvideo",
88 "3gp" => "video/3gpp",
89 "css" => "text/css",
90 "jsc" => "application/javascript",
91 "js" => "application/javascript",
92 "php" => "text/html",
93 "htm" => "text/html",
94 "html" => "text/html"
97 printf('%s %s %s (%s)%s', xlt('Import'), text($arg['limit']), xlt('documents'), text($arg['path']), "\n");
99 $docs = new DirectoryIterator($arg['path']);
100 foreach ($docs as $doc) {
101 if ($doc->isDot()) {
102 continue;
105 $doc_pathname = $doc->getPathname();
106 $doc_url = "file://" . $doc_pathname;
108 $finfo = finfo_open();
109 $str_mime = finfo_file($finfo, $doc_pathname, FILEINFO_MIME_TYPE);
110 finfo_close($finfo);
112 if (!$str_mime) {
113 $str_mime = $ext2mime[$doc->getExtension()];
116 if ($arg['in_situ']) {
117 // Skip prior documents
118 // mdsupport - Check both formats since there is no consistent code for managing urls.
119 $rec_doc = sqlQuery('SELECT id FROM documents WHERE url=? or url=?', array($doc_pathname, $doc_url));
120 if (isset($rec_doc['id'])) {
121 continue;
123 // mdsupport - This should be a standard method with DocStore variations. Until then,
124 // Create a document record for file
125 $objDoc = new Document();
126 $objDoc->set_storagemethod('0');
127 $objDoc->set_mimetype($str_mime);
128 $objDoc->set_url($doc_url);
129 $objDoc->set_size($doc->getSize());
130 $objDoc->set_hash(hash_file('sha3-512', $doc_pathname));
131 $objDoc->set_type($objDoc->type_array['file_url']);
132 $objDoc->set_owner($arg['owner']);
133 $objDoc->set_foreign_id($arg['pid']);
134 $objDoc->persist();
135 $objDoc->populate();
136 // mdsupport - Need set_category method for the Document
137 if (is_numeric($objDoc->get_id())) {
138 sqlStatement("INSERT INTO categories_to_documents(category_id, document_id) VALUES(?,?)", array($arg['category'], $objDoc->get_id()));
140 printf('%s - %s%s', text($doc_pathname), (is_numeric($objDoc->get_id()) ? text($objDoc->get_url()) : xlt('Documents setup error')), "\n");
141 } else {
142 // Too many parameters for the function make the following setup necessary for readability.
143 $doc_params = array(
144 'name' => $doc->getFilename(),
145 'mime_type' => $str_mime,
146 'full_path' => $doc_pathname,
147 'upload_error' => '',
148 'size' => $doc->getSize(),
149 'owner' => $arg['owner'],
150 'patient_id' => $arg['pid'],
151 'category_id' => '1',
152 'higher_level_path' => '',
153 'path_depth' => '1',
154 'skip_acl_check' => true
156 $new_doc = call_user_func_array('addNewDocument', $doc_params);
157 printf('%s - %s%s', text($doc_pathname), (isset($new_doc) ? text($new_doc->get_url()) : xlt('Documents setup error')), "\n");
158 if (!$new_doc) {
159 die();
160 } elseif (!unlink($doc_pathname)) {
161 printf('%s - %s', text($doc_pathname), xlt('Original file deletion error'));
162 die();
165 if (--$arg['limit'] < 1) {
166 break;