Skip to content

Commit

Permalink
Add config panel
Browse files Browse the repository at this point in the history
  • Loading branch information
magicsunday committed Dec 27, 2023
1 parent 9c5fb20 commit 8d50bfd
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 42 deletions.
52 changes: 52 additions & 0 deletions resources/views/modules/descendants-chart/config.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* This file is part of the package magicsunday/webtrees-descendants-chart.
*
* For the full copyright and license information, please read the
* LICENSE file distributed with this source code.
*/

declare(strict_types=1);

use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\Http\RequestHandlers\ModulesAllPage;
use Fisharebest\Webtrees\I18N;
use MagicSunday\Webtrees\DescendantsChart\Configuration;

/**
* @var Configuration $configuration
* @var string $moduleName
* @var string $title
* @var string $description
*/
?>

<?= view('components/breadcrumbs', ['links' => [route(ControlPanel::class) => I18N::translate('Control panel'), route(ModulesAllPage::class) => I18N::translate('Modules'), $title]]) ?>

<h1><?= $title ?></h1>
<p><?= $description ?></p>

<form method="post">
<div class="mb-3">
<?= view($moduleName.'::modules/descendants-chart/form/generations', ['configuration' => $configuration]) ?>
</div>
<div class="mb-3">
<?= view($moduleName.'::modules/descendants-chart/form/orientation', ['configuration' => $configuration]) ?>
</div>

<div class="row mb-3">
<div class="offset-sm-3 col-sm-9">
<button type="submit" class="btn btn-primary">
<?= I18N::translate('save') ?>
</button>

<a href="<?= route(ControlPanel::class) ?>" class="btn btn-secondary">
<?= view('icons/cancel') ?>
<?= I18N::translate('cancel') ?>
</a>
</div>
</div>

<?= csrf_field() ?>
</form>
35 changes: 35 additions & 0 deletions resources/views/modules/descendants-chart/form/generations.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* This file is part of the package magicsunday/webtrees-descendants-chart.
*
* For the full copyright and license information, please read the
* LICENSE file distributed with this source code.
*/

declare(strict_types=1);

use Fisharebest\Webtrees\I18N;
use MagicSunday\Webtrees\DescendantsChart\Configuration;

/**
* @var Configuration $configuration
*/
?>

<div class="form-group row">
<label class="col-sm-3 col-form-label wt-page-options-label" for="generations">
<?= I18N::translate('Generations') ?>
</label>
<div class="col-sm-9 wt-page-options-value">
<?=
view('components/select', [
'id' => 'generations',
'name' => 'generations',
'selected' => $configuration->getGenerations(),
'options' => $configuration->getGenerationsList(),
'class' => 'form-control-sm',
])
?>
</div>
</div>
33 changes: 33 additions & 0 deletions resources/views/modules/descendants-chart/form/orientation.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* This file is part of the package magicsunday/webtrees-descendants-chart.
*
* For the full copyright and license information, please read the
* LICENSE file distributed with this source code.
*/

declare(strict_types=1);

use Fisharebest\Webtrees\I18N;
use MagicSunday\Webtrees\DescendantsChart\Configuration;

/**
* @var Configuration $configuration
*/
?>

<div class="form-group row">
<label class="col-form-label col-sm-3 wt-page-options-label">
<?= I18N::translate('Orientation') ?>
</label>
<div class="col-sm-9 wt-page-options-value" id="layout">
<?=
view('components/radios-inline', [
'name' => 'layout',
'options' => $configuration->getLayouts(),
'selected' => $configuration->getLayout(),
])
?>
</div>
</div>
33 changes: 2 additions & 31 deletions resources/views/modules/descendants-chart/page.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,8 @@ use MagicSunday\Webtrees\DescendantsChart\Configuration;
</div>
</div>

<div class="form-group row">
<label class="col-sm-3 col-form-label wt-page-options-label" for="generations">
<?= I18N::translate('Generations') ?>
</label>
<div class="col-sm-9 wt-page-options-value">
<?=
view('components/select', [
'id' => 'generations',
'name' => 'generations',
'selected' => $configuration->getGenerations(),
'options' => $configuration->getGenerationsList(),
'class' => 'form-control-sm',
])
?>
</div>
</div>

<div class="form-group row">
<label class="col-form-label col-sm-3 wt-page-options-label">
<?= I18N::translate('Orientation') ?>
</label>
<div class="col-sm-9 wt-page-options-value" id="layout">
<?=
view('components/radios-inline', [
'name' => 'layout',
'options' => $configuration->getLayouts(),
'selected' => $configuration->getLayout(),
])
?>
</div>
</div>
<?= view($moduleName.'::modules/descendants-chart/form/generations', ['configuration' => $configuration]) ?>
<?= view($moduleName.'::modules/descendants-chart/form/orientation', ['configuration' => $configuration]) ?>

<div class="collapse" id="showMoreOptions">
<div class="form-group row">
Expand Down
57 changes: 48 additions & 9 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace MagicSunday\Webtrees\DescendantsChart;

use Fig\Http\Message\RequestMethodInterface;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\AbstractModule;
use Fisharebest\Webtrees\Module\PedigreeChartModule;
use Fisharebest\Webtrees\Validator;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -40,28 +42,33 @@ class Configuration
*
* @var int
*/
private const DEFAULT_GENERATIONS = 4;
public const DEFAULT_GENERATIONS = 4;

/**
* Minimum number of displayable generations.
*
* @var int
*/
private const MIN_GENERATIONS = 2;
public const MIN_GENERATIONS = 2;

/**
* Maximum number of displayable generations.
*
* @var int
*/
private const MAX_GENERATIONS = 25;
public const MAX_GENERATIONS = 25;

/**
* Tree layout.
*
* @var string
*/
private const DEFAULT_TREE_LAYOUT = self::LAYOUT_LEFTRIGHT;
public const DEFAULT_TREE_LAYOUT = self::LAYOUT_LEFTRIGHT;

/**
* The calling module.
*/
private AbstractModule $module;

/**
* The current request instance.
Expand All @@ -74,10 +81,12 @@ class Configuration
* Configuration constructor.
*
* @param ServerRequestInterface $request
* @param AbstractModule $module
*/
public function __construct(ServerRequestInterface $request)
public function __construct(ServerRequestInterface $request, AbstractModule $module)
{
$this->request = $request;
$this->module = $module;
}

/**
Expand All @@ -87,9 +96,21 @@ public function __construct(ServerRequestInterface $request)
*/
public function getGenerations(): int
{
return Validator::queryParams($this->request)
if ($this->request->getMethod() === RequestMethodInterface::METHOD_POST) {
$validator = Validator::parsedBody($this->request);
} else {
$validator = Validator::queryParams($this->request);
}

return $validator
->isBetween(self::MIN_GENERATIONS, self::MAX_GENERATIONS)
->integer('generations', self::DEFAULT_GENERATIONS);
->integer(
'generations',
(int) $this->module->getPreference(
'default_generations',
(string) self::DEFAULT_GENERATIONS
)
);
}

/**
Expand All @@ -115,8 +136,26 @@ public function getGenerationsList(): array
*/
public function getLayout(): string
{
return Validator::queryParams($this->request)
->string('layout', self::DEFAULT_TREE_LAYOUT);
if ($this->request->getMethod() === RequestMethodInterface::METHOD_POST) {
$validator = Validator::parsedBody($this->request);
} else {
$validator = Validator::queryParams($this->request);
}

return $validator
->isInArray([
self::LAYOUT_BOTTOMTOP,
self::LAYOUT_LEFTRIGHT,
self::LAYOUT_RIGHTLEFT,
self::LAYOUT_TOPBOTTOM,
])
->string(
'layout',
$this->module->getPreference(
'default_tree_layout',
self::DEFAULT_TREE_LAYOUT
)
);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Module\DescendancyChartModule;
use Fisharebest\Webtrees\Module\ModuleChartInterface;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleThemeInterface;
use Fisharebest\Webtrees\Registry;
Expand All @@ -26,6 +27,7 @@
use MagicSunday\Webtrees\DescendantsChart\Facade\DataFacade;
use MagicSunday\Webtrees\DescendantsChart\Traits\ModuleChartTrait;
use MagicSunday\Webtrees\DescendantsChart\Traits\ModuleCustomTrait;
use MagicSunday\Webtrees\DescendantsChart\Traits\ModuleConfigTrait;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

Expand All @@ -36,10 +38,11 @@
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License v3.0
* @link https://github.com/magicsunday/webtrees-descendants-chart/
*/
class Module extends DescendancyChartModule implements ModuleCustomInterface
class Module extends DescendancyChartModule implements ModuleCustomInterface, ModuleConfigInterface
{
use ModuleCustomTrait;
use ModuleChartTrait;
use ModuleConfigTrait;

public const ROUTE_DEFAULT = 'webtrees-descendants-chart';
public const ROUTE_DEFAULT_URL = '/tree/{tree}/webtrees-descendants-chart/{xref}';
Expand Down Expand Up @@ -179,7 +182,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$individual = Registry::individualFactory()->make($xref, $tree);
$individual = Auth::checkIndividualAccess($individual, false, true);

$this->configuration = new Configuration($request);
$this->configuration = new Configuration($request, $this);

if ($ajax) {
$this->layout = $this->name() . '::layouts/ajax';
Expand Down
73 changes: 73 additions & 0 deletions src/Traits/ModuleConfigTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* This file is part of the package magicsunday/webtrees-descendants-chart.
*
* For the full copyright and license information, please read the
* LICENSE file distributed with this source code.
*/

declare(strict_types=1);

namespace MagicSunday\Webtrees\DescendantsChart\Traits;

use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\I18N;
use MagicSunday\Webtrees\DescendantsChart\Configuration;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
* Trait ModuleConfigTrait.
*
* @author Rico Sonntag <mail@ricosonntag.de>
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License v3.0
* @link https://github.com/magicsunday/webtrees-descendants-chart/
*/
trait ModuleConfigTrait
{
use \Fisharebest\Webtrees\Module\ModuleConfigTrait;

/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function getAdminAction(ServerRequestInterface $request): ResponseInterface
{
$this->layout = 'layouts/administration';

return $this->viewResponse(
$this->name() . '::modules/descendants-chart/config',
[
'configuration' => new Configuration($request, $this),
'moduleName' => $this->name(),
'title' => $this->title(),
'description' => $this->description(),
]
);
}

/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function postAdminAction(ServerRequestInterface $request): ResponseInterface
{
$configuration = new Configuration($request, $this);

$this->setPreference('default_generations', (string) $configuration->getGenerations());
$this->setPreference('default_tree_layout', $configuration->getLayout());

FlashMessages::addMessage(
I18N::translate(
'The preferences for the module “%s” have been updated.',
$this->title()
),
'success'
);

return redirect($this->getConfigLink());
}
}

0 comments on commit 8d50bfd

Please sign in to comment.