Merge pull request #18648 from atm-gauthier/NEW_mass_delete_objects

NEW : mass action delete, no more break if at least one object has child
This commit is contained in:
Laurent Destailleur 2021-09-09 14:38:49 +02:00 committed by GitHub
commit cbe08183ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 7 deletions

View File

@ -1287,6 +1287,7 @@ if (!$error && ($massaction == 'delete' || ($action == 'delete' && $confirm == '
$objecttmp = new $objectclass($db); $objecttmp = new $objectclass($db);
$nbok = 0; $nbok = 0;
$TMsg = array();
foreach ($toselect as $toselectid) { foreach ($toselect as $toselectid) {
$result = $objecttmp->fetch($toselectid); $result = $objecttmp->fetch($toselectid);
if ($result > 0) { if ($result > 0) {
@ -1314,7 +1315,9 @@ if (!$error && ($massaction == 'delete' || ($action == 'delete' && $confirm == '
$result = $objecttmp->delete($user); $result = $objecttmp->delete($user);
} }
if ($result <= 0) { if (empty($result)) { // if delete returns 0, there is at least one object linked
$TMsg = array_merge($objecttmp->errors, $TMsg);
} elseif ($result < 0) { // if delete returns is < 0, there is an error, we break and rollback later
setEventMessages($objecttmp->error, $objecttmp->errors, 'errors'); setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
$error++; $error++;
break; break;
@ -1328,16 +1331,25 @@ if (!$error && ($massaction == 'delete' || ($action == 'delete' && $confirm == '
} }
} }
if (!$error) { if (empty($error)) {
// Message for elements well deleted
if ($nbok > 1) { if ($nbok > 1) {
setEventMessages($langs->trans("RecordsDeleted", $nbok), null, 'mesgs'); setEventMessages($langs->trans("RecordsDeleted", $nbok), null, 'mesgs');
} else { } elseif ($nbok == 1) {
setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs'); setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
} }
// Message for elements which can't be deleted
if (!empty($TMsg)) {
sort($TMsg);
setEventMessages('', array_unique($TMsg), 'warnings');
}
$db->commit(); $db->commit();
} else { } else {
$db->rollback(); $db->rollback();
} }
//var_dump($listofobjectthirdparties);exit; //var_dump($listofobjectthirdparties);exit;
} }

View File

@ -4297,10 +4297,10 @@ abstract class CommonObject
//print 'Found into table '.$table.', type '.$langs->transnoentitiesnoconv($elementname).', haschild='.$haschild; //print 'Found into table '.$table.', type '.$langs->transnoentitiesnoconv($elementname).', haschild='.$haschild;
$haschild += $obj->nb; $haschild += $obj->nb;
if (is_numeric($elementname)) { // old usage if (is_numeric($elementname)) { // old usage
$this->errors[] = $langs->trans("ErrorRecordHasAtLeastOneChildOfType", $table); $this->errors[] = $langs->transnoentities("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $table);
} else // new usage: $elementname=Translation key } else // new usage: $elementname=Translation key
{ {
$this->errors[] = $langs->trans("ErrorRecordHasAtLeastOneChildOfType", $langs->transnoentitiesnoconv($elementname)); $this->errors[] = $langs->transnoentities("ErrorRecordHasAtLeastOneChildOfType", method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref, $langs->transnoentitiesnoconv($elementname));
} }
break; // We found at least one, we stop here break; // We found at least one, we stop here
} }

View File

@ -83,7 +83,7 @@ ErrorCantSaveADoneUserWithZeroPercentage=Can't save an action with "status not s
ErrorRefAlreadyExists=Ref used for creation already exists. ErrorRefAlreadyExists=Ref used for creation already exists.
ErrorPleaseTypeBankTransactionReportName=Please enter the bank statement name where the entry has to be reported (Format YYYYMM or YYYYMMDD) ErrorPleaseTypeBankTransactionReportName=Please enter the bank statement name where the entry has to be reported (Format YYYYMM or YYYYMMDD)
ErrorRecordHasChildren=Failed to delete record since it has some child records. ErrorRecordHasChildren=Failed to delete record since it has some child records.
ErrorRecordHasAtLeastOneChildOfType=Object has at least one child of type %s ErrorRecordHasAtLeastOneChildOfType=Object %s has at least one child of type %s
ErrorRecordIsUsedCantDelete=Can't delete record. It is already used or included into another object. ErrorRecordIsUsedCantDelete=Can't delete record. It is already used or included into another object.
ErrorModuleRequireJavascript=Javascript must not be disabled to have this feature working. To enable/disable Javascript, go to menu Home->Setup->Display. ErrorModuleRequireJavascript=Javascript must not be disabled to have this feature working. To enable/disable Javascript, go to menu Home->Setup->Display.
ErrorPasswordsMustMatch=Both typed passwords must match each other ErrorPasswordsMustMatch=Both typed passwords must match each other

View File

@ -83,7 +83,7 @@ ErrorCantSaveADoneUserWithZeroPercentage=Impossible de sauver une action à l'é
ErrorRefAlreadyExists=Le référence <b>%s</b> existe déjà. ErrorRefAlreadyExists=Le référence <b>%s</b> existe déjà.
ErrorPleaseTypeBankTransactionReportName=Choisissez le nom du relevé bancaire sur lequel la ligne est rapportées (Format AAAAMM ou AAAAMMJJ) ErrorPleaseTypeBankTransactionReportName=Choisissez le nom du relevé bancaire sur lequel la ligne est rapportées (Format AAAAMM ou AAAAMMJJ)
ErrorRecordHasChildren=Impossible de supprimer l'enregistrement car il possède des enregistrements fils. ErrorRecordHasChildren=Impossible de supprimer l'enregistrement car il possède des enregistrements fils.
ErrorRecordHasAtLeastOneChildOfType=L'objet a au moins un enfant de type %s ErrorRecordHasAtLeastOneChildOfType=L'objet %s a au moins un enfant de type %s
ErrorRecordIsUsedCantDelete=Ne peut effacer l'enregistrement. Ce dernier est déjà utilisé ou inclut dans un autre élément. ErrorRecordIsUsedCantDelete=Ne peut effacer l'enregistrement. Ce dernier est déjà utilisé ou inclut dans un autre élément.
ErrorModuleRequireJavascript=Le javascript ne doit pas être désactivé pour que cette fonctionnalité soit utilisable. Pour activer/désactiver l'utilisation de javascript, allez dans le menu Accueil->Configuration->Affichage. ErrorModuleRequireJavascript=Le javascript ne doit pas être désactivé pour que cette fonctionnalité soit utilisable. Pour activer/désactiver l'utilisation de javascript, allez dans le menu Accueil->Configuration->Affichage.
ErrorPasswordsMustMatch=Les 2 mots de passe saisis doivent correspondre ErrorPasswordsMustMatch=Les 2 mots de passe saisis doivent correspondre