<?php

/**
 *--------------------------------------
 * 字段相关函数
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-2
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

/* 获得字段创建信息 */
function get_fieldMakeSQL($f_type, $f_name, $f_default, $f_length, $f_item_name)
{
	$fields = '';
	if('int' == $f_type || 'datetime' == $f_type)
	{
		if(empty($f_default) || preg_match('#[^0-9-]#', $f_default))
		{
			$f_default = 0;
		}
		$fields = " `{$f_name}` int(11) NOT NULL default '{$f_default}' COMMENT '{$f_item_name}'";
	}
	elseif('float' == $f_type)
	{
		if(empty($f_default) || preg_match("#[^0-9\.-]#", $f_default))
		{
			$f_default = 0;
		}
		$fields = " `{$f_name}` float NOT NULL default '{$f_default}' COMMENT '{$f_item_name}'";
	}
	elseif('img' == $f_type || 'addon' == $f_type)
	{
		if(empty($f_default))
		{
			$f_default = '';
		}
		if(empty($f_length))
		{
			$f_length = 96;
		}
		if($f_length <= 255)
		{
			$fields = " `{$f_name}` varchar({$f_length}) NOT NULL DEFAULT '{$f_default}' COMMENT '{$f_item_name}'";
		}
		else
		{
			$fields = " `{$f_name}` text NOT NULL COMMENT '{$f_item_name}'";
		}
	}
	elseif('htmltext' == $f_type)
	{
		$fields = " `{$f_name}` mediumtext NOT NULL COMMENT '{$f_item_name}'";
	}
	elseif('multitext' == $f_type)
	{
		$fields = " `{$f_name}` text NOT NULL COMMENT '{$f_item_name}'";
	}
	elseif('text' == $f_type)
	{
		if(empty($f_default))
		{
			$f_default = '';
		}
		if(empty($f_length))
		{
			$f_length = 96;
		}
		if($f_length <= 255)
		{
			$fields = " `{$f_name}` varchar({$f_length}) NOT NULL DEFAULT '{$f_default}' COMMENT '{$f_item_name}'";
		}
		else
		{
			$fields = " `{$f_name}` text NOT NULL COMMENT '{$f_item_name}'";
		}
	}
	elseif('checkbox' == $f_type)
	{
		if(!empty($f_default))
		{
			$f_comment = $f_default;
			$_t = explode(',', $f_default);
			$f_set = '';
			$f_default = '';
			foreach($_t as $k => $v)
			{
				$_t1 = explode('|', $v);
				$f_set .= "'".$_t1[0]."',";
				if(0 == $k)
				{
					$f_default = $_t1[0];
				}
			}
			$f_set = rtrim($f_set, ',');
			$fields = " `{$f_name}` set({$f_set}) NOT NULL DEFAULT '{$f_default}' COMMENT '{$f_item_name}:{$f_comment}'";
		}
	}
	elseif('radio' == $f_type || 'select' == $f_type)
	{
		if(!empty($f_default))
		{
			$f_comment = $f_default;
			$_t = explode(',', $f_default);
			$f_set = '';
			$f_default = '';
			foreach($_t as $k => $v)
			{
				$_t1 = explode('|', $v);
				$f_set .= "'".$_t1[0]."',";
				if(0 == $k)
				{
					$f_default = $_t1[0];
				}
			}
			$f_set = rtrim($f_set, ',');
			$fields = " `{$f_name}` enum({$f_set}) NOT NULL DEFAULT '{$f_default}' COMMENT '{$f_item_name}:{$f_comment}'";
		}
	}
	return $fields;
}

/* 生成一个附加表单 $data:数据值, $isMember:前台调用 */
function get_formItem($tag, $params, $data = null, $isMember = false)
{
	$formitem = "<tr><td colspan='2' class='inputTitle'>{$params['f_item_name']}</td></tr>";
	if('int' == $params['f_type'] || 'float' == $params['f_type'])
	{
		if(!is_null($data) and isset($data[$tag]))
		{
			$params['f_default'] = $data[$tag];
		}
		$formitem .= "<tr><td colspan='2' class='inputArea'>";
		$formitem .= "<input class='i' type='text' value='{$params['f_default']}' name='{$tag}' maxlength='{$params['f_length']}' size='9' />";
		$formitem .= "</td></tr>";
	}
	elseif('text' == $params['f_type'])
	{
		if(!is_null($data) and isset($data[$tag]))
		{
			$params['f_default'] = $data[$tag];
		}
		$formitem .= "<tr><td colspan='2' class='inputArea'>";
		$formitem .= "<input class='i' type='text' value='{$params['f_default']}' name='{$tag}' maxlength='{$params['f_length']}' size='30' />";
		$formitem .= "</td></tr>";
	}
	elseif('multitext' == $params['f_type'])
	{
		if(!is_null($data) and isset($data[$tag]))
		{
			$params['f_default'] = $data[$tag];
		}
		$formitem .= "<tr><td colspan='2' class='inputArea'>";
		$formitem .= "<textarea class='i' name='{$tag}' style='width:360px;height:60px;'>{$params['f_default']}</textarea>";
		$formitem .= "</td></tr>";
	}
	elseif('htmltext' == $params['f_type'])
	{
		if(!is_null($data) and isset($data[$tag]))
		{
			$params['f_default'] = htmlspecialchars($data[$tag]);
		}
		$formitem .= "<tr><td colspan='2' class='inputArea'>";
		$formitem .= "<div style='padding:0 0 5px'>";
		if(!$isMember)
		{
			if(1 == $params['f_save_remote'])
			{
				$formitem .= "<label><input type='checkbox' value='{$tag}' name='save_remote_source[]'> ".L('SAVE_REMOTE_SOURCE').'</label> ';
			}
			if(1 == $params['f_watermark_remote'])
			{
				$formitem .= "<label><input type='checkbox' value='{$tag}' name='watermark_remote_img[]'> ".L('WATERMARK_REMOTE_IMG').'</label> ';
			}
			if(1 == $params['f_get_thumb'])
			{
				$formitem .= "<label><input type='checkbox' value='{$tag}' name='first_img_as_thumb[]'> ".L('FIRST_IMG_AS_THUMB').'</label> ';
			}
			if(1 == $params['f_get_abstract'])
			{
				$formitem .= "<label><input type='checkbox' value='{$tag}' name='get_abstract[]'> ".L('GET_ABSTRACT').'</label> ';
			}
		}
		if(1 == $params['f_is_paging'])
		{
			$formitem .= "<label class='fc_gry'>[".L('UWA_PAGING_TIP').']</label> ';
		}
		$formitem .= "</div>";
		if(1 == $params['f_is_paging'])
		{
			$formitem .= "<textarea class='editor_paging' id='{$tag}' name='{$tag}' style='width:95%;height:240px;'>{$params['f_default']}</textarea>";
		}
		else
		{
			$formitem .= "<textarea class='editor' id='{$tag}' name='{$tag}' style='width:95%;height:240px;'>{$params['f_default']}</textarea>";
		}
		$formitem .= "</td></tr>";
	}
	elseif('checkbox' == $params['f_type'])
	{
		$_data = array();
		if(!is_null($data) and isset($data[$tag]))
		{
			$_data = explode(',', $data[$tag]);
		}

		if(!empty($params['f_default']))
		{
			$formitem .= "<tr><td colspan='2' class='inputArea'>";
			$_t = explode(',', $params['f_default']);
			foreach($_t as $v)
			{
				$_t1 = explode('|', $v);
				if(in_array($_t1[0], $_data))
				{
					$formitem .= "<label><input type='checkbox' name='{$tag}[]' value='{$_t1[0]}' checked='checked'> {$_t1[1]}</label> ";
				}
				else
				{
					$formitem .= "<label><input type='checkbox' name='{$tag}[]' value='{$_t1[0]}'> {$_t1[1]}</label> ";
				}
			}
			$formitem .= "</td></tr>";
		}
	}
	elseif('radio' == $params['f_type'])
	{
		$_data = '';
		if(!is_null($data) and isset($data[$tag]))
		{
			$_data = $data[$tag];
		}

		if(!empty($params['f_default']))
		{
			$formitem .= "<tr><td colspan='2' class='inputArea'>";
			$_t = explode(',', $params['f_default']);
			foreach($_t as $k=> $v)
			{
				$_t1 = explode('|', $v);
				if(($_data == $_t1[0]) or (empty($_data) and 0==$k))
				{
					$formitem .= "<label><input type='radio' name='{$tag}' value='{$_t1[0]}' checked='checked'> {$_t1[1]}</label> ";
				}
				else
				{
					$formitem .= "<label><input type='radio' name='{$tag}' value='{$_t1[0]}'> {$_t1[1]}</label> ";
				}
			}
			$formitem .= "</td></tr>";
		}
	}
	elseif('select' == $params['f_type'])
	{
		$_data = '';
		if(!is_null($data) and isset($data[$tag]))
		{
			$_data = $data[$tag];
		}

		if(!empty($params['f_default']))
		{
			$formitem .= "<tr><td colspan='2' class='inputArea'><select name='{$tag}'>";
			$_t = explode(',', $params['f_default']);
			foreach($_t as $v)
			{
				$_t1 = explode('|', $v);
				if($_data == $_t1[0])
				{
					$formitem .= "<option value='{$_t1[0]}' selected='selected'>{$_t1[1]}</option>";
				}
				else
				{
					$formitem .= "<option value='{$_t1[0]}'>{$_t1[1]}</option>";
				}
			}
			$formitem .= "</select></td></tr>";
		}
	}
	elseif('datetime' == $params['f_type'])
	{
		if(!is_null($data) and isset($data[$tag]))
		{
			$params['f_default'] = $data[$tag];
		}
		$formitem .= "<tr><td colspan='2' class='inputArea'>";
		$formitem .= "<input size='20' id='{$tag}' class='i' name='{$tag}' value='".date('Y-m-d H:i:s', $params['f_default'])."' />";
		$formitem .= "<script type='text/javascript'>
			Calendar.setup({
				inputField : '{$tag}',
				trigger : '{$tag}',
				onSelect : function(){this.hide()},
				weekNumbers : true,
				showTime : 24,
				dateFormat : '%Y-%m-%d %H:%M:%S'
			});
		</script>";
		$formitem .= "</td></tr>";
	}
	elseif('img' == $params['f_type'])
	{
		if(!is_null($data) and isset($data[$tag]))
		{
			$params['f_default'] = $data[$tag];
		}
		$formitem .= "<tr><td colspan='2' class='inputArea'>";
		$formitem .= "<input id='{$tag}' class='i' type='text' value='{$params['f_default']}' name='{$tag}' maxlength='{$params['f_length']}' size='50' /> ";
		$formitem .= "<input id='{$tag}_uploader' to='#{$tag}' btntext='".L('UPLOAD')."' class='uploader' typeset='image' type='file' /> ";
		$formitem .= "</td></tr>";
	}
	elseif('addon' == $params['f_type'])
	{
		if(!is_null($data) and isset($data[$tag]))
		{
			$params['f_default'] = $data[$tag];
		}
		$formitem .= "<tr><td colspan='2' class='inputArea'>";
		$formitem .= "<input id='{$tag}' class='i' type='text' value='{$params['f_default']}' name='{$tag}' maxlength='{$params['f_length']}' size='50' /> ";
		$formitem .= "<input id='{$tag}_uploader' to='#{$tag}' btntext='".L('UPLOAD')."' class='uploader' typeset='all' type='file' /> ";
		$formitem .= "</td></tr>";
	}
	return $formitem;
}

/* 获取字段值 */
function get_fieldValue($tag, $params, $data)
{
	if(!isset($data[$tag]))
	{
		return '';
	}
	if('int' == $params['f_type'])
	{
		$fieldValue = intval($data[$tag]);
	}
	elseif('float' == $params['f_type'])
	{
		$fieldValue = floatval($data[$tag]);
	}
	elseif('text' == $params['f_type'])
	{
		if(MAGIC_QUOTES_GPC)
		{
			$fieldValue = addslashes(AFilter::plain_text(stripslashes($data[$tag])));
		}
		else
		{
			$fieldValue = AFilter::plain_text($data[$tag]);
		}

	}
	elseif('multitext' == $params['f_type'])
	{
		if(MAGIC_QUOTES_GPC)
		{
			$fieldValue = addslashes(AFilter::text(stripslashes($data[$tag])));
		}
		else
		{
			$fieldValue = AFilter::text($data[$tag]);
		}
	}
	elseif('htmltext' == $params['f_type'])
	{
		if(MAGIC_QUOTES_GPC)
		{
			$fieldValue = addslashes(AFilter::safeHtml(stripslashes($data[$tag])));
		}
		else
		{
			$fieldValue = AFilter::safeHtml($data[$tag]);
		}
	}
	elseif('checkbox' == $params['f_type'])
	{
		if(MAGIC_QUOTES_GPC)
		{
			$fieldValue = addslashes(AFilter::text(stripslashes(implode(',', $data[$tag]))));
		}
		else
		{
			$fieldValue = AFilter::text(implode(',', $data[$tag]));
		}
	}
	elseif('radio' == $params['f_type'])
	{
		if(MAGIC_QUOTES_GPC)
		{
			$fieldValue = addslashes(AFilter::text(stripslashes($data[$tag])));
		}
		else
		{
			$fieldValue = AFilter::text($data[$tag]);
		}
	}
	elseif('select' == $params['f_type'])
	{
		if(MAGIC_QUOTES_GPC)
		{
			$fieldValue = addslashes(AFilter::text(stripslashes($data[$tag])));
		}
		else
		{
			$fieldValue = AFilter::text($data[$tag]);
		}
	}
	elseif('datetime' == $params['f_type'])
	{
		$fieldValue = intval(strtotime($data[$tag]));
	}
	elseif('img' == $params['f_type'])
	{
		if(MAGIC_QUOTES_GPC)
		{
			$fieldValue = addslashes(AFilter::text(stripslashes($data[$tag])));
		}
		else
		{
			$fieldValue = AFilter::text($data[$tag]);
		}
	}
	elseif('addon' == $params['f_type'])
	{
		if(MAGIC_QUOTES_GPC)
		{
			$fieldValue = addslashes(AFilter::text(stripslashes($data[$tag])));
		}
		else
		{
			$fieldValue = AFilter::text($data[$tag]);
		}
	}
	return $fieldValue;
}

?>