commit 0e2d1cd54abfcf359025e498f0bf26e474c665f1 Author: Lewin Probst Date: Wed Mar 18 12:29:36 2020 +0100 Initial version, untested. diff --git a/etc/config.php b/etc/config.php new file mode 100644 index 0000000..b248749 --- /dev/null +++ b/etc/config.php @@ -0,0 +1,5 @@ + "form", +]; diff --git a/module.php b/module.php new file mode 100644 index 0000000..8dd6263 --- /dev/null +++ b/module.php @@ -0,0 +1,148 @@ + 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 { + } +};