Skip to content
2 changes: 1 addition & 1 deletion resources/assets/open-admin/js/open-admin-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
let url = resource_url;
let data = {_method:'delete'};
admin.ajax.post(url,data,function(data){
resolve(data);
resolve(data.data);
if (navigate_url){
admin.ajax.navigate(navigate_url);
}else{
Expand Down
31 changes: 19 additions & 12 deletions resources/lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,25 @@
'time' => 'Time',
'size' => 'Size',
'listbox' => [
'title_available' => 'Available',
'title_selected' => 'Selected',
'text_total' => 'Showing all {0}',
'text_empty' => 'Empty list',
'filtered' => '{0} / {1}',
'filter_clear' => 'Show all',
'filter_placeholder' => 'Filter',
'title_available' => 'Available',
'title_selected' => 'Selected',
'text_total' => 'Showing all {0}',
'text_empty' => 'Empty list',
'filtered' => '{0} / {1}',
'filter_clear' => 'Show all',
'filter_placeholder' => 'Filter',
],
'grid_items_selected' => '{n} items selected',
'grid_items_selected' => '{n} items selected',

'menu_titles' => [],
'prev' => 'Prev',
'next' => 'Next',
'quick_create' => 'Quick create',
'menu_titles' => [],
'prev' => 'Prev',
'next' => 'Next',
'quick_create' => 'Quick create',
'restore_confirm' => 'Are you sure, you want to restore?',
'yes' => 'Yes',
'restore_success' => 'Restored Successfully',
'restore' => 'Restore',
'batch_restore' => 'Batch Restore',
'batch_restore_confirm' => 'Are you sure, you want to restore all selected?',
'recyclebin' => 'Recycle Bin',
];
7 changes: 5 additions & 2 deletions src/Actions/RowAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,19 @@ public function display($value)
*/
public function render()
{
$this->attribute('title', $this->name());
$attributes = $this->formatAttributes();

$linkClass = ($this->parent->getActionClass() != "OpenAdmin\Admin\Grid\Displayers\Actions\Actions") ? 'dropdown-item' : '';
$icon = $this->getIcon();

if ($href = $this->href()) {
return "<a href='{$href}' class='{$linkClass}'>{$icon}<span class='label'>{$this->name()}</span></a>";
return "<a href='{$href}' class='{$linkClass}' {$attributes}>{$icon}<span class='label'>{$this->name()}</span></a>";
}

$this->addScript();

$attributes = $this->formatAttributes();
$attributes = $this->formatAttributes(); // fetch again to get script attributes

return sprintf(
"<a data-_key='%s' href='javascript:void(0);' class='%s {$linkClass}' {$attributes}>{$icon}<span class='label'>%s</span></a>",
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/SweatAlert2.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getOptions()
{
return [
'swal' => [
'type' => $this->type,
'icon' => $this->type,
'title' => $this->title,
],
];
Expand Down
38 changes: 38 additions & 0 deletions src/Controllers/HasRecycleBin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace OpenAdmin\Admin\Controllers;

use OpenAdmin\Admin\Grid\Actions\Restore;
use OpenAdmin\Admin\Grid\Tools\BatchRestore;

trait HasRecycleBin
{
public function __construct()
{
// register hooks

$this->registerHook('alterGrid', function ($controller, $grid) {
$grid->filter(function ($filter) {
$filter->scope('trashed', __('admin.recyclebin'))->onlyTrashed();
});

$grid->actions(function ($actions) {
if (request('_scope_') == 'trashed') {
$actions->add(new Restore());
$actions->disableView();
$actions->disableEdit();
}

return $actions;
});

$grid->batchActions(function ($batch) {
if (request('_scope_') == 'trashed') {
$batch->add(new BatchRestore());
}
});

return $grid;
});
}
}
6 changes: 2 additions & 4 deletions src/Grid/Actions/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ public function name()

public function addScript()
{
$this->attributes = [
'onclick' => 'admin.resource.delete(event,this)',
'data-url'=> "{$this->getResource()}/{$this->getKey()}",
];
$this->attribute('onclick', 'admin.resource.delete(event,this)');
$this->attribute('data-url', "{$this->getResource()}/{$this->getKey()}");
}

/*
Expand Down
31 changes: 31 additions & 0 deletions src/Grid/Actions/Restore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace OpenAdmin\Admin\Grid\Actions;

use Illuminate\Database\Eloquent\Model;
use OpenAdmin\Admin\Actions\Response;
use OpenAdmin\Admin\Actions\RowAction;

class Restore extends RowAction
{
public $name = '';
public $icon = 'icon-trash-restore';

public function name()
{
return $this->name = __('admin.restore');
}

public function handle(Model $model): Response
{
// $model ...
$model->restore();

return $this->response()->success(__('admin.restore_success'))->refresh();
}

public function dialog()
{
$this->confirm(__('admin.restore_confirm'), '', ['icon'=>'question', 'confirmButtonText'=>__('admin.yes')]);
}
}
6 changes: 3 additions & 3 deletions src/Grid/Concerns/HasActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait HasActions
*
* @var Closure
*/
protected $actionsCallback;
protected $actionsCallback = [];

/**
* Actions column display class.
Expand All @@ -31,7 +31,7 @@ trait HasActions
public function actions($actions)
{
if ($actions instanceof Closure) {
$this->actionsCallback = $actions;
$this->actionsCallback[] = $actions;
}

return $this;
Expand All @@ -52,7 +52,7 @@ public function getActionClass()
return $class;
}

return Grid\Displayers\Acions\Actions::class;
return Grid\Displayers\Actions\Actions::class;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Grid/Displayers/Actions/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ public function display($callback = null)
$callback->call($this, $this);
}

if(is_array($callback)) {
foreach ($callback as $instance) {
if($instance instanceof \Closure) {
$instance->call($this, $this);
}
}
}

if ($this->disableAll) {
return '';
}
Expand Down
31 changes: 31 additions & 0 deletions src/Grid/Tools/BatchRestore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace OpenAdmin\Admin\Grid\Tools;

use Illuminate\Database\Eloquent\Collection;
use OpenAdmin\Admin\Actions\BatchAction;

class BatchRestore extends BatchAction
{
public $name = '';
public $icon = 'icon-trash-restore';

public function name()
{
return $this->name = __('admin.batch_restore');
}

public function handle(Collection $collection)
{
foreach ($collection as $model) {
$model->restore();
}

return $this->response()->success(__('admin.restore_success'))->refresh();
}

public function dialog()
{
$this->confirm(__('admin.batch_restore_confirm'), '', ['icon'=>'question', 'confirmButtonText'=>__('admin.yes')]);
}
}