3 require_once(dirname(__FILE__
) . "/../library/classes/Controller.class.php");
4 require_once(dirname(__FILE__
) . "/../library/classes/Document.class.php");
5 require_once(dirname(__FILE__
) . "/../library/classes/CategoryTree.class.php");
6 require_once(dirname(__FILE__
) . "/../library/classes/TreeMenu.php");
7 require_once(dirname(__FILE__
) . "/../library/classes/Note.class.php");
9 class C_Document
extends Controller
{
13 var $document_categories;
19 function C_Document($template_mod = "general") {
21 $this->documents
= array();
22 $this->template_mod
= $template_mod;
23 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
24 $this->assign("CURRENT_ACTION", $GLOBALS['webroot']."/controller.php?" . "document&");
26 //get global config options for this namespace
27 $this->_config
= $GLOBALS['oer_config']['documents'];
29 $this->file_path
= $this->_config
['repository'] . preg_replace("/[^A-Za-z0-9]/","_",$_GET['patient_id']) . "/";
31 $this->_args
= array("patient_id" => $_GET['patient_id']);
33 $this->assign("STYLE", $GLOBALS['style']);
34 $t = new CategoryTree(1);
39 function upload_action($patient_id,$category_id) {
40 $category_name = $this->tree
->get_node_name($category_id);
41 $this->assign("category_id", $category_id);
42 $this->assign("category_name", $category_name);
43 $this->assign("patient_id", $patient_id);
44 $activity = $this->fetch($GLOBALS['template_dir'] . "documents/" . $this->template_mod
. "_upload.html");
45 $this->assign("activity", $activity);
46 return $this->list_action($patient_id);
49 function upload_action_process() {
51 if ($_POST['process'] != "true")
54 if (is_numeric($_POST['category_id'])) {
55 $category_id = $_POST['category_id'];
57 if (is_numeric($_POST['patient_id'])) {
58 $patient_id = $_POST['patient_id'];
61 foreach ($_FILES as $file) {
62 $fname = $file['name'];
64 if ($file['error'] > 0 ||
empty($file['name']) ||
$file['size'] == 0) {
65 $fname = $file['name'];
67 $fname = htmlentities("<empty>");
69 $error = "Error number: " . $file['error'] . " occured while uploading file named: " . $fname . "\n";
70 if ($file['size'] == 0) {
71 $error .= "The system does not permit uploading files of with size 0.\n";
77 if (!file_exists($this->file_path
)) {
78 if (!mkdir($this->file_path
,0700)) {
79 $error .= "The system was unable to create the directory for this upload, '" . $this->file_path
. "'.\n";
83 $fname = preg_replace("/[^a-zA-Z0-9_.]/","_",$fname);
84 if (file_exists($this->file_path
.$file['name'])) {
85 $error .= "File with same name already exists at location: " . $this->file_path
. "\n";
86 $fname = basename($this->_rename_file($this->file_path
.$file['name']));
87 $file['name'] = $fname;
88 $error .= "Current file name was changed to " . $fname ."\n";
90 if (move_uploaded_file($file['tmp_name'],$this->file_path
.$file['name'])) {
91 $error .= "File " . $file['name'] . " successfully stored.\n";
93 $d->url
= "file://" .$this->file_path
.$file['name'];
94 $d->mimetype
= $file['type'];
95 $d->size
= $file['size'];
96 $d->type
= $d->type_array
['file_url'];
97 $d->set_foreign_id($patient_id);
100 $this->assign("file",$d);
102 if (is_numeric($d->get_id()) && is_numeric($category_id)) {
103 $sql = "REPLACE INTO categories_to_documents set category_id = '" . $category_id . "', document_id = '" . $d->get_id() . "'";
104 $d->_db
->Execute($sql);
108 $error .= "The file could not be succesfully stored, this error is usually related to permissions problems on the storage system.\n";
112 $this->assign("error", nl2br($error));
113 //$this->_state = false;
114 $_POST['process'] = "";
115 //return $this->fetch($GLOBALS['template_dir'] . "documents/" . $this->template_mod . "_upload.html");
118 function note_action_process($patient_id) {
120 if ($_POST['process'] != "true")
124 parent
::populate_object($n);
127 $this->_state
= false;
128 $_POST['process'] = "";
129 return $this->view_action($patient_id,$n->get_foreign_id());
132 function default_action() {
133 return $this->list_action();
136 function view_action($patient_id="",$doc_id) {
137 // Added by Rod to support document delete:
138 global $gacl_object, $phpgacl_location;
141 require_once(dirname(__FILE__
) . "/../library/acl.inc");
142 require_once(dirname(__FILE__
) . "/../library/lists.inc");
144 $d = new Document($doc_id);
147 $notes = $n->notes_factory($doc_id);
149 $this->assign("file", $d);
150 $this->assign("web_path", $this->_link("retrieve") . "document_id=" . $d->get_id() . "&");
151 $this->assign("NOTE_ACTION",$this->_link("note"));
152 $this->assign("MOVE_ACTION",$this->_link("move") . "document_id=" . $d->get_id() . "&process=true");
154 // Added by Rod to support document delete:
156 if (acl_check('admin', 'super')) {
157 $delete_string = "<a href='' onclick='return deleteme(" . $d->get_id() .
158 ")'><font color='red'>(Delete this document)</font></a>";
160 $this->assign("delete_string", $delete_string);
161 $this->assign("REFRESH_ACTION",$this->_link("list"));
163 // Added by Rod to support document date update:
164 $this->assign("DOCDATE", $d->get_docdate());
165 $this->assign("UPDATE_ACTION",$this->_link("update") .
166 "document_id=" . $d->get_id() . "&process=true");
168 // Added by Rod to support document issue update:
169 $issues_options = "<option value='0'>-- Select Issue --</option>";
170 $ires = sqlStatement("SELECT id, type, title, begdate FROM lists WHERE " .
171 "pid = $patient_id " . // AND enddate IS NULL " .
172 "ORDER BY type, begdate");
173 while ($irow = sqlFetchArray($ires)) {
174 $desc = $irow['type'];
175 if ($ISSUE_TYPES[$desc]) $desc = $ISSUE_TYPES[$desc][2];
176 $desc .= ": " . $irow['begdate'] . " " . htmlspecialchars(substr($irow['title'], 0, 40));
177 $sel = ($irow['id'] == $d->get_list_id()) ?
' selected' : '';
178 $issues_options .= "<option value='" . $irow['id'] . "'$sel>$desc</option>";
180 $this->assign("ISSUES_LIST", $issues_options);
182 $this->assign("notes",$notes);
184 $this->_last_node
= null;
186 $menu = new HTML_TreeMenu();
188 //pass an empty array because we don't want the documents for each category showing up in this list box
189 $rnode = $this->_array_recurse($this->tree
->tree
,array());
190 $menu->addItem($rnode);
191 $treeMenu_listbox = &new HTML_TreeMenu_Listbox($menu, array("promoText" => "Move Document to Category:"));
193 $this->assign("tree_html_listbox",$treeMenu_listbox->toHTML());
195 $activity = $this->fetch($GLOBALS['template_dir'] . "documents/" . $this->template_mod
. "_view.html");
196 $this->assign("activity", $activity);
198 return $this->list_action($patient_id);
201 function retrieve_action($patient_id="",$document_id,$as_file=true) {
202 $d = new Document($document_id);
203 $url = $d->get_url();
205 //strip url of protocol handler
206 $url = preg_replace("|^(.*)://|","",$url);
208 //change full path to current webroot. this is for documents that may have
209 //been moved from a different filesystem and the full path in the database
211 $temp_url = $GLOBALS["fileroot"].'/documents/'.$_SESSION["pid"].'/'.basename($url);
212 if (file_exists($temp_url)) {
215 if (!file_exists($url)) {
216 echo "The requested document is not present at the expected location on the filesystem or there are not sufficient permissions to access it. $url";
219 header("Pragma: public");
220 header("Expires: 0");
221 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
223 header("Content-Disposition: attachment; filename=\"" . basename($d->get_url()) . "\"");
225 header("Content-Type: " . $d->get_mimetype());
226 header("Content-Length: " . $d->get_size());
227 $f = fopen($url,"r");
233 function queue_action($patient_id="") {
234 $messages = $this->_tpl_vars
['messages'];
235 $queue_files = array();
237 //see if the repository exists and it is a directory else error
238 if (file_exists($this->_config
['repository']) && is_dir($this->_config
['repository'])) {
239 $dir = opendir($this->_config
['repository']);
240 //read each entry in the directory
241 while (($file = readdir($dir)) !== false) {
242 //concat the filename and path
243 $file = $this->_config
['repository'] .$file;
244 $file_info = array();
245 //if the filename is a file get its info and put into a tmp array
246 if (is_file($file) && strpos(basename($file),".") !== 0) {
247 $file_info['filename'] = basename($file);
248 $file_info['mtime'] = date("m/d/Y H:i:s",filemtime($file));
249 $d = Document
::document_factory_url("file://" . $file);
250 preg_match("/^([0-9]+)_/",basename($file),$patient_match);
251 $file_info['patient_id'] = $patient_match[1];
252 $file_info['document_id'] = $d->get_id();
253 $file_info['web_path'] = $this->_link("retrieve",true) . "document_id=" . $d->get_id() . "&";
255 //merge the tmp array into the larger array
256 $queue_files[] = $file_info;
262 $messages .= "The repository directory does not exist, it is not a directory or there are not sufficient permissions to access it. '" . $this->config
['repository'] . "'\n";
266 $this->assign("queue_files",$queue_files);
267 $this->_last_node
= null;
269 $menu = new HTML_TreeMenu();
271 //pass an empty array because we don't want the documents for each category showing up in this list box
272 $rnode = $this->_array_recurse($this->tree
->tree
,array());
273 $menu->addItem($rnode);
274 $treeMenu_listbox = &new HTML_TreeMenu_Listbox($menu, array());
276 $this->assign("tree_html_listbox",$treeMenu_listbox->toHTML());
278 $this->assign("messages",nl2br($messages));
279 return $this->fetch($GLOBALS['template_dir'] . "documents/" . $this->template_mod
. "_queue.html");
282 function queue_action_process() {
283 if ($_POST['process'] != "true")
286 $messages = $this->_tpl_vars
['messages'];
288 //build a category tree so we can have a list of category ids that are valid
289 $ct = new CategoryTree(1);
290 $categories = $ct->_id_name
;
292 //see if there were and posted files and assign them
294 is_array($_POST['files']) ?
$files = $_POST['files']: $files = array();
296 //loop through posted files
297 foreach($files as $doc_id=> $file) {
298 //only operate on files checked as active
299 if (!$file['active']) continue;
301 //run basic validation checks
302 if (!is_numeric($file['patient_id']) ||
!is_numeric($file['category_id']) ||
!is_numeric($doc_id)) {
303 $messages .= "Error processing file '" . $file['name'] ."' the patient id must be a number and the category must exist.\n";
307 //validate that the pod exists
308 $d = new Document($doc_id);
309 $sql = "SELECT pid from patient_data where pubpid = '" . $file['patient_id'] . "'";
310 $result = $d->_db
->Execute($sql);
312 if (!$result ||
$result->EOF
) {
313 //patient id does not exist
314 $messages .= "Error processing file '" . $file['name'] ." the specified patient id '" . $file['patient_id'] . "' could not be found.\n";
318 //validate that the category id exists
319 if (!isset($categories[$file['category_id']])) {
320 $messages .= "Error processing file '" . $file['name'] . " the specified category with id '" . $file['category_id'] . "' could not be found.\n";
324 //now do the work of moving the file
325 $new_path = $this->_config
['repository'] . $file['patient_id'] ."/";
327 //see if the patient dir exists in the repository and create if not
328 if (!file_exists($new_path)) {
329 if (!mkdir($new_path,0700)) {
330 $messages .= "The system was unable to create the directory for this upload, '" . $this->file_path
. "'.\n";
335 //fname is the name of the file after it is moved
336 $fname = $file['name'];
338 //see if patient autonumbering is used in this filename, if so strip out the autonumber part
339 preg_match("/^([0-9]+)_/",basename($fname),$patient_match);
340 if ($patient_match[1] == $file['patient_id']) {
341 $fname = preg_replace("/^([0-9]+)_/","",$fname);
344 //filenames should not have funny chars
345 $fname = preg_replace("/[^a-zA-Z0-9_.]/","_",$fname);
347 //see if there is an existing file with the same name and rename as necessary
348 if (file_exists($new_path.$file['name'])) {
349 $messages .= "File with same name already exists at location: " . $new_path . "\n";
350 $fname = basename($this->_rename_file($new_path.$file['name']));
351 $messages .= "Current file name was changed to " . $fname ."\n";
355 if (rename($this->_config
['repository'].$file['name'],$new_path.$fname)) {
356 $messages .= "File " . $fname . " moved to patient id '" . $file['patient_id'] ."' and category '" . $categories[$file['category_id']]['name'] . "' successfully.\n";
357 $d->url
= "file://" .$new_path.$fname;
358 $d->set_foreign_id($file['patient_id']);
359 $d->set_mimetype($mimetype);
363 if (is_numeric($d->get_id()) && is_numeric($file['category_id'])) {
364 $sql = "REPLACE INTO categories_to_documents set category_id = '" . $file['category_id'] . "', document_id = '" . $d->get_id() . "'";
365 $d->_db
->Execute($sql);
369 $error .= "The file could not be succesfully stored, this error is usually related to permissions problems on the storage system.\n";
372 $this->assign("messages",$messages);
373 $_POST['process'] = "";
376 function move_action_process($patient_id="",$document_id) {
377 if ($_POST['process'] != "true")
380 $new_category_id = $_POST['new_category_id'];
381 $new_patient_id = $_POST['new_patient_id'];
383 //move to new category
384 if (is_numeric($new_category_id) && is_numeric($document_id)) {
385 $sql = "UPDATE categories_to_documents set category_id = '" . $new_category_id . "' where document_id = '" . $document_id ."'";
386 $messages .= "Document moved to new category '" . $this->tree
->_id_name
[$new_category_id]['name'] . "' successfully.\n";
388 $this->tree
->_db
->Execute($sql);
391 //move to new patient
392 if (is_numeric($new_patient_id) && is_numeric($document_id)) {
393 $d = new Document($document_id);
394 // $sql = "SELECT pid from patient_data where pubpid = '" . $new_patient_id . "'";
395 $sql = "SELECT pid from patient_data where pid = '" . $new_patient_id . "'";
396 $result = $d->_db
->Execute($sql);
398 if (!$result ||
$result->EOF
) {
399 //patient id does not exist
400 $messages .= "Document could not be moved to patient id '" . $new_patient_id . "' because that id does not exist.\n";
403 //set the new patient
404 $d->set_foreign_id($new_patient_id);
406 $this->_state
= false;
407 $messages .= "Document moved to patient id '" . $new_patient_id . "' successfully.\n";
408 $this->assign("messages",$messages);
409 return $this->list_action($patient_id);
412 //in this case return the document to the queue instead of moving it
413 elseif (strtolower($new_patient_id) == "q" && is_numeric($document_id)) {
414 $d = new Document($document_id);
415 $new_path = $this->_config
['repository'];
416 $fname = $d->get_url_file();
418 //see if there is an existing file with the same name and rename as necessary
419 if (file_exists($new_path.$d->get_url_file())) {
420 $messages .= "File with same name already exists in the queue.\n";
421 $fname = basename($this->_rename_file($new_path.$d->get_url_file()));
422 $messages .= "Current file name was changed to " . $fname ."\n";
426 if (rename($d->get_url_filepath(),$new_path.$fname)) {
427 $d->url
= "file://" .$new_path.$fname;
428 $d->set_foreign_id("");
433 $sql = "DELETE FROM categories_to_documents where document_id =" . $d->_db
->qstr($document_id);
434 $d->_db
->Execute($sql);
435 $messages .= "Document returned to queue successfully.\n";
439 $messages .= "The file could not be succesfully stored, this error is usually related to permissions problems on the storage system.\n";
442 $this->_state
= false;
443 $this->assign("messages",$messages);
444 return $this->list_action($patient_id);
447 $this->_state
= false;
448 $this->assign("messages",$messages);
449 return $this->view_action($patient_id,$document_id);
452 // Added by Rod for metadata update.
454 function update_action_process($patient_id="", $document_id) {
455 if ($_POST['process'] != "true") {
456 die("process is '" . $_POST['process'] . "', expected 'true'");
460 $docdate = $_POST['docdate'];
461 $issue_id = $_POST['issue_id'];
463 if (is_numeric($document_id)) {
464 if (preg_match('/^\d\d\d\d-\d+-\d+$/', $docdate)) {
465 $docdate = "'$docdate'";
469 if (!is_numeric($issue_id)) {
472 $sql = "UPDATE documents SET docdate = $docdate, " .
473 "list_id = '$issue_id' " .
474 "WHERE id = '$document_id'";
475 $this->tree
->_db
->Execute($sql);
476 $messages .= "Document date and issue updated successfully\n";
479 $this->_state
= false;
480 $this->assign("messages", $messages);
481 return $this->view_action($patient_id, $document_id);
484 function list_action($patient_id = "") {
485 $this->_last_node
= null;
486 $categories_list = $this->tree
->_get_categories_array($patient_id);
487 //print_r($categories_list);
489 $menu = new HTML_TreeMenu();
490 $rnode = $this->_array_recurse($this->tree
->tree
,$categories_list);
491 $menu->addItem($rnode);
492 $treeMenu = &new HTML_TreeMenu_DHTML($menu, array('images' => 'images', 'defaultClass' => 'treeMenuDefault'));
493 $treeMenu_listbox = &new HTML_TreeMenu_Listbox($menu, array('linkTarget' => '_self'));
495 $this->assign("tree_html",$treeMenu->toHTML());
497 return $this->fetch($GLOBALS['template_dir'] . "documents/" . $this->template_mod
. "_list.html");
501 * This is a recursive function to rename a file to something that doesn't already exist. It appends a numeric to the end of the file, if
502 * the file already has a numeric it increments that.
503 * It supports numeric endings upto 1000, after which it uses an md5sum on now() instead. This is because some files end in
504 * dates and we don't want to increment those.
506 function _rename_file($fname) {
507 $file = basename($fname);
508 $fparts = split("\.",$fname);
509 $path = dirname($fname);
510 if (is_numeric($fparts[count($fparts) -1]) && $fparts[count($fparts) -1] < 1000) {
512 for($i=0;$i<count($fparts);$i++
) {
513 if ($i == (count($fparts) -1)) {
514 $new_name .= ($fparts[$i] +
1);
517 $new_name .= $fparts[$i] . ".";
522 elseif (is_numeric($fparts[count($fparts)])) {
523 $fname = $fname . "." . md5sum(now());
526 $fname = $fname . ".1";
528 if (file_exists($fname)) {
529 return $this->_rename_file($fname);
536 function &_array_recurse($array,$categories = array()) {
537 if (!is_array($array)) {
540 $node = &$this->_last_node
;
541 $current_node = &$node;
542 $expandedIcon = 'folder-expanded.gif';
543 foreach($array as $id => $ar) {
544 $icon = 'folder.gif';
545 if (is_array($ar) ||
!empty($id)) {
547 //echo "r:" . $this->tree->get_node_name($id) . "<br>";
548 $rnode = new HTML_TreeNode(array("id" => $id, 'text' => $this->tree
->get_node_name($id), 'link' => $this->_link("upload") . "parent_id=" . $id . "&", 'icon' => $icon, 'expandedIcon' => $expandedIcon, 'expanded' => false));
549 $this->_last_node
= &$rnode;
551 $current_node =&$rnode;
554 //echo "p:" . $this->tree->get_node_name($id) . "<br>";
555 $this->_last_node
= &$node->addItem(new HTML_TreeNode(array("id" => $id, 'text' => $this->tree
->get_node_name($id), 'link' => $this->_link("upload") . "parent_id=" . $id . "&", 'icon' => $icon, 'expandedIcon' => $expandedIcon)));
556 $current_node =&$this->_last_node
;
559 $this->_array_recurse($ar,$categories);
562 if ($id === 0 && !empty($ar)) {
563 $info = $this->tree
->get_node_info($id);
564 //echo "b:" . $this->tree->get_node_name($id) . "<br>";
565 $current_node = &$node->addItem(new HTML_TreeNode(array("id" => $id, 'text' => $info['value'], 'link' => $this->_link("upload") . "parent_id=" . $id . "&", 'icon' => $icon, 'expandedIcon' => $expandedIcon)));
568 //there is a third case that is implicit here when title === 0 and $ar is empty, in that case we do not want to do anything
569 //this conditional tree could be more efficient but working with recursive trees makes my head hurt, TODO
570 if ($id !== 0 && is_object($node)) {
571 //echo "n:" . $this->tree->get_node_name($id) . "<br>";
572 $current_node = &$node->addItem(new HTML_TreeNode(array("id" => $id, 'text' => $this->tree
->get_node_name($id), 'link' => $this->_link("upload") . "parent_id=" . $id . "&", 'icon' => $icon, 'expandedIcon' => $expandedIcon)));
578 // If there are documents in this document category, then add their
579 // attributes to the current node.
581 if (is_array($categories[$id])) {
582 foreach ($categories[$id] as $doc) {
583 $current_node->addItem(new HTML_TreeNode(array(
584 'text' => $doc['docdate'] . ' ' . basename($doc['url']),
585 'link' => $this->_link("view") . "doc_id=" . $doc['document_id'] . "&",
587 'expandedIcon' => $expandedIcon
597 //place to hold optional code
598 //$first_node = array_keys($t->tree);
599 //$first_node = $first_node[0];
600 //$node1 = new HTML_TreeNode(array('text' => $t->get_node_name($first_node), 'link' => "test.php", 'icon' => $icon, 'expandedIcon' => $expandedIcon, 'expanded' => true), array('onclick' => "alert('foo'); return false", 'onexpand' => "alert('Expanded')"));
602 //$this->_last_node = &$node1;