function change_patient($new_patient_id) { $couch_docid = $this->get_couch_docid(); $couch_revid = $this->get_couch_revid(); // Set the new patient in CouchDB. if ($couch_docid && $couch_revid) { $couch = new CouchDB(); $db = $GLOBALS['couchdb_dbase']; $data = array($db, $couch_docid); $couchresp = $couch->retrieve_doc($data); // CouchDB doesnot support updating a single value in a document. // Have to retrieve the entire document, update the necessary value and save again list($db, $docid, $revid, $patient_id, $encounter, $type, $json) = $data; $data = array($db, $couch_docid, $couch_revid, $new_patient_id, $couchresp->encounter, $couchresp->mimetype, json_encode($couchresp->data)); $resp = $couch->update_doc($data); // Sometimes the response from CouchDB is not available, still it would // have saved in the DB. Hence check one more time. if (!$resp->_id || !$resp->_rev) { $data = array($db, $couch_docid, $new_patient_id, $couchresp->encounter); $resp = $couch->retrieve_doc($data); } if ($resp->_rev == $couch_revid) { return false; } else { $this->set_couch_revid($resp->_rev); } } // Set the new patient in mysql. $this->set_foreign_id($new_patient_id); $this->persist(); // Return true for success. return true; }
function move_action_process($patient_id = "", $document_id) { if ($_POST['process'] != "true") { return; } $new_category_id = $_POST['new_category_id']; $new_patient_id = $_POST['new_patient_id']; //move to new category if (is_numeric($new_category_id) && is_numeric($document_id)) { $sql = "UPDATE categories_to_documents set category_id = '" . $new_category_id . "' where document_id = '" . $document_id . "'"; $messages .= xl('Document moved to new category', '', '', ' \'') . $this->tree->_id_name[$new_category_id]['name'] . xl('successfully.', '', '\' ') . "\n"; //echo $sql; $this->tree->_db->Execute($sql); } //move to new patient if (is_numeric($new_patient_id) && is_numeric($document_id)) { $d = new Document($document_id); // $sql = "SELECT pid from patient_data where pubpid = '" . $new_patient_id . "'"; $sql = "SELECT pid from patient_data where pid = '" . $new_patient_id . "'"; $result = $d->_db->Execute($sql); if (!$result || $result->EOF) { //patient id does not exist $messages .= xl('Document could not be moved to patient id', '', '', ' \'') . $new_patient_id . xl('because that id does not exist.', '', '\' ') . "\n"; } else { $couch_docid = $d->get_couch_docid(); $couch_revid = $d->get_couch_revid(); //set the new patient in CouchDB $couchsavefailed = false; if ($couch_docid && $couch_revid) { $couch = new CouchDB(); $db = $GLOBALS['couchdb_dbase']; $data = array($db, $couch_docid); $couchresp = $couch->retrieve_doc($data); //CouchDB doesnot support updating a single value in a document. //Have to retrieve the entire document,update the necessary value and save again list($db, $docid, $revid, $patient_id, $encounter, $type, $json) = $data; $data = array($db, $couch_docid, $couch_revid, $new_patient_id, $couchresp->encounter, $couchresp->mimetype, json_encode($couchresp->data)); $resp = $couch->update_doc($data); //Sometimes the response from CouchDB is not available //still it would have saved in the DB. Hence check one more time if (!$resp->_id || !$resp->_rev) { $data = array($db, $couch_docid, $new_patient_id, $couchresp->encounter); $resp = $couch->retrieve_doc($data); } if ($resp->_rev == $couch_revid) { $couchsavefailed = true; } else { $d->set_couch_revid($resp->_rev); } } //set the new patient in mysql $d->set_foreign_id($new_patient_id); $d->persist(); $this->_state = false; if (!$couchsavefailed) { $messages .= xl('Document moved to patient id', '', '', ' \'') . $new_patient_id . xl('successfully.', '', '\' ') . "\n"; } else { $messages .= xl('Document moved to patient id', '', '', ' \'') . $new_patient_id . xl('Failed.', '', '\' ') . "\n"; } $this->assign("messages", $messages); return $this->list_action($patient_id); } } elseif (strtolower($new_patient_id) == "q" && is_numeric($document_id)) { $d = new Document($document_id); $new_path = $this->_config['repository']; $fname = $d->get_url_file(); //see if there is an existing file with the same name and rename as necessary if (file_exists($new_path . $d->get_url_file())) { $messages .= "File with same name already exists in the queue.\n"; $fname = basename($this->_rename_file($new_path . $d->get_url_file())); $messages .= "Current file name was changed to " . $fname . "\n"; } //now move the file if (rename($d->get_url_filepath(), $new_path . $fname)) { $d->url = "file://" . $new_path . $fname; $d->set_foreign_id(""); $d->persist(); $d->persist(); $d->populate(); $sql = "DELETE FROM categories_to_documents where document_id =" . $d->_db->qstr($document_id); $d->_db->Execute($sql); $messages .= "Document returned to queue successfully.\n"; } else { $messages .= "The file could not be succesfully stored, this error is usually related to permissions problems on the storage system.\n"; } $this->_state = false; $this->assign("messages", $messages); return $this->list_action($patient_id); } $this->_state = false; $this->assign("messages", $messages); return $this->view_action($patient_id, $document_id); }