Skip to content

Commit

Permalink
Add config panel
Browse files Browse the repository at this point in the history
  • Loading branch information
papjul committed Mar 20, 2023
1 parent 1d8faf3 commit da5bc94
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 46 deletions.
37 changes: 37 additions & 0 deletions resources/views/modules/pedigree-chart/config.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

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

/**
* @var Configuration $configuration
* @var string $moduleName
* @var string $title
*/

?>

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

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

<form method="post">
<?= view($moduleName.'::modules/pedigree-chart/form/generations', ['configuration' => $configuration]) ?>

<?= view($moduleName.'::modules/pedigree-chart/form/orientation', ['configuration' => $configuration]) ?>

<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>

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

declare(strict_types=1);

use Fisharebest\Webtrees\I18N;
use MagicSunday\Webtrees\PedigreeChart\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>
32 changes: 32 additions & 0 deletions resources/views/modules/pedigree-chart/form/orientation.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

use Fisharebest\Webtrees\I18N;
use MagicSunday\Webtrees\PedigreeChart\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::LAYOUT_RIGHTLEFT => view('icons/pedigree-left') . I18N::translate('left'),
Configuration::LAYOUT_LEFTRIGHT => view('icons/pedigree-right') . I18N::translate('right'),
Configuration::LAYOUT_BOTTOMTOP => view('icons/pedigree-up') . I18N::translate('up'),
Configuration::LAYOUT_TOPBOTTOM => view('icons/pedigree-down') . I18N::translate('down'),
],
'selected' => $configuration->getLayout(),
])
?>
</div>
</div>
37 changes: 2 additions & 35 deletions resources/views/modules/pedigree-chart/page.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,9 @@ use MagicSunday\Webtrees\PedigreeChart\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>
<?= view($moduleName.'::modules/pedigree-chart/form/generations', ['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::LAYOUT_RIGHTLEFT => view('icons/pedigree-left') . I18N::translate('left'),
Configuration::LAYOUT_LEFTRIGHT => view('icons/pedigree-right') . I18N::translate('right'),
Configuration::LAYOUT_BOTTOMTOP => view('icons/pedigree-up') . I18N::translate('up'),
Configuration::LAYOUT_TOPBOTTOM => view('icons/pedigree-down') . I18N::translate('down'),
],
'selected' => $configuration->getLayout(),
])
?>
</div>
</div>
<?= view($moduleName.'::modules/pedigree-chart/form/orientation', ['configuration' => $configuration]) ?>

<div class="collapse" id="showMoreOptions">
<div class="form-group row">
Expand Down
41 changes: 32 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\PedigreeChart;

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 @@ -75,9 +82,10 @@ class Configuration
*
* @param ServerRequestInterface $request
*/
public function __construct(ServerRequestInterface $request)
public function __construct(ServerRequestInterface $request, AbstractModule $module)
{
$this->request = $request;
$this->module = $module;
}

/**
Expand All @@ -87,9 +95,15 @@ 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 Down Expand Up @@ -126,7 +140,16 @@ public function getShowEmptyBoxes(): bool
*/
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));
}
}
7 changes: 5 additions & 2 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Module\ModuleChartInterface;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\PedigreeChartModule;
use Fisharebest\Webtrees\Registry;
Expand All @@ -28,6 +29,7 @@
use MagicSunday\Webtrees\ModuleBase\Processor\ImageProcessor;
use MagicSunday\Webtrees\ModuleBase\Processor\NameProcessor;
use MagicSunday\Webtrees\PedigreeChart\Traits\ModuleChartTrait;
use MagicSunday\Webtrees\PedigreeChart\Traits\ModuleConfigTrait;
use MagicSunday\Webtrees\PedigreeChart\Traits\ModuleCustomTrait;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -39,10 +41,11 @@
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License v3.0
* @link https://github.com/magicsunday/webtrees-pedigree-chart/
*/
class Module extends PedigreeChartModule implements ModuleCustomInterface
class Module extends PedigreeChartModule implements ModuleCustomInterface, ModuleConfigInterface
{
use ModuleCustomTrait;
use ModuleChartTrait;
use ModuleConfigTrait;

private const ROUTE_DEFAULT = 'webtrees-pedigree-chart';
private const ROUTE_DEFAULT_URL = '/tree/{tree}/webtrees-pedigree-chart/{xref}';
Expand Down Expand Up @@ -165,7 +168,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
66 changes: 66 additions & 0 deletions src/Traits/ModuleConfigTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

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

declare(strict_types=1);

namespace MagicSunday\Webtrees\PedigreeChart\Traits;

use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Validator;
use MagicSunday\Webtrees\PedigreeChart\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-pedigree-chart/
*/
trait ModuleConfigTrait
{
use \Fisharebest\Webtrees\Module\ModuleConfigTrait;

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

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

/**
* @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 da5bc94

Please sign in to comment.