dump db version
[openemr.git] / services / FhirResourcesService.php
blobd655df2c32b9bf541ad80644a5656652c8e63359
1 <?php
2 /**
3 * FHIRResources service class
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Jerry Padgett <sjpadgett@gmail.com>
8 * @copyright Copyright (c) 2018 Jerry Padgett <sjpadgett@gmail.com>
9 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 namespace OpenEMR\Services;
15 use HL7\FHIR\STU3\FHIRDomainResource\FHIREncounter;
16 use HL7\FHIR\STU3\FHIRDomainResource\FHIRPatient;
17 use HL7\FHIR\STU3\FHIRDomainResource\FHIRPractitioner;
18 use HL7\FHIR\STU3\FHIRElement\FHIRAddress;
19 use HL7\FHIR\STU3\FHIRElement\FHIRAdministrativeGender;
20 use HL7\FHIR\STU3\FHIRElement\FHIRCodeableConcept;
21 use HL7\FHIR\STU3\FHIRElement\FHIRHumanName;
22 use HL7\FHIR\STU3\FHIRElement\FHIRId;
23 use HL7\FHIR\STU3\FHIRElement\FHIRReference;
24 use HL7\FHIR\STU3\FHIRResource\FHIREncounter\FHIREncounterParticipant;
25 use HL7\FHIR\STU3\FHIRResource\FHIRBundle;
26 use HL7\FHIR\STU3\FHIRResource\FHIRBundle\FHIRBundleLink;
27 use HL7\FHIR\STU3\PHPFHIRResponseParser;
29 //use HL7\FHIR\STU3\FHIRResource\FHIREncounter\FHIREncounterLocation;
30 //use HL7\FHIR\STU3\FHIRResource\FHIREncounter\FHIREncounterDiagnosis;
31 //use HL7\FHIR\STU3\FHIRElement\FHIRPeriod;
32 //use HL7\FHIR\STU3\FHIRElement\FHIRParticipantRequired;
34 class FhirResourcesService
36 public function createBundle($resource = '', $resource_array = [], $encode = true)
38 $bundleUrl = \RestConfig::$REST_FULL_URL;
39 $nowDate = date("Y-m-d\TH:i:s");
40 $meta = array('lastUpdated' => $nowDate);
41 $bundleLink = new FHIRBundleLink(array('relation' => 'self', 'url' => $bundleUrl));
42 // set bundle type default to collection so may include different
43 // resource types. at least I hope thats how it works....
44 $bundleInit = array(
45 'identifier' => $resource . "bundle",
46 'type' => 'collection',
47 'total' => count($resource_array),
48 'meta' => $meta);
49 $bundle = new FHIRBundle($bundleInit);
50 $bundle->addLink($bundleLink);
51 foreach ($resource_array as $addResource) {
52 $bundle->addEntry($addResource);
55 if ($encode) {
56 return json_encode($bundle);
59 return $bundle;
62 public function createPatientResource($resourceId = '', $data = '', $encode = true)
64 // @todo add display text after meta
65 $nowDate = date("Y-m-d\TH:i:s");
66 $id = new FhirId();
67 $id->setValue($resourceId);
68 $name = new FHIRHumanName();
69 $address = new FHIRAddress();
70 $gender = new FHIRAdministrativeGender();
71 $meta = array('versionId' => '1', 'lastUpdated' => $nowDate);
72 $initResource = array('id' => $id, 'meta' => $meta);
73 $name->setUse('official');
74 $name->setFamily($data['lname']);
75 $name->given = [$data['fname'], $data['mname']];
76 $address->addLine($data['street']);
77 $address->setCity($data['city']);
78 $address->setState($data['state']);
79 $address->setPostalCode($data['postal_code']);
80 $gender->setValue(strtolower($data['sex']));
82 $patientResource = new FHIRPatient($initResource);
83 //$patientResource->setId($id);
84 $patientResource->setActive(true);
85 $patientResource->setGender($gender);
86 $patientResource->addName($name);
87 $patientResource->addAddress($address);
89 if ($encode) {
90 return json_encode($patientResource);
91 } else {
92 return $patientResource;
96 public function createPractitionerResource($id = '', $data = '', $encode = true)
98 $resource = new FHIRPractitioner();
99 $id = new FhirId();
100 $name = new FHIRHumanName();
101 $address = new FHIRAddress();
102 $id->setValue($id);
103 $name->setUse('official');
104 $name->setFamily($data['lname']);
105 $name->given = [$data['fname'], $data['mname']];
106 $address->addLine($data['street']);
107 $address->setCity($data['city']);
108 $address->setState($data['state']);
109 $address->setPostalCode($data['zip']);
110 $resource->setId($id);
111 $resource->setActive(true);
112 $gender = new FHIRAdministrativeGender();
113 $gender->setValue('unknown');
114 $resource->setGender($gender);
115 $resource->addName($name);
116 $resource->addAddress($address);
118 if ($encode) {
119 return json_encode($resource);
120 } else {
121 return $resource;
125 public function createEncounterResource($eid = '', $data = '', $encode = true)
127 $pid = 'patient-' . $data['pid'];
128 $temp = $data['provider_id'];
129 //$r = $this->createPractitionerResource($data['provider_id'], $temp);
130 $resource = new FHIREncounter();
131 $id = new FhirId();
132 $id->setValue('encounter-' . $eid);
133 $resource->setId($id);
134 $participant = new FHIREncounterParticipant();
135 $prtref = new FHIRReference;
136 $temp = 'Practitioner/provider-' . $data['provider_id'];
137 $prtref->setReference($temp);
138 $participant->setIndividual($prtref);
139 $date = date('Y-m-d', strtotime($data['date']));
140 $participant->setPeriod(['start' => $date]);
142 $resource->addParticipant($participant);
143 $reason = new FHIRCodeableConcept();
144 $reason->setText($data['reason']);
145 $resource->addReason($reason);
146 $resource->status = 'finished';
147 $resource->setSubject(['reference' => "Patient/$pid"]);
149 if ($encode) {
150 return json_encode($resource);
151 } else {
152 return $resource;
156 public function parseResource($rjson = '', $scheme = 'json')
158 $parser = new PHPFHIRResponseParser(false);
159 if ($scheme == 'json') {
160 $class_object = $parser->parse($rjson);
161 } else {
162 // @todo xml- not sure yet.
164 return $class_object; // feed to resource class or use as is object