<?php
 /**
 * Magic CMS 表单
 * ============================================================================
 * Copyright 2014 大秦科技，并保留所有权利。
 * 网站地址: http://www.qintech.net；
 * ----------------------------------------------------------------------------
 * 未获商业授权之前，不得将Magic CMS用于商业用途（包括但不限于企业网站、经营性网站
 * 以营利为目的或实现盈利的网站）未经官方许可，禁止在Magic CMS的整体或任何部分基础
 * 上以发展任何派生版本、修改版本或第三方版本用于重新分发。如果您未能遵守本协议的
 * 条款，您的授权将被终止，所被许可的权利将被收回，并承担相应法律责任。
 * ============================================================================
 * Date:2014/12/31
**/
if (!defined('KERNEL_PATH'))exit('No direct script access allowed');

	//文档摘要
	function _abstract($att,$data){
		return '<textarea name="abstract" if="abstract" rows="4" class="l textinput" cols="60" class="l">'.$data[$att['field']].'</textarea><label style="line-height:70px"> &nbsp;&nbsp;<input type="checkbox" name="isinter" value="1" checked>&nbsp;自动截取内容 <input type="text" name="internum" value="200" class="insty" size="5"> 字符(不含HTML代码)</label>';
	}

	//文档置顶
	function _sortrank($att,$data){
		$ctime  = round(($data[$att['field']] - $data['pubtime'])/3600/24);
		$forms = '<select name="'.$att['field'].'" id="'.$att['field'].'" class="selectbox" ><option value="0"';
		if($ctime==0){
			$forms .= 'selected';
		}
		$forms .= '>正常排序</option><option value="1"';
		if($ctime==1){
			$forms .= 'selected';
		}
		$forms .= '>置顶一天</option><option value="2"';
		if($ctime==2){
			$forms .= 'selected';
		}
		$forms .= '>置顶两天</option><option value="3"';
		if($ctime==3){
			$forms .= 'selected';
		}
		$forms .= '>置顶三天</option><option value="7"';
		if($ctime==7){
			$forms .= 'selected';
		}
		$forms .= '>置顶一周</option><option value="15"';
		if($ctime==15){
			$forms .= 'selected';
		}
		$forms .= '>置顶半个月</option><option value="30"';
		if($ctime==30){
			$forms .= 'selected';
		}
		$forms .= '>置顶一个月</option><option value="90"';
		if($ctime==90){
			$forms .= 'selected';
		}
		$forms .= '>置顶三个月</option><option value="180"';
		if($ctime==180){
			$forms .= 'selected';
		}
		$forms .= '>置顶半年</option><option value="365"';
		if($ctime==365){
			$forms .= 'selected';
		}
		$forms .= '>置顶一年</option>';

		$forms .= '</option></select>';
		return $forms;
	}

	//内容模板
	function _contenttpl($att,$data){
		$tips = empty($att['tips']) ? '' : '<cite>'.$att['tips'].'</cite>';
		return '<input type="text" name="'.$att['field'].'" id="'.$att['field'].'" value="'.$data[$att['field']].'" class="insty '.$att['css'].'" size="'.$att['attribute']['size'].'"> &nbsp;&nbsp;<input type="button" value=" 浏览 " class="scbtn7" onclick="selecttpl(\'contenttpl\')">'.$tips;
	}

	//点击量
	function _hits($att,$data){
		$tips = empty($att['tips']) ? '' : '<cite>'.$att['tips'].'</cite>';
		return '<input type="text" name="'.$att['field'].'" value="'.$data[$att['field']].'" class="insty '.$att['css'].'" size="'.$att['attribute']['size'].'"> 次'.$tips;
	}

	//浏览权限
	function _arcrank($att,$data){
		$forms = '<select name="'.$att['field'].'" id="'.$att['field'].'" class="selectbox" ><option value="-1"';
		if($data[$att['field']]==-1){
			$forms .= 'selected';
		}
		$forms .= '>等待审核</option><option value="0"';
		if(empty($data[$att['field']])){
			$forms .= 'selected';
		}
		$forms .= '>开放浏览</option>';

		$forms .= '</select>';
		return $forms;
	}


	//文档属性
	function _flag($att,$data){

		//读取Flag属性
		$flag=M('flag')->where("status=1 AND (model='".$att['model']."' OR model='-1')")->all();

		foreach ($flag as $v){
			$forms .= '&nbsp;&nbsp;<input type="checkbox" name="formsysflag[]" value="'.$v['flag'].'" ';
			if (stristr($data['flag'],$v['flag'])){
				$forms .= ' checked ';
			}
			$forms .= '> '.$v['title'].'['.$v['flag'].']';
		}

		$forms .= '&nbsp; <input type="checkbox" name="jump" value="1" onClick="if(this.checked){$(\'#linkurl\').show();}else{$(\'#linkurl\').hide();$(\'#linkurl2\').val(\'\');}"';
		if (!empty($data['linkurl'])){$forms .= ' checked ';}

		$forms .= ' >&nbsp;跳转[j] </tr><tr id="linkurl" ';

		if (empty($data['linkurl'])){ $forms .= ' style="display:none"';};

		$forms .= '><td width="100" style="padding-top:10px;" align="right">跳转网址</td><td  style="padding-top:10px;" ><input type="text" name="linkurl" value="'.$data['linkurl'].'" id="linkurl2" class="insty" size="45">&nbsp;&nbsp;';

		$forms .= '&nbsp;&nbsp;打开方式：<select name="target" class="selectbox"><option value="_blank"';

		if($data['target']=='_blank'){$forms .= 'selected';}
		$forms .= '>_blank</option><option value="_self"';
		if($data['target']=='_self'){$forms .= ' selected ';}
		$forms .= '>_self</option><option value="_parent"';
		if($data['target']=='_parent'){$forms .= ' selected ';}
		$forms .= '>_parent</option><option value="_top"';
		if($data['target']=='_top'){$forms .= ' selected ';}
		$forms .= '>_top</option></select></td> </tr>';
		return $forms;
	}

	//文档栏目
	function _catid($att,$data){

		$category = M('class')
					->field('catid,pid,name,model,status,sorter,ismenu')
					->where('status=1')
					->order('sorter asc')
					->all();

		$category = Data::tree($category,'name','catid','pid');

		$forms  = '<select name="catid" class="selectbox"><option value="">==选择栏目==</option>';
		foreach ($category as $v){
			$forms .= '<option value="'.$v['catid'].'"';
			if ($v['catid']==$data['catid']){
				$forms .= ' selected ';
			}
			if ($v['model']!=$data['model']){
				$forms .= ' disabled ';
			}
			$forms .= '>'.$v['_name'].'</option>';
		}
		$forms .= '</select>';
		return $forms;
	}

//========================================== 自定义字段  ==========================================================

	//输入域
	function text($att,$data){
		$tips  = empty($att['tips']) ? '' : '<cite>'.$att['tips'].'</cite>';
		$value = empty($data[$att['field']]) ? $att['attribute']['value'] : $data[$att['field']];
		return '<input type="text" name="'.$att['field'].'" value="'.$value.'" id="'.$att['field'].'" class="insty '.$att['css'].'" size="'.$att['attribute']['size'].'" '.$att['addattach'].'>'.$tips;
	}

	//密码域
	function password($att,$data){
		$tips = empty($att['tips']) ? '' : '<cite>'.$att['tips'].'</cite>';
		return '<input type="password" name="'.$att['field'].'" value="'.$data[$att['field']].'" id="'.$att['field'].'" class="insty '.$att['css'].'" size="'.$att['attribute']['size'].'" '.$att['addattach'].'>'.$tips;
	}

	//文本域
	function textarea($att,$data){
		$tips = empty($att['tips']) ? '' : '<cite>'.$att['tips'].'</cite>';
		return '<textarea name="'.$att['field'].'" id="'.$att['field'].'" id="'.$att['field'].'" rows="'.$att['attribute']['rows'].'" cols="'.$att['attribute']['cols'].'" class="textinput '.$att['css'].'" '.$att['addattach'].'>'.$data[$att['field']].'</textarea>'.$tips;
	}

	//隐藏字段
	function hidden($att,$data){
		return '<input type="hidden" name="'.$att['field'].'" id="'.$att['field'].'" value="'.$data[$att['field']].'">';
	}

	//单选按钮
	function radio($att,$data){
		$tips = empty($att['tips']) ? '' : '<cite>'.$att['tips'].'</cite>';
		$option    = $att['attribute']['option'];
		$optionarr = explode("~",$option);
		foreach($optionarr as $ov){
			$opcont = explode('|',$ov);
			$forms .= ' <input type="radio" name="'.$att['field'].'" id="'.$att['field'].'" value="'.$opcont[1].'"';
			if($att['attribute']['value']==trim($opcont[1]) && empty($data[$att['field']])){
				$forms .= ' checked ';
			}elseif($data[$att['field']] == $opcont[1]){
				$forms .= ' checked ';
			}
			$forms .= $att['addattach'].' > '.$opcont[0];
		}
		return $forms.$tips;
	}

	//复选框
	function checkbox($att,$data){
		$tips = empty($att['tips']) ? '' : '<cite>'.$att['tips'].'</cite>';
		$option    = $att['attribute']['option'];
		$optionarr = explode("~",$option);

		foreach($optionarr as $ov){
			$opcont = explode('|',$ov);
			$forms .= ' <input type="checkbox" name="'.$att['field'].'" id="'.$att['field'].'" value="'.$opcont[1].'"';
			if($att['attribute']['value']==trim($opcont[1]) && empty($data[$att['field']])){
				$forms .= ' checked ';
			}elseif($data[$att['field']] == $opcont[1]){
				$forms .= ' checked ';
			}
			$forms .= $att['addattach'].' > '.$opcont[0];
		}
		return $forms.$tips;
	}

	//富文本编辑器
	function editor($att,$data){

		$sid   = PHPSESSID;
		$uid   = session('uid');
		$super = session('super_admin');

		if ($att['attribute']['type']==1){
			//$forms .= ",items : ['fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline','removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist','insertunorderedlist', '|', 'emoticons', 'image', 'link'],";
		}
		$forms .= '<textarea plugin="editor" super="'.$super.'" uid="'.$uid.'" sid="'.$sid.'" name="'.$att['field'].'" id="'.$att['field'].'" style="width:'.$att['attribute']['cols'].'px;height:'.$att['attribute']['rows'].'px">'.$data[$att['field']].'</textarea>';
		return $forms;
	}

	//时间面板
	function datetime($att,$data){
		$tips = empty($att['tips']) ? '' : '<cite>'.$att['tips'].'</cite>';
		$format = fordate($att['attribute']['format']);
		$istime = $att['attribute']['istime'] ? $att['attribute']['istime'] : 'true';
		$value  = $data[$att['field']] ? date($format,$data[$att['field']]) : date($format);

		$att['attribute']['format'] = $att['attribute']['format'] ?
									  $att['attribute']['format'] : 'YYYY-MM-DD hh:mm:ss';


		if ($att['attribute']['isinte']) {
			$times  = explode('.',$data[$att['field']]);

			$value1 = $times[0] ? date($format,$times[0]) : date($format);
			$value2 = $times[1] ? date($format,$times[1]) : date($format,strtotime('+1 month'));

			$forms = '从 <input type="text" name="'.$att['field'].'[]" id="'.$att['field'].'" readonly value="'.$value1.'" onclick="calendar({format:\''.$att['attribute']['format'].'\',istime:'.$istime.'})" class="insty tico  '.$att['css'].'" size="'.$att['attribute']['size'].'"> 到 <input type="text" name="'.$att['field'].'[]" id="'.$att['field'].'" readonly value="'.$value2.'" onclick="calendar({format:\''.$att['attribute']['format'].'\',istime:'.$istime.'})" class="insty tico  '.$att['css'].'" size="'.$att['attribute']['size'].'">'.$tips;

		}else{
			$value  = $data[$att['field']] ? date($format,$data[$att['field']]) : date($format);
			$forms = '<input type="text" name="'.$att['field'].'" id="'.$att['field'].'" readonly value="'.$value.'" onclick="calendar({format:\''.$att['attribute']['format'].'\',istime:'.$istime.'})" class="insty tico  '.$att['css'].'" size="'.$att['attribute']['size'].'">'.$tips;
		}
		return $forms;
	}



	//下拉菜单
	function select($att,$data){
	$tips = empty($att['tips']) ? '' : '<cite>'.$att['tips'].'</cite>';
	$multiple  = $att['attribute']['type']==1 ? '' : 'multiple="multiple"';
	$option    = $att['attribute']['option'];
	$optionarr = explode("~",$option);
	foreach($optionarr as $ov){
		$opcont = explode('|',$ov);
		$optionlist .= '<option value="'.$opcont[1].'" ';
		if($att['attribute']['value']==trim($opcont[1]) && empty($data[$att['field']])){
			$optionlist .= 'selected';
		}elseif($data[$att['field']] == $opcont[1]){
			$optionlist .= 'selected';
		}
		$optionlist .= '>'.trim($opcont[0]).'</option>';
	}
	$forms =
<<<EOT
	<select name="{$att['field']}" id="{$att['field']}" {$multiple} class="selectbox" {$att['addattach']}>
		{$optionlist}
	</select>{$tips}
EOT;
	 return $forms;
	}




	//文件上传
	function upload($att,$data){
		//上传类型
		if ($att['attribute']['type']==1) {
			$plugin = 'image';
		}elseif($att['attribute']['type']==2){
			$plugin = 'file';
		}elseif($att['attribute']['type']==3){
			$plugin = 'media';
		}elseif($att['attribute']['type']==4){
			$plugin = 'media';
		}

		//图片空间
		if ($att['attribute']['manager']==1) {
			$manager = '<input type="button"  plugin="filemanager" name="'.$att['field'].'" class="scbtn7" value="图片空间" />';
		}

		if($att['attribute']['type']==4){

		$videolist = unserialize($data[$att['field']]);

		if ($videolist['type']=='youku'){
			$youku = 'selected';
		}elseif ($videolist['type']=='tudou'){
			$tudou = 'selected';
		}elseif ($videolist['type']=='mp3'){
			$mp3 = 'selected';
		}else{
			$oneself = 'selected';
		}

		$amount = empty($videolist['amount']) ? 10 : $videolist['amount'];

		$forms =
<<<EOT
		<tr>
		<td width="120" height="40" align="right">播放器类型：</td><td>
			<select name="{$att['field']}[type]" id="players" class="selectbox">
				<option value="youku" {$youku}>优酷视频</option>
				<option value="tudou" {$tudou}>土豆视频</option>
				<option value="mp3" {$mp3}>mp3音乐</option>
				<option value="oneself" {$oneself}>独立播放器</option>
			</select>&nbsp;&nbsp;

			集数设置： <input name="{$att['field']}[amount]" type="text" id="multi_amount_{$att['field']}" class="insty" value="{$amount}" size="5" />
					   <input type="button" name="button2" id="button2" onClick="makevideo('{$att['field']}');" value="提交" class="scbtn7" style="width:60px"/>
		</td></tr>
		<tr><td width="120" height="30" align="right" style="background:#f5f5f5"><b>视频列表：</b></td><td  style="background:#f5f5f5">{$att['tips']}</td></tr>
		<tr>
		<td  width="120" align="right">视频列表：</td>
		<td class="multivideo" id="multi_videolist_{$att['field']}">
EOT;

		foreach ($videolist['list']  as $key=>$value) {

		$forms .=
<<<EOT
		<p id="vl{$key}">
		<input type="text"   id="{$att['field']}_name1"      name="{$att['field']}[list][{$key}][name]"      value="{$value['name']}" size="10" class="insty"/>&nbsp;
		<input type="text"   id="{$att['field']}_path1"      name="{$att['field']}[list][{$key}][path]"      value="{$value['path']}" size="30"  class="insty"/>
		<input type="hidden" id="{$att['field']}_width1"     name="{$att['field']}[list][{$key}][width]"     value="{$value['width']}" />
		<input type="hidden" id="{$att['field']}_height1"    name="{$att['field']}[list][{$key}][height]"    value="{$value['height']}" />
		<input type="hidden" id="{$att['field']}_autostart1" name="{$att['field']}[list][{$key}][autostart]" value="{$value['autostart']}"/>
		<input type="hidden" id="{$att['field']}_note1"      name="{$att['field']}[list][{$key}][note]"      value="{$value['note']}"/>
		<input onclick="multimedia('{$att['field']}',{$key})"     name="{$att['field']}" type="button"  value="选取"  class="scbtn7 insertfile"/>
		</p>
EOT;
		}


		}else{
		//单文件上传
		if ($att['attribute']['multi']==0) {

		//图片预览
		if ($att['attribute']['view']=='true') {
			$preimg = $data[$att['field']] ? '..'.$data[$att['field']] : __PUBLIC__.'/images/pview.gif';
			$view = '<img src="'.$preimg.'" id="'.$att['field'].'_view" style="_height:100px;max-width:400px;max-height: 100px;">';
		}

		$forms =
<<<EOT
		<table border="0" cellspacing="0" cellpadding="0" width="90%" align="left" height="100%">
			<tr>
				<td width="480" style="border:none;" align="left">
					<input type="text" name="{$att['field']}" id="{$att['field']}"  class="insty" size="45" value="{$data[$att['field']]}" >&nbsp;&nbsp;
					<input type="button" plugin="{$plugin}"  name="{$att['field']}" view="{$att['attribute']['view']}" class="scbtn7" value="{$att['attribute']['butname']}" /> &nbsp;&nbsp;{$manager}
				</td>
				<td style="border:none;padding:2px;text-align:left">{$view}</td>
			</tr>
		</table>
EOT;
		}elseif($att['attribute']['multi']==1){ //批量上传

		//获取管理员与权限的session,传入批量上传按钮，解决flash批量上传session丢失的问题，同时传入session_id防止注入
		$sid   = PHPSESSID;
		$uid   = session('uid');
		$super = session('super_admin');

		$imglist = unserialize($data[$att['field']]);

		$i=0;
		foreach ($imglist as $key=>$vo){
			if ($field['cover'] == $vo['path']){
				$cover = 'style="border:2px solid #417EB7"';
			}

			$multilist .=
<<<EOT
		<div class="imagebox" id="{$att['field']}_imgbox_{$i}">
			<div style="width:160px;height:160px;margin-bottom:10px;border:2px solid #f1f1f1; display: table-cell; vertical-align:middle;">
				<img src="{$vo['path']}" id="img_{$att['field']}{$i}" {$cover} onclick="setcover('{$i}','{$att['field']}')" style="max-height:155px;max-width:150px;_width:155px;_height:155px;"></div>
				<input type="hidden" id="{$att['field']}_path_{$i}" name="{$att['field']}[{$i}][path]" value="{$vo['path']}">
			<div class="batact">
				<p>排序：<input type="text" class="insty" style="height:24px" name="{$att['field']}[{$i}][sort]" value="{$vo['sort']}"  size="10">
				<a href="javascript:void(0)" onclick="$('#multimg_imgbox_{$i}').remove()" style="margin-left:10px">删除</a></p>
				<p>描述：<input type="text" name="{$att['field']}[{$i}][note]" value="{$vo['note']}" size="18" class="insty" style="height:24px"></p>
			</div>
		</div>
EOT;
		$i++;
		}

		//图片预览
		if ($att['attribute']['view']=='true') {
			$view = '<div id="'.$att['field'].'_view" class="multilist l">'.$multilist.'</div>';
		}

		$forms =
<<<EOT
		<input type="hidden" id="{$att['field']}_cover" name="{$att['field']}_cover"  value="{$data[$att['field']][cover]}">

		<div style="width:100%;float:left">
			<input type="button" name="{$att['field']}" plugin="multiimage" super="{$super}" uid="{$uid}" sid="{$sid}" view="{$att['attribute']['view']}" value="{$att['attribute']['butname']}" class="scbtn5 l"/>
			<div style="margin-top:9px;margin-left:10px;float:left">{$att['tips']}</div>
			{$view}
		</div>
EOT;
		}
		}
		return $forms;
	}



	//联动菜单
	function linkage($att,$data){
		if (empty($data[$att['field']])) {
			$value = $att['attribute']['defval'];
		}else{
			$value = $data[$att['field']];
		}
		$forms = '<input type="hidden" name="'.$att['field'].'" value="'.$value.'" >';
		$forms .= '<script type="text/javascript">linkage.select("'.$att['attribute']['lid'].'","'.$att['field'].'","'.$att['attribute']['default'].'","'.$att['attribute']['depth'].'");</script>';
		return $forms;
	}

	//货币
	function currency($att,$data){
		//获取默认货币设置
		$currency = M('currency')->where('curdef=1 AND status=1')->find();

		$tips  = empty($att['tips']) ? '' : '<cite>'.$att['tips'].'</cite>';
		$value = empty($data[$att['field']]) ? $att['attribute']['value'] : $data[$att['field']];
		return $currency['cursign'].' <input type="text" name="'.$att['field'].'" value="'.format_money($value,'','',2,false,false).'" id="'.$att['field'].'" class="insty '.$att['css'].'" size="'.$att['attribute']['size'].'" '.$att['addattach'].' onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}"> '.$currency['curunit'].$tips;
	}


	function fordate($datetime){
		switch ($datetime){
			case 'YYYY-MM-DD HH:mm:ss':
				$format = 'y-m-d H:i:s';
				break;
			case 'YYYY-MM-DD':
				$format = 'Y-m-d';
				break;
			case 'YYYY-MM':
				$format = 'Y-m';
				break;
			case 'MM-dd':
				$format = 'm-d';
				break;
			case 'H:i:s':
				$format = 'H:i:s';
				break;
			case 'yyyy年MM月dd日 HH时mm分ss秒':
				$format = 'Y年m月d日 H时i分s秒';
				break;
			case 'yyyy年MM月dd日':
				$format = 'Y年m月d日';
				break;
			case 'MM月dd日':
				$format = 'm月d日';
				break;
			case 'HH时mm分ss秒':
				$format = 'H时i分s秒';
				break;
			default:
				$format = 'Y-m-d H:i:s';
		}
		return $format;
	}

?>