Fortify index_onDelete a little more

Refs #1805
This commit is contained in:
Samuel Georges 2016-02-27 13:58:59 +11:00
parent 65eac15b00
commit fa61ef2df9
2 changed files with 52 additions and 8 deletions

View File

@ -108,7 +108,7 @@ class ListController extends ControllerBehavior
* Create the model
*/
$class = $listConfig->modelClass;
$model = new $class();
$model = new $class;
$model = $this->controller->listExtendModel($model, $definition);
/*
@ -245,25 +245,68 @@ class ListController extends ControllerBehavior
$this->makeLists();
}
/**
* Bulk delete records.
* @return void
*/
public function index_onDelete()
{
if (method_exists($this->controller, 'onDelete')) {
return $this->controller->onDelete();
return call_user_func_array([$this->controller, 'onDelete'], func_get_args());
}
$model = $this->config->modelClass;
/*
* Validate checked identifiers
*/
$checkedIds = post('checked');
if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {
foreach ($checkedIds as $id) {
if (!$record = $model::find($id)) {
continue;
if (!$checkedIds || !is_array($checkedIds) || !count($checkedIds)) {
Flash::error(Lang::get('backend::lang.list.delete_selected_empty'));
return $this->controller->listRefresh();
}
/*
* Establish the list definition
*/
$definition = post('definition', $this->primaryDefinition);
if (!isset($this->listDefinitions[$definition])) {
throw new ApplicationException(Lang::get('backend::lang.list.missing_parent_definition', compact('definition')));
}
$listConfig = $this->makeConfig($this->listDefinitions[$definition], $this->requiredConfig);
/*
* Create the model
*/
$class = $listConfig->modelClass;
$model = new $class;
$model = $this->controller->listExtendModel($model, $definition);
/*
* Create the query
*/
$query = $model->newQuery();
$this->controller->listExtendQueryBefore($query, $definition);
$query->whereIn($model->getKeyName(), $checkedIds);
$this->controller->listExtendQuery($query, $definition);
/*
* Delete records
*/
$records = $query->get();
if ($records->count()) {
foreach ($records as $record) {
$record->delete();
}
Flash::success(Lang::get('backend::lang.list.delete_selected_success'));
}
else {
Flash::error(Lang::get('backend::lang.list.delete_selected_empty'));
}
return $this->controller->listRefresh();
}

View File

@ -125,6 +125,7 @@ return [
'missing_column' => 'There are no column definitions for :columns.',
'missing_columns' => 'List used in :class has no list columns defined.',
'missing_definition' => "List behavior does not contain a column for ':field'.",
'missing_parent_definition' => "List behavior does not contain a definition for ':definition'.",
'behavior_not_ready' => 'List behavior has not been initialized, check that you have called makeLists() in your controller.',
'invalid_column_datetime' => "Column value ':column' is not a DateTime object, are you missing a \$dates reference in the Model?",
'pagination' => 'Displayed records: :from-:to of :total',