diff --git a/src/Actions/SingleResourceAction.php b/src/Actions/SingleResourceAction.php
new file mode 100644
index 0000000..dfbfd24
--- /dev/null
+++ b/src/Actions/SingleResourceAction.php
@@ -0,0 +1,32 @@
+title = $title;
+ $action->icon = $icon;
+ return $action;
+ }
+
+ abstract public function handle();
+
+ public function getClassForJs()
+ {
+ return str_replace('\\', '\\\\', get_class($this));
+ }
+
+
+}
diff --git a/src/Controllers/ThrustActionsController.php b/src/Controllers/ThrustActionsController.php
index 4005470..6096cd6 100644
--- a/src/Controllers/ThrustActionsController.php
+++ b/src/Controllers/ThrustActionsController.php
@@ -12,12 +12,11 @@ public function toggle($resourceName, $id, $field)
{
$resource = Thrust::make($resourceName);
$object = $resource->find($id);
+ if(! method_exists($object, 'toggleActive')){
+ $object->update([$field => !$object->{$field}]);
+ return back();
+ }
try {
- if(! method_exists($object, 'toggleActive')){
- $object->update([$field => !$object->{$field}]);
- return back();
- }
-
$object->toggleActive();
return back();
}catch(\Exception $e){
@@ -51,7 +50,41 @@ public function perform($resourceName)
$ids = is_string(request('ids')) ? explode(',', request('ids')) : request('ids');
try {
- $response = $action->handle(collect($action->resource->find($ids)));
+ return $this->handleResponse($action, $action->handle(collect($action->resource->find($ids))));
+ } catch (\Exception $e) {
+ return $this->handleException($e);
+ }
+ }
+
+ private function handleException(\Exception $e) {
+ return request()->ajax()
+ ? response()->json([
+ 'ok' => false,
+ 'message' => $e->getMessage(),
+ 'shouldReload' => false,
+ 'responseAsPopup' => false
+ ])
+ : back()->withErrors(['msg' => $e->getMessage()]);
+ }
+
+ private function handleResponse($action, $response){
+ if (request()->ajax()) {
+ return response()->json([
+ 'ok' => true,
+ 'message' => $response ?? 'done',
+ 'shouldReload' => $action->shouldReload,
+ 'responseAsPopup' => $action->responseAsPopup
+ ]);
+ }
+
+ return back()->withMessage($response);
+ }
+
+ public function singleResourcePerform($resourceName)
+ {
+ $action = $this->findActionForResource($resourceName, request('action'));
+ try {
+ $response = $action->handle();
} catch (\Exception $e) {
return request()->ajax() ?
response()->json(['ok' => false, 'message' => $e->getMessage(), 'shouldReload' => false, 'responseAsPopup' => false]) :
diff --git a/src/Controllers/ThrustSingleResourceActionsController.php b/src/Controllers/ThrustSingleResourceActionsController.php
new file mode 100644
index 0000000..e45ea42
--- /dev/null
+++ b/src/Controllers/ThrustSingleResourceActionsController.php
@@ -0,0 +1,64 @@
+findActionForResource($resourceName, request('action'));
+ try {
+ return $this->handleResponse($action, $action->handle());
+ } catch (\Exception $e) {
+ return $this->handleException($e);
+ }
+ }
+
+ private function handleException(\Exception $e) {
+ return request()->ajax()
+ ? response()->json([
+ 'ok' => false,
+ 'message' => $e->getMessage(),
+ 'shouldReload' => false,
+ 'responseAsPopup' => false
+ ])
+ : back()->withErrors(['msg' => $e->getMessage()]);
+ }
+
+ private function handleResponse($action, $response){
+ if (request()->ajax()) {
+ return response()->json([
+ 'ok' => true,
+ 'message' => $response ?? 'done',
+ 'shouldReload' => $action->shouldReload,
+ 'responseAsPopup' => $action->responseAsPopup
+ ]);
+ }
+
+ return back()->withMessage($response);
+ }
+
+ private function findActionForResource($resourceName, $actionClass)
+ {
+ $resource = Thrust::make($resourceName);
+ $action = collect($resource->singleResourceActions())->first(function ($action) use ($actionClass) {
+ return get_class($action) === $actionClass;
+ });
+
+ $action->resource = request('search') && $resource::$searchResource
+ ? Thrust::make($resource::$searchResource)
+ : $resource;
+
+ return $action;
+ }
+
+ private function searchingInResource(): bool
+ {
+ return (bool) request('search', false);
+ }
+}
diff --git a/src/Html/Edit.php b/src/Html/Edit.php
index 94f570a..99b6936 100644
--- a/src/Html/Edit.php
+++ b/src/Html/Edit.php
@@ -54,19 +54,20 @@ public function show($id, $fullPage = false, $multiple = false, $inline = false)
view()->share('fullPage', $fullPage);
$object = is_numeric($id) ? $this->resource->find($id) : $id;
return view('thrust::edit', [
- 'title' => $this->resource->getTitle(),
- 'nameField' => $this->resource->nameField,
- 'breadcrumbs' => $this->resource->breadcrumbs($object),
- 'resourceName' => $this->resourceName ? : $this->resource->name(),
+ 'resource' => $this->resource,
+ 'title' => $this->resource->getTitle(),
+ 'nameField' => $this->resource->nameField,
+ 'breadcrumbs' => $this->resource->breadcrumbs($object),
+ 'resourceName' => $this->resourceName ? : $this->resource->name(),
'fields' => $this->getEditFields($multiple),
- 'object' => $object,
- 'hideVisibility' => Field::getPanelHideVisibilityJson(collect($this->resource->panels($object))),
- 'showVisibility' => Field::getPanelShowVisibilityJson(collect($this->resource->panels($object))),
- 'fullPage' => $fullPage,
+ 'object' => $object,
+ 'hideVisibility' => Field::getPanelHideVisibilityJson(collect($this->resource->panels($object))),
+ 'showVisibility' => Field::getPanelShowVisibilityJson(collect($this->resource->panels($object))),
+ 'fullPage' => $fullPage,
'updateConfirmationMessage' => $this->resource->getUpdateConfirmationMessage(),
- 'multiple' => $multiple,
- 'eventListeners' => $this->resource->getValidationEventListeners(),
- 'inline' => $inline,
+ 'multiple' => $multiple,
+ 'eventListeners' => $this->resource->getValidationEventListeners(),
+ 'inline' => $inline,
])->render();
}
diff --git a/src/Resource.php b/src/Resource.php
index b38fa24..b11a0e6 100644
--- a/src/Resource.php
+++ b/src/Resource.php
@@ -333,6 +333,10 @@ public function mainActions()
];
}
+ public function singlePageActions(){
+ return [];
+ }
+
public function actions()
{
return $this->canDelete(static::$model)
diff --git a/src/resources/views/actions/singleResourceActions.blade.php b/src/resources/views/actions/singleResourceActions.blade.php
new file mode 100644
index 0000000..55a6cc4
--- /dev/null
+++ b/src/resources/views/actions/singleResourceActions.blade.php
@@ -0,0 +1,37 @@
+@if(count($resource->singleResourceActions()))
+