Skip to content

Commit

Permalink
Added set title and redirect url
Browse files Browse the repository at this point in the history
  • Loading branch information
balajidharma committed Nov 23, 2024
1 parent d2b8d2c commit 6514038
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 32 deletions.
5 changes: 4 additions & 1 deletion resources/views/list.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<div class="py-2">
<div class="min-w-full border-base-200 shadow overflow-x-auto">
<form method="GET" action="{{ $routes['index'] }}">
<form method="GET" action="{{ url()->current() }}">
<div class="py-2 flex flex-row-reverse">
<div class="flex">
<input type="search" name="{{$identifier}}search" value="{{ request()->input($identifier.'search') }}" class="input input-bordered w-full max-w-xs" placeholder="{{ __('Search') }}">
Expand Down Expand Up @@ -63,6 +63,9 @@
@can('adminDelete', $item)
@csrf
@method('DELETE')
@if ($redirectUrl)
<input type="hidden" name="_redirect" value="{{ $redirectUrl }}">
@endif
<button class="btn btn-square btn-ghost" onclick="return confirm('{{ __('Are you sure you want to delete?') }}')">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" class="w-5">
<path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0"></path>
Expand Down
126 changes: 95 additions & 31 deletions src/CrudBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Http\Request;

class CrudBuilder
{
Expand Down Expand Up @@ -46,6 +47,8 @@ class CrudBuilder

public $addtional = [];

public $redirectUrl = null;

public function __construct()
{
$this->crudHelper = new CrudHelper;
Expand All @@ -66,6 +69,23 @@ public function setIdentifier()
public function setAddtional($addtional)
{
$this->addtional = array_merge($this->addtional, $addtional);
return $this;
}

public function setTitle($title)
{
$this->title = $title;
return $this;
}

public function setRedirectUrl($redirectUrl = null)
{
if ($redirectUrl) {
$this->redirectUrl = $redirectUrl;
} else {
$this->redirectUrl = url()->current();
}
return $this;
}

public function list($dataProvider)
Expand Down Expand Up @@ -319,20 +339,20 @@ public function buildRoutes($mainRoute = null)
}

return [
'index' => route($mainRoute.'.index'),
'create' => route($mainRoute.'.create'),
'store' => route($mainRoute.'.store'),
'index' => $this->route($mainRoute.'.index'),
'create' => $this->route($mainRoute.'.create'),
'store' => $this->route($mainRoute.'.store'),
'edit' => function ($id) use ($mainRoute) {
return route($mainRoute.'.edit', $id);
return $this->route($mainRoute.'.edit', $id);
},
'update' => function ($id) use ($mainRoute) {
return route($mainRoute.'.update', $id);
return $this->route($mainRoute.'.update', $id);
},
'show' => function ($id) use ($mainRoute) {
return route($mainRoute.'.show', $id);
return $this->route($mainRoute.'.show', $id);
},
'destroy' => function ($id) use ($mainRoute) {
return route($mainRoute.'.destroy', $id);
return $this->route($mainRoute.'.destroy', $id);
},
];
}
Expand All @@ -341,40 +361,35 @@ public function render($view)
{
switch ($view) {
case 'list':
return view('crud::list', [
return view('crud::list', array_merge([
'items' => $this->items,
'fields' => $this->fields,
'routes' => $this->routes,
'title' => $this->title,
'description' => $this->description,
'model' => $this->model,
'identifier' => $this->identifier,
]);
]), $this->commonRenderData());
case 'create':
case 'edit':
return view('crud::edit', [
'fields' => $this->fields,
'routes' => $this->routes,
'title' => $this->title,
'description' => $this->description,
'model' => $this->model,
'identifier' => $this->identifier,
return view('crud::edit', array_merge([
'form' => $this->form,
'mode' => $this->mode,
]);
]), $this->commonRenderData());
case 'show':
return view('crud::show', [
return view('crud::show', array_merge([
'item' => $this->item,
'fields' => $this->fields,
'routes' => $this->routes,
'title' => $this->title,
'description' => $this->description,
'model' => $this->model,
'identifier' => $this->identifier,
]);
]), $this->commonRenderData());
}
}

protected function commonRenderData()
{
return [
'fields' => $this->fields,
'routes' => $this->routes,
'title' => $this->title,
'description' => $this->description,
'model' => $this->model,
'identifier' => $this->identifier,
'redirectUrl' => $this->redirectUrl
];
}

public function buildForm()
{
if ($this->mode == 'create') {
Expand Down Expand Up @@ -413,10 +428,59 @@ public function buildForm()
}
}

if($this->request->input('_redirect')){
$form->add('_redirect', 'hidden', [
'value' => $this->request->input('_redirect'),
]);
}

$form->add('submit', 'submit', [
'label' => $submitLabel,
]);

return $form;
}

public function route($name, $parameters = [], $absolute = true)
{
if ($this->redirectUrl)
{
return $this->mergeQueryParams(route($name, $parameters), ['_redirect' => $this->redirectUrl]);
} else {
return route($name, $parameters);
}
}

public function mergeQueryParams($url, array $parameters = [])
{
if (empty($parameters)) {
return $url;
}

try {
// Create URL instance
$request = Request::create($url);

// Get existing query parameters and merge with new ones
$query = array_merge(
$request->query->all(),
$parameters
);

// Remove null/empty parameters
$query = array_filter($query, function ($value) {
return !is_null($value) && $value !== '';
});

// Build base URL without query string
$baseUrl = explode('?', $url)[0];

// Return URL with merged query parameters
return $query
? $baseUrl . '?' . http_build_query($query)
: $baseUrl;
} catch (\Exception $e) {
return $url;
}
}
}
10 changes: 10 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ function crud(CrudBuilder $crud, $view = 'list')
}

}

if (! function_exists('crudRedirect')) {

function crudRedirect($name, $message)
{
$redirectUrl = request()->input('_redirect', route($name));
return redirect($redirectUrl)->withMessage(__($message));
}

}

0 comments on commit 6514038

Please sign in to comment.