1 [mytable] => forms [group_id] => 1 [order_id] => 1 [param] => ldap_host [desc] => LDAP server [def] => 192.168.1.3 [cur] => [new] => [mandatory] => 1 [attr] => maxlen=60 [xattr] => [type] => inputbox [link] => ) */ class Forms { private $db=''; function __construct($jname,$helper='',$db_path=false) { if($jname==''){ $database=$helper; }else{ $database=array('jname'=>$jname,'helper'=>$helper); } if($jname=='cbsd-settings'){ $this->db=new Db('cbsd-settings'); } else if($helper!=''){ if($db_path!==false){ $this->db=new Db('file',$db_path); }else{ $this->db=new Db('helper',$database); } } } private function fetch_from_db($link) { // return $this->db->select("select * from ? order by order_id asc", array([$link])); return $this->db->select("select * from {$link} order by order_id asc", array()); } function generate(){ if($this->db->error) return; $fields=$this->db->select("select * from forms order by groupname asc, group_id asc, order_id asc", array()); // Строим карту формы с группами элементов $groups=array(); foreach($fields as $key=>$field){ $group=$field['groupname']; if(!empty($group)){ if($field['type']=='group_add'){ // Expand $groups[$group]['_title']=$field['desc']; }else if($field['type']=='delimer'){ // Delimer $groups[$group][$field['group_id']]=$key; }else{ // Other elements $groups[$group][$field['group_id']]['_group_id']=$field['group_id']; $groups[$group][$field['group_id']][$field['order_id']]=$key; } }else{ $groups[]=$key; } } //print_r($fields);print_r($groups);exit; $arr=array(); $last_type=''; foreach($fields as $key=>$field){ /* if($last_type=='delimer' && $field['type']!='delimer') $html.='
'; */ $last_type=$field['type']; if(isset($field['cur']) && isset($field['def'])){ if(empty($field['cur'])) $field['cur']=$field['def']; } $tpl=$this->getElement($field['type'],$field); $params=array('param','desc','attr','cur'); foreach($params as $param){ if(isset($field[$param])) $tpl=str_replace('${'.$param.'}',$field[$param],$tpl); } //$value=$field['def']; //if(isset($field['cur']) && !empty($field['cur'])) $value=$field['cur']; $value=$field['cur']; $tpl=str_replace('${value}',$value,$tpl); $value=$field['def']; $tpl=str_replace('${def}',$value,$tpl); $required=($field['mandatory']==1)?' required':''; $tpl=str_replace('${required}',$required,$tpl); $arr[$key]=$tpl; //if($field['param']!='-') $currents[$field['param']]=$field['cur']; //if($field['param']!='-') $defaults[$field['param']]=$field['def']; } // Выстраиваем форму по карте $html='
'; foreach($groups as $key=>$txt){ if(is_numeric($key)){ $html.=$arr[$key]; }else if(is_array($txt)){ $group_name=key($txt); $group_title=$txt['_title']; unset($txt['_title']); foreach($txt as $key1=>$val1){ $group_id=$val1['_group_id']; unset($val1['_group_id']); if(is_array($val1)){ $html.='
'.$group_title.''; foreach($val1 as $key2=>$val2){ $html.=$arr[$val2]; } $html.='
'; }else{ $html.=$arr[$key1]; } } $html.='
'; } } $html.='
'; $html.='
 
'; $html.='
'; return $html; } function getElement($el,$arr=array()) { $tpl=''; switch(trim($el)){ case 'inputbox': $res=$this->getInputAutofill($arr); if($res===false){ $list=''; $datalist=''; }else{ $list=' list="'.$res['list'].'"'; $datalist=$res['datalist']; } $tpl='
[default]${desc}'.$datalist.'
'; //'.$default.' break; case 'password': $tpl='
[default]${desc}
'; break; case 'delimer': $tpl='

${desc}

'; break; case 'checkbox': $tpl=''; break; case 'select': $tpl=$this->getSelect($el,$arr); break; case 'radio': $tpl=$this->getRadio($el,$arr); break; } return $tpl; } function getInputAutofill($arr) { if(isset($arr['link']) && $arr['link']!=''){ $id=$arr['link']; //$arr['param'].'-'. $tpl=''; $opts = $this->fetch_from_db($arr['link']); foreach($opts as $key=>$opt){ $tpl.=''; } $tpl.=''; Utils::clonos_syslog("forms.php template: " . $tpl); return array('list'=>$id,'datalist'=>$tpl); }else { return false; } } function getSelect($el,$arr) { $tpl='
[default]${desc}
'; return $tpl; } function getRadio($el,$arr) { $tpl='
${desc}'; if(isset($arr['link'])){ $opts = $this->fetch_from_db($arr['link']); foreach($opts as $key=>$opt){ $checked=($opt['id']==$arr['cur'])?' checked':''; $tpl.=''; } } $tpl.='
'; return $tpl; } }