add ext-form

This commit is contained in:
2026-11-22 16:09:43 +01:00
parent ef4be66673
commit 932c7d21fe
25 changed files with 2681 additions and 0 deletions
@@ -0,0 +1,29 @@
<?php
namespace Plugi\Extensions\Form\Finishers;
use Plugi\Extensions\Form\DefinitionsRepository;
class DefaultFinisher {
public $formId;
public $form;
public $data;
public function __construct()
{
$formRepository = new DefinitionsRepository();
$this->formId = $_GET['id'] ?? 0;
$this->form = $formRepository->findWithId($this->formId);
$this->data = file_get_contents('php://input');
}
public function execute (): bool {
return true;
}
public function getFormFields (): array {
return json_decode($this->form['fields'], true);
}
public function getEntryFields (): array {
return json_decode($this->data, true)['fields'];
}
public function getFormName (): string {
return $this->form['name'];
}
}
@@ -0,0 +1,22 @@
<?php
namespace Plugi\Extensions\Form\Finishers;
class FinishersRepository {
private $config;
public function __construct()
{
$this->config = require __DIR__ . '/config.php';
}
public function getFinisherConfig (string $key) : array|null
{
return $this->config['finishers'][$key] ?? null;
}
public function finisherExists (string $key) : bool
{
return array_key_exists($key, $this->config['finishers']);
}
}
@@ -0,0 +1,15 @@
<?php
namespace Plugi\Extensions\Form\Finishers;
class SaveEntryToDatabase extends DefaultFinisher
{
public function execute(): bool
{
$entriesRepository = new \Plugi\Extensions\Form\EntriesRepository();
$entriesRepository->create([
'formId' => $this->formId,
'data' => $this->data,
]);
return true;
}
}
@@ -0,0 +1,28 @@
<?php
namespace Plugi\Extensions\Form\Finishers;
use Plugi\Extensions;
class SendEmailToAdmin extends DefaultFinisher
{
public function execute(): bool
{
$to = Extensions::getSettings('form')['email']['value'];
$subject = 'Formulareinsendung: '.$this->getFormName();
$message = $this->getNiceFields();
return mail(
$to,
$subject,
$message,
$additional_headers = [],
$additional_params = ""
);
}
private function getNiceFields(): string {
$text = '';
foreach ($this->getFormFields() as $field) {
$text .= $field['label'] . ': ' . $this->getEntryFields()[$field['name']] . "\n";
}
return $text;
}
}
@@ -0,0 +1,14 @@
<?php
return [
'finishers' => [
'saveToDatabase' => [
'class' => \Plugi\Extensions\Form\Finishers\SaveEntryToDatabase::class,
'label' => 'In Datenbank speichern'
],
'sendMailToAdmin' => [
'class' => \Plugi\Extensions\Form\Finishers\SendEmailToAdmin::class,
'label' => 'Email an Admin senden'
]
]
];