<?php
class manageModel extends Model{
	function __construct(){
		parent::__construct();
		$this->tabName = "elin_models_reg";
		$this->fieldList=array("id","type","fieldset","model_name","version","readonly");
	}

	function getModelList(){
		return $result = $this->findAll(array("type","model_name"), 0, 0,"","id");

	}
	function getModelfieldset(){
			$this->channel = new channelModel();
			$row = $this->channel->find($_REQUEST['channelid']);
			$result = $this->findAll(array('fieldset'), 0, 1,"where type ='product'","id");
			if(!empty($result)){
				$fieldset = unserialize($result[0][fieldset]);
				return $this->makeFieldHtml($fieldset);
			}


	}


	function makeFieldHtml($fieldset){
	$thml='';
		if(!empty($fieldset)){
			$i=1;
			$html .= "<div id=\"h".$i."\" class=\"mytable\"";
			$html .= $i == 0 ? "" : " style=\"display:none\"";
			$html .= "><table width=\"96%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"4\">";
			foreach($fieldset as $k => $a){
			    $html .= "<tr ><td class=\"txlrow\" width=\"95\" >".$a['des']."</td><td width=\"850\" colspan=\"2\" valign=\"top\">";
                if ( $a['type'] == "multitext")
                {
                    $html .= "<textarea name=\"".$a[fieldname]."\" style=\"height:150px; width:400px\"></textarea>";
                }elseif($a['type'] == "checkbox"){
                	preg_match('/set\((.*)\)/',$a['build'],$val);
                	$val=str_replace("'","",$val[1]);
                	$arr = explode(",",$val);
                	if($_POST[$a[fieldname]]){
                		$value = explode(",",$_POST[$a[fieldname]]);
                	}
                	foreach($arr as $key => $va){
						if(is_array($value) && in_array($va,$value)){
							$html .= "<input type=\"checkbox\" name=\"".$a[fieldname]."[]\" value=$va checked=\"checked\" class=\"text\" />&nbsp;".$va."&nbsp;";
						}else{
							$html .= "<input type=\"checkbox\" name=\"".$a[fieldname]."[]\" value=$va class=\"text\" />&nbsp;".$va."&nbsp;";
						}

                	}

                }elseif($a['type'] == "img"){
                	if(!empty($_POST[$a[fieldname]])){
						$picdir=ATTCHMENT_PATH.$_POST[$a[fieldname]];
						$newname="elincms".$a[fieldname];
                		$html .="<img src=\"$picdir\" width=\"150\" height=\"100\" id=\"picview_".$a[fieldname]."\" name=\"picview_".$a[fieldname]."\" /><br>";

						$html .= "<input type=\"file\" name=\"".$a[fieldname]."\" id=\"".$a[fieldname]."\"  size=\"11\"  onChange=\"SeePic(document.picview_".$a[fieldname].",document.form1.".$a[fieldname].");\"/>";
						$html .= "<input type=\"hidden\" name=\"$newname\" value=\"".$_POST[$a[fieldname]]."\"  />";
                	}else{
                		$html .= "<input type=\"file\" name=\"".$a[fieldname]."\" size=\"45\"  />";
                	}



                }elseif($a['type'] == "radio"){
                	preg_match('/enum\((.*)\)/',$a['build'],$val);
                	$val=str_replace("'","",$val[1]);
                	$arr = explode(",",$val);
                	foreach($arr as $key => $va){
                		if($_POST[$a[fieldname]]==$va){
                			$html .= "<input type=\"radio\" name=\"".$a[fieldname]."\" value=$va checked=\"checked\" class=\"text\" />&nbsp;".$va."&nbsp;";
                		}else{
                			$html .= "<input type=\"radio\" name=\"".$a[fieldname]."\" value=$va  class=\"text\" />&nbsp;".$va."&nbsp;";
                		}

                	}

                } elseif($a['type'] == "select"){
                	preg_match('/enum\((.*)\)/',$a['build'],$val);
                	$val=str_replace("'","",$val[1]);
                	$arr = explode(",",$val);

                	foreach($arr as $key => $va){
                		if($key==0){
                			$html .= "<select name=\"".$a[fieldname]."\">";
                		}
                		$html .= "<option  value=$key >$va</option>";
                	}
                	$html .="</select>";

                }

                else
                {
                    $html .= "<input name=\"".$a[fieldname]."\" value=\"\" class=\"text\" size=50/>";
                }
                $html .= "</td></tr>";
			}
			$html .= "</table></div>";


		}
	return $html;
	}




}
?>
