Highway to PSR2
[openemr.git] / interface / modules / zend_modules / module / Documents / src / Documents / Controller / DocumentsController.php
blobad4a184cece6b53bf1d87758a48bdf1a5ea8a4b0
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');
49 return $this->documentsTable;
53 * Upload document
55 public function uploadAction()
57 $request = $this->getRequest();
58 if ($request->isPost()) {
59 $error = false;
60 $files = array();
61 $uploaddir = $GLOBALS['OE_SITE_DIR'].'/documents/'.$request->getPost('file_location');
62 $pid = $request->getPost('patient_id');
63 $encounter = $request->getPost('encounter_id');
64 $batch_upload = $request->getPost('batch_upload');
65 $category_id = $request->getPost('document_category');
66 $encrypted_file = $request->getPost('encrypted_file');
67 $encryption_key = $request->getPost('encryption_key');
68 $storage_method = $GLOBALS['document_storage_method'];
69 $documents = array();
70 $i = 0;
71 foreach ($_FILES as $file) {
72 $i++;
73 $dateStamp = date('Y-m-d-H-i-s');
74 $file_name = $dateStamp."_".basename($file["name"]);
75 $file["name"] = $file_name;
77 $documents[$i] = array(
78 'name' => $file_name,
79 'type' => $file['type'],
80 'batch_upload'=> $batch_upload,
81 'storage' => $storage_method,
82 'category_id' => $category_id,
83 'pid' => $pid,
86 // Read File Contents
87 $tmpfile = fopen($file['tmp_name'], "r");
88 $filetext = fread($tmpfile, $file['size']);
90 // Decrypt Encryped Files
91 if ($encrypted_file == '1') {
92 $plaintext = \Documents\Plugin\Documents::decrypt($filetext, $encryption_key);
93 fclose($tmpfile);
94 unlink($file['tmp_name']);
96 // Write new file contents
97 $tmpfile = fopen($file['tmp_name'], "w+");
98 fwrite($tmpfile, $plaintext);
99 fclose($tmpfile);
100 $file['size'] = filesize($file['tmp_name']);
103 $ob = new \Document();
104 $ret = $ob->createDocument($pid, $category_id, $file_name, $file['type'], $filetext, '', 1, 0);
110 * Retrieve document
112 public function retrieveAction()
115 // List of Preview Available File types
116 $previewAvailableFiles = array(
117 'application/pdf',
118 'image/jpeg',
119 'image/png',
120 'image/gif',
121 'text/plain',
122 'text/html',
123 'text/xml',
126 $request = $this->getRequest();
127 $documentId = $this->params()->fromRoute('id');
128 $doEncryption = ($this->params()->fromRoute('doencryption') == '1') ? true : false;
129 $encryptionKey = $this->params()->fromRoute('key');
130 $type = ($this->params()->fromRoute('download') == '1') ? "attachment" : "inline";
132 $result = $this->getDocumentsTable()->getDocument($documentId);
133 $skip_headers = false;
134 $contentType = $result['mimetype'];
136 $document = \Documents\Plugin\Documents::getDocument($documentId, $doEncryption, $encryptionKey);
137 $categoryIds = $this->getDocumentsTable()->getCategoryIDs(array('CCD','CCR','CCDA'));
138 if (in_array($result['category_id'], $categoryIds) && $contentType == 'text/xml' && !$doEncryption) {
139 $xml = simplexml_load_string($document);
140 $xsl = new \DomDocument;
142 switch ($result['category_id']) {
143 case $categoryIds['CCD']:
144 $style = "ccd.xsl";
145 break;
146 case $categoryIds['CCR']:
147 $style = "ccr.xsl";
148 break;
149 case $categoryIds['CCDA']:
150 $style = "ccda.xsl";
151 break;
154 $xsl->load(__DIR__.'/../../../../../public/xsl/'.$style);
155 $proc = new \XSLTProcessor;
156 $proc->importStyleSheet($xsl);
157 $document = $proc->transformToXML($xml);
160 if ($type=="inline" && !$doEncryption) {
161 if (in_array($result['mimetype'], $previewAvailableFiles)) {
162 if (in_array($result['category_id'], $categoryIds) && $contentType == 'text/xml') {
163 $contentType = 'text/html';
165 } else {
166 $skip_headers = true;
168 } else {
169 if ($doEncryption) {
170 $contentType = "application/octet-stream";
171 } else {
172 $contentType = $result['mimetype'];
176 if (!$skip_headers) {
177 $response = $this->getResponse();
178 $response->setContent($document);
179 $headers = $response->getHeaders();
180 $headers->clearHeaders()
181 ->addHeaderLine('Content-Type', $contentType)
182 ->addHeaderLine('Content-Disposition', $type . '; filename="' . $result['name'] . '"')
183 ->addHeaderLine('Content-Length', strlen($document));
184 $response->setHeaders($headers);
185 return $this->response;