added 1st initial to provider name in calendar Day and Week views
[openemr.git] / library / classes / Document.class.php
blobfdea1cfd0865b2321a108665e93b1a47016cd922
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;
83 /**
84 * Constructor sets all Document attributes to their default value
85 * @param int $id optional existing id of a specific document, if omitted a "blank" document is created
87 function Document($id = "") {
88 //call the parent constructor so we have a _db to work with
89 parent::ORDataObject();
91 //shore up the most basic ORDataObject bits
92 $this->id = $id;
93 $this->_table = "documents";
95 //load the enum type from the db using the parent helper function, this uses psuedo-class variables so it is really cheap
96 $this->type_array = $this->_load_enum("type");
98 $this->type = $this->type_array[0];
99 $this->size = 0;
100 $this->date = date("Y-m-d H:i:s");
101 $this->url = "";
102 $this->mimetype = "";
104 if ($id != "") {
105 $this->populate();
110 * Convenience function to get an array of many document objects
111 * For really large numbers of documents there is a way more efficient way to do this by overwriting the populate method
112 * @param int $foreign_id optional id use to limit array on to a specific relation, otherwise every document object is returned
114 function documents_factory($foreign_id = "") {
115 $documents = array();
117 if (empty($foreign_id)) {
118 $foreign_id= "like '%'";
120 else {
121 $foreign_id= " = '" . mysql_real_escape_string(strval($foreign_id)) . "'";
124 $d = new Document();
125 $sql = "SELECT id FROM " . $d->_table . " WHERE foreign_id " .$foreign_id ;
126 $result = $d->_db->Execute($sql);
128 while ($result && !$result->EOF) {
129 $documents[] = new Document($result->fields['id']);
130 $result->MoveNext();
133 return $documents;
137 * Convenience function to get a document object from a url
138 * Checks to see if there is an existing document with that URL and if so returns that object, otherwise
139 * creates a new one, persists it and returns it
140 * @param string $url
141 * @return object new or existing document object with the specified URL
143 function document_factory_url($url) {
144 $d = new Document();
145 //strip url handler, for now we always assume file://
146 $filename = preg_replace("|^(.*)://|","",$url);
148 if (!file_exists($filename)) {
149 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");
152 $sql = "SELECT id FROM " . $d->_table . " WHERE url= '" . mysql_real_escape_string($url) ."'" ;
153 $result = $d->_db->Execute($sql);
155 if ($result && !$result->EOF) {
156 if (file_exists($filename)) {
157 $d = new Document($result->fields['id']);
159 else {
160 $sql = "DELETE FROM " . $d->_table . " WHERE id= '" . $result->fields['id'] ."'";
161 $result = $d->_db->Execute($sql);
162 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");
165 else {
166 $file_command = $GLOBALS['oer_config']['document']['file_command_path'] ;
167 $cmd_args = "-i ".escapeshellarg($new_path.$fname);
169 $command = $file_command." ".$cmd_args;
170 $mimetype = exec($command);
171 $mime_array = split(":", $mimetype);
172 $mimetype = $mime_array[1];
174 $d->set_mimetype($mimetype);
175 $d->url = $url;
176 $d->size = filesize($filename);
177 $d->type = $d->type_array['file_url'];
178 $d->persist();
179 $d->populate();
182 return $d;
186 * Convenience function to generate string debug data about the object
188 function toString($html = false) {
189 $string .= "\n"
190 . "ID: " . $this->id."\n"
191 . "FID: " . $this->foreign_id."\n"
192 . "type: " . $this->type . "\n"
193 . "type_array: " . print_r($this->type_array,true) . "\n"
194 . "size: " . $this->size . "\n"
195 . "date: " . $this->date . "\n"
196 . "url: " . $this->url . "\n"
197 . "mimetype: " . $this->mimetype . "\n"
198 . "pages: " . $this->pages . "\n"
199 . "owner: " . $this->owner . "\n"
200 . "revision: " . $this->revision. "\n";
202 if ($html) {
203 return nl2br($string);
205 else {
206 return $string;
210 /**#@+
211 * Getter/Setter methods used by reflection to affect object in persist/poulate operations
212 * @param mixed new value for given attribute
214 function set_id($id) {
215 $this->id = $id;
217 function get_id() {
218 return $this->id;
220 function set_foreign_id($fid) {
221 $this->foreign_id = $fid;
223 function get_foreign_id() {
224 return $this->foreign_id;
226 function set_type($type) {
227 $this->type = $type;
229 function get_type() {
230 return $this->type;
232 function set_size($size) {
233 $this->size = $size;
235 function get_size() {
236 return $this->size;
238 function set_date($date) {
239 $this->date = $date;
241 function get_date() {
242 return $this->date;
244 function set_url($url) {
245 $this->url = $url;
247 function get_url() {
248 return $this->url;
251 * this returns the url stripped down to basename
253 function get_url_web() {
254 return basename($this->url);
257 * get the url without the protocol handler
259 function get_url_filepath() {
260 return preg_replace("|^(.*)://|","",$this->url);
263 * get the url filename only
265 function get_url_file() {
266 return basename(preg_replace("|^(.*)://|","",$this->url));
269 * get the url path only
271 function get_url_path() {
272 return dirname(preg_replace("|^(.*)://|","",$this->url)) ."/";
274 function set_mimetype($mimetype) {
275 $this->mimetype = $mimetype;
277 function get_mimetype() {
278 return $this->mimetype;
280 function set_pages($pages) {
281 $this->pages = $pages;
283 function get_pages() {
284 return $this->pages;
286 function set_owner($owner) {
287 $this->owner = $owner;
289 function get_owner() {
290 return $this->owner;
293 * No getter for revision because it is updated automatically by the DB.
295 function set_revision($revision) {
296 $this->revision = $revision;
300 * Overridden function to stor current object state in the db.
301 * current overide is to allow for a just in time foreign id, often this is needed
302 * when the object is never directly exposed and is handled as part of a larger
303 * object hierarchy.
304 * @param int $fid foreign id that should be used so that this document can be related (joined) on it later
307 function persist($fid ="") {
308 if (!empty($fid)) {
309 $this->foreign_id = $fid;
311 parent::persist();
314 } // end of Document
317 $d = new Document(3);
318 $d->type = $d->type_array[1];
319 $d->url = "file:///tmp/test.gif";
320 $d->pages = 0;
321 $d->owner = 60;
322 $d->size = 8000;
323 $d->foreign_id = 25;
324 $d->persist();
325 $d->populate();
327 echo $d->toString(true);*/