Initial version, untested.
This commit is contained in:
commit
0e2d1cd54a
2 changed files with 153 additions and 0 deletions
5
etc/config.php
Normal file
5
etc/config.php
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
"element_type_module" => "form",
|
||||||
|
];
|
||||||
148
module.php
Normal file
148
module.php
Normal file
|
|
@ -0,0 +1,148 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Lewp\Resolve;
|
||||||
|
use Lewp\VarFolder;
|
||||||
|
|
||||||
|
return new class extends Lewp\Module
|
||||||
|
{
|
||||||
|
const OPTIONS_ELEMENTS = 'elements';
|
||||||
|
const OPTIONS_ELEMENT = 'element';
|
||||||
|
|
||||||
|
// element specific
|
||||||
|
const OPTIONS_SHOW_PLACEHOLDER = 'show_placeholder';
|
||||||
|
const OPTIONS_TYPE = 'type';
|
||||||
|
const OPTIONS_CONTENT = 'content';
|
||||||
|
|
||||||
|
// form generics
|
||||||
|
const OPTIONS_METHOD = 'method';
|
||||||
|
const OPTIONS_ACTION = 'action';
|
||||||
|
|
||||||
|
private $non_attribute_options = [
|
||||||
|
self::OPTIONS_ELEMENTS => false,
|
||||||
|
self::OPTIONS_ELEMENT => false,
|
||||||
|
self::OPTIONS_CONTENT => false,
|
||||||
|
];
|
||||||
|
|
||||||
|
private $element_id = 0;
|
||||||
|
|
||||||
|
private function canContainData(array $element)
|
||||||
|
{
|
||||||
|
$non_data_elements = [
|
||||||
|
[
|
||||||
|
self::OPTIONS_ELEMENT => "button"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
self::OPTIONS_ELEMENT => "input",
|
||||||
|
self::OPTIONS_TYPE => "submit",
|
||||||
|
],
|
||||||
|
];
|
||||||
|
foreach ($non_data_elements as $non) {
|
||||||
|
if (count(array_intersect($non, $element)) == count($non)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createFormElement(array $element)
|
||||||
|
{
|
||||||
|
$element += [self::OPTIONS_CONTENT => ''];
|
||||||
|
if ($this->canContainData($element)) {
|
||||||
|
$element["name"] = implode('-', [
|
||||||
|
$this->getModuleName(),
|
||||||
|
$this->getExecutionCount(),
|
||||||
|
$this->element_id++,
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
unset($element["name"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$form_element = $this->createAndSetupElement(
|
||||||
|
$element[self::OPTIONS_ELEMENT],
|
||||||
|
$element[self::OPTIONS_CONTENT],
|
||||||
|
array_diff_key($element, $this->non_attribute_options)
|
||||||
|
);
|
||||||
|
foreach ($element[self::OPTIONS_ELEMENTS] as $element) {
|
||||||
|
$form_element->appendChild($this->createFormElement($element));
|
||||||
|
}
|
||||||
|
return $form_element;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setupForm(array $elements)
|
||||||
|
{
|
||||||
|
foreach ($elements as $element) {
|
||||||
|
$this->appendChild($this->createFormElement($element));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function collectFormKeys() {
|
||||||
|
return preg_grep(
|
||||||
|
'/'.
|
||||||
|
$this->getModuleName()
|
||||||
|
.'-'
|
||||||
|
.$this->getExecutionCount()
|
||||||
|
.'-\d+'
|
||||||
|
.'/',
|
||||||
|
array_keys($_POST)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief IMPORTANT: Needs to be called AFTER setupForm to work correctly!
|
||||||
|
*/
|
||||||
|
private function isSubmitted()
|
||||||
|
{
|
||||||
|
return count($this->collectFormKeys()) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getData()
|
||||||
|
{
|
||||||
|
if (!$this->isSubmitted()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$data = [];
|
||||||
|
$keys = $this->collectFormKeys();
|
||||||
|
sort($keys, SORT_NUMERIC);
|
||||||
|
foreach ($keys as $key) {
|
||||||
|
$data []= $_POST[$key];
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function processAction($action)
|
||||||
|
{
|
||||||
|
if ($action === '/') {
|
||||||
|
return '/'.$this->getLanguage();
|
||||||
|
}
|
||||||
|
if (strpos($action, '/') === 0) {
|
||||||
|
return '/'.$this->getLanguage().$action;
|
||||||
|
}
|
||||||
|
return implode('/', [
|
||||||
|
'',
|
||||||
|
$this->getLanguage(),
|
||||||
|
($action ?? Resolve::idToUri($this->getPageId())),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run(array $options = []) : bool
|
||||||
|
{
|
||||||
|
// SETUP FORM ATTRIBUTES
|
||||||
|
$this->setAttribute('method', $options[self::OPTIONS_METHOD] ?? 'POST');
|
||||||
|
$this->setAttribute(
|
||||||
|
'action',
|
||||||
|
$this->processAction($options[self::OPTIONS_ACTION]) ?? '/'
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
$this->setupForm($options[self::OPTIONS_ELEMENTS]);
|
||||||
|
if ($this->isSubmitted()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onWebsocketRequest(
|
||||||
|
\Lewp\Websocket\Message $message
|
||||||
|
) : \Lewp\Websocket\Message {
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue