CCDA server fixups (#4356)
[openemr.git] / interface / eRxPage.php
bloba492cc26839d24c244d4eb8345e68f94b7f8aefd
1 <?php
3 /**
4 * interface/eRxPage.php Functions for redirecting to NewCrop pages.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Sam Likins <sam.likins@wsi-services.com>
9 * @copyright Copyright (c) 2015 Sam Likins <sam.likins@wsi-services.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 class eRxPage
17 const DEBUG_XML = 1;
18 const DEBUG_RESULT = 2;
20 private $xmlBuilder;
21 private $authUserId;
22 private $destination;
23 private $patientId;
24 private $prescriptionIds;
25 private $prescriptionCount;
27 public function __construct($xmlBuilder = null)
29 if ($xmlBuilder) {
30 $this->setXMLBuilder($xmlBuilder);
34 /**
35 * Set XMLBuilder to handle eRx XML
36 * @param object $xmlBuilder The eRx XMLBuilder object to use for processing
37 * @return eRxPage This object is returned for method chaining
39 public function setXMLBuilder($xmlBuilder)
41 $this->xmlBuilder = $xmlBuilder;
43 return $this;
46 /**
47 * Get XMLBuilder for handling eRx XML
48 * @return object The eRx XMLBuilder object to use for processing
50 public function getXMLBuilder()
52 return $this->xmlBuilder;
55 /**
56 * Set the Id of the authenticated user
57 * @param integer $userId The Id for the authenticated user
58 * @return eRxPage This object is returned for method chaining
60 public function setAuthUserId($userId)
62 $this->authUserId = $userId;
64 return $this;
67 /**
68 * Get the Id of the authenticated user
69 * @return integer The Id of the authenticated user
71 public function getAuthUserId()
73 return $this->authUserId;
76 /**
77 * Set the destination for the page request
78 * @param string $destination The destination for the page request
79 * @return eRxPage This object is returned for method chaining
81 public function setDestination($destination)
83 $this->destination = $destination;
85 return $this;
88 /**
89 * Get the destination for the page request
90 * @return string The destination for the page request
92 public function getDestination()
94 return $this->destination;
97 /**
98 * Set the Patient Id for the page request
99 * @param integer $patientId The Patient Id for the page request
100 * @return eRxPage This object is returned for method chaining
102 public function setPatientId($patientId)
104 $this->patientId = $patientId;
106 return $this;
110 * Get the Patient Id for the page request
111 * @return string The Patient Id for the page request
113 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)
125 $this->prescriptions = explode(':', $prescriptionIds);
127 return $this;
131 * Get the Prescription Ids for the page request
132 * @return string The Prescription Ids for the page request
134 public function getPrescriptionIds()
136 $this->prescriptionIds;
140 * Set the prescription count for the page request
141 * @param string $count The prescription count for the page request
142 * @return eRxPage This object is returned for method chaining
144 public function setPrescriptionCount($count)
146 $this->prescriptionCount = $count;
148 return $this;
152 * Get the prescription count for the page request
153 * @return string The prescription count for the page request
155 public function getPrescriptionCount()
157 return $this->prescriptionCount;
161 * Check for required PHP extensions, return array of messages for missing extensions
162 * @return array Array of messages for missing extensions
164 public function checkForMissingExtensions()
166 $extensions = array(
167 'XML',
168 'SOAP',
169 'cURL',
170 'OpenSSL',
173 $messages = array();
175 foreach ($extensions as $extension) {
176 if (!extension_loaded(strtolower($extension))) {
177 $messages[] = xl('Enable Extension') . ' ' . $extension;
181 return $messages;
185 * Construct the XML document
186 * @return eRxPage This object is returned for method chaining
188 public function buildXML()
190 $XMLBuilder = $this->getXMLBuilder();
191 $NCScript = $XMLBuilder->getNCScript();
192 $Store = $XMLBuilder->getStore();
193 $authUserId = $this->getAuthUserId();
194 $destination = $this->getDestination();
195 $patientId = $this->getPatientId();
197 $NCScript->appendChild($XMLBuilder->getCredentials());
198 $NCScript->appendChild($XMLBuilder->getUserRole($authUserId));
199 $NCScript->appendChild($XMLBuilder->getDestination($authUserId, $destination));
200 $NCScript->appendChild($XMLBuilder->getAccount());
201 $XMLBuilder->appendChildren($NCScript, $XMLBuilder->getStaffElements($authUserId, $destination));
202 $XMLBuilder->appendChildren($NCScript, $XMLBuilder->getPatientElements($patientId, $this->getPrescriptionCount(), $this->getPrescriptionIds()));
204 return array(
205 'demographics' => $XMLBuilder->getDemographicsCheckMessages(),
206 'empty' => $XMLBuilder->getFieldEmptyMessages(),
207 'warning' => $XMLBuilder->getWarningMessages(),
212 * Return a string version of the constructed XML cleaned-up for NewCrop
213 * @return string NewCrop ready string of the constructed XML.
215 * XML has had double-quotes converted to single-quotes and \r and \t has been removed.
217 public function getXML()
219 return preg_replace(
220 '/\t/',
222 preg_replace(
223 '/&#xD;/',
225 preg_replace(
226 '/"/',
227 '\'',
228 $this->getXMLBuilder()->getDocument()->saveXML()
234 protected function errorLog($message)
236 $date = date('Y-m-d');
237 $path = $this->getXMLBuilder()->getGlobals()
238 ->getOpenEMRSiteDirectory() . '/documents/erx_error';
240 if (!is_dir($path)) {
241 mkdir($path, 0777, true);
244 $fileHandler = fopen($path . '/erx_error' . '-' . $date . '.log', 'a');
246 fwrite($fileHandler, date('Y-m-d H:i:s') . ' ==========> ' . $message . PHP_EOL);
248 fclose($fileHandler);
251 public function checkError($xml)
253 $XMLBuilder = $this->getXMLBuilder();
255 $result = $XMLBuilder->checkError($xml);
257 preg_match('/<textarea.*>(.*)Original XML:/is', $result, $errorMessage);
259 if (count($errorMessage) > 0) {
260 $errorMessages = explode('Error', $errorMessage[1]);
261 array_shift($errorMessages);
262 } else {
263 $errorMessages = array();
266 if (strpos($result, 'RxEntry.aspx')) {
267 $this->errorLog($xml);
268 $this->errorLog($result);
270 if (!count($errorMessages)) {
271 $errorMessages[] = xl('An undefined error occurred, please contact your systems administrator.');
273 } elseif ($XMLBuilder->getGlobals()->getDebugSetting() !== 0) {
274 $debugString = '( ' . xl('DEBUG OUTPUT') . ' )' . PHP_EOL;
276 if ($XMLBuilder->getGlobals()->getDebugSetting() & self::DEBUG_XML) {
277 $this->errorLog($debugString . $xml);
280 if ($XMLBuilder->getGlobals()->getDebugSetting() & self::DEBUG_RESULT) {
281 $this->errorLog($debugString . $result);
285 return $errorMessages;
288 public function updatePatientData()
290 $XMLBuilder = $this->getXMLBuilder();
291 $Store = $XMLBuilder->getStore();
292 $page = $this->getDestination();
293 $patientId = $this->getPatientId();
295 if ($page == 'compose') {
296 $Store->updatePatientImportStatusByPatientId($patientId, 1);
297 } elseif ($page == 'medentry') {
298 $Store->updatePatientImportStatusByPatientId($patientId, 3);
301 $allergyIds = $XMLBuilder->getSentAllergyIds();
302 if (count($allergyIds)) {
303 foreach ($allergyIds as $allergyId) {
304 $Store->updateAllergyUploadedByPatientIdAllergyId(1, $patientId, $allergyId);
308 $prescriptionIds = $XMLBuilder->getSentPrescriptionIds();
309 if (count($prescriptionIds)) {
310 foreach ($prescriptionIds as $prescriptionId) {
311 $Store->updatePrescriptionsUploadActiveByPatientIdPrescriptionId(1, 0, $patientId, $prescriptionId);
315 $medicationIds = $XMLBuilder->getSentMedicationIds();
316 if (count($medicationIds)) {
317 foreach ($medicationIds as $medicationId) {
318 $Store->updateErxUploadedByListId($medicationId, 1);