diff --git a/module.php b/module.php index 8dd6263..5a62f83 100644 --- a/module.php +++ b/module.php @@ -1,12 +1,12 @@ false, self::OPTIONS_ELEMENT => false, self::OPTIONS_CONTENT => false, + self::OPTIONS_LABEL => false, ]; private $element_id = 0; @@ -44,15 +45,50 @@ return new class extends Lewp\Module return true; } + private function generateFormElementName(bool $increase_element_id): string + { + return implode( + '-', + [ + $this->getModuleName(), + $this->getExecutionCount(), + ($increase_element_id) ? $this->element_id++ : $this->element_id, + ] + ); + } + + private function createFormElementGroup(array $element) + { + if (!isset($element[self::OPTIONS_LABEL])) { + return $this->createFormElement($element); + } else { + $label = $this->createLabel($element); + } + + $wrapper = $this->createAndSetupElement( + 'div', + '', + [ + 'class' => 'element-container' + ] + ); + $wrapper->appendChild($label); + + $form_element = $this->createFormElement($element); + + $wrapper->appendChild($form_element); + return $wrapper; + } + private function createFormElement(array $element) { $element += [self::OPTIONS_CONTENT => '']; if ($this->canContainData($element)) { - $element["name"] = implode('-', [ - $this->getModuleName(), - $this->getExecutionCount(), - $this->element_id++, - ]); + // if no label given, the element id should be increased + $element["name"] = $this->generateFormElementName( + true//!array_key_exists($element[self::OPTIONS_LABEL]) + ); + $element["id"] = $element["name"]; } else { unset($element["name"]); } @@ -62,27 +98,42 @@ return new class extends Lewp\Module $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)); + $form_element->appendChild($this->createFormElementGroup($element)); } return $form_element; } + private function createLabel(array $element) + { + + $label = $this->createAndSetupElement( + 'label', + $element[self::OPTIONS_LABEL], + [ + 'for' => $this->generateFormElementName(false), + ] + ); + return $label; + } + private function setupForm(array $elements) { foreach ($elements as $element) { - $this->appendChild($this->createFormElement($element)); + $this->appendChild($this->createFormElementGroup($element)); } } - private function collectFormKeys() { + private function collectFormKeys() + { return preg_grep( - '/'. + '/' . $this->getModuleName() - .'-' - .$this->getExecutionCount() - .'-\d+' - .'/', + . '-' + . $this->getExecutionCount() + . '-\d+' + . '/', array_keys($_POST) ); } @@ -98,13 +149,13 @@ return new class extends Lewp\Module public function getData() { if (!$this->isSubmitted()) { - return null; + return []; } $data = []; $keys = $this->collectFormKeys(); sort($keys, SORT_NUMERIC); foreach ($keys as $key) { - $data []= $_POST[$key]; + $data [] = $_POST[$key]; } return $data; } @@ -112,10 +163,10 @@ return new class extends Lewp\Module private function processAction($action) { if ($action === '/') { - return '/'.$this->getLanguage(); + return '/' . $this->getLanguage(); } if (strpos($action, '/') === 0) { - return '/'.$this->getLanguage().$action; + return '/' . $this->getLanguage() . $action; } return implode('/', [ '',