<?php
/*-------------------------------------
	版权所有：泗洪牛逼软件科技有限公司TEL-15896098989
	程序版本：1.1
	更新日期：2014-11-26 
-------------------------------------*/

class form
{
	private $fieldname='info';
	private $db;
	private $table;
	private $fieldtable;
	
	function __construct($fieldname='info')
	{
		global $db;
		$this->fieldname=$fieldname?$fieldname:'info';
		$this->db=$db;
		$this->table=LA_PRE.'model';
		$this->fieldtable=LA_PRE.'field';
	}

	function  get($modelid,$value=array())
	{
		global $_userid,$_gradeid,$_roleid;
		$modelid=intval($modelid);
		$info=array();

		$r=$this->db->fetch_all("SELECT * FROM `$this->fieldtable` WHERE `$this->fieldtable`.`modelid`=$modelid ORDER BY `$this->fieldtable`.`orderby` ASC,`$this->fieldtable`.`id` ASC");
		if($r)foreach($r as $key => $_r)
		{
				$membergrade=explode(',',$_r['membergrade']);
				if(in_array($_gradeid,$membergrade) || $_roleid)
				{
					
					$_value=$value?$value[$_r['enname']]:$_r['default'];
					$info[$key]['name']=trim($_r['name']);
					$info[$key]['enname']=trim($_r['enname']);
					$info[$key]['tips']=trim($_r['tips']);
					$info[$key]['unit']=trim($_r['unit']);
					$info[$key]['notnull']=trim($_r['notnull']);
					$_value=trim(htmlspecialchars($_value));
					if(!$_value && $_userid)
					{
						$t='_'.$_r['enname'];
						global $$t;
						$_value=isset($$t) && $$t?trim(htmlspecialchars($$t)):'';
					}
					$info[$key]['form']=$this->$_r['form']($_r['name'],$_r['enname'],$_value,$_r['options'],$_r['regex'],$_r['notnull'],$_r['length'],$_r['css']);
				}
		}
		return $info;
	}

	/*
		基本字段
	*/
	function text($name,$fieldname,$value='',$options='',$regex='limit',$notnull=0,$length='255',$css='')
	{
		$regex=empty($regex)?'limit':$regex;
		$value='value="'.$value.'"';
		$css=empty($css)?'class="input-txt"':'class="'.$css.'"';
		$length=intval($length)?intval($length):255;
		return '<input type="text" title="'.$name.'" name="'.$this->fieldname.'['.$fieldname.']" datatype="'.$regex.'" notnull="'.$notnull.'" min="0" max="'.intval($length).'" '.$value.' '.$css.' size="'.min(58,max(2,intval($length))).'"  />';
	}

	function password($name,$fieldname,$value='',$options='',$regex='limit',$notnull=0,$length='255',$css='')
	{
		$regex=empty($regex)?'limit':$regex;
		$css=empty($css)?'class="input-txt"':'class="'.$css.'"';
		$length=intval($length)?intval($length):255;
		return '<input type="password" title="'.$name.'" value="" name="'.$this->fieldname.'['.$fieldname.']" datatype="'.$regex.'" notnull="'.($value?0:1).'" min="0" max="'.intval($length).'"  '.$css.' size="'.min(30,max(2,intval($length))).'"/>';
	}

	function textarea($name,$fieldname,$value='',$options='',$regex='limit',$notnull=0,$length='255',$css='')
	{
		$regex=empty($regex)?'limit':$regex;
		$css=empty($css)?'class="form-input-textarea"':'class="'.$css.'"';
		$length=intval($length)?intval($length):255;
		return '<textarea title="'.$name.'" name="'.$this->fieldname.'['.$fieldname.']" datatype="'.$regex.'" notnull="'.$notnull.'" min="0" max="'.intval($length).'" '.$css.'>'.$value.'</textarea>';
	}

	function number($name,$fieldname,$value='',$options='',$regex='number',$notnull=0,$length='',$css='')
	{
		$regex=empty($regex)?'number':$regex;
		$value=empty($value)?'':'value="'.$value.'"';
		$css=empty($css)?'':'class="'.$css.'"';
		return '<input type="text" title="'.$name.'" name="'.$this->fieldname.'['.$fieldname.']" notnull="'.$notnull.'" datatype="'.$regex.'" '.$value.' '.$css.' />';
	}

	function radio($name,$fieldname,$value='0',$options='',$regex='',$notnull=0,$length='',$css='')
	{
		$str='';
		$css=empty($css)?'':'class="'.$css.'"';
		$r=explode("\r\n",$options);
		$value=$value?$value:0;
		if($r)foreach($r as $v)
		{
			$varray=explode("|",$v);
			$ischecked=$value==$varray[0]?' checked="checked" ':'';
			$str.='<label><input type="radio" style="border:0px" value="'.$varray[0].'" name="'.$this->fieldname.'['.$fieldname.']" '.$css.$ischecked.'>'.$varray[1].'</label> &nbsp;';
		}
		return $str;
	}

	function istop($name,$fieldname,$value='0',$options='',$regex='',$notnull=0,$length='',$css='')
	{
		$str='';
		$value=$value?$value:0;

		$str.='<label><input type="radio" style="border:0px" '.($value>=TIME?' checked="checked" ':'').' value="1" name="'.$this->fieldname.'[istop]" '.$css.$ischecked.'>加入</label> &nbsp;';
		$str.='<label><input type="radio" style="border:0px" '.($value<TIME?' checked="checked" ':'').' value="0" name="'.$this->fieldname.'[istop]" '.$css.$ischecked.'>不加入</label> &nbsp;';
		return $str;
	}

	function istopic($name,$fieldname,$value='0',$options='',$regex='',$notnull=0,$length='',$css='')
	{
		$str='';
		$value=$value?$value:0;

		$str.='<label><input type="radio" style="border:0px" '.($value>=TIME?' checked="checked" ':'').' value="1" name="'.$this->fieldname.'[istopic]" '.$css.$ischecked.'>加入</label> &nbsp;';
		$str.='<label><input type="radio" style="border:0px" '.($value<TIME?' checked="checked" ':'').' value="0" name="'.$this->fieldname.'[istopic]" '.$css.$ischecked.'>不加入</label> &nbsp;';
		return $str;
	}

	function isfocus($name,$fieldname,$value='0',$options='',$regex='',$notnull=0,$length='',$css='')
	{
		$str='';
		$value=$value?$value:0;

		$str.='<label><input type="radio" style="border:0px" '.($value>=TIME?' checked="checked" ':'').' value="1" name="'.$this->fieldname.'[isfocus]" '.$css.$ischecked.'>加入</label> &nbsp;';
		$str.='<label><input type="radio" style="border:0px" '.($value<TIME?' checked="checked" ':'').' value="0" name="'.$this->fieldname.'[isfocus]" '.$css.$ischecked.'>不加入</label> &nbsp;';
		return $str;
	}

	function checkbox($name,$fieldname,$value='',$options='',$regex='',$notnull=0,$length='',$css='')
	{
		$str='';
		$css=empty($css)?'':' class="'.$css.'" ';
		$r=explode("\r\n",$options);
		$value=explode(',',str_replace('，',',',$value));
		if($r)foreach($r as $v)
		{
			$varray=explode("|",$v);
			$ischecked=in_array($varray[0],$value)?' checked="checked" ':'';
			$str.='<label><input type="checkbox" style="border:0px" value="'.$varray[0].'" name="'.$this->fieldname.'['.$fieldname.'][]" '.$css.$ischecked.'>'.$varray[1].'</label> &nbsp;';
		}
		return $str;
	}

	function select($name,$fieldname,$value='',$options='',$regex='',$notnull=0,$length='',$css='')
	{
		$css=empty($css)?'':' class="'.$css.'" ';
		$str='<select title="'.$name.'" name="'.$this->fieldname.'['.$fieldname.']" '.$css.' notnull="'.$notnull.'">';	
		$r=explode("\r\n",$options);
		if($r)foreach($r as $v)
		{
			$varray=explode("|",$v);
			$ischecked=$value==$varray[0]?' selected="selected"':'';
			$str.='<option value="'.$varray[0].'"'.$ischecked.'>'.$varray[1].'</option>';
		}
		$str.='</select>';
		return $str;
	}
	
	function expire($name,$fieldname,$value='',$options='',$regex='limit',$notnull=0,$length='255',$css='')
	{
		$str='<select title="'.$name.'" name="'.$this->fieldname.'['.$fieldname.']" '.$css.' notnull="'.$notnull.'">';
		$str.='<option value="0" '.($value==0?' selected="selected"':'').'>长期有效</option>';
		$str.='<option value="7" '.($value==7?' selected="selected"':'').'>一周</option>';
		$str.='<option value="30" '.($value==30?' selected="selected"':'').'>一个月</option>';
		$str.='<option value="60" '.($value==60?' selected="selected"':'').'>两个月</option>';
		$str.='<option value="180" '.($value==180?' selected="selected"':'').'>半年</option>';
		$str.='<option value="365" '.($value==365?' selected="selected"':'').'>一年</option>';
		$str.='</select>';
		return $str;
	}

	function industy($name,$fieldname,$value='',$options='',$regex='limit',$notnull=0,$length='255',$css='')
	{
		global $db;
		$regex=empty($regex)?'limit':$regex;
		$value='value="'.$value.'"';
		$css=empty($css)?'class="input-txt"':'class="'.$css.'"';
		$length=intval($length)?intval($length):255;

		$str='<select name="'.$this->fieldname.'[typeid]" id="typeid">';
		$r=$this->db->fetch_all("SELECT * FROM `".LA_PRE."industy` WHERE `".LA_PRE."industy`.`parentid`=0 ORDER BY `$this->type_table`.`orderby` ASC");
		foreach($r as $_r)
		{
			$str.='<option value="'.$_r['id'].'">'.$_r['name'].'</option>';
		}
		$str.='</select>';
		return $str;

	}
	function area($name,$fieldname,$value='',$options='',$regex='limit',$notnull=0,$length='255',$css='')
	{
		global $db,$info;
		$info['areaid']=intval($info['areaid']);

		$regex=empty($regex)?'limit':$regex;
		$value='value="'.$value.'"';
		$css=empty($css)?'class="input-txt"':'class="'.$css.'"';
		$length=intval($length)?intval($length):255;
		$str='<input type="hidden" value="'.intval($info['areaid']).'" name="'.$this->fieldname.'[areaid]" id="areaid" /><select onchange="changeareaid(this.value);"><option value="0">请选择</option>';

	/*	if(SITETYPE==1)
		{
			
			$r=$this->db->fetch_all("SELECT * FROM `".LA_PRE."area` WHERE `".LA_PRE."area`.`parentid`=".intval($_SESSION['siteid'])." ORDER BY `$this->type_table`.`orderby` ASC");
			foreach($r as $_r)
			{
				$r2=$this->db->fetch_all("SELECT * FROM `".LA_PRE."area` WHERE `".LA_PRE."area`.`parentid`=".intval($_r['id'])." ORDER BY `".LA_PRE."area`.`orderby` ASC");
				if($r2)
				{
					$str.='<option value="'.$_r['id'].'">'.$_r['name'].'</option>';
					foreach($r2 as $_r2)
					{
						$str.='<option value="'.$_r2['id'].'">|—'.$_r2['name'].'</option>';
						$r3=$this->db->fetch_all("SELECT * FROM `".LA_PRE."area` WHERE `".LA_PRE."area`.`parentid`=".intval($_r2['id'])." ORDER BY `".LA_PRE."area`.`orderby` ASC");
						foreach($r3 as $_r3)
						{
							$str.='<option value="'.$_r3['id'].'">|——'.$_r3['name'].'</option>';	
						}
					}
					$str.='</option>';
				}
				else
				{
					$str.='<option value="'.$_r['id'].'">'.$_r['name'].'</option>';
				}
			}
		}
		else
		{*/
			$r=$this->db->fetch_all("SELECT * FROM `".LA_PRE."area` WHERE `".LA_PRE."area`.`parentid`=0 ORDER BY `".LA_PRE."area`.`orderby` ASC");
			if($info['areaid'])
			{
				$str.='<option value="'.$info['areaid'].'" selected="selected">'.areaname($info['areaid']).'</option>';
			}
			foreach($r as $_r)
			{
				$str.='<option value="'.$_r['id'].'">'.$_r['name'].'</option>';
			}
		//}
		$str.='</select> <span id="selectareaidspan"></span> <span id="selectareaidspan2"></span> <span id="selectareaidspan3"></span> <input type="text" title="'.$name.'" name="'.$this->fieldname.'['.$fieldname.']" datatype="max" notnull="0"  min="0" max="255" '.$value.' '.$css.' size="58" />';
		return $str;
	}

	function fckeditor($name,$fieldname,$value='',$options='',$regex='',$notnull=0,$length='',$css='')
	{
		global $LA,$_roleid,$MEM;
		$mtrand=mt_rand(10000,99999);
		if($_roleid)
		{
			$options=in_array($options,array('Textarea','Basic','Standard'))?$options:'Standard';
		}
		else
		{
			$options=$MEM['gradeinfo']['fckeditor'];
		}

		if($options=='Textarea')
		{
			return $this->textarea($name,$fieldname,$value,$options,'min',$notnull,$length,$css);
		}
		else
		{
			$toolbars=$options=='Standard'?'["source","|","undo","redo","|","forecolor","backcolor","fontfamily","fontsize","bold","italic","underline","strikethrough","superscript","subscript","removeformat","pasteplain","formatmatch","autotypeset","|","justifyleft","justifycenter","justifyright","justifyjustify","|","link","unlink","|","map","gmap","insertimage","attachment","date","time","music","insertvideo","emotion","|","BlockQuote","Anchor","HighlightCode","Horizontal","Spechars","|","InsertOrderedList","InsertUnorderedList","InsertTable","MergeRight","InsertParagraphBeforeTable","DeleteTable","|","ClearDoc","SelectAll","SearchReplace","print","pagebreak","fullscreen","|","preview","help"]':'["undo","redo","|","forecolor","fontfamily","fontsize","bold","italic","|","justifyleft","justifycenter","justifyright","|","link","unlink","|","emotion","insertimage"]';
			return '
			<script type="text/javascript">var SITEURL="'.$LA['siteurl'].'";window.UEDITOR_HOME_URL = "'.$LA['siteurl'].'ueditor/";</script>
			<script type="text/javascript" src="'.$LA['siteurl'].'ueditor/editor_config.js"></script>
			<script type="text/javascript" src="'.$LA['siteurl'].'ueditor/editor_all.js"></script>
			<textarea name="'.$this->fieldname.'['.$fieldname.']" id="myEditor'.$mtrand.'">'.$value.'</textarea>
			<script type="text/javascript">
				var editorOption_content = {
					toolbars:['.$toolbars.'],
					autoHeightEnabled:false,
					initialContent:"",
					minFrameHeight:250,
					initialStyle:"body{font-size:12px;line-height:24px;img{border:0px}",
					//关闭elementPath
					elementPathEnabled:false,
					maximumWords:8000
				};
				var myEditor'.$mtrand.' = new baidu.editor.ui.Editor(editorOption_content);
				myEditor'.$mtrand.'.render( "myEditor'.$mtrand.'" );
			</script>';
		}
	}

	function image($name,$fieldname,$value='',$options='',$regex='',$notnull=0,$length='',$css='') // 单张图片
	{
		return imagebutton($this->fieldname,$fieldname,$value);
	}

	function images($name,$fieldname,$value='',$options='',$regex='',$notnull=0,$length='',$css='') // 多图上传
	{
		global $MEM,$_roleid,$LA;

		$list='';
		$val=array();
		if($value)
		{
			$val=explode(',',$value);
			if($val)foreach($val as $k => $v)
			{
				$v=explode('`',$v);
				$list.='<li><label for="checkbox_images_'.$k.'"><img src="'.$v[0].'" alt="'.$v[1].'"/> <input type="hidden" name="urlimgs_'.$fieldname.'['.$k.']" value="'.$v[0].'" /><input type="checkbox" style="border:0px" name=deleteimgs_'.$fieldname.'['.$k.'] value="0" id="checkbox_images_'.$k.'" checked="checked" /></label><div style="margin:0px; padding:0px;margin-top:10px; clear:both"><input type="text" name="nameimgs_'.$fieldname.'['.$k.']" value="'.$v[1].'" size="20" style="border:#ccc 1px solid; font-size:12px; line-height:22px;height:22px; padding:2px" placeholder="请输入图片名称" /></div></li>';

			}
		}
		if(!isset($_SESSION))session_start();
		$_SESSION["file_info"] = array();
		return '<input type="hidden" value="'.$value.'" name="'.$this->fieldname.'['.$fieldname.']" />
<script type="text/javascript" src="'.LA_PATH.'statics/swfupload/swfupload.js"></script>
<script type="text/javascript" src="'.LA_PATH.'statics/swfupload/handlers.js"></script>
<script type="text/javascript">
		var swfu'.$fieldname.';
		var func_'.$fieldname.' = window.onload;
		
		window.onload = function () {
			func_'.$fieldname.' ? func_'.$fieldname.'() : 0;
			swfu'.$fieldname.' = new SWFUpload({
				// Backend Settings
				upload_url: "'.LA_PATH.'api/upload/swfupload.php",
				post_params: {"PHPSESSID": "'.session_id().'"},

				file_size_limit : "'.min(round($LA['uploadsizelimit']/1024/1024,3),substr(ini_get('post_max_size'),0,-1)).' MB",	// '.min(round($LA['uploadsizelimit']/1024/1024,3),substr(ini_get('post_max_size'),0,-1)).'MB
				file_types : "*.jpg;*.gif;*.png;*.jpeg",
				file_types_description : "图片文件",
				file_upload_limit : '.($_roleid?127:$MEM['gradeinfo']['images']).',

				swfupload_preload_handler : preLoad,
				swfupload_load_failed_handler : loadFailed,
				file_queue_error_handler : fileQueueError,
				file_dialog_complete_handler : fileDialogComplete,
				upload_progress_handler : uploadProgress,
				upload_error_handler : uploadError,
				upload_success_handler : uploadSuccess,
				upload_complete_handler : uploadComplete,

				button_image_url : "'.LA_PATH.'statics/swfupload/images/SmallSpyGlassWithTransperancy_17x18.png",
				button_placeholder_id : "spanButtonPlaceholder'.$fieldname.'",
				button_width: 180,
				button_height: 18,
				button_text : \'<span class="button">您可以同时上传 '.($_roleid?127:$MEM['gradeinfo']['images']).' 张图片</span>\',
				button_text_style : ".button { font-family: Helvetica, Arial, sans-serif; font-size: 12pt; }",
				button_text_top_padding: 0,
				button_text_left_padding: 18,
				button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
				button_cursor: SWFUpload.CURSOR.HAND,
				
				flash_url : "'.LA_PATH.'statics/swfupload/swfupload.swf",
				flash9_url : "'.LA_PATH.'statics/swfupload/swfupload_FP9.swf",

				custom_settings : {
					upload_target : "divFileProgressContainer'.$fieldname.'",
					thumbnail_width : "800",
					thumbnail_height : "800",
					fieldid : "'.$fieldname.'",
					startid : "'.(count($val)+1).'"
				},
				
				// Debug Settings
				debug: false
			});
		};
	</script>
<style type="text/css">
	#thumbnails'.$fieldname.'{padding:0px; margin:0px; clear:both}
	#thumbnails'.$fieldname.' ul{width:550px; clear:both; margin:0px; list-style:none; padding:0px}
	#thumbnails'.$fieldname.' ul li{width:150px; float:left; margin:4px; padding:0px; overflow:hidden}
	#thumbnails'.$fieldname.' ul li img{width:120px; height:75px}
</style>
<div id="content" style="width:550px;overflow:hidden">
	<div style="width: 180px; height: 18px; border: solid 1px #7FAAFF; background-color: #C5D9FF; padding: 2px;">
		<span id="spanButtonPlaceholder'.$fieldname.'"></span>
	</div>
<div id="divFileProgressContainer'.$fieldname.'"></div>
<div id="thumbnails'.$fieldname.'"><ul id="thumbnailsul'.$fieldname.'">'.$list.'</ul></div></div>';
	}

	function map($name,$fieldname,$value='',$options='',$regex='',$notnull=0,$length='',$css='') // 地图
	{
		return pointbaidumap($this->fieldname,$fieldname,$value);
	}

	function datetime($name,$fieldname,$value='',$options='',$regex='',$notnull=0,$length='',$css='') // 时间日期 2014-11-26 
	{
		global $sitelang;

		$mtrand=mt_rand(10000,99999);
		$value=$value?$value:TIME;
		$value=is_numeric($value)?$value:strtotime($value);
		return '<input id="datepicker'.$mtrand.'" readonly="1" class="input-txt" size="12" name="'.$this->fieldname.'['.$fieldname.']" value="'.date('Y-m-d',$value).'"/><script language="javascript">$(function() {
		var pickerOpts = {
		changeMonth: true,
		changeYear: true,
		dateFormat: "yy-mm-dd",
		dayNamesMin:['.$sitelang['datetime-dayNamesMin'].'],
		firstDay: 0,
		nextText: "'.$sitelang['datetime-nextText'].'",
		prevText: "'.$sitelang['datetime-prevText'].'",
		closeText: "'.$sitelang['datetime-closeText'].'",
		currentText: "'.$sitelang['datetime-currentText'].'",
		monthNamesShort: ['.$sitelang['datetime-monthNamesShort'].'],
		isRTL: false,
		showButtonPanel: true,
		yearRange: "-100:+5",
		showOtherMonths: true,
		selectOtherMonths: true
        };$("#datepicker'.$mtrand.'").datepicker(pickerOpts);});</script>';
	}

	function attachment($name,$fieldname,$value='',$options='',$regex='',$notnull=0,$length='',$css='') // 附件上传
	{
		return attachmentbutton($this->fieldname,$fieldname,$value);
	}
	
	function video($name,$fieldname,$value='',$options='',$regex='',$notnull=0,$length='',$css='') // 媒体文件
	{
		return videobutton($this->fieldname,$fieldname,$value);
	}
}
?>
