commit fonctionne ou pas ?

This commit is contained in:
val
2022-11-30 17:27:28 +01:00
commit e9dd38c3a8
3397 changed files with 593718 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+38
View File
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);
namespace Grav\Plugin\Form;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Page\Page;
use Grav\Framework\Form\Interfaces\FormFactoryInterface;
use Grav\Framework\Form\Interfaces\FormInterface;
class FormFactory implements FormFactoryInterface
{
/**
* Create form using the header of the page.
*
* @param Page $page
* @param string $name
* @param array $form
* @return Form|null
* @deprecated 1.6 Use FormFactory::createFormByPage() instead.
*/
public function createPageForm(Page $page, string $name, array $form): ?FormInterface
{
return new Form($page, $name, $form);
}
/**
* Create form using the header of the page.
*
* @param PageInterface $page
* @param string $name
* @param array $form
* @return Form|null
*/
public function createFormForPage(PageInterface $page, string $name, array $form): ?FormInterface
{
return new Form($page, $name, $form);
}
}
+129
View File
@@ -0,0 +1,129 @@
<?php
namespace Grav\Plugin\Form;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Page\Page;
use Grav\Framework\Form\Interfaces\FormFactoryInterface;
use Grav\Framework\Form\Interfaces\FormInterface;
class Forms
{
/** @var array|FormFactoryInterface[] */
private $types;
/** @var FormInterface|null */
private $form;
/**
* Forms constructor.
*/
public function __construct()
{
$this->registerType('form', new FormFactory());
}
/**
* @param string $type
* @param FormFactoryInterface $factory
*/
public function registerType(string $type, FormFactoryInterface $factory): void
{
$this->types[$type] = $factory;
}
/**
* @param string $type
*/
public function unregisterType($type): void
{
unset($this->types[$type]);
}
/**
* @param string $type
* @return bool
*/
public function hasType(string $type): bool
{
return isset($this->types[$type]);
}
/**
* @return array
*/
public function getTypes(): array
{
return array_keys($this->types);
}
/**
* @param PageInterface $page
* @param string|null $name
* @param array|null $form
* @return FormInterface|null
*/
public function createPageForm(PageInterface $page, string $name = null, array $form = null): ?FormInterface
{
if (null === $form) {
[$name, $form] = $this->getPageParameters($page, $name);
}
if (null === $form) {
return null;
}
$type = $form['type'] ?? 'form';
$factory = $this->types[$type] ?? null;
if ($factory) {
if (is_callable([$factory, 'createFormForPage'])) {
return $factory->createFormForPage($page, $name, $form);
}
if ($page instanceof Page) {
// @phpstan-ignore-next-line
return $factory->createPageForm($page, $name, $form);
}
}
return null;
}
/**
* @return FormInterface|null
*/
public function getActiveForm(): ?FormInterface
{
return $this->form;
}
/**
* @param FormInterface $form
* @return void
*/
public function setActiveForm(FormInterface $form): void
{
$this->form = $form;
}
/**
* @param PageInterface $page
* @param string|null $name
* @return array
*/
protected function getPageParameters(PageInterface $page, ?string $name): array
{
$forms = $page->forms();
if ($name) {
// If form with given name was found, use that.
$form = $forms[$name] ?? null;
} else {
// Otherwise pick up the first form.
$form = reset($forms) ?: null;
$name = (string)key($forms);
}
return [$name, $form];
}
}
+155
View File
@@ -0,0 +1,155 @@
<?php declare(strict_types=1);
namespace Grav\Plugin\Form;
use Grav\Framework\Form\Interfaces\FormInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use function is_string;
/**
* Class GravExtension
* @package Grav\Common\Twig\Extension
*/
class TwigExtension extends AbstractExtension
{
public function getFilters()
{
return [
new TwigFilter('value_and_label', [$this, 'valueAndLabel'])
];
}
/**
* Return a list of all functions.
*
* @return array
*/
public function getFunctions(): array
{
return [
new TwigFunction('prepare_form_fields', [$this, 'prepareFormFields'], ['needs_context' => true]),
new TwigFunction('prepare_form_field', [$this, 'prepareFormField'], ['needs_context' => true]),
new TwigFunction('include_form_field', [$this, 'includeFormField']),
];
}
public function valueAndLabel($value): array
{
if (!is_array($value)) {
return [];
}
$list = [];
foreach ($value as $key => $label) {
$list[] = ['value' => $key, 'label' => $label];
}
return $list;
}
/**
* Filters form fields for the current parent.
*
* @param array $context
* @param array $fields Form fields
* @param string|null $parent Parent field name if available
* @return array
*/
public function prepareFormFields(array $context, $fields, $parent = null): array
{
$list = [];
if (is_iterable($fields)) {
foreach ($fields as $name => $field) {
$field = $this->prepareFormField($context, $field, $name, $parent);
if ($field) {
$list[$field['name']] = $field;
}
}
}
return $list;
}
/**
* Filters field name by changing dot notation into array notation.
*
* @param array $context
* @param array $field Form field
* @param string|int|null $name Field name (defaults to field.name)
* @param string|null $parent Parent field name if available
* @param array|null $options List of options to override
* @return array|null
*/
public function prepareFormField(array $context, $field, $name = null, $parent = null, array $options = []): ?array
{
// Make sure that the field is a valid form field type and is not being ignored.
if (empty($field['type']) || ($field['validate']['ignore'] ?? false)) {
return null;
}
// Check if we have just a list of fields (no name given).
if (is_int($name)) {
// Look at the field.name and if not set, fall back to the key.
$name = (string)($field['name'] ?? $name);
}
// Make sure that the field has a name.
$name = $name ?? $field['name'] ?? null;
if (!is_string($name) || $name === '') {
return null;
}
// Prefix name with the parent name if needed.
if (str_starts_with($name, '.')) {
$name = $parent ? $parent . $name : (string)substr($name, 1);
} elseif (isset($options['key'])) {
$name = str_replace('*', $options['key'], $name);
}
unset($options['key']);
// Set fields as readonly if form is in readonly mode.
/** @var FormInterface $form */
$form = $context['form'] ?? null;
if ($form && method_exists($form, 'isEnabled') && !$form->isEnabled()) {
$options['disabled'] = true;
}
// Loop through options
foreach ($options as $key => $option) {
$field[$key] = $option;
}
// Always set field name.
$field['name'] = $name;
return $field;
}
/**
* @param string $type
* @param string|string[]|null $layouts
* @param string|null $default
* @return string[]
*/
public function includeFormField(string $type, $layouts = null, string $default = null): array
{
$list = [];
foreach ((array)$layouts as $layout) {
$list[] = "forms/fields/{$type}/{$layout}-{$type}.html.twig";
}
$list[] = "forms/fields/{$type}/{$type}.html.twig";
if ($default) {
foreach ((array)$layouts as $layout) {
$list[] = "forms/fields/{$default}/{$layout}-{$default}.html.twig";
}
$list[] = "forms/fields/{$default}/{$default}.html.twig";
}
return $list;
}
}