Skip to content

Commit

Permalink
Merge pull request #392 from Steinbeck-Lab/fix-timeline-2d-3d
Browse files Browse the repository at this point in the history
fix: display and processing for 3d and 2d coordinate audits is fixed
  • Loading branch information
CS76 authored Feb 10, 2025
2 parents 831cade + ac274af commit d57953b
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 204 deletions.
75 changes: 58 additions & 17 deletions app/Livewire/MoleculeHistoryTimeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,34 @@ class MoleculeHistoryTimeline extends Component

public $audit_data = [];

/**
* Format coordinate data for display
*/
private function formatCoordinateData(?string $value): ?string
{
if (empty($value)) {
return null;
}

// Clean up the coordinate data
$value = trim($value);
// Remove any multiple spaces
$value = preg_replace('/\s+/', ' ', $value);
// Ensure consistent line endings
$value = str_replace(["\r\n", "\r"], "\n", $value);

return $value;
}

public function getHistory()
{
$audit_data = [];
$audits_collection = $this->mol->audits->merge($this->mol->properties()->get()[0]->audits);

if ($this->mol->structures()->get()->count() > 0) {
$audits_collection = $audits_collection->merge($this->mol->structures()->get()[0]->audits);
}

foreach ($audits_collection->sortByDesc('created_at') as $index => $audit) {
$audit_data[$index]['user_name'] = $audit->getMetadata()['user_name'];
$audit_data[$index]['event'] = $audit->getMetadata()['audit_event'];
Expand All @@ -32,28 +53,48 @@ public function getHistory()
$audit_data[$index]['affected_columns'][$affected_column]['new_value'] = $audit->new_values ? array_values($audit->new_values)[0] : null;
} else {
foreach ($audit->getModified() as $affected_column => $value) {
// Pre-process IUPAC names with italic conversion
if ($affected_column === 'iupac_name') {
$audit_data[$index]['affected_columns'][$affected_column]['old_value'] =
array_key_exists('old', $value) ? convert_italics_notation($value['old']) : null;
$audit_data[$index]['affected_columns'][$affected_column]['new_value'] =
array_key_exists('new', $value) ? convert_italics_notation($value['new']) : null;
} else {
$audit_data[$index]['affected_columns'][$affected_column]['old_value'] =
array_key_exists('old', $value) ? $value['old'] : null;
$audit_data[$index]['affected_columns'][$affected_column]['new_value'] =
array_key_exists('new', $value) ? $value['new'] : null;
if ($audit['auditable_type'] == "App\Models\Structure" && ($affected_column == 'molecule_id' || $affected_column == 'id')) {
continue;
}

// Handle different column types
switch ($affected_column) {
case 'iupac_name':
$old_value = array_key_exists('old', $value) ? convert_italics_notation($value['old']) : null;
$new_value = array_key_exists('new', $value) ? convert_italics_notation($value['new']) : null;
break;

case '2d':
case '3d':
$old_value = array_key_exists('old', $value) ? $this->formatCoordinateData($value['old']) : null;
$new_value = array_key_exists('new', $value) ? $this->formatCoordinateData($value['new']) : null;
break;

default:
$old_value = array_key_exists('old', $value) ? $value['old'] : null;
$new_value = array_key_exists('new', $value) ? $value['new'] : null;
}

$audit_data[$index]['affected_columns'][$affected_column] = [
'old_value' => $old_value,
'new_value' => $new_value,
];
}
}
}

$initial_audit = [];
$initial_audit['user_name'] = null;
$initial_audit['event'] = null;
$initial_audit['created_at'] = $this->mol->created_at->format('Y/m/d');
$initial_audit['affected_columns']['created']['old_value'] = null;
$initial_audit['affected_columns']['created']['new_value'] = null;
// Add initial audit entry
$initial_audit = [
'user_name' => null,
'event' => null,
'created_at' => $this->mol->created_at->format('Y/m/d'),
'affected_columns' => [
'created' => [
'old_value' => null,
'new_value' => null,
],
],
];

array_push($audit_data, $initial_audit);
$this->audit_data = $audit_data;
Expand Down
Loading

0 comments on commit d57953b

Please sign in to comment.