dated reminders bug fix
[openemr.git] / library / classes / Document.class.php
blob499c1d90b30bd03140539094104d162c5208483c
1 <?php
3 require_once(dirname(__FILE__) . "/ORDataObject.class.php");
5 /**
6 * class Document
7 * This class is the logical representation of a physical file on some system somewhere that can be referenced with a URL
8 * 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.
9 * 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
10 * id and categories which do the same.
13 class Document extends ORDataObject{
16 * Database unique identifier
17 * @var id
19 var $id;
22 * DB unique identifier reference to some other table, this is not unique in the document table
23 * @var int
25 var $foreign_id;
28 * Enumerated DB field which is met information about how to use the URL
29 * @var int can also be a the properly enumerated string
31 var $type;
34 * Array mapping of possible for values for the type variable
35 * mapping is array text name to index
36 * @var array
38 var $type_array = array();
41 * Size of the document in bytes if that is available
42 * @var int
44 var $size;
47 * Date the document was first persisted
48 * @var string
50 var $date;
53 * URL which point to the document, may be a file URL, a web URL, a db BLOB URL, or others
54 * @var string
56 var $url;
59 * Mimetype of the document if available
60 * @var string
62 var $mimetype;
65 * 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
66 * @var int
68 var $pages;
71 * Foreign key identifier of who initially persisited the document,
72 * potentially ownership could be changed but that would be up to an external non-document object process
73 * @var int
75 var $owner;
78 * Timestamp of the last time the document was changed and persisted, auto maintained by DB, manually change at your own peril
79 * @var int
81 var $revision;
84 * Date (YYYY-MM-DD) logically associated with the document, e.g. when a picture was taken.
85 * @var string
87 var $docdate;
90 * 40-character sha1 hash key of the document from when it was uploaded.
91 * @var string
93 var $hash;
96 * DB identifier reference to the lists table (the related issue), 0 if none.
97 * @var int
99 var $list_id;
102 * Constructor sets all Document attributes to their default value
103 * @param int $id optional existing id of a specific document, if omitted a "blank" document is created
105 function Document($id = "") {
106 //call the parent constructor so we have a _db to work with
107 parent::ORDataObject();
109 //shore up the most basic ORDataObject bits
110 $this->id = $id;
111 $this->_table = "documents";
113 //load the enum type from the db using the parent helper function, this uses psuedo-class variables so it is really cheap
114 $this->type_array = $this->_load_enum("type");
116 $this->type = $this->type_array[0];
117 $this->size = 0;
118 $this->date = date("Y-m-d H:i:s");
119 $this->url = "";
120 $this->mimetype = "";
121 $this->docdate = date("Y-m-d");
122 $this->hash = "";
123 $this->list_id = 0;
125 if ($id != "") {
126 $this->populate();
131 * Convenience function to get an array of many document objects
132 * For really large numbers of documents there is a way more efficient way to do this by overwriting the populate method
133 * @param int $foreign_id optional id use to limit array on to a specific relation, otherwise every document object is returned
135 function documents_factory($foreign_id = "") {
136 $documents = array();
138 if (empty($foreign_id)) {
139 $foreign_id= "like '%'";
141 else {
142 $foreign_id= " = '" . mysql_real_escape_string(strval($foreign_id)) . "'";
145 $d = new Document();
146 $sql = "SELECT id FROM " . $d->_table . " WHERE foreign_id " .$foreign_id ;
147 $result = $d->_db->Execute($sql);
149 while ($result && !$result->EOF) {
150 $documents[] = new Document($result->fields['id']);
151 $result->MoveNext();
154 return $documents;
158 * Convenience function to get a document object from a url
159 * Checks to see if there is an existing document with that URL and if so returns that object, otherwise
160 * creates a new one, persists it and returns it
161 * @param string $url
162 * @return object new or existing document object with the specified URL
164 function document_factory_url($url) {
165 $d = new Document();
166 //strip url handler, for now we always assume file://
167 $filename = preg_replace("|^(.*)://|","",$url);
169 if (!file_exists($filename)) {
170 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");
173 $sql = "SELECT id FROM " . $d->_table . " WHERE url= '" . mysql_real_escape_string($url) ."'" ;
174 $result = $d->_db->Execute($sql);
176 if ($result && !$result->EOF) {
177 if (file_exists($filename)) {
178 $d = new Document($result->fields['id']);
180 else {
181 $sql = "DELETE FROM " . $d->_table . " WHERE id= '" . $result->fields['id'] ."'";
182 $result = $d->_db->Execute($sql);
183 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");
186 else {
187 $file_command = $GLOBALS['oer_config']['document']['file_command_path'] ;
188 $cmd_args = "-i ".escapeshellarg($new_path.$fname);
190 $command = $file_command." ".$cmd_args;
191 $mimetype = exec($command);
192 $mime_array = split(":", $mimetype);
193 $mimetype = $mime_array[1];
194 $d->set_mimetype($mimetype);
195 $d->url = $url;
196 $d->size = filesize($filename);
197 $d->type = $d->type_array['file_url'];
198 $d->persist();
199 $d->populate();
202 return $d;
206 * Convenience function to generate string debug data about the object
208 function toString($html = false) {
209 $string .= "\n"
210 . "ID: " . $this->id."\n"
211 . "FID: " . $this->foreign_id."\n"
212 . "type: " . $this->type . "\n"
213 . "type_array: " . print_r($this->type_array,true) . "\n"
214 . "size: " . $this->size . "\n"
215 . "date: " . $this->date . "\n"
216 . "url: " . $this->url . "\n"
217 . "mimetype: " . $this->mimetype . "\n"
218 . "pages: " . $this->pages . "\n"
219 . "owner: " . $this->owner . "\n"
220 . "revision: " . $this->revision . "\n"
221 . "docdate: " . $this->docdate . "\n"
222 . "hash: " . $this->hash . "\n"
223 . "list_id: " . $this->list_id . "\n";
225 if ($html) {
226 return nl2br($string);
228 else {
229 return $string;
233 /**#@+
234 * Getter/Setter methods used by reflection to affect object in persist/poulate operations
235 * @param mixed new value for given attribute
237 function set_id($id) {
238 $this->id = $id;
240 function get_id() {
241 return $this->id;
243 function set_foreign_id($fid) {
244 $this->foreign_id = $fid;
246 function get_foreign_id() {
247 return $this->foreign_id;
249 function set_type($type) {
250 $this->type = $type;
252 function get_type() {
253 return $this->type;
255 function set_size($size) {
256 $this->size = $size;
258 function get_size() {
259 return $this->size;
261 function set_date($date) {
262 $this->date = $date;
264 function get_date() {
265 return $this->date;
267 function set_hash($hash) {
268 $this->hash = $hash;
270 function get_hash() {
271 return $this->hash;
273 function set_url($url) {
274 $this->url = $url;
276 function get_url() {
277 return $this->url;
280 * this returns the url stripped down to basename
282 function get_url_web() {
283 return basename($this->url);
286 * get the url without the protocol handler
288 function get_url_filepath() {
289 return preg_replace("|^(.*)://|","",$this->url);
292 * get the url filename only
294 function get_url_file() {
295 return basename(preg_replace("|^(.*)://|","",$this->url));
298 * get the url path only
300 function get_url_path() {
301 return dirname(preg_replace("|^(.*)://|","",$this->url)) ."/";
303 function set_mimetype($mimetype) {
304 $this->mimetype = $mimetype;
306 function get_mimetype() {
307 return $this->mimetype;
309 function set_pages($pages) {
310 $this->pages = $pages;
312 function get_pages() {
313 return $this->pages;
315 function set_owner($owner) {
316 $this->owner = $owner;
318 function get_owner() {
319 return $this->owner;
322 * No getter for revision because it is updated automatically by the DB.
324 function set_revision($revision) {
325 $this->revision = $revision;
327 function set_docdate($docdate) {
328 $this->docdate = $docdate;
330 function get_docdate() {
331 return $this->docdate;
333 function set_list_id($list_id) {
334 $this->list_id = $list_id;
336 function get_list_id() {
337 return $this->list_id;
339 function get_ccr_type($doc_id){
340 $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));
341 return $type['name'];
344 * Overridden function to stor current object state in the db.
345 * current overide is to allow for a just in time foreign id, often this is needed
346 * when the object is never directly exposed and is handled as part of a larger
347 * object hierarchy.
348 * @param int $fid foreign id that should be used so that this document can be related (joined) on it later
351 function persist($fid ="") {
352 if (!empty($fid)) {
353 $this->foreign_id = $fid;
355 parent::persist();
358 } // end of Document
361 $d = new Document(3);
362 $d->type = $d->type_array[1];
363 $d->url = "file:///tmp/test.gif";
364 $d->pages = 0;
365 $d->owner = 60;
366 $d->size = 8000;
367 $d->foreign_id = 25;
368 $d->persist();
369 $d->populate();
371 echo $d->toString(true);*/