-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(smart-actions): added smart-actions support (#22)
- Loading branch information
1 parent
9a4bca1
commit bdef099
Showing
23 changed files
with
1,698 additions
and
362 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace ForestAdmin\LaravelForestAdmin\Http\Controllers; | ||
|
||
use ForestAdmin\LaravelForestAdmin\Services\SmartActions\SmartAction; | ||
use ForestAdmin\LaravelForestAdmin\Utils\Traits\Schema; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
use Illuminate\Contracts\Container\BindingResolutionException; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Routing\Route; | ||
use Illuminate\Support\Str; | ||
use Psr\Container\ContainerExceptionInterface; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
|
||
/** | ||
* Class SmartActionController | ||
* | ||
* @package Laravel-forestadmin | ||
* @license GNU https://www.gnu.org/licences/licences.html | ||
* @link https://github.com/ForestAdmin/laravel-forestadmin | ||
*/ | ||
class SmartActionController extends ForestController | ||
{ | ||
/** | ||
* @var Model $collection | ||
*/ | ||
protected Model $collection; | ||
|
||
/** | ||
* @var SmartAction | ||
*/ | ||
protected SmartAction $smartAction; | ||
|
||
/** | ||
* @param Route $route | ||
* @return JsonResponse | ||
* @throws \Exception | ||
*/ | ||
public function __invoke(Route $route) | ||
{ | ||
[$collection, $name] = explode('_', $route->parameter('action')); | ||
$this->collection = Schema::getModel($collection); | ||
$this->smartAction = $this->collection->getSmartAction($name); | ||
|
||
if ($type = $route->parameter('hook')) { | ||
return $type === 'load' ? $this->executeLoadHook() : $this->executeChangeHook(); | ||
} else { | ||
return $this->executeAction(); | ||
} | ||
} | ||
|
||
/** | ||
* @return JsonResponse | ||
* @throws AuthorizationException | ||
*/ | ||
public function executeAction(): JsonResponse | ||
{ | ||
$this->authorize('smartAction', [$this->collection, Str::slug($this->smartAction->getKey())]); | ||
|
||
return response()->json( | ||
call_user_func($this->smartAction->getExecute()) | ||
); | ||
} | ||
|
||
/** | ||
* @return JsonResponse | ||
*/ | ||
public function executeLoadHook(): JsonResponse | ||
{ | ||
return response()->json( | ||
[ | ||
'fields' => array_values( | ||
$this->smartAction | ||
->getLoad() | ||
->call($this->smartAction) | ||
) | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return JsonResponse | ||
* @throws \Exception | ||
*/ | ||
public function executeChangeHook(): JsonResponse | ||
{ | ||
return response()->json( | ||
[ | ||
'fields' => array_values( | ||
$this->smartAction | ||
->getChange(request()->input('data.attributes.changed_field')) | ||
->call($this->smartAction->mergeRequestFields(request()->input('data.attributes.fields'))) | ||
) | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.