menu fix for patient menu (#1582)
[openemr.git] / interface / eRxPage.php
blobfb43dc6b82ee59644bd1fd914a9378d05a385d1a
1 <?php
3 /**
4 * interface/eRxPage.php Functions for redirecting to NewCrop pages.
6 * Copyright (C) 2015 Sam Likins <sam.likins@wsi-services.com>
8 * LICENSE: This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
11 * later version. This program is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 * Public License for more details. You should have received a copy of the GNU
15 * General Public License along with this program.
16 * If not, see <http://opensource.org/licenses/gpl-license.php>.
18 * @package OpenEMR
19 * @subpackage NewCrop
20 * @author Sam Likins <sam.likins@wsi-services.com>
21 * @link http://www.open-emr.org
24 class eRxPage
27 const DEBUG_XML = 1;
28 const DEBUG_RESULT = 2;
30 private $xmlBuilder;
31 private $authUserId;
32 private $destination;
33 private $patientId;
34 private $prescriptionIds;
35 private $prescriptionCount;
37 public function __construct($xmlBuilder = null)
39 if ($xmlBuilder) {
40 $this->setXMLBuilder($xmlBuilder);
44 /**
45 * Set XMLBuilder to handle eRx XML
46 * @param object $xmlBuilder The eRx XMLBuilder object to use for processing
47 * @return eRxPage This object is returned for method chaining
49 public function setXMLBuilder($xmlBuilder)
51 $this->xmlBuilder = $xmlBuilder;
53 return $this;
56 /**
57 * Get XMLBuilder for handling eRx XML
58 * @return object The eRx XMLBuilder object to use for processing
60 public function getXMLBuilder()
62 return $this->xmlBuilder;
65 /**
66 * Set the Id of the authenticated user
67 * @param integer $userId The Id for the authenticated user
68 * @return eRxPage This object is returned for method chaining
70 public function setAuthUserId($userId)
72 $this->authUserId = $userId;
74 return $this;
77 /**
78 * Get the Id of the authenticated user
79 * @return integer The Id of the authenticated user
81 public function getAuthUserId()
83 return $this->authUserId;
86 /**
87 * Set the destination for the page request
88 * @param string $destination The destination for the page request
89 * @return eRxPage This object is returned for method chaining
91 public function setDestination($destination)
93 $this->destination = $destination;
95 return $this;
98 /**
99 * Get the destination for the page request
100 * @return string The destination for the page request
102 public function getDestination()
104 return $this->destination;
108 * Set the Patient Id for the page request
109 * @param integer $patientId The Patient Id for the page request
110 * @return eRxPage This object is returned for method chaining
112 public function setPatientId($patientId)
114 $this->patientId = $patientId;
116 return $this;
120 * Get the Patient Id for the page request
121 * @return string The Patient Id for the page request
123 public function getPatientId()
125 return $this->patientId;
129 * Set the Prescription Ids to send with page request
130 * @param string $prescriptionIds The Prescription Ids for the page request
131 * @return eRxPage This object is returned for method chaining
133 public function setPrescriptionIds($prescriptionIds)
135 $this->prescriptions = explode(':', $prescriptionIds);
137 return $this;
141 * Get the Prescription Ids for the page request
142 * @return string The Prescription Ids for the page request
144 public function getPrescriptionIds()
146 $this->prescriptionIds;
150 * Set the prescription count for the page request
151 * @param string $count The prescription count for the page request
152 * @return eRxPage This object is returned for method chaining
154 public function setPrescriptionCount($count)
156 $this->prescriptionCount = $count;
158 return $this;
162 * Get the prescription count for the page request
163 * @return string The prescription count for the page request
165 public function getPrescriptionCount()
167 return $this->prescriptionCount;
171 * Check for required PHP extensions, return array of messages for missing extensions
172 * @return array Array of messages for missing extensions
174 public function checkForMissingExtensions()
176 $extensions = array(
177 'XML',
178 'SOAP',
179 'cURL',
180 'OpenSSL',
183 $messages = array();
185 foreach ($extensions as $extension) {
186 if (!extension_loaded(strtolower($extension))) {
187 $messages[] = xl('Enable Extension').' '.$extension;
191 return $messages;
195 * Construct the XML document
196 * @return eRxPage This object is returned for method chaining
198 public function buildXML()
200 $XMLBuilder = $this->getXMLBuilder();
201 $NCScript = $XMLBuilder->getNCScript();
202 $Store = $XMLBuilder->getStore();
203 $authUserId = $this->getAuthUserId();
204 $destination = $this->getDestination();
205 $patientId = $this->getPatientId();
207 $NCScript->appendChild($XMLBuilder->getCredentials());
208 $NCScript->appendChild($XMLBuilder->getUserRole($authUserId));
209 $NCScript->appendChild($XMLBuilder->getDestination($authUserId, $destination));
210 $NCScript->appendChild($XMLBuilder->getAccount());
211 $XMLBuilder->appendChildren($NCScript, $XMLBuilder->getStaffElements($authUserId, $destination));
212 $XMLBuilder->appendChildren($NCScript, $XMLBuilder->getPatientElements($patientId, $this->getPrescriptionCount(), $this->getPrescriptionIds()));
214 return array(
215 'demographics' => $XMLBuilder->getDemographicsCheckMessages(),
216 'empty' => $XMLBuilder->getFieldEmptyMessages(),
217 'warning' => $XMLBuilder->getWarningMessages(),
222 * Return a string version of the constructed XML cleaned-up for NewCrop
223 * @return string NewCrop ready string of the constructed XML.
225 * XML has had double-quotes converted to single-quotes and \r and \t has been removed.
227 public function getXML()
229 return preg_replace(
230 '/\t/',
232 preg_replace(
233 '/&#xD;/',
235 preg_replace(
236 '/"/',
237 '\'',
238 $this->getXMLBuilder()->getDocument()->saveXML()
244 protected function errorLog($message)
246 $date = date('Y-m-d');
247 $path = $this->getXMLBuilder()->getGlobals()
248 ->getOpenEMRSiteDirectory().'/documents/erx_error';
250 if (!is_dir($path)) {
251 mkdir($path, 0777, true);
254 $fileHandler = fopen($path.'/erx_error'.'-'.$date.'.log', 'a');
256 fwrite($fileHandler, date('Y-m-d H:i:s').' ==========> '.$message.PHP_EOL);
258 fclose($fileHandler);
261 public function checkError($xml)
263 $XMLBuilder = $this->getXMLBuilder();
265 $result = $XMLBuilder->checkError($xml);
267 preg_match('/<textarea.*>(.*)Original XML:/is', $result, $errorMessage);
269 if (count($errorMessage) > 0) {
270 $errorMessages = explode('Error', $errorMessage[1]);
271 array_shift($errorMessages);
272 } else {
273 $errorMessages = array();
276 if (strpos($result, 'RxEntry.aspx')) {
277 $this->errorLog($xml);
278 $this->errorLog($result);
280 if (!count($errorMessages)) {
281 $errorMessages[] = xl('An undefined error occurred, please contact your systems administrator.');
283 } elseif ($XMLBuilder->getGlobals()->getDebugSetting() !== 0) {
284 $debugString = '( '.xl('DEBUG OUTPUT').' )'.PHP_EOL;
286 if ($XMLBuilder->getGlobals()->getDebugSetting() & self::DEBUG_XML) {
287 $this->errorLog($debugString.$xml);
290 if ($XMLBuilder->getGlobals()->getDebugSetting() & self::DEBUG_RESULT) {
291 $this->errorLog($debugString.$result);
295 return $errorMessages;
298 public function updatePatientData()
300 $XMLBuilder = $this->getXMLBuilder();
301 $Store = $XMLBuilder->getStore();
302 $page = $this->getDestination();
303 $patientId = $this->getPatientId();
305 if ($page == 'compose') {
306 $Store->updatePatientImportStatusByPatientId($patientId, 1);
307 } elseif ($page == 'medentry') {
308 $Store->updatePatientImportStatusByPatientId($patientId, 3);
311 $allergyIds = $XMLBuilder->getSentAllergyIds();
312 if (count($allergyIds)) {
313 foreach ($allergyIds as $allergyId) {
314 $Store->updateAllergyUploadedByPatientIdAllergyId(1, $patientId, $allergyId);
318 $prescriptionIds = $XMLBuilder->getSentPrescriptionIds();
319 if (count($prescriptionIds)) {
320 foreach ($prescriptionIds as $prescriptionId) {
321 $Store->updatePrescriptionsUploadActiveByPatientIdPrescriptionId(1, 0, $patientId, $prescriptionId);
325 $medicationIds = $XMLBuilder->getSentMedicationIds();
326 if (count($medicationIds)) {
327 foreach ($medicationIds as $medicationId) {
328 $Store->updateErxUploadedByListId($medicationId, 1);