Bootstrapped admin.php for multisite installation (#2155)
[openemr.git] / interface / eRxPage.php
blob625bc7915e8317ba83a18283aac85894f1a6e719
1 <?php
2 /**
3 * interface/eRxPage.php Functions for redirecting to NewCrop pages.
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Sam Likins <sam.likins@wsi-services.com>
8 * @copyright Copyright (c) 2015 Sam Likins <sam.likins@wsi-services.com>
9 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 class eRxPage
16 const DEBUG_XML = 1;
17 const DEBUG_RESULT = 2;
19 private $xmlBuilder;
20 private $authUserId;
21 private $destination;
22 private $patientId;
23 private $prescriptionIds;
24 private $prescriptionCount;
26 public function __construct($xmlBuilder = null)
28 if ($xmlBuilder) {
29 $this->setXMLBuilder($xmlBuilder);
33 /**
34 * Set XMLBuilder to handle eRx XML
35 * @param object $xmlBuilder The eRx XMLBuilder object to use for processing
36 * @return eRxPage This object is returned for method chaining
38 public function setXMLBuilder($xmlBuilder)
40 $this->xmlBuilder = $xmlBuilder;
42 return $this;
45 /**
46 * Get XMLBuilder for handling eRx XML
47 * @return object The eRx XMLBuilder object to use for processing
49 public function getXMLBuilder()
51 return $this->xmlBuilder;
54 /**
55 * Set the Id of the authenticated user
56 * @param integer $userId The Id for the authenticated user
57 * @return eRxPage This object is returned for method chaining
59 public function setAuthUserId($userId)
61 $this->authUserId = $userId;
63 return $this;
66 /**
67 * Get the Id of the authenticated user
68 * @return integer The Id of the authenticated user
70 public function getAuthUserId()
72 return $this->authUserId;
75 /**
76 * Set the destination for the page request
77 * @param string $destination The destination for the page request
78 * @return eRxPage This object is returned for method chaining
80 public function setDestination($destination)
82 $this->destination = $destination;
84 return $this;
87 /**
88 * Get the destination for the page request
89 * @return string The destination for the page request
91 public function getDestination()
93 return $this->destination;
96 /**
97 * Set the Patient Id for the page request
98 * @param integer $patientId The Patient Id for the page request
99 * @return eRxPage This object is returned for method chaining
101 public function setPatientId($patientId)
103 $this->patientId = $patientId;
105 return $this;
109 * Get the Patient Id for the page request
110 * @return string The Patient Id for the page request
112 public function getPatientId()
114 return $this->patientId;
118 * Set the Prescription Ids to send with page request
119 * @param string $prescriptionIds The Prescription Ids for the page request
120 * @return eRxPage This object is returned for method chaining
122 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()
135 $this->prescriptionIds;
139 * Set the prescription count for the page request
140 * @param string $count The prescription count for the page request
141 * @return eRxPage This object is returned for method chaining
143 public function setPrescriptionCount($count)
145 $this->prescriptionCount = $count;
147 return $this;
151 * Get the prescription count for the page request
152 * @return string The prescription count for the page request
154 public function getPrescriptionCount()
156 return $this->prescriptionCount;
160 * Check for required PHP extensions, return array of messages for missing extensions
161 * @return array Array of messages for missing extensions
163 public function checkForMissingExtensions()
165 $extensions = array(
166 'XML',
167 'SOAP',
168 'cURL',
169 'OpenSSL',
172 $messages = array();
174 foreach ($extensions as $extension) {
175 if (!extension_loaded(strtolower($extension))) {
176 $messages[] = xl('Enable Extension').' '.$extension;
180 return $messages;
184 * Construct the XML document
185 * @return eRxPage This object is returned for method chaining
187 public function buildXML()
189 $XMLBuilder = $this->getXMLBuilder();
190 $NCScript = $XMLBuilder->getNCScript();
191 $Store = $XMLBuilder->getStore();
192 $authUserId = $this->getAuthUserId();
193 $destination = $this->getDestination();
194 $patientId = $this->getPatientId();
196 $NCScript->appendChild($XMLBuilder->getCredentials());
197 $NCScript->appendChild($XMLBuilder->getUserRole($authUserId));
198 $NCScript->appendChild($XMLBuilder->getDestination($authUserId, $destination));
199 $NCScript->appendChild($XMLBuilder->getAccount());
200 $XMLBuilder->appendChildren($NCScript, $XMLBuilder->getStaffElements($authUserId, $destination));
201 $XMLBuilder->appendChildren($NCScript, $XMLBuilder->getPatientElements($patientId, $this->getPrescriptionCount(), $this->getPrescriptionIds()));
203 return array(
204 'demographics' => $XMLBuilder->getDemographicsCheckMessages(),
205 'empty' => $XMLBuilder->getFieldEmptyMessages(),
206 'warning' => $XMLBuilder->getWarningMessages(),
211 * Return a string version of the constructed XML cleaned-up for NewCrop
212 * @return string NewCrop ready string of the constructed XML.
214 * XML has had double-quotes converted to single-quotes and \r and \t has been removed.
216 public function getXML()
218 return preg_replace(
219 '/\t/',
221 preg_replace(
222 '/&#xD;/',
224 preg_replace(
225 '/"/',
226 '\'',
227 $this->getXMLBuilder()->getDocument()->saveXML()
233 protected function errorLog($message)
235 $date = date('Y-m-d');
236 $path = $this->getXMLBuilder()->getGlobals()
237 ->getOpenEMRSiteDirectory().'/documents/erx_error';
239 if (!is_dir($path)) {
240 mkdir($path, 0777, true);
243 $fileHandler = fopen($path.'/erx_error'.'-'.$date.'.log', 'a');
245 fwrite($fileHandler, date('Y-m-d H:i:s').' ==========> '.$message.PHP_EOL);
247 fclose($fileHandler);
250 public function checkError($xml)
252 $XMLBuilder = $this->getXMLBuilder();
254 $result = $XMLBuilder->checkError($xml);
256 preg_match('/<textarea.*>(.*)Original XML:/is', $result, $errorMessage);
258 if (count($errorMessage) > 0) {
259 $errorMessages = explode('Error', $errorMessage[1]);
260 array_shift($errorMessages);
261 } else {
262 $errorMessages = array();
265 if (strpos($result, 'RxEntry.aspx')) {
266 $this->errorLog($xml);
267 $this->errorLog($result);
269 if (!count($errorMessages)) {
270 $errorMessages[] = xl('An undefined error occurred, please contact your systems administrator.');
272 } elseif ($XMLBuilder->getGlobals()->getDebugSetting() !== 0) {
273 $debugString = '( '.xl('DEBUG OUTPUT').' )'.PHP_EOL;
275 if ($XMLBuilder->getGlobals()->getDebugSetting() & self::DEBUG_XML) {
276 $this->errorLog($debugString.$xml);
279 if ($XMLBuilder->getGlobals()->getDebugSetting() & self::DEBUG_RESULT) {
280 $this->errorLog($debugString.$result);
284 return $errorMessages;
287 public function updatePatientData()
289 $XMLBuilder = $this->getXMLBuilder();
290 $Store = $XMLBuilder->getStore();
291 $page = $this->getDestination();
292 $patientId = $this->getPatientId();
294 if ($page == 'compose') {
295 $Store->updatePatientImportStatusByPatientId($patientId, 1);
296 } elseif ($page == 'medentry') {
297 $Store->updatePatientImportStatusByPatientId($patientId, 3);
300 $allergyIds = $XMLBuilder->getSentAllergyIds();
301 if (count($allergyIds)) {
302 foreach ($allergyIds as $allergyId) {
303 $Store->updateAllergyUploadedByPatientIdAllergyId(1, $patientId, $allergyId);
307 $prescriptionIds = $XMLBuilder->getSentPrescriptionIds();
308 if (count($prescriptionIds)) {
309 foreach ($prescriptionIds as $prescriptionId) {
310 $Store->updatePrescriptionsUploadActiveByPatientIdPrescriptionId(1, 0, $patientId, $prescriptionId);
314 $medicationIds = $XMLBuilder->getSentMedicationIds();
315 if (count($medicationIds)) {
316 foreach ($medicationIds as $medicationId) {
317 $Store->updateErxUploadedByListId($medicationId, 1);