Skip to content

Commit aef3368

Browse files
author
Marvin Durot
committed
Refactor code
1 parent 43cd3a7 commit aef3368

3 files changed

Lines changed: 45 additions & 22 deletions

File tree

modules/backend/behaviors/ListController.php

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,16 @@ public function index_onDelete()
366366

367367
/**
368368
* Bulk replicate records.
369-
* @return void
370-
* @throws \Winter\Storm\Exception\ApplicationException when the parent definition is missing.
369+
* @throws \Winter\Storm\Exception\ApplicationException when the parent definition is missing or when replication fails due to validation errors.
371370
*/
372-
public function index_onReplicate()
371+
public function index_onReplicate(): array
373372
{
374-
if (method_exists($this->controller, 'onCopy')) {
375-
return call_user_func_array([$this->controller, 'onCopy'], func_get_args());
373+
if (method_exists($this->controller, 'onReplicate')) {
374+
return call_user_func_array([$this->controller, 'onReplicate'], func_get_args());
376375
}
377376

378377
/*
379-
* Establish the list definition
378+
* Establish the list replication definition
380379
*/
381380
$definition = post('definition', $this->primaryDefinition);
382381

@@ -386,6 +385,12 @@ public function index_onReplicate()
386385

387386
$listConfig = $this->controller->listGetConfig($definition);
388387

388+
if (!isset($listConfig->replication)) {
389+
throw new ApplicationException(Lang::get('backend::lang.list.missing_replication_definition', compact('definition')));
390+
}
391+
392+
$replicationConfig = $this->makeConfig($listConfig->replication, ['enabled']);
393+
389394
/*
390395
* Validate checked identifiers
391396
*/
@@ -417,30 +422,44 @@ public function index_onReplicate()
417422
$this->controller->listExtendQuery($query, $definition);
418423

419424
/*
420-
* Retrieve protected columns from list column configuration
425+
* If replication is enabled and neither is provided then
426+
* we should default to looking at the model's fillable / guarded definitions
427+
* (allowing explicit configuration in the config_list.yaml to overrule those values if desired).
421428
*/
422-
$columnConfig = $this->makeConfig($listConfig->list);
429+
if (!$replicationConfig->enabled) {
430+
throw new ApplicationException(Lang::get('backend::lang.list.replication_disabled'));
431+
}
423432

424-
$protectedColumns = [];
433+
if ($model->totallyGuarded()) {
434+
$allowed = [];
435+
} else {
436+
$allowed = array_diff($model->getFillable(), $model->getGuarded());
437+
}
425438

426-
foreach ($columnConfig as $column => $config) {
427-
if (array_key_exists('protected', $config) && $config['protected'] === true) {
428-
$protectedColumns[] = $column;
439+
if (isset($replicationConfig->allowed)) {
440+
$allowed = $replicationConfig->allowed;
429441
}
442+
if (isset($replicationConfig->ignored)) {
443+
$allowed = array_diff($allowed, $replicationConfig->ignored);
430444
}
431445

432446
/*
433-
* Copy records
447+
* Replicate records
434448
*/
435449
$records = $query->get();
436450

437451
if ($records->count()) {
438452
try {
439453
foreach ($records as $record) {
440-
$copy = $record->replicate($protectedColumns);
441-
$this->controller->listBeforeCopy($record);
442-
$copy->save();
443-
$this->controller->listAfterCopy($record);
454+
// Replicate the model without protected attributes
455+
$replicated = $record->replicate(
456+
array_diff($allowed, array_keys($record->getAttributes()))
457+
);
458+
$this->controller->listBeforeReplicate($replicated, $record);
459+
460+
$replicated->save();
461+
462+
$this->controller->listAfterReplicate($replicated, $record);
444463
}
445464
Flash::success(Lang::get(
446465
(!empty($listConfig->replicateMessage))
@@ -568,18 +587,18 @@ public function listGetConfig(string $definition = null)
568587
//
569588

570589
/**
571-
* Called before a list record is copied.
590+
* Called before a list record is replicated.
572591
* @param \Winter\Storm\Database\Model|\Winter\Storm\Halcyon\Model
573592
*/
574-
public function listBeforeCopy($model)
593+
public function listBeforeReplicate($model, $original)
575594
{
576595
}
577596

578597
/**
579-
* Called after a list record is copied.
598+
* Called after a list record is replicated.
580599
* @param \Winter\Storm\Database\Model|\Winter\Storm\Halcyon\Model
581600
*/
582-
public function listAfterCopy($model)
601+
public function listAfterReplicate($model, $original)
583602
{
584603
}
585604

modules/backend/lang/en/lang.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
'missing_columns' => 'List used in :class has no list columns defined.',
210210
'missing_definition' => "List behavior does not contain a column for ':field'.",
211211
'missing_parent_definition' => "List behavior does not contain a definition for ':definition'.",
212+
'missing_replication_definition' => "List behavior does not contain a definition for replication of ':definition'.",
212213
'behavior_not_ready' => 'List behavior has not been initialized, check that you have called makeLists() in your controller.',
213214
'invalid_column_datetime' => "Column value ':column' is not a DateTime object, are you missing a \$dates reference in the Model?",
214215
'pagination' => 'Displayed records: :from-:to of :total',
@@ -233,6 +234,7 @@
233234
'replicate_selected_confirm' => 'Duplicate the selected records?',
234235
'replicate_selected_failed' => 'An error has occured when duplicating records.',
235236
'replicate_selected_success' => 'Selected records has been duplicated.',
237+
'replication_disabled' => 'Replication is disabled.',
236238
'column_switch_true' => 'Yes',
237239
'column_switch_false' => 'No',
238240
],

modules/backend/lang/fr/lang.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
'missing_columns' => 'La liste utilisée dans la classe :class n\'a pas de colonne de liste définie.',
210210
'missing_definition' => 'La liste utilisée ne contient de pas de colonne pour le champ ":field".',
211211
'missing_parent_definition' => "Le behavior List ne contient pas de définition pour ':definition'.",
212+
'missing_replication_definition' => "Le behavior List ne contient pas de définition pour la replication de ':definition'.",
212213
'behavior_not_ready' => 'La liste utilisée n\'a pas été initialisée, vérifier que la méthode d\'appel de makeLists() a été soumise au contrôleur.',
213214
'invalid_column_datetime' => 'La valeur de la colonne ":column" n\'est pas un objet DateTime, manque-t-il une référence dans la propriété \$dates du modèle ?',
214215
'pagination' => 'Enregistrements affichés : :from-:to sur :total',
@@ -226,13 +227,14 @@
226227
'check' => 'Sélectionner',
227228
'delete_selected' => 'Supprimer la sélection',
228229
'delete_selected_empty' => 'Il n\'y a aucun enregistrement à supprimer',
229-
'delete_selected_confirm' => 'Confirmer la suppression des enregistrements sélectionnés ?',
230+
'delete_selected_confirm' => 'Confirmer la suppression des eanregistrements sélectionnés ?',
230231
'delete_selected_success' => 'Les enregistrements ont été supprimés.',
231232
'replicate_selected' => 'Dupliquer la sélection',
232233
'replicate_selected_empty' => 'Il n\'y a aucun enregistrement à dupliquer',
233234
'replicate_selected_confirm' => 'Confirmer la duplication des enregistrements sélectionnés ?',
234235
'replicate_selected_success' => 'Les enregistrements ont été dupliqués.',
235236
'replicate_selected_failed' => 'Une erreur s\'est produite pendant la duplication des enregistrements.',
237+
'replication_disabled' => 'La réplication est désactivée.',
236238
'column_switch_true' => 'Oui',
237239
'column_switch_false' => 'Non'
238240
],

0 commit comments

Comments
 (0)