From 2b4ed611e7e001ad8ea93ef3e8cdce35bc56dc07 Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Mon, 1 May 2023 04:33:06 +0300 Subject: form designer; setup main forms of the application --- public/app/controllers/JsonToForm.php | 92 ++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 38 deletions(-) (limited to 'public/app/controllers/JsonToForm.php') diff --git a/public/app/controllers/JsonToForm.php b/public/app/controllers/JsonToForm.php index fe08d5c..5a60891 100644 --- a/public/app/controllers/JsonToForm.php +++ b/public/app/controllers/JsonToForm.php @@ -142,8 +142,15 @@ class JsonToForm } - public static function make_a_form($formJson) + public static function make_a_form($form) { + $formJson = json_decode(json_encode($form)); + + $default_outer = $form->defaults->outer_class ?? ''; + $default_inner = $form->defaults->inner_class ?? ''; + $default_type = $form->defaults->type ?? 'text'; + + // TODO handle $form->defaults->source; // Script Workflow: // --------------------------------------------------------------------------- @@ -174,26 +181,22 @@ class JsonToForm - $html = ' -
-

Εισαγωγή Εγγραφής

'; + $html = ''; - foreach ($formJson as $key => $field) { + foreach ($formJson->form as $key => $field) { + $attributes = $field->attributes ?? []; // attributes array + $type = $field->type ?? $default_type; + $class = $field->class ?? ""; + $label = $field->label; + $name = $field->name ?? "$field->label"; - // get attributes + // TODO: auto value - if (isset($field->atr)) { - $atrs = explode('|', $field->atr); // attributes array - } else { - $atrs = []; - } - $appendKey = in_array('append-key', $atrs) ? true : false; // append key (for selects) - $required = in_array('required', $atrs) ? 'required' : ''; // required - $asterisk = in_array('required', $atrs) ? '*' : ''; // asterisk in label if required - $class = (isset($field->len)) ? ' col-md-'.$field->len : 'col-md-12'; // column class (for width) - $name = $field->nam; - $label = $field->lab; + $appendKey = in_array('append-key', $attributes) ? true : false; // append key (for selects) + $required = in_array('required', $attributes) ? 'required' : ''; // required + $asterisk = in_array('required', $attributes) ? '*' : ''; // asterisk in label if required + $html .= " @@ -201,19 +204,19 @@ class JsonToForm "; - switch ($field->typ) { + switch ($type) { case 'select': - if (in_array('multiple', $atrs)) { // if select is multiple + if (in_array('multiple', $attributes)) { // if select is multiple $html .=" - "; // prepare initialization of select2 (multiple) $initSelectsJS .= " var {$name} = $('#{$name}'); - {$name}.select2({ width: '100%' });"; + {$name}.select2({ width: '100%', placeholde: '{$label}' });"; // prepare check if empty field $checkFilledJS .= " @@ -224,13 +227,13 @@ class JsonToForm } else { // select is single; draw select + add empty option $html .=" - + "; - // prepare initialization of select2 (single) - $initSelectsJS .= " - var {$name} = $('#{$name}'); - {$name}.select2({ allowClear: true });"; + // // prepare initialization of select2 (single) + // $initSelectsJS .= " + // var {$name} = $('#{$name}'); + // {$name}.select2({ allowClear: true });"; // prepare check if empty field $checkFilledJS .= " @@ -240,10 +243,11 @@ class JsonToForm } // inject options - foreach ($field->opt as $key => $val) { + if (isset($field->options)) { + foreach ($field->options as $key => $val) { $html .= " - "; - } + "; + }} // close select $html .= ""; @@ -254,7 +258,19 @@ class JsonToForm case 'text': $html .= " - "; + "; + + $checkFilledJS = " + if ($('input[name={$name}]').val() =='') { empty += '{$label} {$asterisk}
'; } + values.{$name} = $('input[name={$name}]').val();"; + + break; + + + case 'password': + + $html .= " + "; $checkFilledJS = " if ($('input[name={$name}]').val() =='') { empty += '{$label} {$asterisk}
'; } @@ -266,7 +282,7 @@ class JsonToForm case 'integer': $html .= " - "; + "; $checkFilledJS .= " if ($('input[name={$name}]').val() =='') { empty += '{$label} {$asterisk}
'; } @@ -278,7 +294,7 @@ class JsonToForm case 'textarea': $html .= " - "; + "; $checkFilledJS .= " if ($('textarea[name={$name}]').val() =='') { empty += '{$label} {$asterisk}
'; } @@ -290,7 +306,7 @@ class JsonToForm case 'date': $html .= " - "; + "; $checkFilledJS .= " if ($('input[name={$name}]').val() =='') { empty += '{$label} {$asterisk}
'; } @@ -302,7 +318,7 @@ class JsonToForm case 'email': $html .= " - "; + "; $checkFilledJS .= " if ($('input[name={$name}]').val() =='') { empty += '{$label} {$asterisk}
'; } @@ -313,7 +329,7 @@ class JsonToForm default: // label, acts as common text - $html .= "