CCR Import, made changes per comments in github.
[openemr.git] / library / classes / Document.class.php
blobe206ea4d66d04eb79b216ced174a9f3b96a15f1c
1 <?php
3 require_once(dirname(__FILE__) . "/ORDataObject.class.php");
4 require_once(dirname(__FILE__) . "/CouchDB.class.php");
6 /**
7 * class Document
8 * This class is the logical representation of a physical file on some system somewhere that can be referenced with a URL
9 * of some type. This URL is not necessarily a web url, it could be a file URL or reference to a BLOB in a db.
10 * It is implicit that a document can have other related tables to it at least a one document to many notes which join on a documents
11 * id and categories which do the same.
14 class Document extends ORDataObject{
17 * Database unique identifier
18 * @var id
20 var $id;
23 * DB unique identifier reference to some other table, this is not unique in the document table
24 * @var int
26 var $foreign_id;
29 * Enumerated DB field which is met information about how to use the URL
30 * @var int can also be a the properly enumerated string
32 var $type;
35 * Array mapping of possible for values for the type variable
36 * mapping is array text name to index
37 * @var array
39 var $type_array = array();
42 * Size of the document in bytes if that is available
43 * @var int
45 var $size;
48 * Date the document was first persisted
49 * @var string
51 var $date;
54 * URL which point to the document, may be a file URL, a web URL, a db BLOB URL, or others
55 * @var string
57 var $url;
60 * Mimetype of the document if available
61 * @var string
63 var $mimetype;
66 * If the document is a multi-page format like tiff and has at least 1 page this will be 1 or greater, if a non-multi-page format this should be null or empty
67 * @var int
69 var $pages;
72 * Foreign key identifier of who initially persisited the document,
73 * potentially ownership could be changed but that would be up to an external non-document object process
74 * @var int
76 var $owner;
79 * Timestamp of the last time the document was changed and persisted, auto maintained by DB, manually change at your own peril
80 * @var int
82 var $revision;
85 * Date (YYYY-MM-DD) logically associated with the document, e.g. when a picture was taken.
86 * @var string
88 var $docdate;
91 * 40-character sha1 hash key of the document from when it was uploaded.
92 * @var string
94 var $hash;
97 * DB identifier reference to the lists table (the related issue), 0 if none.
98 * @var int
100 var $list_id;
103 * Whether the file is already imported
104 * @var int
106 var $imported;
109 * Constructor sets all Document attributes to their default value
110 * @param int $id optional existing id of a specific document, if omitted a "blank" document is created
112 function Document($id = "") {
113 //call the parent constructor so we have a _db to work with
114 parent::ORDataObject();
116 //shore up the most basic ORDataObject bits
117 $this->id = $id;
118 $this->_table = "documents";
120 //load the enum type from the db using the parent helper function, this uses psuedo-class variables so it is really cheap
121 $this->type_array = $this->_load_enum("type");
123 $this->type = $this->type_array[0];
124 $this->size = 0;
125 $this->date = date("Y-m-d H:i:s");
126 $this->url = "";
127 $this->mimetype = "";
128 $this->docdate = date("Y-m-d");
129 $this->hash = "";
130 $this->list_id = 0;
132 if ($id != "") {
133 $this->populate();
138 * Convenience function to get an array of many document objects
139 * For really large numbers of documents there is a way more efficient way to do this by overwriting the populate method
140 * @param int $foreign_id optional id use to limit array on to a specific relation, otherwise every document object is returned
142 function documents_factory($foreign_id = "") {
143 $documents = array();
145 if (empty($foreign_id)) {
146 $foreign_id= "like '%'";
148 else {
149 $foreign_id= " = '" . mysql_real_escape_string(strval($foreign_id)) . "'";
152 $d = new Document();
153 $sql = "SELECT id FROM " . $d->_table . " WHERE foreign_id " .$foreign_id ;
154 $result = $d->_db->Execute($sql);
156 while ($result && !$result->EOF) {
157 $documents[] = new Document($result->fields['id']);
158 $result->MoveNext();
161 return $documents;
165 * Convenience function to get a document object from a url
166 * Checks to see if there is an existing document with that URL and if so returns that object, otherwise
167 * creates a new one, persists it and returns it
168 * @param string $url
169 * @return object new or existing document object with the specified URL
171 function document_factory_url($url) {
172 $d = new Document();
173 //strip url handler, for now we always assume file://
174 $filename = preg_replace("|^(.*)://|","",$url);
176 if (!file_exists($filename)) {
177 die("An invalid URL was specified to crete a new document, this would only be caused if files are being deleted as you are working through the queue. '$filename'\n");
180 $sql = "SELECT id FROM " . $d->_table . " WHERE url= '" . mysql_real_escape_string($url) ."'" ;
181 $result = $d->_db->Execute($sql);
183 if ($result && !$result->EOF) {
184 if (file_exists($filename)) {
185 $d = new Document($result->fields['id']);
187 else {
188 $sql = "DELETE FROM " . $d->_table . " WHERE id= '" . $result->fields['id'] ."'";
189 $result = $d->_db->Execute($sql);
190 echo("There is a database for the file but it no longer exists on the file system. Its document entry has been deleted. '$filename'\n");
193 else {
194 $file_command = $GLOBALS['oer_config']['document']['file_command_path'] ;
195 $cmd_args = "-i ".escapeshellarg($new_path.$fname);
197 $command = $file_command." ".$cmd_args;
198 $mimetype = exec($command);
199 $mime_array = split(":", $mimetype);
200 $mimetype = $mime_array[1];
201 $d->set_mimetype($mimetype);
202 $d->url = $url;
203 $d->size = filesize($filename);
204 $d->type = $d->type_array['file_url'];
205 $d->persist();
206 $d->populate();
209 return $d;
213 * Convenience function to generate string debug data about the object
215 function toString($html = false) {
216 $string .= "\n"
217 . "ID: " . $this->id."\n"
218 . "FID: " . $this->foreign_id."\n"
219 . "type: " . $this->type . "\n"
220 . "type_array: " . print_r($this->type_array,true) . "\n"
221 . "size: " . $this->size . "\n"
222 . "date: " . $this->date . "\n"
223 . "url: " . $this->url . "\n"
224 . "mimetype: " . $this->mimetype . "\n"
225 . "pages: " . $this->pages . "\n"
226 . "owner: " . $this->owner . "\n"
227 . "revision: " . $this->revision . "\n"
228 . "docdate: " . $this->docdate . "\n"
229 . "hash: " . $this->hash . "\n"
230 . "list_id: " . $this->list_id . "\n";
232 if ($html) {
233 return nl2br($string);
235 else {
236 return $string;
240 /**#@+
241 * Getter/Setter methods used by reflection to affect object in persist/poulate operations
242 * @param mixed new value for given attribute
244 function set_id($id) {
245 $this->id = $id;
247 function get_id() {
248 return $this->id;
250 function set_foreign_id($fid) {
251 $this->foreign_id = $fid;
253 function get_foreign_id() {
254 return $this->foreign_id;
256 function set_type($type) {
257 $this->type = $type;
259 function get_type() {
260 return $this->type;
262 function set_size($size) {
263 $this->size = $size;
265 function get_size() {
266 return $this->size;
268 function set_date($date) {
269 $this->date = $date;
271 function get_date() {
272 return $this->date;
274 function set_hash($hash) {
275 $this->hash = $hash;
277 function get_hash() {
278 return $this->hash;
280 function set_url($url) {
281 $this->url = $url;
283 function get_url() {
284 return $this->url;
287 * this returns the url stripped down to basename
289 function get_url_web() {
290 return basename($this->url);
293 * get the url without the protocol handler
295 function get_url_filepath() {
296 return preg_replace("|^(.*)://|","",$this->url);
299 * get the url filename only
301 function get_url_file() {
302 return basename(preg_replace("|^(.*)://|","",$this->url));
305 * get the url path only
307 function get_url_path() {
308 return dirname(preg_replace("|^(.*)://|","",$this->url)) ."/";
310 function get_path_depth() {
311 return $this->path_depth;
313 function set_path_depth($path_depth) {
314 $this->path_depth = $path_depth;
316 function set_mimetype($mimetype) {
317 $this->mimetype = $mimetype;
319 function get_mimetype() {
320 return $this->mimetype;
322 function set_pages($pages) {
323 $this->pages = $pages;
325 function get_pages() {
326 return $this->pages;
328 function set_owner($owner) {
329 $this->owner = $owner;
331 function get_owner() {
332 return $this->owner;
335 * No getter for revision because it is updated automatically by the DB.
337 function set_revision($revision) {
338 $this->revision = $revision;
340 function set_docdate($docdate) {
341 $this->docdate = $docdate;
343 function get_docdate() {
344 return $this->docdate;
346 function set_list_id($list_id) {
347 $this->list_id = $list_id;
349 function get_list_id() {
350 return $this->list_id;
352 function get_ccr_type($doc_id){
353 $type = sqlQuery("SELECT c.name FROM categories AS c LEFT JOIN categories_to_documents AS ctd ON c.id = ctd.category_id WHERE ctd.document_id = ?",array($doc_id));
354 return $type['name'];
356 function set_imported($imported) {
357 $this->imported = $imported;
359 function get_imported() {
360 return $this->imported;
362 function update_imported($doc_id) {
363 sqlQuery("UPDATE documents SET imported = 1 WHERE id = ?",array($doc_id));
366 * Overridden function to stor current object state in the db.
367 * current overide is to allow for a just in time foreign id, often this is needed
368 * when the object is never directly exposed and is handled as part of a larger
369 * object hierarchy.
370 * @param int $fid foreign id that should be used so that this document can be related (joined) on it later
373 function persist($fid ="") {
374 if (!empty($fid)) {
375 $this->foreign_id = $fid;
377 parent::persist();
380 function set_storagemethod($str) {
381 $this->storagemethod = $str;
384 function get_storagemethod() {
385 return $this->storagemethod;
388 function set_couch_docid($str) {
389 $this->couch_docid = $str;
392 function get_couch_docid() {
393 return $this->couch_docid;
396 function set_couch_revid($str) {
397 $this->couch_revid = $str;
400 function get_couch_revid() {
401 return $this->couch_revid;
404 function get_couch_url($pid,$encounter){
405 $couch_docid = $this->get_couch_docid();
406 $couch_url = $this->get_url();
407 $couch = new CouchDB();
408 $data = array($GLOBALS['couchdb_dbase'],$couch_docid,$pid,$encounter);
409 $resp = $couch->retrieve_doc($data);
410 $content = $resp->data;
411 $temp_url=$couch_url;
412 $temp_url = $GLOBALS['OE_SITE_DIR'] . '/documents/temp/' . $pid . '_' . $couch_url;
413 $f_CDB = fopen($temp_url,'w');
414 fwrite($f_CDB,base64_decode($content));
415 fclose($f_CDB);
416 return $temp_url;
419 // Function added by Rod to change the patient associated with a document.
420 // This just moves some code that used to be in C_Document.class.php,
421 // changing it as little as possible since I'm not set up to test it.
423 function change_patient($new_patient_id) {
424 $couch_docid = $this->get_couch_docid();
425 $couch_revid = $this->get_couch_revid();
427 // Set the new patient in CouchDB.
428 if ($couch_docid && $couch_revid) {
429 $couch = new CouchDB();
430 $db = $GLOBALS['couchdb_dbase'];
431 $data = array($db, $couch_docid);
432 $couchresp = $couch->retrieve_doc($data);
433 // CouchDB doesnot support updating a single value in a document.
434 // Have to retrieve the entire document, update the necessary value and save again
435 list ($db, $docid, $revid, $patient_id, $encounter, $type, $json) = $data;
436 $data = array($db, $couch_docid, $couch_revid, $new_patient_id, $couchresp->encounter,
437 $couchresp->mimetype, json_encode($couchresp->data));
438 $resp = $couch->update_doc($data);
439 // Sometimes the response from CouchDB is not available, still it would
440 // have saved in the DB. Hence check one more time.
441 if(!$resp->_id || !$resp->_rev){
442 $data = array($db, $couch_docid, $new_patient_id, $couchresp->encounter);
443 $resp = $couch->retrieve_doc($data);
445 if($resp->_rev == $couch_revid) {
446 return false;
448 else {
449 $this->set_couch_revid($resp->_rev);
453 // Set the new patient in mysql.
454 $this->set_foreign_id($new_patient_id);
455 $this->persist();
457 // Return true for success.
458 return true;
461 } // end of Document
464 $d = new Document(3);
465 $d->type = $d->type_array[1];
466 $d->url = "file:///tmp/test.gif";
467 $d->pages = 0;
468 $d->owner = 60;
469 $d->size = 8000;
470 $d->foreign_id = 25;
471 $d->persist();
472 $d->populate();
474 echo $d->toString(true);*/