Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Former{
- public $data = array();
- public $label = array();
- public $form = array();
- public $template = array(
- "label" => "<label class='col-sm-{div} col-form-label text-{align}'>{label}</label>",
- "input" => "<input type='{type}' class='form-control {class}' id='{id}' name='{name}' placeholder='{placeholder}' value='{value}'>",
- "textarea" => "<textarea class='form-control {class}' id='{id}' name='{name}' placeholder='{placeholder}'>{value}</textarea>",
- "select" => "<select class='form-control {class}' id='{id}' name='{name}'>{option}</select>",
- "option" => "<option value='{value}'>{option}</option>"
- );
- public static function run($config, $data){
- $result = "";
- $FORMER = new Former();
- $FORMER->data = $data;
- $form = json_decode($config);
- $label = array(
- "div" => $form->label_division,
- "row" => ($form->dom == "inline") ? "row" : "",
- "align" => ($form->dom == "inline") ? "right" : "left"
- );
- $FORMER->label = $label;
- foreach($form->form as $f){
- if($f->type != "hidden"){
- $FORMER->label['label'] = $f->label;
- $result .= "<div class='form-group " . $FORMER->label['row'] . "'>";
- $result .= $FORMER->add_label($FORMER->label);
- }
- if(empty($f->double)){
- $result .= "<div class='col'>";
- $FORMER->form = $f;
- $result .= $FORMER->switcher();
- $result .= "</div>";
- } else {
- foreach($f->double as $fd){
- $result .= "<div class='col'>";
- $FORMER->form = $fd;
- $result .= $FORMER->switcher();
- $result .= "</div>";
- }
- }
- if($f->type != "hidden"){
- $result .= "</div>";
- }
- }
- return $result;
- }
- public function switcher(){
- $result = "";
- switch($this->form->type){
- case "select":
- $result .= $this->add_select();
- break;
- case "radiobutton":
- break;
- case "textarea":
- $result .= $this->add_input("textarea");
- break;
- case "checkbox":
- break;
- default:
- $result .= $this->add_input();
- break;
- }
- $result = preg_replace("/\{\w*\}/", "", $result);
- return $result;
- }
- public function add_label($label){
- $template = $this->template['label'];
- foreach($label as $head => $value){
- $template = str_replace("{" . $head . "}", $value, $template);
- }
- return $template;
- }
- private function add_input($kind = ""){
- $template = $this->template[($kind == "") ? 'input' : 'textarea'];
- foreach($this->form as $index => $value){
- $template = str_replace("{" . $index . "}", $value, $template);
- if($index == "value"){
- if(substr($value,0,1) == "$"){
- $template = str_replace($value, $this->data[substr($value,1)], $template);
- }
- }
- }
- return $template;
- }
- private function add_select(){
- $select_option = "";
- $select = $this->template['select'];
- $option = $this->template['option'];
- foreach($this->form as $index => $value){
- if($index == "option"){
- if(is_string($value)){
- $func = substr($value,6);
- foreach($func() as $op){
- $select_option .= str_replace("{value}", $op->value, str_replace("{option}", $op->option, $option));
- }
- } else {
- foreach($value as $op => $o){
- $select_option .= str_replace("{value}", $op, str_replace("{option}", $o, $option));
- }
- }
- } else {
- $select = str_replace("{" . $index . "}", $value, $select);
- }
- }
- return str_replace("option", $select_option, $select);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement