3 require_once(dirname(__FILE__
) . "/ORDataObject.class.php");
4 require_once(dirname(__FILE__
) . "/CouchDB.class.php");
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
23 * DB unique identifier reference to some other table, this is not unique in the document table
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
35 * Array mapping of possible for values for the type variable
36 * mapping is array text name to index
39 var $type_array = array();
42 * Size of the document in bytes if that is available
48 * Date the document was first persisted
54 * URL which point to the document, may be a file URL, a web URL, a db BLOB URL, or others
60 * Mimetype of the document if available
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
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
79 * Timestamp of the last time the document was changed and persisted, auto maintained by DB, manually change at your own peril
85 * Date (YYYY-MM-DD) logically associated with the document, e.g. when a picture was taken.
91 * 40-character sha1 hash key of the document from when it was uploaded.
97 * DB identifier reference to the lists table (the related issue), 0 if none.
103 * Whether the file is already 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
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];
125 $this->date
= date("Y-m-d H:i:s");
127 $this->mimetype
= "";
128 $this->docdate
= date("Y-m-d");
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 '%'";
149 $foreign_id= " = '" . mysql_real_escape_string(strval($foreign_id)) . "'";
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']);
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
169 * @return object new or existing document object with the specified URL
171 function document_factory_url($url) {
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']);
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");
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);
203 $d->size
= filesize($filename);
204 $d->type
= $d->type_array
['file_url'];
213 * Convenience function to generate string debug data about the object
215 function toString($html = false) {
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";
233 return nl2br($string);
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) {
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) {
259 function get_type() {
262 function set_size($size) {
265 function get_size() {
268 function set_date($date) {
271 function get_date() {
274 function set_hash($hash) {
277 function get_hash() {
280 function set_url($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() {
328 function set_owner($owner) {
329 $this->owner
= $owner;
331 function get_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
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 ="") {
375 $this->foreign_id
= $fid;
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));
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) {
449 $this->set_couch_revid($resp->_rev
);
453 // Set the new patient in mysql.
454 $this->set_foreign_id($new_patient_id);
457 // Return true for success.
464 $d = new Document(3);
465 $d->type = $d->type_array[1];
466 $d->url = "file:///tmp/test.gif";
474 echo $d->toString(true);*/