prepping ubuntu packages
[openemr.git] / interface / eRxPage.php
blobc4f96b98b334fbf20199ee447fa15e366c0038b4
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 {
26 const DEBUG_XML = 1;
27 const DEBUG_RESULT = 2;
29 private $xmlBuilder;
30 private $authUserId;
31 private $destination;
32 private $patientId;
33 private $prescriptionIds;
34 private $prescriptionCount;
36 public function __construct($xmlBuilder = null) {
37 if($xmlBuilder) {
38 $this->setXMLBuilder($xmlBuilder);
42 /**
43 * Set XMLBuilder to handle eRx XML
44 * @param object $xmlBuilder The eRx XMLBuilder object to use for processing
45 * @return eRxPage This object is returned for method chaining
47 public function setXMLBuilder($xmlBuilder) {
48 $this->xmlBuilder = $xmlBuilder;
50 return $this;
53 /**
54 * Get XMLBuilder for handling eRx XML
55 * @return object The eRx XMLBuilder object to use for processing
57 public function getXMLBuilder() {
58 return $this->xmlBuilder;
61 /**
62 * Set the Id of the authenticated user
63 * @param integer $userId The Id for the authenticated user
64 * @return eRxPage This object is returned for method chaining
66 public function setAuthUserId($userId) {
67 $this->authUserId = $userId;
69 return $this;
72 /**
73 * Get the Id of the authenticated user
74 * @return integer The Id of the authenticated user
76 public function getAuthUserId() {
77 return $this->authUserId;
80 /**
81 * Set the destination for the page request
82 * @param string $destination The destination for the page request
83 * @return eRxPage This object is returned for method chaining
85 public function setDestination($destination){
86 $this->destination = $destination;
88 return $this;
91 /**
92 * Get the destination for the page request
93 * @return string The destination for the page request
95 public function getDestination(){
96 return $this->destination;
99 /**
100 * Set the Patient Id for the page request
101 * @param integer $patientId The Patient Id for the page request
102 * @return eRxPage This object is returned for method chaining
104 public function setPatientId($patientId){
105 $this->patientId = $patientId;
107 return $this;
111 * Get the Patient Id for the page request
112 * @return string The Patient Id for the page request
114 public function getPatientId(){
115 return $this->patientId;
119 * Set the Prescription Ids to send with page request
120 * @param string $prescriptionIds The Prescription Ids for the page request
121 * @return eRxPage This object is returned for method chaining
123 public function setPrescriptionIds($prescriptionIds){
124 $this->prescriptions = explode(':', $prescriptionIds);
126 return $this;
130 * Get the Prescription Ids for the page request
131 * @return string The Prescription Ids for the page request
133 public function getPrescriptionIds(){
134 $this->prescriptionIds;
138 * Set the prescription count for the page request
139 * @param string $count The prescription count for the page request
140 * @return eRxPage This object is returned for method chaining
142 public function setPrescriptionCount($count){
143 $this->prescriptionCount = $count;
145 return $this;
149 * Get the prescription count for the page request
150 * @return string The prescription count for the page request
152 public function getPrescriptionCount(){
153 return $this->prescriptionCount;
157 * Check for required PHP extensions, return array of messages for missing extensions
158 * @return array Array of messages for missing extensions
160 public function checkForMissingExtensions() {
161 $extensions = array(
162 'XML',
163 'SOAP',
164 'cURL',
165 'OpenSSL',
168 $messages = array();
170 foreach ($extensions as $extension) {
171 if(!extension_loaded(strtolower($extension))) {
172 $messages[] = xl('Enable Extension').' '.$extension;
176 return $messages;
180 * Construct the XML document
181 * @return eRxPage This object is returned for method chaining
183 public function buildXML() {
184 $XMLBuilder = $this->getXMLBuilder();
185 $NCScript = $XMLBuilder->getNCScript();
186 $Store = $XMLBuilder->getStore();
187 $authUserId = $this->getAuthUserId();
188 $destination = $this->getDestination();
189 $patientId = $this->getPatientId();
191 $NCScript->appendChild($XMLBuilder->getCredentials());
192 $NCScript->appendChild($XMLBuilder->getUserRole($authUserId));
193 $NCScript->appendChild($XMLBuilder->getDestination($authUserId, $destination));
194 $NCScript->appendChild($XMLBuilder->getAccount());
195 $XMLBuilder->appendChildren($NCScript, $XMLBuilder->getStaffElements($authUserId, $destination));
196 $XMLBuilder->appendChildren($NCScript, $XMLBuilder->getPatientElements($patientId, $this->getPrescriptionCount(), $this->getPrescriptionIds()));
198 return array(
199 'demographics' => $XMLBuilder->getDemographicsCheckMessages(),
200 'empty' => $XMLBuilder->getFieldEmptyMessages(),
201 'warning' => $XMLBuilder->getWarningMessages(),
206 * Return a string version of the constructed XML cleaned-up for NewCrop
207 * @return string NewCrop ready string of the constructed XML.
209 * XML has had double-quotes converted to single-quotes and \r and \t has been removed.
211 public function getXML() {
212 return preg_replace(
213 '/\t/',
215 preg_replace(
216 '/&#xD;/',
218 preg_replace(
219 '/"/',
220 '\'',
221 $this->getXMLBuilder()->getDocument()->saveXML()
227 protected function errorLog($message) {
228 $date = date('Y-m-d');
229 $path = $this->getXMLBuilder()->getGlobals()
230 ->getOpenEMRSiteDirectory().'/documents/erx_error';
232 if(!is_dir($path)) {
233 mkdir($path, 0777, true);
236 $fileHandler = fopen($path.'/erx_error'.'-'.$date.'.log', 'a');
238 fwrite($fileHandler, date('Y-m-d H:i:s').' ==========> '.$message.PHP_EOL);
240 fclose($fileHandler);
243 public function checkError($xml) {
244 $XMLBuilder = $this->getXMLBuilder();
246 $result = $XMLBuilder->checkError($xml);
248 preg_match('/<textarea.*>(.*)Original XML:/is', $result, $errorMessage);
250 if(count($errorMessage) > 0) {
251 $errorMessages = explode('Error', $errorMessage[1]);
252 array_shift($errorMessages);
253 } else {
254 $errorMessages = array();
257 if(strpos($result, 'RxEntry.aspx')) {
258 $this->errorLog($xml);
259 $this->errorLog($result);
261 if(!count($errorMessages)) {
262 $errorMessages[] = xl('An undefined error occurred, please contact your systems administrator.');
264 } elseif($XMLBuilder->getGlobals()->getDebugSetting() !== 0) {
265 $debugString = '( '.xl('DEBUG OUTPUT').' )'.PHP_EOL;
267 if($XMLBuilder->getGlobals()->getDebugSetting() & self::DEBUG_XML) {
268 $this->errorLog($debugString.$xml);
271 if($XMLBuilder->getGlobals()->getDebugSetting() & self::DEBUG_RESULT) {
272 $this->errorLog($debugString.$result);
276 return $errorMessages;
279 public function updatePatientData() {
280 $XMLBuilder = $this->getXMLBuilder();
281 $Store = $XMLBuilder->getStore();
282 $page = $this->getDestination();
283 $patientId = $this->getPatientId();
285 if($page == 'compose') {
286 $Store->updatePatientImportStatusByPatientId($patientId, 1);
287 } elseif($page == 'medentry') {
288 $Store->updatePatientImportStatusByPatientId($patientId, 3);
291 $allergyIds = $XMLBuilder->getSentAllergyIds();
292 if(count($allergyIds)) {
293 foreach($allergyIds as $allergyId) {
294 $Store->updateAllergyUploadedByPatientIdAllergyId(1, $patientId, $allergyId);
298 $prescriptionIds = $XMLBuilder->getSentPrescriptionIds();
299 if(count($prescriptionIds)) {
300 foreach($prescriptionIds as $prescriptionId) {
301 $Store->updatePrescriptionsUploadActiveByPatientIdPrescriptionId(1, 0, $patientId, $prescriptionId);
305 $medicationIds = $XMLBuilder->getSentMedicationIds();
306 if(count($medicationIds)) {
307 foreach($medicationIds as $medicationId) {
308 $Store->updateErxUploadedByListId($medicationId, 1);