feat(settings): allow to edit a setting in plain page

This commit is contained in:
Simon Vieille 2025-11-26 15:19:47 +01:00
commit 799ae00730
Signed by: deblan
GPG key ID: 579388D585F70417
12 changed files with 258 additions and 59 deletions

View file

@ -10,6 +10,7 @@
* feat(collection): add delete_attr, add_attr options
* feat(builder): allow to add block between children
* feat(builder): improve UI to add new block
* feat(settings): allow to edit a setting in plain page
## [v1.26.0] - 2025-03-17
### Added

View file

@ -35,6 +35,8 @@ class NavigationSettingAdminController extends AdminController
$session = $request->getSession();
$lastRequestId = sprintf('setting_request_%s_%s', get_class($entity), $entity->getId());
$lastRequest = $session->get($lastRequestId);
$options = $entity->getOptions();
$optionView = $options['view'] ?? 'modal';
if (null !== $lastRequest && !$request->isMethod('POST')) {
$fakeRequest = Request::create(
@ -64,17 +66,19 @@ class NavigationSettingAdminController extends AdminController
$session->set($lastRequestId, $request->request->get('form'));
$this->addFlash('warning', 'The form is not valid.');
return $this->redirect(sprintf(
'%s?data-modal=%s',
$redirectTo,
urlencode($request->getUri())
));
if ($optionView === 'modal') {
return $this->redirect(sprintf(
'%s?data-modal=%s',
$redirectTo,
urlencode($request->getUri())
));
}
}
return $this->render('@Core/setting/navigation_setting_admin/edit.html.twig', [
'form' => $form->createView(),
'entity' => $entity,
'options' => $event->getData()['options'],
'options' => $options,
'redirectTo' => $redirectTo,
]);
}

View file

@ -55,6 +55,8 @@ class SettingAdminController extends AdminController
$session = $request->getSession();
$lastRequestId = sprintf('setting_request_%s_%s', get_class($entity), $entity->getId());
$lastRequest = $session->get($lastRequestId);
$options = $entity->getOptions();
$optionView = $options['view'] ?? 'modal';
if (null !== $lastRequest && !$request->isMethod('POST')) {
$fakeRequest = Request::create(
@ -82,11 +84,13 @@ class SettingAdminController extends AdminController
$session->set($lastRequestId, $request->request->get('form'));
$this->addFlash('warning', 'The form is not valid.');
return $this->redirect(sprintf(
'%s?data-modal=%s',
$redirectTo,
urlencode($request->getUri())
));
if ($optionView === 'modal') {
return $this->redirect(sprintf(
'%s?data-modal=%s',
$redirectTo,
urlencode($request->getUri())
));
}
}
return $this->render('@Core/setting/setting_admin/edit.html.twig', [

View file

@ -26,6 +26,9 @@ class NavigationSetting implements EntityInterface
#[ORM\Column(type: 'text', nullable: true)]
protected $value;
#[ORM\Column(type: 'text', nullable: true)]
protected $options;
#[ORM\ManyToOne(targetEntity: Navigation::class, inversedBy: 'navigationSettings')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
protected $navigation;
@ -94,4 +97,16 @@ class NavigationSetting implements EntityInterface
return $this;
}
public function getOptions()
{
return json_decode($this->options, true) ?? [];
}
public function setOptions(?array $options): self
{
$this->options = json_encode($options ?? []);
return $this;
}
}

View file

@ -25,6 +25,9 @@ class Setting implements EntityInterface
#[ORM\Column(type: 'text', nullable: true)]
protected $value;
#[ORM\Column(type: 'text', nullable: true)]
protected $options;
public function getId(): ?int
{
return $this->id;
@ -77,4 +80,16 @@ class Setting implements EntityInterface
return $this;
}
public function getOptions()
{
return json_decode($this->options, true) ?? [];
}
public function setOptions(?array $options): self
{
$this->options = json_encode($options ?? []);
return $this;
}
}

View file

@ -1,4 +1,5 @@
{% apply spaceless %}
{% block html %}
<!DOCTYPE html>
<html>
<head>
@ -62,4 +63,5 @@
{% endblock %}
</body>
</html>
{% endblock %}
{% endapply %}

View file

@ -1,20 +1,77 @@
<div class="modal-dialog {% if options['view']|default('') == 'large' %}modal-dialog-large{% endif %}">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{ entity.section|trans }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form action="{{ path('admin_navigation_setting_edit', {entity: entity.id, redirectTo: redirectTo}) }}" id="form-entity-edit" method="POST">
{{ include('@Core/setting/navigation_setting_admin/_form.html.twig') }}
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ 'Cancel'|trans }}</button>
<button type="submit" form="form-entity-edit" class="btn btn-primary">{{ 'Save'|trans }}</button>
</div>
</div>
</div>
{% extends '@Core/admin/layout.html.twig' %}
{% set view = entity.options['view']|default('modal') %}
{% block html %}
{% if view == 'modal' %}
<div class="modal-dialog {% if options['view']|default('') == 'large' %}modal-dialog-large{% endif %}">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{ entity.section|trans }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form action="{{ path('admin_setting_edit', {entity: entity.id, redirectTo: redirectTo}) }}" id="form-entity-edit" method="POST">
{{ include('@Core/setting/navigation_setting_admin/_form.html.twig') }}
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ 'Cancel'|trans }}</button>
<button type="submit" form="form-entity-edit" class="btn btn-primary">{{ 'Save'|trans }}</button>
</div>
</div>
</div>
{% else %}
{{ parent() }}
{% endif %}
{% endblock %}
{% block body_class %}has-form{% endblock %}
{% block title %}{{ entity.label }}{% endblock %}
{% block body %}
{% block header %}
<div class="bg-light">
<div class="crud-header">
{% block header_title %}
<h1 class="crud-header-title">{{ entity.label }}</h1>
{% endblock %}
{% block header_actions %}
<div class="crud-header-actions">
<div class="btn-group">
<a href="{{ path('admin_setting_index') }}" class="btn btn-light">
<span class="fa fa-list pr-1"></span>
<span class="d-none d-md-inline">
{{ 'Back to the list'|trans }}
</span>
</a>
<button type="submit" form="form-main" class="btn btn-primary">
<span class="fa fa-save pr-1"></span>
<span class="d-none d-md-inline">
{{ 'Save'|trans|build_string(entity) }}
</span>
</button>
</div>
</div>
{% endblock %}
</div>
</div>
{% endblock %}
{% block form %}
<form action="{{ app.request.uri }}" method="post" id="form-main" enctype="multipart/form-data">
<div class="tab-content">
<div class="tab-pane active">
<div class="tab-form">
{{ include('@Core/setting/navigation_setting_admin/_form.html.twig') }}
</div>
</div>
</div>
</form>
{% endblock %}
{% endblock %}

View file

@ -1,20 +1,77 @@
<div class="modal-dialog {% if options['view']|default('') == 'large' %}modal-dialog-large{% endif %}">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{ entity.section|trans }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form action="{{ path('admin_setting_edit', {entity: entity.id, redirectTo: redirectTo}) }}" id="form-entity-edit" method="POST">
{{ include('@Core/setting/setting_admin/_form.html.twig') }}
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ 'Cancel'|trans }}</button>
<button type="submit" form="form-entity-edit" class="btn btn-primary">{{ 'Save'|trans }}</button>
</div>
</div>
</div>
{% extends '@Core/admin/layout.html.twig' %}
{% set view = entity.options['view']|default('modal') %}
{% block html %}
{% if view == 'modal' %}
<div class="modal-dialog {% if options['view']|default('') == 'large' %}modal-dialog-large{% endif %}">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{ entity.section|trans }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form action="{{ path('admin_setting_edit', {entity: entity.id, redirectTo: redirectTo}) }}" id="form-entity-edit" method="POST">
{{ include('@Core/setting/setting_admin/_form.html.twig') }}
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ 'Cancel'|trans }}</button>
<button type="submit" form="form-entity-edit" class="btn btn-primary">{{ 'Save'|trans }}</button>
</div>
</div>
</div>
{% else %}
{{ parent() }}
{% endif %}
{% endblock %}
{% block body_class %}has-form{% endblock %}
{% block title %}{{ entity.label }}{% endblock %}
{% block body %}
{% block header %}
<div class="bg-light">
<div class="crud-header">
{% block header_title %}
<h1 class="crud-header-title">{{ entity.label }}</h1>
{% endblock %}
{% block header_actions %}
<div class="crud-header-actions">
<div class="btn-group">
<a href="{{ path('admin_setting_index') }}" class="btn btn-light">
<span class="fa fa-list pr-1"></span>
<span class="d-none d-md-inline">
{{ 'Back to the list'|trans }}
</span>
</a>
<button type="submit" form="form-main" class="btn btn-primary">
<span class="fa fa-save pr-1"></span>
<span class="d-none d-md-inline">
{{ 'Save'|trans|build_string(entity) }}
</span>
</button>
</div>
</div>
{% endblock %}
</div>
</div>
{% endblock %}
{% block form %}
<form action="{{ app.request.uri }}" method="post" id="form-main" enctype="multipart/form-data">
<div class="tab-content">
<div class="tab-pane active">
<div class="tab-form">
{{ include('@Core/setting/setting_admin/_form.html.twig') }}
</div>
</div>
</div>
</form>
{% endblock %}
{% endblock %}

View file

@ -22,21 +22,35 @@
</thead>
<tbody>
{% for item in pager %}
{% set view = item.options['view']|default('modal') %}
{% set edit = path('admin_setting_edit', {entity: item.id, redirectTo: app.request.pathInfo}) %}
<tr>
<td class="col-5">
<a href="#" data-modal="{{ edit }}" data-modal-create class="font-weight-bold text-body d-block">
{{ item.label|trans }}
</a>
{% if view == 'modal' %}
<a href="#" data-modal="{{ edit }}" data-modal-create class="font-weight-bold text-body d-block">
{{ item.label }}
</a>
{% else %}
<a href="{{ edit }}" class="font-weight-bold text-body d-block">
{{ item.label }}
</a>
{% endif %}
</td>
<td class="col-5">
<span class="btn btn-light">{{ item.section|trans }}</span>
</td>
<td class="col-2 miw-100 text-right">
<span data-modal="{{ edit }}" data-modal-create class="btn btn-sm btn-primary mr-1">
<span data-modal="{{ edit }}" class="fa fa-edit"></span>
</span>
{% if view == 'modal' %}
<span data-modal="{{ edit }}" data-modal-create class="btn btn-sm btn-primary mr-1">
<span data-modal="{{ edit }}" class="fa fa-edit"></span>
</span>
{% else %}
<a href="{{ edit }}" class="btn btn-sm btn-primary mr-1">
<span class="fa fa-edit"></span>
</a>
{% endif %}
<button type="submit" form="form-delete-{{ item.id }}" class="btn btn-sm btn-danger">
<span class="fa fa-trash"></span>
</button>

View file

@ -54,21 +54,36 @@
</thead>
<tbody>
{% for item in datas.settings %}
{% set view = item.options['view']|default('modal') %}
{% set edit = path('admin_navigation_setting_edit', {entity: item.id, redirectTo: app.request.pathInfo}) %}
<tr data-dblclick="{{ edit }}">
<td class="col-5">
<a href="#" data-modal="{{ edit }}" class="font-weight-bold text-body d-block">
{{ item.label|trans }}
</a>
{% if view == 'modal' %}
<a href="#" data-modal="{{ edit }}" data-modal-create class="font-weight-bold text-body d-block">
{{ item.label }}
</a>
{% else %}
<a href="{{ edit }}" class="font-weight-bold text-body d-block">
{{ item.label }}
</a>
{% endif %}
</td>
<td class="col-5">
<span class="btn btn-light">{{ item.section|trans }}</span>
</td>
<td class="col-2 miw-100 text-right">
<span data-modal="{{ edit }}" class="btn btn-sm btn-primary mr-1">
<span data-modal="{{ edit }}" class="fa fa-edit"></span>
</span>
{% if view == 'modal' %}
<span data-modal="{{ edit }}" data-modal-create class="btn btn-sm btn-primary mr-1">
<span data-modal="{{ edit }}" class="fa fa-edit"></span>
</span>
{% else %}
<a href="{{ edit }}" class="btn btn-sm btn-primary mr-1">
<span class="fa fa-edit"></span>
</a>
{% endif %}
<button type="submit" form="form-delete-{{ item.id }}" class="btn btn-sm btn-danger">
<span class="fa fa-trash"></span>
</button>

View file

@ -24,7 +24,14 @@ class NavigationSettingManager
) {
}
public function init($navigation, string $code, string $section, string $label, $value = null)
public function init(
$navigation,
string $code,
string $section,
string $label,
$value = null,
array $options = [],
)
{
$entity = $this->get($this->getNavigation($navigation), $code);
$isNew = null === $entity;
@ -37,6 +44,7 @@ class NavigationSettingManager
$entity
->setSection($section)
->setLabel($label)
->setOptions($options)
;
if ($isNew) {

View file

@ -21,7 +21,13 @@ class SettingManager
) {
}
public function init(string $code, string $section, string $label, $value = null)
public function init(
string $code,
string $section,
string $label,
$value = null,
array $options = [],
)
{
$entity = $this->get($code);
$isNew = null === $entity;
@ -34,6 +40,7 @@ class SettingManager
$entity
->setSection($section)
->setLabel($label)
->setOptions($options)
;
if ($isNew) {