SHARE
TWEET

Bootstrap 4 Form Generator

a guest Dec 14th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Former{
  2.  
  3.     public $data = array();
  4.     public $label = array();
  5.     public $form = array();
  6.  
  7.     public $template = array(
  8.         "label" => "<label class='col-sm-{div} col-form-label text-{align}'>{label}</label>",
  9.         "input" => "<input type='{type}' class='form-control {class}' id='{id}' name='{name}' placeholder='{placeholder}' value='{value}'>",
  10.         "textarea" => "<textarea class='form-control {class}' id='{id}' name='{name}' placeholder='{placeholder}'>{value}</textarea>",
  11.         "select" => "<select class='form-control {class}' id='{id}' name='{name}'>{option}</select>",
  12.         "option" => "<option value='{value}'>{option}</option>"
  13.     );
  14.  
  15.     public static function run($config, $data){
  16.         $result = "";
  17.         $FORMER = new Former();
  18.         $FORMER->data = $data;
  19.         $form = json_decode($config);
  20.  
  21.         $label = array(
  22.             "div" => $form->label_division,
  23.             "row" => ($form->dom == "inline") ? "row" : "",
  24.             "align" => ($form->dom == "inline") ? "right" : "left"
  25.         );
  26.         $FORMER->label = $label;
  27.  
  28.         foreach($form->form as $f){
  29.             if($f->type != "hidden"){
  30.                 $FORMER->label['label'] = $f->label;
  31.                 $result .= "<div class='form-group " . $FORMER->label['row'] . "'>";
  32.                 $result .= $FORMER->add_label($FORMER->label);
  33.             }
  34.  
  35.             if(empty($f->double)){
  36.                 $result .= "<div class='col'>";
  37.                 $FORMER->form = $f;
  38.                 $result .= $FORMER->switcher();
  39.                 $result .= "</div>";
  40.             } else {
  41.                 foreach($f->double as $fd){
  42.                     $result .= "<div class='col'>";
  43.                     $FORMER->form = $fd;
  44.                     $result .= $FORMER->switcher();
  45.                     $result .= "</div>";
  46.                 }
  47.             }
  48.  
  49.             if($f->type != "hidden"){
  50.                 $result .= "</div>";
  51.             }
  52.         }
  53.        
  54.         return $result;
  55.     }
  56.  
  57.     public function switcher(){
  58.         $result = "";
  59.         switch($this->form->type){
  60.             case "select":
  61.                 $result .= $this->add_select();
  62.                 break;
  63.             case "radiobutton":
  64.                 break;
  65.             case "textarea":
  66.                 $result .= $this->add_input("textarea");
  67.                 break;
  68.             case "checkbox":
  69.                 break;
  70.             default:
  71.                 $result .= $this->add_input();
  72.                 break;
  73.         }
  74.  
  75.         $result = preg_replace("/\{\w*\}/", "", $result);
  76.         return $result;
  77.     }
  78.  
  79.     public function add_label($label){
  80.         $template = $this->template['label'];
  81.         foreach($label as $head => $value){
  82.             $template = str_replace("{" . $head . "}", $value, $template);
  83.         }
  84.         return $template;
  85.     }
  86.  
  87.     private function add_input($kind = ""){
  88.         $template = $this->template[($kind == "") ? 'input' : 'textarea'];
  89.         foreach($this->form as $index => $value){
  90.             $template = str_replace("{" . $index . "}", $value, $template);
  91.             if($index == "value"){
  92.                 if(substr($value,0,1) == "$"){
  93.                     $template = str_replace($value, $this->data[substr($value,1)], $template);
  94.                 }
  95.             }
  96.         }
  97.         return $template;
  98.     }
  99.  
  100.     private function add_select(){
  101.         $select_option = "";
  102.         $select = $this->template['select'];
  103.         $option = $this->template['option'];
  104.        
  105.           foreach($this->form as $index => $value){
  106.             if($index == "option"){
  107.                 if(is_string($value)){
  108.                     $func = substr($value,6);
  109.                     foreach($func() as $op){
  110.                         $select_option .= str_replace("{value}", $op->value, str_replace("{option}", $op->option, $option));
  111.                     }
  112.                 } else {
  113.                     foreach($value as $op => $o){
  114.                         $select_option .= str_replace("{value}", $op, str_replace("{option}", $o, $option));
  115.                     }
  116.                 }
  117.             } else {
  118.                 $select = str_replace("{" . $index . "}", $value, $select);
  119.             }
  120.         }
  121.  
  122.         return str_replace("option", $select_option, $select);
  123.     }
  124.  
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top