generation of forms
authorEduardo <[email protected]>
Sat, 2 Dec 2023 20:28:18 +0000 (21:28 +0100)
committerEduardo <[email protected]>
Sat, 2 Dec 2023 20:28:18 +0000 (21:28 +0100)
page-generator/generator.php

index 29a99320a3c888dc8a1259c0c2bcbb30d00dbb63..191ed06d7ab43c1ed9d353151ad40f7ca4cbd884 100644 (file)
@@ -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 .= "<input type='$type'";
+                $html .= $this->class_html_or_empty($filtered_classes);
+
+                foreach ($attributes as $attribute) {
+                    if (isset($data[$attribute])) {
+                        $html .= " $attribute='" . $data[$attribute] . "'";
+                    }
+                }
+
+                $html .= ">$text</input>";
+                break;
+
+            case 'radio':
+            case 'checkbox':
+                $html .= "<input type='$type'";
+                $html .= $this->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 .= "<label";
+
+                $filtered_classes = $this->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) . "</label>";
+                } else {
+                    $html .= ">$text</label>";
+                }
+
+                break;
+
+            case 'textarea':
+                $html .= "<textarea";
+                $html .= $this->class_html_or_empty($filtered_classes);
+
+                foreach ($attributes as $attribute) {
+                    if (isset($data[$attribute])) {
+                        $html .= " $attribute='" . $data[$attribute] . "'";
+                    }
+                }
+
+                $html .= ">$text</textarea>";
+                break;
+
+            case 'select':
+                $html .= "<select";
+                $html .= $this->class_html_or_empty($filtered_classes);
+
+                foreach ($attributes as $attribute) {
+                    if (isset($data[$attribute])) {
+                        $html .= " $attribute='" . $data[$attribute] . "'";
+                    }
+                }
+
+                $html .= ">$text</select>";
+                break;
+        }
+
+        if (!in_array("same-line", $class)) {
+            $html .= '<br';
+            $html .= $this->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 .= '<form';
+            $is_form_closed = false;
+
+            // if class not empty add class
+            $html .= $this->class_html_or_empty($this->filter_meta_classes($class));
+
+            $html .= '>';
+        } elseif (!$is_form_closed && !$open) {
+            $html .= '</form>';
+            $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);
     }