Interim autoloaded library/classes via composer classmap, take 4. (#422)
[openemr.git] / interface / modules / zend_modules / module / Documents / src / Documents / Controller / DocumentsController.php
blob77b0ea2dca1f7416935b85acb9192ae92adb9ff4
1 <?php
2 /* +-----------------------------------------------------------------------------+
3 * OpenEMR - Open Source Electronic Medical Record
4 * Copyright (C) 2013 Z&H Consultancy Services Private Limited <sam@zhservices.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * @author Basil PT <basil@zhservices.com>
19 * @author Chandni Babu <chandnib@zhservices.com>
20 * @author Riju KP <rijukp@zhservices.com>
21 * +------------------------------------------------------------------------------+
24 namespace Documents\Controller;
26 use Zend\Mvc\Controller\AbstractActionController;
27 use Zend\View\Model\ViewModel;
28 use Zend\View\Model\JsonModel;
29 use Application\Listener\Listener;
30 use Document;
32 class DocumentsController extends AbstractActionController
34 protected $documentsTable;
35 protected $listenerObject;
37 public function __construct()
39 $this->listenerObject = new Listener;
42 public function getDocumentsTable()
44 if (!$this->documentsTable) {
45 $sm = $this->getServiceLocator();
46 $this ->documentsTable = $sm->get('Documents\Model\DocumentsTable');
48 return $this->documentsTable;
52 * Upload document
54 public function uploadAction() {
55 $request = $this->getRequest();
56 if($request->isPost()) {
57 $error = false;
58 $files = array();
59 $uploaddir = $GLOBALS['OE_SITE_DIR'].'/documents/'.$request->getPost('file_location');
60 $pid = $request->getPost('patient_id');
61 $encounter = $request->getPost('encounter_id');
62 $batch_upload = $request->getPost('batch_upload');
63 $category_id = $request->getPost('document_category');
64 $encrypted_file = $request->getPost('encrypted_file');
65 $encryption_key = $request->getPost('encryption_key');
66 $storage_method = $GLOBALS['document_storage_method'];
67 $documents = array();
68 $i = 0;
69 foreach($_FILES as $file){
70 $i++;
71 $dateStamp = date('Y-m-d-H-i-s');
72 $file_name = $dateStamp."_".basename($file["name"]);
73 $file["name"] = $file_name;
75 $documents[$i] = array(
76 'name' => $file_name,
77 'type' => $file['type'],
78 'batch_upload'=> $batch_upload,
79 'storage' => $storage_method,
80 'category_id' => $category_id,
81 'pid' => $pid,
84 // Read File Contents
85 $tmpfile = fopen($file['tmp_name'], "r");
86 $filetext = fread($tmpfile,$file['size']);
88 // Decrypt Encryped Files
89 if($encrypted_file == '1') {
90 $plaintext = \Documents\Plugin\Documents::decrypt($filetext,$encryption_key);
91 fclose($tmpfile);
92 unlink($file['tmp_name']);
94 // Write new file contents
95 $tmpfile = fopen($file['tmp_name'],"w+");
96 fwrite($tmpfile,$plaintext);
97 fclose($tmpfile);
98 $file['size'] = filesize($file['tmp_name']);
101 $ob = new \Document();
102 $ret = $ob->createDocument($pid, $category_id, $file_name, $file['type'], $filetext,'', 1, 0);
108 * Retrieve document
110 public function retrieveAction() {
112 // List of Preview Available File types
113 $previewAvailableFiles = array(
114 'application/pdf',
115 'image/jpeg',
116 'image/png',
117 'image/gif',
118 'text/plain',
119 'text/html',
120 'text/xml',
123 $request = $this->getRequest();
124 $documentId = $this->params()->fromRoute('id');
125 $doEncryption = ($this->params()->fromRoute('doencryption') == '1') ? true : false;
126 $encryptionKey = $this->params()->fromRoute('key');
127 $type = ($this->params()->fromRoute('download') == '1') ? "attachment" : "inline";
129 $result = $this->getDocumentsTable()->getDocument($documentId);
130 $skip_headers = false;
131 $contentType = $result['mimetype'];
133 $document = \Documents\Plugin\Documents::getDocument($documentId,$doEncryption,$encryptionKey);
134 $categoryIds = $this->getDocumentsTable()->getCategoryIDs(array('CCD','CCR','CCDA'));
135 if(in_array($result['category_id'],$categoryIds) && $contentType == 'text/xml' && !$doEncryption) {
136 $xml = simplexml_load_string($document);
137 $xsl = new \DomDocument;
139 switch($result['category_id']){
140 case $categoryIds['CCD']:
141 $style = "ccd.xsl";
142 break;
143 case $categoryIds['CCR']:
144 $style = "ccr.xsl";
145 break;
146 case $categoryIds['CCDA']:
147 $style = "ccda.xsl";
148 break;
151 $xsl->load(__DIR__.'/../../../../../public/xsl/'.$style);
152 $proc = new \XSLTProcessor;
153 $proc->importStyleSheet($xsl);
154 $document = $proc->transformToXML($xml);
157 if($type=="inline" && !$doEncryption) {
158 if(in_array($result['mimetype'],$previewAvailableFiles)){
159 if(in_array($result['category_id'],$categoryIds) && $contentType == 'text/xml') {
160 $contentType = 'text/html';
162 } else {
163 $skip_headers = true;
165 } else {
166 if($doEncryption) {
167 $contentType = "application/octet-stream";
168 } else {
169 $contentType = $result['mimetype'];
173 if(!$skip_headers) {
174 $response = $this->getResponse();
175 $response->setContent($document);
176 $headers = $response->getHeaders();
177 $headers->clearHeaders()
178 ->addHeaderLine('Content-Type',$contentType)
179 ->addHeaderLine('Content-Disposition', $type . '; filename="' . $result['name'] . '"')
180 ->addHeaderLine('Content-Length', strlen($document));
181 $response->setHeaders($headers);
182 return $this->response;