¨4.0.1¨

This commit is contained in:
¨NW¨
2023-12-03 14:07:47 +00:00
parent c08b36d1b6
commit f35052522d
1112 changed files with 43019 additions and 24987 deletions

View File

@@ -3,16 +3,21 @@
namespace Modules\Admin\Traits;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\Admin\Ui\AdminTable;
use Modules\Support\Eloquent\Model;
use Modules\Support\Search\Searchable;
use Modules\Admin\Ui\Facades\TabManager;
use Illuminate\Database\Eloquent\Model as EloquentModel;
trait HasCrudActions
{
/**
* Display a listing of the resource.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
* @param Request $request
*
* @return Response
*/
public function index(Request $request)
{
@@ -24,32 +29,14 @@ trait HasCrudActions
->get();
}
if ($request->has('table')) {
return $this->getModel()->table($request);
}
return view("{$this->viewPath}.index");
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$data = array_merge([
'tabs' => TabManager::get($this->getModel()->getTable()),
$this->getResourceName() => $this->getModel(),
], $this->getFormData('create'));
return view("{$this->viewPath}.create", $data);
}
/**
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
* @return Response
*/
public function store()
{
@@ -65,15 +52,42 @@ trait HasCrudActions
return $this->redirectTo($entity);
}
if (request()->wantsJson()) {
return response()->json(
[
'success' => true,
'message' => trans('admin::messages.resource_created', ['resource' => $this->getLabel()]),
], 200
);
}
return redirect()->route("{$this->getRoutePrefix()}.index")
->withSuccess(trans('admin::messages.resource_saved', ['resource' => $this->getLabel()]));
->withSuccess(trans('admin::messages.resource_created', ['resource' => $this->getLabel()]));
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$data = array_merge([
'tabs' => TabManager::get($this->getModel()->getTable()),
$this->getResourceName() => $this->getModel(),
], $this->getFormData('create'));
return view("{$this->viewPath}.create", $data);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*
* @return Response
*/
public function show($id)
{
@@ -86,11 +100,13 @@ trait HasCrudActions
return view("{$this->viewPath}.show")->with($this->getResourceName(), $entity);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*
* @return Response
*/
public function edit($id)
{
@@ -99,14 +115,17 @@ trait HasCrudActions
$this->getResourceName() => $this->getEntity($id),
], $this->getFormData('edit', $id));
return view("{$this->viewPath}.edit", $data);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*
* @return Response
*/
public function update($id)
{
@@ -118,24 +137,39 @@ trait HasCrudActions
$this->getRequest('update')->all()
);
$entity->withoutEvents(function () use ($entity) {
$entity->touch();
});
$this->searchable($entity);
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo($entity)
->withSuccess(trans('admin::messages.resource_saved', ['resource' => $this->getLabel()]));
->withSuccess(trans('admin::messages.resource_updated', ['resource' => $this->getLabel()]));
}
if (request()->wantsJson()) {
return response()->json(
[
'success' => true,
'message' => trans('admin::messages.resource_updated', ['resource' => $this->getLabel()]),
], 200
);
}
return redirect()->route("{$this->getRoutePrefix()}.index")
->withSuccess(trans('admin::messages.resource_saved', ['resource' => $this->getLabel()]));
->withSuccess(trans('admin::messages.resource_updated', ['resource' => $this->getLabel()]));
}
/**
* Destroy resources by given ids.
*
* @param string $ids
*
* @return void
*/
public function destroy($ids)
public function destroy(string $ids): void
{
$this->getModel()
->withoutGlobalScope('active')
@@ -143,13 +177,153 @@ trait HasCrudActions
->delete();
}
/**
* Prepare the table response for the resource.
*
* @param Request $request
*
* @return AdminTable
*/
public function table(Request $request): AdminTable
{
return $this->getModel()->table($request);
}
/**
* Get a new instance of the model.
*
* @return Model
*/
protected function getModel()
{
return new $this->model;
}
/**
* Disable search syncing for the entity.
*
* @return void
*/
protected function disableSearchSyncing(): void
{
if ($this->isSearchable()) {
$this->getModel()->disableSearchSyncing();
}
}
/**
* Determine if the entity is searchable.
*
* @return bool
*/
protected function isSearchable(): bool
{
return in_array(Searchable::class, class_uses_recursive($this->getModel()));
}
/**
* Get name of the resource.
*
* @return string
*/
protected function getResourceName(): string
{
return match (true) {
isset($this->resourceName) => $this->resourceName,
default => lcfirst(class_basename($this->model))
};
}
/**
* Get form data for the given action.
*
* @param string $action
* @param mixed ...$args
*
* @return array
*/
protected function getFormData(string $action, ...$args): array
{
return match (true) {
method_exists($this, 'formData') => $this->formData(...$args),
($action === 'create' && method_exists($this, 'createFormData')) => $this->createFormData(),
($action === 'edit' && method_exists($this, 'editFormData')) => $this->editFormData(...$args),
default => []
};
}
/**
* Get request object
*
* @param string $action
*
* @return Request
*/
protected function getRequest(string $action): Request
{
return match (true) {
!isset($this->validation) => request(),
isset($this->validation[$action]) => resolve($this->validation[$action]),
default => resolve($this->validation),
};
}
/**
* Make the given model instance searchable.
*
* @param $entity
*
* @return void
*/
protected function searchable($entity): void
{
if ($this->isSearchable()) {
$entity->searchable();
}
}
/**
* Get label of the resource.
*
* @return void
*/
protected function getLabel(): string
{
return trans($this->label);
}
/**
* Get route prefix of the resource.
*
* @return string
*/
protected function getRoutePrefix(): string
{
return match (true) {
isset($this->routePrefix) => $this->routePrefix,
default => "admin.{$this->getModel()->getTable()}"
};
}
/**
* Get an entity by the given id.
*
* @param int $id
* @return \Illuminate\Database\Eloquent\Model
*
* @return EloquentModel
*/
protected function getEntity($id)
protected function getEntity(int|string $id): EloquentModel
{
return $this->getModel()
->with($this->relations())
@@ -157,142 +331,20 @@ trait HasCrudActions
->findOrFail($id);
}
/**
* Get the relations that should be eager loaded.
*
* @return array
*/
private function relations()
private function relations(): array
{
return collect($this->with ?? [])->mapWithKeys(function ($relation) {
return [$relation => function ($query) {
return $query->withoutGlobalScope('active');
}];
})->all();
}
/**
* Get form data for the given action.
*
* @param string $action
* @param mixed ...$args
* @return array
*/
protected function getFormData($action, ...$args)
{
if (method_exists($this, 'formData')) {
return $this->formData(...$args);
}
if ($action === 'create' && method_exists($this, 'createFormData')) {
return $this->createFormData();
}
if ($action === 'edit' && method_exists($this, 'editFormData')) {
return $this->editFormData(...$args);
}
return [];
}
/**
* Get name of the resource.
*
* @return string
*/
protected function getResourceName()
{
if (isset($this->resourceName)) {
return $this->resourceName;
}
return lcfirst(class_basename($this->model));
}
/**
* Get label of the resource.
*
* @return void
*/
protected function getLabel()
{
return trans($this->label);
}
/**
* Get route prefix of the resource.
*
* @return string
*/
protected function getRoutePrefix()
{
if (isset($this->routePrefix)) {
return $this->routePrefix;
}
return "admin.{$this->getModel()->getTable()}";
}
/**
* Get a new instance of the model.
*
* @return \Modules\Support\Eloquent\Model
*/
protected function getModel()
{
return new $this->model;
}
/**
* Get request object
*
* @param string $action
* @return \Illuminate\Http\Request
*/
protected function getRequest($action)
{
if (! isset($this->validation)) {
return request();
}
if (isset($this->validation[$action])) {
return resolve($this->validation[$action]);
}
return resolve($this->validation);
}
/**
* Disable search syncing for the entity.
*
* @return void
*/
protected function disableSearchSyncing()
{
if ($this->isSearchable()) {
$this->getModel()->disableSearchSyncing();
}
}
/**
* Determine if the entity is searchable.
*
* @return bool
*/
protected function isSearchable()
{
return in_array(Searchable::class, class_uses_recursive($this->getModel()));
}
/**
* Make the given model instance searchable.
*
* @return void
*/
protected function searchable($entity)
{
if ($this->isSearchable($entity)) {
$entity->searchable();
}
return collect($this->with ?? [])->mapWithKeys(
function ($relation) {
return [$relation => function ($query) {
return $query->withoutGlobalScope('active');
}];
}
)->all();
}
}