Skip to content

Commit

Permalink
Merge pull request #219 from NotFoundNL/develop
Browse files Browse the repository at this point in the history
feat: model editor settings (#218)
  • Loading branch information
64knl authored Mar 8, 2024
2 parents 1cb2a07 + f9c9eb2 commit 4bce578
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 58 deletions.
15 changes: 8 additions & 7 deletions src/Http/Controllers/Assets/TableEditorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function index(Request $request, Table $table, int $recordId, string $lan

$params = sprintf('?page=%d&sort=%s&asc=%s', $request->page ?? 1, $request->sort ?? '', $request->asc ?? '');

$formUrl = sprintf('/table/%s/%s/%s/%s', $table->url, $recordId ?? 0, urlencode($langUrl), $params.$editor->filterToParams());
$formUrl = sprintf('/table/%s/%s/%s/%s', $table->url, $recordId ?? 0, urlencode($langUrl), $params.'&'.$editor->filterParameters());

$form = new LayoutForm($formUrl);

Expand Down Expand Up @@ -108,7 +108,6 @@ public function update(FormDataRequest $request, Table $table, int $recordId, st
}

$tableService = new TableService($table, $lang, $recordId);

$tableService->setRequestParameters($request->query());

$editor = $this->customEditor($table, $tableService);
Expand Down Expand Up @@ -142,8 +141,8 @@ public function update(FormDataRequest $request, Table $table, int $recordId, st
// Stay on page
if ($newTableRecord) {
$url = '/table/'.$table->url.'/'.$id;
if ($params = $editor->filterToParams()) {
$url .= '?'.ltrim($params, '&');
if ($params = $editor->filterParameters()) {
$url .= '?'.$params;
}
$response->addAction(new Redirect($url));
} else {
Expand All @@ -152,9 +151,11 @@ public function update(FormDataRequest $request, Table $table, int $recordId, st
} else {
// Redirect

$params = sprintf('?page=%d&sort=%s&asc=%s', $request->page ?? 1, $request->sort ?? '', $request->asc ?? '');
$params .= $editor->filterToParams();
$response->addAction(new Redirect('/table/'.$table->url.'/?'.$params));
// Get the overview URL from the editor
$editor = $this->customEditor($table, $tableService);
$overviewUrl = $editor->getOverviewUrl();

$response->addAction(new Redirect($overviewUrl));
}

return $response->build();
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/Assets/TableOverviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function index(Request $request, Table $table)
$editor = $this->customEditor($table, $tableService);

foreach ($siteTableRowsPaginator as $row) {
$link = sprintf('/table/%s/%d/?page=%d&sort=%s&asc=%s', $table->url, $row->id, $request->page ?? 1, $request->sort ?? '', $request->asc ?? '').$editor->filterToParams();
$link = sprintf('/table/%s/%d/?page=%d&sort=%s&asc=&%s', $table->url, $row->id, $request->page ?? 1, $request->sort ?? '', $request->asc ?? '').$editor->filterParameters();
$layoutRow = new LayoutTableRow($row->id, link: $link);

foreach ($components as $component) {
Expand Down
50 changes: 2 additions & 48 deletions src/Models/Editor/AbstractEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

use NotFound\Framework\Services\Assets\TableService;
use NotFound\Layout\Elements\LayoutBar;
use NotFound\Layout\Elements\LayoutBarButton;
use NotFound\Layout\Elements\LayoutBreadcrumb;
use NotFound\Layout\Elements\LayoutPager;
use NotFound\Layout\Elements\LayoutSearchBox;

abstract class AbstractEditor
{
Expand All @@ -23,51 +21,7 @@ abstract public function getBreadCrumbs(): LayoutBreadCrumb;

abstract public function getBreadCrumbsEdit(): LayoutBreadCrumb;

public function filters(): array
{
return $this->ts->getRequestParameters('filter') ?? [];
}

public function filterToParams(): string
{
if (empty($this->filters())) {
return '';
}
$filterParams = '';
foreach ($this->filters() as $key => $value) {
$filterParams .= '&filter['.$key.']='.$value;
}

return $filterParams;
}

protected function addNewButton(LayoutBar $bar): LayoutBar
{
$table = $this->ts->getAssetModel();

if (! $table->allow_create) {
return $bar;
}
abstract public function getOverviewUrl(): string;

$addNew = new LayoutBarButton('Nieuw');
$table = $this->ts->getAssetModel();
$addNew->setIcon('plus');
$url = '/table/'.$table->url.'/0';
if ($params = $this->filterToParams()) {
$url .= '?'.ltrim($params, '&');
}
$addNew->setLink($url);

return $bar->addBarButton($addNew);
}

protected function addPager(LayoutBar $bar, LayoutPager $pager): LayoutBar
{
return $bar->addPager($pager);
}

protected function addSearchBox(LayoutBar $bar): LayoutBar
{
return $bar->addSearchBox(new LayoutSearchBox(''));
}
abstract public function filterParameters(): string;
}
29 changes: 27 additions & 2 deletions src/Models/Editor/DefaultEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function addNewButton(LayoutBar $bar): LayoutBar
$table = $this->ts->getAssetModel();
$addNew->setIcon('plus');
$url = '/table/'.$table->url.'/0';
if ($params = $this->filterToParams()) {
if ($params = $this->filterParameters()) {
$url .= '?'.ltrim($params, '&');
}
$addNew->setLink($url);
Expand All @@ -68,9 +68,34 @@ public function getBreadCrumbsEdit(): LayoutBreadCrumb
{
$table = $this->ts->getAssetModel();
$breadcrumb = $this->getBreadCrumbs();
end($breadcrumb->properties->items)->link = '/table/'.$table->url.'/?'.$this->filterToParams();
end($breadcrumb->properties->items)->link = '/table/'.$table->url.'/?'.$this->filterParameters();
$breadcrumb->addItem('edit');

return $breadcrumb;
}

public function getOverviewUrl(): string
{
$table = $this->ts->getAssetModel();
$request = $this->ts->getRequestParameters() ?? [];
$params = sprintf('page=%d&sort=%s&asc=%s', $request['page'] ?? 1, $request['sort'] ?? '', $request['asc'] ?? '');

$url = sprintf('/table/%s/?%s&%s', $table->url, $params, $this->filterParameters());

return $url;
}

public function filterParameters(): string
{
$filters = $this->ts->getRequestParameters('filter') ?? [];
if (empty($filters)) {
return '';
}
$filterParams = [];
foreach ($filters as $key => $value) {
$filterParams[] = 'filter['.$key.']='.urlencode($value);
}

return implode('&', $filterParams);
}
}

0 comments on commit 4bce578

Please sign in to comment.