From: Eduardo Date: Sat, 2 Dec 2023 20:28:18 +0000 (+0100) Subject: generation of forms X-Git-Url: http://git.edufdez.es/?a=commitdiff_plain;h=f4d6a4082b26f2f1e22a4415ea0c9f7b2bc0ce3a;p=thericegoat.com.git generation of forms --- diff --git a/page-generator/generator.php b/page-generator/generator.php index 29a9932..191ed06 100644 --- a/page-generator/generator.php +++ b/page-generator/generator.php @@ -222,6 +222,138 @@ class CodeGenerator return $html; } + private function form_element(array $data, string $type, string $text, array $class): string + { + $html = ""; + $filtered_classes = $this->filter_meta_classes($class); + + // Add additional attributes + $attributes = [ + 'placeholder', + 'autocomplete', + 'required', + 'autofocus', + 'checked', + 'accept', + 'dirname', + 'disabled', + 'max', + 'maxlength', + 'min', + 'minlength', + 'multiple', + 'pattern', + 'readonly', + 'size', + 'src', + 'step', + 'value', + 'name', + 'title', + 'id', + ]; + + switch ($type) { + case 'text': + case 'email': + case 'number': + case 'password': + case 'range': + case 'tel': + case 'url': + case 'color': + case 'submit': + case 'file': + case 'hidden': + case 'image': + case 'button': + case 'date': + case 'datetime-local': + $html .= "class_html_or_empty($filtered_classes); + + foreach ($attributes as $attribute) { + if (isset($data[$attribute])) { + $html .= " $attribute='" . $data[$attribute] . "'"; + } + } + + $html .= ">$text"; + break; + + case 'radio': + case 'checkbox': + $html .= "class_html_or_empty($filtered_classes); + + foreach ($attributes as $attribute) { + if (isset($data[$attribute])) { + $html .= " $attribute='" . $data[$attribute] . "'"; + } + } + + $html .= "/>"; + // pls note: this one continues to the label. this is because the label is not a child of the input but a sibling + case 'label': + $html .= "filter_meta_classes($class); + $html .= $this->class_html_or_empty($filtered_classes); + + foreach ($attributes as $attribute) { + if (isset($data[$attribute])) { + if ($attribute == 'id') { + $html .= " for='" . $data[$attribute] . "'"; + } else { + $html .= " $attribute='" . $data[$attribute] . "'"; + } + } + } + + if (isset($data['url']) && !empty($data['url'])) { + $html .= ">". $this->a($data['url'], $text, $data['title'], $filtered_classes) . ""; + } else { + $html .= ">$text"; + } + + break; + + case 'textarea': + $html .= "class_html_or_empty($filtered_classes); + + foreach ($attributes as $attribute) { + if (isset($data[$attribute])) { + $html .= " $attribute='" . $data[$attribute] . "'"; + } + } + + $html .= ">$text"; + break; + + case 'select': + $html .= "class_html_or_empty($filtered_classes); + + foreach ($attributes as $attribute) { + if (isset($data[$attribute])) { + $html .= " $attribute='" . $data[$attribute] . "'"; + } + } + + $html .= ">$text"; + break; + } + + if (!in_array("same-line", $class)) { + $html .= 'class_html_or_empty($filtered_classes); + $html .= '>'; + } + + return $html; + } + #endregion #region html containers management @@ -234,8 +366,7 @@ class CodeGenerator $is_p_closed = false; // if class not empty add class - $filtered_classes = $this->filter_meta_classes($class); - $html .= $this->class_html_or_empty($filtered_classes); + $html .= $this->class_html_or_empty($this->filter_meta_classes($class)); $html .= '>'; } elseif (!$is_p_closed && !$open) { @@ -246,6 +377,26 @@ class CodeGenerator return [$html, $is_p_closed]; } + private function open_or_close_form(bool $is_form_closed, array | null $class, bool $open): array + { + $html = ""; + + if ($is_form_closed && $open) { + $html .= 'class_html_or_empty($this->filter_meta_classes($class)); + + $html .= '>'; + } elseif (!$is_form_closed && !$open) { + $html .= ''; + $is_form_closed = true; + } + + return [$html, $is_form_closed]; + } + private function open_or_close_ul(bool $is_list_closed, array | null $class, bool $open, string $text): array { $html = ""; @@ -255,9 +406,8 @@ class CodeGenerator $is_list_closed = false; // if class not empty and text empty add class - $filtered_classes = $this->filter_meta_classes($class); if (empty($text)) { - $html .= $this->class_html_or_empty($filtered_classes); + $html .= $this->class_html_or_empty($this->filter_meta_classes($class)); } $html .= '>'; @@ -290,6 +440,7 @@ class CodeGenerator $is_p_closed = true; $is_list_closed = true; $is_div_closed = true; + $is_form_closed = true; foreach ($data as $i => $line) { $section = $this->str_val_or_empty($line, 'section'); @@ -339,6 +490,17 @@ class CodeGenerator break; + case in_array("form", $class): + // looks like this will be a form! + $html .= $this->form_h2_title($data, $i, $section, $url, $title, $class); + [$open_close_tag, $is_form_closed] = $this->open_or_close_form($is_form_closed, $class, OPEN); + $html .= $open_close_tag; + + $html .= $this->form_element($line, $this->str_val_or_empty($line, 'type'), $this->str_val_or_empty($line, 'text'), $class); + //$html .= $this->form_form($line, $class, $url, $title, $this->str_val_or_empty($line, 'element'), $this->str_val_or_empty($line, 'type')); + + break; + default: // if previous iteration was diferent section close p if ($i != 0 && $data[$i - 1]['section'] != $section) { @@ -362,8 +524,9 @@ class CodeGenerator } } - // close last p + // close last elements $html .= $this->open_or_close_p($is_p_closed, null, CLOSE)[0]; + $html .= $this->open_or_close_form($is_form_closed, null, CLOSE)[0]; return $html; } @@ -455,6 +618,14 @@ class CodeGenerator return $html; } + private function form_form(string $text, array $class, string $url, string $title, string $element, string $type): string + { + $html = ""; + $filtered_classes = $this->filter_meta_classes($class); + + return $html; + } + #endregion #region array manipulation utils @@ -463,6 +634,7 @@ class CodeGenerator */ private function filter_meta_classes(array $class): array { + $meta_classes = ["same-line", "no-space", "new-col", "list", "form"]; return array_diff($class, $meta_classes); }