Skip to content

Commit

Permalink
Merge pull request openemr#7726 from stephenwaite/iss7568-rel-702
Browse files Browse the repository at this point in the history
Refactor previous name into dedicated service (openemr#7571) for rel-702
  • Loading branch information
stephenwaite authored Sep 17, 2024
2 parents 5a81e01 + 171a489 commit b759180
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OpenEMR\Services\CodeTypesService;
use OpenEMR\Services\ContactService;
use OpenEMR\Services\EncounterService;
use OpenEMR\Services\PatientNameHistoryService;
use OpenEMR\Services\PatientService;
use OpenEMR\Services\Search\DateSearchField;
use OpenEMR\Services\Search\SearchComparator;
Expand Down Expand Up @@ -204,8 +205,8 @@ public function getPreviousAddresses($pid): array
*/
public function getPreviousNames($pid): array
{
$patientService = new PatientService();
return $patientService->getPatientNameHistory($pid) ?? [];
$nameService = new PatientNameHistoryService();
return $nameService->getPatientNameHistory($pid) ?? [];
}

/* Fetch Patient data from EMR
Expand Down
12 changes: 6 additions & 6 deletions library/ajax/specialty_form_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
require_once(__DIR__ . "/../../interface/globals.php");

use OpenEMR\Common\Csrf\CsrfUtils;
use OpenEMR\Services\PatientService;
use OpenEMR\Services\PatientNameHistoryService;

if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
CsrfUtils::csrfNotVerified();
Expand All @@ -31,8 +31,8 @@

function nameHistoryDelete($id)
{
$patientService = new PatientService();
$is_ok = $patientService->deletePatientNameHistoryById($id);
$patientNameService = new PatientNameHistoryService();
$is_ok = $patientNameService->deletePatientNameHistoryById($id);
$is_ok = empty($is_ok) ? xlt("Success") : xlt("Failed");
echo js_escape($is_ok);
exit;
Expand All @@ -44,9 +44,9 @@ function nameHistorySave($post_items)
$date = new DateTime($post_items['previous_name_enddate']);
$post_items['previous_name_enddate'] = $date->format('Y-m-d');
}
$patientService = new PatientService('patient_history');
$is_new = $patientService->createPatientNameHistory($post_items['pid'], $post_items);
$name = $patientService->formatPreviousName($post_items);
$patientNameService = new PatientNameHistoryService();
$is_new = $patientNameService->createPatientNameHistory($post_items['pid'], $post_items);
$name = $patientNameService->formatPreviousName($post_items);

$ret = array();
if (!empty($is_new)) {
Expand Down
9 changes: 5 additions & 4 deletions library/options.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
use OpenEMR\Services\EncounterService;
use OpenEMR\Services\FacilityService;
use OpenEMR\Services\PatientService;
use OpenEMR\Services\PatientNameHistoryService;
use OpenEMR\Events\PatientDemographics\RenderPharmacySectionEvent;

$facilityService = new FacilityService();
Expand Down Expand Up @@ -1626,8 +1627,8 @@ class='form-control{$smallform}'
} elseif ($data_type == 52) {
global $pid;
$pid = ($frow['blank_form'] ?? null) ? null : $pid;
$patientService = new PatientService();
$res = $patientService->getPatientNameHistory($pid);
$patientNameService = new PatientNameHistoryService();
$res = $patientNameService->getPatientNameHistory($pid);
echo "<div class='input-group w-75'>";
echo "<select name='form_$field_id_esc" . "[]'" . " id='form_$field_id_esc' title='$description' $lbfonchange $disabled class='form-control$smallform select-previous-names' multiple='multiple'>";
foreach ($res as $row) {
Expand Down Expand Up @@ -2854,8 +2855,8 @@ function generate_display_field($frow, $currvalue)
}
} elseif ($data_type == 52) {
global $pid;
$patientService = new PatientService();
$rows = $patientService->getPatientNameHistory($pid);
$patientNameService = new PatientNameHistoryService();
$rows = $patientNameService->getPatientNameHistory($pid);
$i = 0;
foreach ($rows as $row) {
// name escaped in fetch
Expand Down
148 changes: 148 additions & 0 deletions src/Services/PatientNameHistoryService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

/**
* Handles the interactions with the patient_history table.
*
* @package OpenEMR
* @link http://www.open-emr.org
*
* @author Jerry Padgett <[email protected]>
* @author Stephen Nielson <[email protected]>
* @copyright Copyright (c) 2024 Care Management Solutions, Inc. <[email protected]>
* @copyright Copyright (c) 2020 Jerry Padgett <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/

namespace OpenEMR\Services;

use OpenEMR\Common\Database\QueryUtils;
use OpenEMR\Common\Logging\SystemLogger;
use OpenEMR\Common\Uuid\UuidRegistry;
use OpenEMR\Services\PatientService;

class PatientNameHistoryService extends BaseService
{
public const TABLE_NAME = "patient_history";
public function __construct()
{
// note the constant for this table is PRIVATE and should either be protected or PUBLIC
parent::__construct(self::TABLE_NAME);
}

public function deletePatientNameHistoryById($id)
{
$sql = "DELETE FROM patient_history WHERE id = ?";
return sqlQuery($sql, array($id));
}

/**
* Create a previous patient name history
* Updates not allowed for this history feature.
*
* @param string $pid patient internal id
* @param array $record array values to insert
* @return int | false new id or false if name already exist
*/
public function createPatientNameHistory($pid, $record)
{
// we should never be null here but for legacy reasons we are going to default to this
$createdBy = $_SESSION['authUserID'] ?? null; // we don't let anyone else but the current user be the createdBy
if ($pid <= 0) {
return false;
}
$insertData = [
'pid' => $pid,
'history_type_key' => 'name_history',
'previous_name_prefix' => $record['previous_name_prefix'],
'previous_name_first' => $record['previous_name_first'],
'previous_name_middle' => $record['previous_name_middle'],
'previous_name_last' => $record['previous_name_last'],
'previous_name_suffix' => $record['previous_name_suffix'],
'previous_name_enddate' => $record['previous_name_enddate']
];
$sql = "SELECT pid FROM " . self::TABLE_NAME . " WHERE
pid = ? AND
history_type_key = ? AND
previous_name_prefix = ? AND
previous_name_first = ? AND
previous_name_middle = ? AND
previous_name_last = ? AND
previous_name_suffix = ? AND
previous_name_enddate = ?
";
$go_flag = QueryUtils::fetchSingleValue($sql, 'pid', $insertData);
// return false which calling routine should understand as existing name record
if (!empty($go_flag)) {
return false;
}
// finish up the insert
$insertData['created_by'] = $createdBy;
$insertData['uuid'] = UuidRegistry::getRegistryForTable(self::TABLE_NAME)->createUuid();
$insert = $this->buildInsertColumns($insertData);
$sql = "INSERT INTO " . self::TABLE_NAME . " SET " . $insert['set'];

return QueryUtils::sqlInsert($sql, $insert['bind']);
}


public static function formatPreviousName($item)
{
if (
$item['previous_name_enddate'] === '0000-00-00'
|| $item['previous_name_enddate'] === '00/00/0000'
) {
$item['previous_name_enddate'] = '';
}
$item['previous_name_enddate'] = oeFormatShortDate($item['previous_name_enddate']);
$name = ($item['previous_name_prefix'] ? $item['previous_name_prefix'] . " " : "") .
$item['previous_name_first'] .
($item['previous_name_middle'] ? " " . $item['previous_name_middle'] . " " : " ") .
$item['previous_name_last'] .
($item['previous_name_suffix'] ? " " . $item['previous_name_suffix'] : "") .
($item['previous_name_enddate'] ? " " . $item['previous_name_enddate'] : "");

return text($name);
}


public function getPatientNameHistory($pid)
{
$sql = "SELECT pid,
id,
previous_name_prefix,
previous_name_first,
previous_name_middle,
previous_name_last,
previous_name_suffix,
previous_name_enddate
FROM patient_history
WHERE pid = ? AND history_type_key = ?";
$results = QueryUtils::sqlStatementThrowException($sql, array($pid, 'name_history'));
$rows = [];
while ($row = sqlFetchArray($results)) {
$row['formatted_name'] = $this->formatPreviousName($row);
$rows[] = $row;
}

return $rows;
}


public function getPatientNameHistoryById($pid, $id)
{
$sql = "SELECT pid,
id,
previous_name_prefix,
previous_name_first,
previous_name_middle,
previous_name_last,
previous_name_suffix,
previous_name_enddate
FROM patient_history
WHERE pid = ? AND id = ? AND history_type_key = ?";
$result = sqlQuery($sql, array($pid, $id, 'name_history'));
$result['formatted_name'] = $this->formatPreviousName($result);

return $result;
}
}
112 changes: 2 additions & 110 deletions src/Services/PatientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use OpenEMR\Common\Database\QueryPagination;
use OpenEMR\Common\Database\QueryUtils;
use OpenEMR\Common\Logging\SystemLogger;
use OpenEMR\Common\ORDataObject\Address;
use OpenEMR\Common\ORDataObject\ContactAddress;
use OpenEMR\Common\Uuid\UuidRegistry;
Expand Down Expand Up @@ -662,118 +663,9 @@ private function saveCareTeamHistory($patientData, $oldProviders, $oldFacilities
$careTeamService->createCareTeamHistory($patientData['pid'], $oldProviders, $oldFacilities);
}

public function getPatientNameHistory($pid)
{
$sql = "SELECT pid,
id,
previous_name_prefix,
previous_name_first,
previous_name_middle,
previous_name_last,
previous_name_suffix,
previous_name_enddate
FROM patient_history
WHERE pid = ? AND history_type_key = ?";
$results = QueryUtils::sqlStatementThrowException($sql, array($pid, 'name_history'));
$rows = [];
while ($row = sqlFetchArray($results)) {
$row['formatted_name'] = $this->formatPreviousName($row);
$rows[] = $row;
}

return $rows;
}

public function deletePatientNameHistoryById($id)
{
$sql = "DELETE FROM patient_history WHERE id = ?";
return sqlQuery($sql, array($id));
}

public function getPatientNameHistoryById($pid, $id)
{
$sql = "SELECT pid,
id,
previous_name_prefix,
previous_name_first,
previous_name_middle,
previous_name_last,
previous_name_suffix,
previous_name_enddate
FROM patient_history
WHERE pid = ? AND id = ? AND history_type_key = ?";
$result = sqlQuery($sql, array($pid, $id, 'name_history'));
$result['formatted_name'] = $this->formatPreviousName($result);

return $result;
}

/**
* Create a previous patient name history
* Updates not allowed for this history feature.
*
* @param string $pid patient internal id
* @param array $record array values to insert
* @return int | false new id or false if name already exist
*/
public function createPatientNameHistory($pid, $record)
{
// we should never be null here but for legacy reasons we are going to default to this
$createdBy = $_SESSION['authUserID'] ?? null; // we don't let anyone else but the current user be the createdBy
if ($pid <= 0) {
return false;
}
$insertData = [
'pid' => $pid,
'history_type_key' => 'name_history',
'previous_name_prefix' => $record['previous_name_prefix'],
'previous_name_first' => $record['previous_name_first'],
'previous_name_middle' => $record['previous_name_middle'],
'previous_name_last' => $record['previous_name_last'],
'previous_name_suffix' => $record['previous_name_suffix'],
'previous_name_enddate' => $record['previous_name_enddate']
];
$sql = "SELECT pid FROM " . self::PATIENT_HISTORY_TABLE . " WHERE
pid = ? AND
history_type_key = ? AND
previous_name_prefix = ? AND
previous_name_first = ? AND
previous_name_middle = ? AND
previous_name_last = ? AND
previous_name_suffix = ? AND
previous_name_enddate = ?
";
$go_flag = QueryUtils::fetchSingleValue($sql, 'pid', $insertData);
// return false which calling routine should understand as existing name record
if (!empty($go_flag)) {
return false;
}
// finish up the insert
$insertData['created_by'] = $createdBy;
$insertData['uuid'] = UuidRegistry::getRegistryForTable(self::PATIENT_HISTORY_TABLE)->createUuid();
$insert = $this->buildInsertColumns($insertData);
$sql = "INSERT INTO " . self::PATIENT_HISTORY_TABLE . " SET " . $insert['set'];

return QueryUtils::sqlInsert($sql, $insert['bind']);
}

public function formatPreviousName($item)
{
if (
$item['previous_name_enddate'] === '0000-00-00'
|| $item['previous_name_enddate'] === '00/00/0000'
) {
$item['previous_name_enddate'] = '';
}
$item['previous_name_enddate'] = oeFormatShortDate($item['previous_name_enddate']);
$name = ($item['previous_name_prefix'] ? $item['previous_name_prefix'] . " " : "") .
$item['previous_name_first'] .
($item['previous_name_middle'] ? " " . $item['previous_name_middle'] . " " : " ") .
$item['previous_name_last'] .
($item['previous_name_suffix'] ? " " . $item['previous_name_suffix'] : "") .
($item['previous_name_enddate'] ? " " . $item['previous_name_enddate'] : "");

return text($name);
return PatientNameHistoryService::formatPreviousName($item);
}

/**
Expand Down

0 comments on commit b759180

Please sign in to comment.