diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-05-01 04:33:06 +0300 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-05-01 04:33:06 +0300 |
| commit | 2b4ed611e7e001ad8ea93ef3e8cdce35bc56dc07 (patch) | |
| tree | 0da2582de2cef450f6366a9cff28e47353eddb2e /public/app/controllers/JsonToForm.php | |
| parent | 77b3a3ebf23598b82d035ead6df43b2c2ca885f4 (diff) | |
| download | gyraf1gov-2b4ed611e7e001ad8ea93ef3e8cdce35bc56dc07.tar.gz gyraf1gov-2b4ed611e7e001ad8ea93ef3e8cdce35bc56dc07.tar.bz2 gyraf1gov-2b4ed611e7e001ad8ea93ef3e8cdce35bc56dc07.zip | |
form designer; setup main forms of the application
Diffstat (limited to 'public/app/controllers/JsonToForm.php')
| -rw-r--r-- | public/app/controllers/JsonToForm.php | 92 |
1 files changed, 54 insertions, 38 deletions
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 = ' - <div class="pi-container"> - <h2>Εισαγωγή Εγγραφής</h2>'; + $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 <label for='{$name}'>{$label} {$asterisk}</label> "; - 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 .=" - <select id='{$name}$' class='select-field' name='{$name}[]' multiple='multiple' style='width:100%'>"; + <select id='{$name}' class='form-select form-select-sm' name='{$name}[]' multiple='multiple' style='width:100%'>"; // 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 .=" - <select id='{$name}$' class='select-field' name='{$name}' style='width:100%'> - <option></option>"; + <select id='{$name}' class='form-select form-select-sm' name='{$name}' style='width:100%'> + <option value='0'>(επιλέξτε)</option>"; - // 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 .= " - <option value='{$val->id}'>{$val->tag}</option>"; - } + <option value='{$key}'>{$val}</option>"; + }} // close select $html .= "</select>"; @@ -254,7 +258,19 @@ class JsonToForm case 'text': $html .= " - <input type='text' class='form-control' name='{$name}' {$required}>"; + <input type='text' class='form-control form-control-sm' name='{$name}' {$required}>"; + + $checkFilledJS = " + if ($('input[name={$name}]').val() =='') { empty += '{$label} {$asterisk}<br />'; } + values.{$name} = $('input[name={$name}]').val();"; + + break; + + + case 'password': + + $html .= " + <input type='password' class='form-control form-control-sm' name='{$name}' {$required}>"; $checkFilledJS = " if ($('input[name={$name}]').val() =='') { empty += '{$label} {$asterisk}<br />'; } @@ -266,7 +282,7 @@ class JsonToForm case 'integer': $html .= " - <input type='number' class='form-control' name='{$name}' min='0' step='1' {$required}>"; + <input type='number' class='form-control form-control-sm' name='{$name}' min='0' step='1' {$required}>"; $checkFilledJS .= " if ($('input[name={$name}]').val() =='') { empty += '{$label} {$asterisk}<br />'; } @@ -278,7 +294,7 @@ class JsonToForm case 'textarea': $html .= " - <textarea class='form-control' name='{$name}' {$required}></textarea>"; + <textarea class='form-control form-control-sm' name='{$name}' {$required}></textarea>"; $checkFilledJS .= " if ($('textarea[name={$name}]').val() =='') { empty += '{$label} {$asterisk}<br />'; } @@ -290,7 +306,7 @@ class JsonToForm case 'date': $html .= " - <input type='date' class='form-control' name='{$name}' {$required}>"; + <input type='date' class='form-control form-control-sm' name='{$name}' {$required}>"; $checkFilledJS .= " if ($('input[name={$name}]').val() =='') { empty += '{$label} {$asterisk}<br />'; } @@ -302,7 +318,7 @@ class JsonToForm case 'email': $html .= " - <input type='email' class='form-control' name='{$name}' {$required}>"; + <input type='email' class='form-control form-control-sm' name='{$name}' {$required}>"; $checkFilledJS .= " if ($('input[name={$name}]').val() =='') { empty += '{$label} {$asterisk}<br />'; } @@ -313,7 +329,7 @@ class JsonToForm default: // label, acts as common text - $html .= "<label>{$label}</label"; + //$html .= "<label>{$label}</label"; } @@ -405,7 +421,7 @@ class JsonToForm * <!-- custom field --> * <div class="form-group"> * <label for="more">Συμπληρωματική Πληροφορία</label> - * <textarea name="note" class="form-control tall"></textarea> + * <textarea name="note" class="form-control form-control-sm tall"></textarea> * <?php * // append js code * // for checking if field is empty @@ -559,7 +575,7 @@ class JsonToForm * * * - * // 03. FORM-CONTROLS + * // 03. form-control form-control-smS * // --------------------------------------------------------------------------- * ////////////////////////////////////////////////////////////////////////////// * |
