Skip to content

Commit

Permalink
Construct better flash message on successful delete action
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Feb 26, 2025
1 parent 1aa68bc commit 90166a6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
38 changes: 30 additions & 8 deletions app/src/Controller/StandardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,39 @@ public function delete($id) {
$useHardDelete = ($modelsName == "Cos");

$table->deleteOrFail($obj, ['useHardDelete' => $useHardDelete]);

$msgid = 'deleted';

// Use the display field to generate the flash message

// TODO: This needs to be moved to its own function. Keeping for now while we confirm
// everything is working correctly
$field = $table->getDisplayField();

if(!empty($obj->$field)) {
$this->Flash->success(__d('result', 'deleted.a', [$obj->$field]));
} else {
$this->Flash->success(__d('result', 'deleted'));
$primaryLinks = $table->getPrimaryLinks();

if ($modelsName === 'People') {
$Names = TableRegistry::getTableLocator()->get('Names');
$personName = $Names->primaryName(
id:(int)$obj->id,
options: ['archived' => true],
)->full_name;
$message = "$personName ($obj->id)";
} elseif (in_array('person_id', $primaryLinks)) {
$Names = TableRegistry::getTableLocator()->get('Names');
$personName = $Names->primaryName((int)$obj->person_id)->full_name;
$displayValue = !empty($obj->$field) ? $obj->$field : $modelsName;
$message = "$personName/$displayValue ($obj->id)";
} elseif(!empty($obj->$field)) {
$displayValue = !empty($obj->$field) ? $obj->$field : $modelsName;
$message = $displayValue;
}


// By default, we pass the empty msgid. Then one with no placeholder.
// If we have a message use the text rich one.
if (!empty($message)) {
$msgid = "$msgid.a";
}

$this->Flash->success(__d('result', $msgid, [$message]));

// Trigger provisioning, letting errors bubble up (AR-GMR-5)
// In general, tables should check that they were passed a deleted
// record and martial data/set eligibility appropriately
Expand Down
14 changes: 7 additions & 7 deletions app/src/Model/Table/NamesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,23 +233,23 @@ public function localAfterSave(\Cake\Event\EventInterface $event, \Cake\Datasour
* @since COmanage Registry v5.0.0
* @param int $id Record ID
* @param string $recordType Type of record to find primary name for, 'person' or 'external_identity'
* @param array $options
* @return Name Name Entity
*/

public function primaryName(int $id, string $recordType='person') {
public function primaryName(int $id, string $recordType='person', array $options = []) {
$query = empty($options) ? $this->find() : $this->find('all', $options);
if($recordType == 'person') {
// Return the Primary Name

return $this->find()
->where(['person_id' => $id,
return $query->where(['person_id' => $id,
'primary_name' => true])
->firstOrFail();
->firstOrFail();
} else {
// Return the first name, whatever it is

return $this->find()
->where(['external_identity_id' => $id])
->firstOrFail();
return $query->where(['external_identity_id' => $id])
->firstOrFail();
}
}

Expand Down

0 comments on commit 90166a6

Please sign in to comment.