updated US CDC website link to current immunization VIS page (#7855)
[openemr.git] / src / Services / PatientNameHistoryService.php
blobe8adb418bf7b768f6413b525f87f6317acc7de95
1 <?php
3 /**
4 * Handles the interactions with the patient_history table.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
9 * @author Jerry Padgett <sjpadgett@gmail.com>
10 * @author Stephen Nielson <snielson@discoverandchange.com>
11 * @copyright Copyright (c) 2024 Care Management Solutions, Inc. <stephen.waite@cmsvt.com>
12 * @copyright Copyright (c) 2020 Jerry Padgett <sjpadgett@gmail.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 namespace OpenEMR\Services;
18 use OpenEMR\Common\Database\QueryUtils;
19 use OpenEMR\Common\Logging\SystemLogger;
20 use OpenEMR\Common\Uuid\UuidRegistry;
21 use OpenEMR\Services\PatientService;
23 class PatientNameHistoryService extends BaseService
25 public const TABLE_NAME = "patient_history";
26 public function __construct()
28 // note the constant for this table is PRIVATE and should either be protected or PUBLIC
29 parent::__construct(self::TABLE_NAME);
32 public function deletePatientNameHistoryById($id)
34 $sql = "DELETE FROM patient_history WHERE id = ?";
35 return sqlQuery($sql, array($id));
38 /**
39 * Create a previous patient name history
40 * Updates not allowed for this history feature.
42 * @param string $pid patient internal id
43 * @param array $record array values to insert
44 * @return int | false new id or false if name already exist
46 public function createPatientNameHistory($pid, $record)
48 // we should never be null here but for legacy reasons we are going to default to this
49 $createdBy = $_SESSION['authUserID'] ?? null; // we don't let anyone else but the current user be the createdBy
50 if ($pid <= 0) {
51 return false;
53 $insertData = [
54 'pid' => $pid,
55 'history_type_key' => 'name_history',
56 'previous_name_prefix' => $record['previous_name_prefix'],
57 'previous_name_first' => $record['previous_name_first'],
58 'previous_name_middle' => $record['previous_name_middle'],
59 'previous_name_last' => $record['previous_name_last'],
60 'previous_name_suffix' => $record['previous_name_suffix'],
61 'previous_name_enddate' => $record['previous_name_enddate']
63 $sql = "SELECT pid FROM " . self::TABLE_NAME . " WHERE
64 pid = ? AND
65 history_type_key = ? AND
66 previous_name_prefix = ? AND
67 previous_name_first = ? AND
68 previous_name_middle = ? AND
69 previous_name_last = ? AND
70 previous_name_suffix = ? AND
71 previous_name_enddate = ?
73 $go_flag = QueryUtils::fetchSingleValue($sql, 'pid', $insertData);
74 // return false which calling routine should understand as existing name record
75 if (!empty($go_flag)) {
76 return false;
78 // finish up the insert
79 $insertData['created_by'] = $createdBy;
80 $insertData['uuid'] = UuidRegistry::getRegistryForTable(self::TABLE_NAME)->createUuid();
81 $insert = $this->buildInsertColumns($insertData);
82 $sql = "INSERT INTO " . self::TABLE_NAME . " SET " . $insert['set'];
84 return QueryUtils::sqlInsert($sql, $insert['bind']);
88 public static function formatPreviousName($item)
90 if (
91 $item['previous_name_enddate'] === '0000-00-00'
92 || $item['previous_name_enddate'] === '00/00/0000'
93 ) {
94 $item['previous_name_enddate'] = '';
96 $item['previous_name_enddate'] = oeFormatShortDate($item['previous_name_enddate']);
97 $name = ($item['previous_name_prefix'] ? $item['previous_name_prefix'] . " " : "") .
98 $item['previous_name_first'] .
99 ($item['previous_name_middle'] ? " " . $item['previous_name_middle'] . " " : " ") .
100 $item['previous_name_last'] .
101 ($item['previous_name_suffix'] ? " " . $item['previous_name_suffix'] : "") .
102 ($item['previous_name_enddate'] ? " " . $item['previous_name_enddate'] : "");
104 return text($name);
108 public function getPatientNameHistory($pid)
110 $sql = "SELECT pid,
112 previous_name_prefix,
113 previous_name_first,
114 previous_name_middle,
115 previous_name_last,
116 previous_name_suffix,
117 previous_name_enddate
118 FROM patient_history
119 WHERE pid = ? AND history_type_key = ?";
120 $results = QueryUtils::sqlStatementThrowException($sql, array($pid, 'name_history'));
121 $rows = [];
122 while ($row = sqlFetchArray($results)) {
123 $row['formatted_name'] = $this->formatPreviousName($row);
124 $rows[] = $row;
127 return $rows;
131 public function getPatientNameHistoryById($pid, $id)
133 $sql = "SELECT pid,
135 previous_name_prefix,
136 previous_name_first,
137 previous_name_middle,
138 previous_name_last,
139 previous_name_suffix,
140 previous_name_enddate
141 FROM patient_history
142 WHERE pid = ? AND id = ? AND history_type_key = ?";
143 $result = sqlQuery($sql, array($pid, $id, 'name_history'));
144 $result['formatted_name'] = $this->formatPreviousName($result);
146 return $result;