update functionality. (#1427)
[openemr.git] / phpfhir / classes / src / oeFHIRResource.php
blob2e926d72b24195d8a943083f8520741527713895
1 <?php
2 /**
3 * oeFHIRResource 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://www.gnu.org/licenses/agpl-3.0.en.html GNU Affero General Public License 3
12 namespace oeFHIR;
14 use HL7\FHIR\STU3\FHIRDomainResource\FHIREncounter;
15 use HL7\FHIR\STU3\FHIRDomainResource\FHIRPatient;
16 use HL7\FHIR\STU3\FHIRDomainResource\FHIRPractitioner;
17 use HL7\FHIR\STU3\FHIRElement\FHIRAddress;
18 use HL7\FHIR\STU3\FHIRElement\FHIRAdministrativeGender;
19 use HL7\FHIR\STU3\FHIRElement\FHIRCodeableConcept;
20 use HL7\FHIR\STU3\FHIRElement\FHIRHumanName;
21 use HL7\FHIR\STU3\FHIRElement\FHIRId;
22 use HL7\FHIR\STU3\FHIRElement\FHIRReference;
23 use HL7\FHIR\STU3\FHIRResource\FHIREncounter\FHIREncounterParticipant;
24 use HL7\FHIR\STU3\PHPFHIRResponseParser;
26 //use HL7\FHIR\STU3\FHIRResource\FHIREncounter\FHIREncounterLocation;
27 //use HL7\FHIR\STU3\FHIRResource\FHIREncounter\FHIREncounterDiagnosis;
28 //use HL7\FHIR\STU3\FHIRElement\FHIRPeriod;
29 //use HL7\FHIR\STU3\FHIRElement\FHIRParticipantRequired;
31 class oeFHIRResource
33 public function createBundle($pid = '', $encode = true, $resource_array = '')
35 $fs = new FetchLiveData();
38 public function createPatientResource($pid = '', $rid = '', $encode = true)
40 $fs = new FetchLiveData();
41 $patientResource = new FHIRPatient();
42 $id = new FhirId();
43 $name = new FHIRHumanName();
44 $address = new FHIRAddress();
45 $oept = $fs->getDemographicsCurrent($pid);
46 // fhir spec allows either create or update on update put endpoint, but only if id contains alphanumerics.
47 // uri id and resource patient id must match.
48 $id->setValue($rid);
49 $name->setUse('official');
50 $name->setFamily($oept['lname']);
51 $name->given = [$oept['fname'], $oept['mname']];
52 $address->addLine($oept['street']);
53 $address->setCity($oept['city']);
54 $address->setState($oept['state']);
55 $address->setPostalCode($oept['postal_code']);
56 $patientResource->setId($id);
57 $patientResource->setActive(true);
58 $gender = new FHIRAdministrativeGender();
59 $gender->setValue(strtolower($oept['sex']));
60 $patientResource->setGender($gender);
61 $patientResource->addName($name);
62 $patientResource->addAddress($address);
64 return json_encode($patientResource);
67 public function createPractitionerResource($id = '', $rid = '', $encode = true)
69 $fs = new FetchLiveData();
70 $oept = $fs->getUser($id);
72 $resource = new FHIRPractitioner();
73 $id = new FhirId();
74 $name = new FHIRHumanName();
75 $address = new FHIRAddress();
76 $id->setValue($rid);
77 $name->setUse('official');
78 $name->setFamily($oept['lname']);
79 $name->given = [$oept['fname'], $oept['mname']];
80 $address->addLine($oept['street']);
81 $address->setCity($oept['city']);
82 $address->setState($oept['state']);
83 $address->setPostalCode($oept['zip']);
84 $resource->setId($id);
85 $resource->setActive(true);
86 $gender = new FHIRAdministrativeGender();
87 $gender->setValue('unknown');
88 $resource->setGender($gender);
89 $resource->addName($name);
90 $resource->addAddress($address);
92 return json_encode($resource);
95 public function createEncounterResource($pid = '', $rid = '', $eid = '', $encode = true)
97 // If you set an endpoint to an element and resource doesn't exist on server, then create will fail.
98 // Practitioner here is an example so I auto create or update the resource so encounter will go...
99 // Same with conditions once it it built. Encounters can be tied to episodes and care plans e.t.c.
100 $fs = new FetchLiveData();
101 $oept = $fs->getEncounterData($pid, $eid);
102 $temp = 'provider-' . $oept['provider_id'];
103 $rs = new oeFHIRResource(); // update
104 $r = $rs->createPractitionerResource($oept['provider_id'], $temp);
105 $client = new oeFHIRHttpClient();
106 $pt = $client->sendResource('Practitioner', $temp, $r); // update or add practitioner
107 $resource = new FHIREncounter();
108 $id = new FhirId();
109 $id->setValue($rid);
110 $resource->setId($id);
111 $participant = new FHIREncounterParticipant();
112 $prtref = new FHIRReference;
113 $temp = 'Practitioner/provider-' . $oept['provider_id'];
114 $prtref->setReference($temp);
115 $participant->setIndividual($prtref);
116 $date = date('Y-m-d', strtotime($oept['date']));
117 $participant->setPeriod(['start' => $date]);
119 $resource->addParticipant($participant);
120 $reason = new FHIRCodeableConcept();
121 $reason->setText($oept['reason']);
122 $resource->addReason($reason);
123 $resource->status = 'finished';
125 $resource->setSubject(['reference' => 'Patient/patient-' . $pid]);
127 return json_encode($resource);
130 public function parseResource($rjson = '', $scheme = 'json')
132 $parser = new PHPFHIRResponseParser(false);
133 if ($scheme == 'json') {
134 $class_object = $parser->parse($rjson);
135 } else {
136 // @todo xml- not sure yet.
138 return $class_object; // feed to resource class or use as is object