3 require_once(dirname(__FILE__
) . "/ORDataObject.class.php");
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
22 * DB unique identifier reference to some other table, this is not unique in the document table
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
34 * Array mapping of possible for values for the type variable
35 * mapping is array text name to index
38 var $type_array = array();
41 * Size of the document in bytes if that is available
47 * Date the document was first persisted
53 * URL which point to the document, may be a file URL, a web URL, a db BLOB URL, or others
59 * Mimetype of the document if available
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
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
78 * Timestamp of the last time the document was changed and persisted, auto maintained by DB, manually change at your own peril
84 * Date (YYYY-MM-DD) logically associated with the document, e.g. when a picture was taken.
90 * 40-character sha1 hash key of the document from when it was uploaded.
96 * DB identifier reference to the lists table (the related issue), 0 if none.
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
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];
118 $this->date
= date("Y-m-d H:i:s");
120 $this->mimetype
= "";
121 $this->docdate
= date("Y-m-d");
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 '%'";
142 $foreign_id= " = '" . mysql_real_escape_string(strval($foreign_id)) . "'";
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']);
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
162 * @return object new or existing document object with the specified URL
164 function document_factory_url($url) {
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']);
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");
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);
196 $d->size
= filesize($filename);
197 $d->type
= $d->type_array
['file_url'];
206 * Convenience function to generate string debug data about the object
208 function toString($html = false) {
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";
226 return nl2br($string);
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) {
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) {
252 function get_type() {
255 function set_size($size) {
258 function get_size() {
261 function set_date($date) {
264 function get_date() {
267 function set_hash($hash) {
270 function get_hash() {
273 function set_url($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() {
315 function set_owner($owner) {
316 $this->owner
= $owner;
318 function get_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
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 ="") {
353 $this->foreign_id
= $fid;
361 $d = new Document(3);
362 $d->type = $d->type_array[1];
363 $d->url = "file:///tmp/test.gif";
371 echo $d->toString(true);*/