Merge pull request #7535 from stephenwaite/bug_fix_w1
[openemr.git] / interface / patient_file / summary / demographics_save.php
bloba29a455145357544811cc97e174f5fad09effd97
1 <?php
3 /**
4 * demographics_save.php
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @author Stephen Nielson <snielson@discoverandchange.com>
10 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
11 * @copyright Copyright (c) 2024 Care Management Solutions, Inc. <stephen.waite@cmsvt.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once("../../globals.php");
16 require_once("$srcdir/patient.inc.php");
17 require_once("$srcdir/options.inc.php");
19 use OpenEMR\Common\Acl\AclMain;
20 use OpenEMR\Common\Csrf\CsrfUtils;
21 use OpenEMR\Services\ContactService;
22 use OpenEMR\Events\Patient\PatientUpdatedEventAux;
25 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
26 CsrfUtils::csrfNotVerified();
28 global $pid;
29 // Check authorization.
30 if ($pid) {
31 if (!AclMain::aclCheckCore('patients', 'demo', '', 'write')) {
32 die(xlt('Updating demographics is not authorized.'));
35 $tmp = getPatientData($pid, "squad");
36 if ($tmp['squad'] && ! AclMain::aclCheckCore('squads', $tmp['squad'])) {
37 die(xlt('You are not authorized to access this squad.'));
39 } else {
40 if (!AclMain::aclCheckCore('patients', 'demo', '', array('write','addonly'))) {
41 die(xlt('Adding demographics is not authorized.'));
45 foreach ($_POST as $key => $val) {
46 if ($val == "MM/DD/YYYY") {
47 $_POST[$key] = "";
51 // Update patient_data and employer_data:
53 $newdata = array();
54 $newdata['patient_data']['id'] = $_POST['db_id'];
55 $fres = sqlStatement("SELECT * FROM layout_options " .
56 "WHERE form_id = 'DEM' AND uor > 0 AND field_id != '' " .
57 "ORDER BY group_id, seq");
59 $addressFieldsToSave = array();
60 while ($frow = sqlFetchArray($fres)) {
61 $data_type = $frow['data_type'];
62 if ((int)$data_type === 52) {
63 // patient name history is saved in add.
64 continue;
66 $field_id = $frow['field_id'];
67 $colname = $field_id;
68 $table = 'patient_data';
69 if (str_starts_with($field_id, 'em_')) {
70 $colname = substr($field_id, 3);
71 $table = 'employer_data';
74 // Get value only if field exist in $_POST (prevent deleting of field with disabled attribute)
75 // *unless* the data_type is a checkbox ("21"), because if the checkbox is unchecked, then it will not
76 // have a value set on the form, it will be empty.
77 if ($data_type == 54) { // address list
78 $addressFieldsToSave[$field_id] = get_layout_form_value($frow);
79 } elseif (isset($_POST["form_$field_id"]) || $data_type == 21) {
80 $newdata[$table][$colname] = get_layout_form_value($frow);
84 // TODO: All of this should be bundled up inside a transaction...
86 updatePatientData($pid, $newdata['patient_data']);
87 if (!$GLOBALS['omit_employers']) {
88 updateEmployerData($pid, $newdata['employer_data']);
91 if (!empty($addressFieldsToSave)) {
92 // TODO: we would handle other types of address fields here,
93 // for now we will just go through and populate the patient
94 // address information
95 // TODO: how are error messages supposed to display if the save fails?
96 foreach ($addressFieldsToSave as $field => $addressFieldData) {
97 // if we need to save other kinds of addresses we could do that here with our field column...
98 $contactService = new ContactService();
99 $contactService->saveContactsForPatient($pid, $addressFieldData);
104 * trigger events to listeners who want data that is not directly available in
105 * the patient_data table on update
107 $GLOBALS["kernel"]->getEventDispatcher()->dispatch(new PatientUpdatedEventAux($pid, $_POST), PatientUpdatedEventAux::EVENT_HANDLE, 10);
108 // if refresh tab after saving then results in csrf error
109 include_once("demographics.php");