correct include path (#3559)
[openemr.git] / src / RestControllers / FHIR / FhirPatientRestController.php
blobce6951f92cc9307e5dcf545ba9843a0a5e62df97
1 <?php
3 /**
4 * FhirPatientRestController
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Jerry Padgett <sjpadgett@gmail.com>
9 * @copyright Copyright (c) 2018 Jerry Padgett <sjpadgett@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 namespace OpenEMR\RestControllers\FHIR;
15 use OpenEMR\Services\FHIR\FhirResourcesService;
16 use OpenEMR\Services\FHIR\FhirPatientService;
17 use OpenEMR\Services\FHIR\FhirValidationService;
18 use OpenEMR\RestControllers\RestControllerHelper;
19 use OpenEMR\FHIR\R4\FHIRResource\FHIRBundle\FHIRBundleEntry;
20 use OpenEMR\Validators\ProcessingResult;
22 require_once(__DIR__ . '/../../../_rest_config.php');
24 /**
25 * Supports REST interactions with the FHIR patient resource
27 class FhirPatientRestController
29 private $fhirPatientService;
30 private $fhirService;
31 private $fhirValidate;
33 public function __construct()
35 $this->fhirService = new FhirResourcesService();
36 $this->fhirPatientService = new FhirPatientService();
37 $this->fhirValidate = new FhirValidationService();
40 /**
41 * Creates a new FHIR patient resource
42 * @param $fhirJson The FHIR patient resource
43 * @returns 201 if the resource is created, 400 if the resource is invalid
45 public function post($fhirJson)
47 $fhirValidate = $this->fhirValidate->validate($fhirJson);
48 if (!empty($fhirValidate)) {
49 return RestControllerHelper::responseHandler($fhirValidate, null, 400);
52 $processingResult = $this->fhirPatientService->insert($fhirJson);
53 return RestControllerHelper::handleProcessingResult($processingResult, 201);
56 /**
57 * Updates an existing FHIR patient resource
58 * @param $fhirId The FHIR patient resource id (uuid)
59 * @param $fhirJson The updated FHIR patient resource (complete resource)
60 * @returns 200 if the resource is created, 400 if the resource is invalid
62 public function put($fhirId, $fhirJson)
64 $fhirValidate = $this->fhirValidate->validate($fhirJson);
65 if (!empty($fhirValidate)) {
66 return RestControllerHelper::responseHandler($fhirValidate, null, 400);
69 $processingResult = $this->fhirPatientService->update($fhirId, $fhirJson);
70 return RestControllerHelper::handleProcessingResult($processingResult, 200);
73 /**
74 * Queries for a single FHIR patient resource by FHIR id
75 * @param $fhirId The FHIR patient resource id (uuid)
76 * @returns 200 if the operation completes successfully
78 public function getOne($fhirId)
80 $processingResult = $this->fhirPatientService->getOne($fhirId, true);
81 return RestControllerHelper::handleProcessingResult($processingResult, 200);
84 /**
85 * Queries for FHIR patient resources using various search parameters.
86 * Search parameters include:
87 * - address (stree, postal code, city, or state)
88 * - address-city
89 * - address-postalcode
90 * - address-state
91 * - birthdate
92 * - email
93 * - family
94 * - gender
95 * - given (first name or middle name)
96 * - name (title, first name, middle name, last name)
97 * - phone (home, business, cell)
98 * - telecom (email, phone)
99 * @return FHIR bundle with query results, if found
101 public function getAll($searchParams)
103 $processingResult = $this->fhirPatientService->getAll($searchParams);
104 $bundleEntries = array();
105 foreach ($processingResult->getData() as $index => $searchResult) {
106 $bundleEntry = [
107 'fullUrl' => \RestConfig::$REST_FULL_URL . '/fhir/Patient/' . $searchResult->getId(),
108 'resource' => $searchResult
110 $fhirBundleEntry = new FHIRBundleEntry($bundleEntry);
111 array_push($bundleEntries, $fhirBundleEntry);
113 $bundleSearchResult = $this->fhirService->createBundle('Patient', $bundleEntries, false);
114 $searchResponseBody = RestControllerHelper::responseHandler($bundleSearchResult, null, 200);
115 return $searchResponseBody;