File: /www/wwwroot/bs.kntsleep.com/system/basic/class/form.class.php
<?php
/*
* Copyright (c) Huyin Information Technology Co., Ltd. All Rights Reserved.
* BOSSCMS Content Management System (https://www.bosscms.net/)
*/
defined('IS_OK') or exit('Access Forbidden');
class form
{
/**
* 返回选中所需字符
*
* @param string $judge 筛选项的值
* @param string|array $value 选中项的值
* @param string|array $default 默认选中项的值
* @param string $export 选中输出的内容
*/
public static function choose($judge, $value, $default=null, $export='selected'){
if(isset($value)){
if(!is_array($value)){
$val = json::decode($value);
$value = $val?$val:array($value);
}
foreach($value as $val){
if($val == $judge){
return " {$export}";
}
}
}else if(isset($default)){
if(!is_array($default)){
$def = json::decode($default);
$default = $def?$def:array($default);
}
foreach($default as $def){
if($def == $judge){
return " {$export}";
}
}
}else{
return '';
}
}
/**
* 返回值
*
* @param string $value 输出值
* @param string $default 默认值
*/
public static function value($value, $default=null){
$result = null;
if(isset($value)){
$result = $value;
}else if(isset($default)){
$result = $default;
}
return is_array($result)?json::encode($result):$result;
}
/**
* input文本框控件设置
*
* @param string $name
* @param string $value 文本框的值
* @param string $default 文本框的默认值
* @param string $type 文本框类型
* @param string $attribute 控件的各种属性数组(其中‘必填’的required属性为开启)
*/
public static function input($name, $value, $default=null, $type='text', $attribute=array('required'), $code=true){
$html = '';
if($code){
$html .= "<code class=\"input\">";
}
if($attribute['tag']){
$html .= "<ins class=\"tag\">{$attribute['tag']}</ins>";
}else if($attribute['tags']){
$html .= "<ins class=\"tags\">{$attribute['tags']}</ins>";
}
$html .= "<input";
if($name){
$html .= " name=\"{$name}\"";
}
if(isset($attribute)){
foreach($attribute as $key=>$val){
$val = quotesFilter($val);
if(is_string($key)){
$html .= " {$key}=\"{$val}\"";
}else{
$html .= " {$val}";
}
}
}
if($type){
$html .= " type=\"{$type}\"";
}
$value = self::value($value, $default);
if(isset($value)){
$value = quotesFilter($value);
$html .= " value=\"{$value}\"";
}
$html .= "/>";
if($attribute['unit']){
$html .= "<ul class=\"unit\"><li>{$attribute['unit']}</li></ul>";
}
if($code){
$html .= "</code>";
}
if($type=='hidden' && !isset($value)){
$html = '';
}
return $html;
}
/**
* file文件上传控件设置
*
* @param string $name
* @param string $value 文本框的值
* @param string $default 文本框的默认值
* @param string $attribute 控件的各种属性数组(其中‘必填’的required,multiple属性为开启)
*/
public static function files($name, $value, $default=null, $attribute=array('required','multiple'), $code=true){
$html = '';
if(defined('IS_INSIDE')){
$attribute['file'] = null;
$html .= self::textarea($name, $value, $default, $attribute, $code);
}else{
if($code){
$html .= "<code class=\"file\">";
}
$html .= self::input($name, $value, $default, 'file', $attribute, false);
if($code){
$html .= "</code>";
}
}
return $html;
}
/**
* radio单选项控件设置
*
* @param string $name
* @param string $value 选中该选项的值
* @param string $default 选中该选项的默认值
* @param array $param 选项控件的所属选项列表数组
* @param string $attribute 控件的各种属性数组(其中‘必填’的required属性为开启)
*/
public static function radio($name, $value, $default=null, $param, $attribute=array(), $code=true){
$html = '';
if(!is_array($attribute)){
$attribute = array();
}
if($code){
if(defined('IS_INSIDE') && count($param)==2 && !in_array('no',$attribute) && $attribute['color']){
$color = $attribute['color'];
}
$html .= "<code class=\"radio {$color}\">";
}
$i = 0;
$required = '';
if(isset($attribute['required'])){
unset($attribute['required']);
$required = 'required';
}
if($param){
if(defined('IS_INSIDE') && count($param)==2 && !in_array('no',$attribute)){
$html .= '<div><ul'.(self::choose(key($param), $value, $default, 'checked')?' class="on"':'').'>';
foreach($param as $key=>$val){
$html .= "<li value=\"{$key}\">{$val}</li>";
}
$html .= '</ul></div>';
$attribute['type'] = 'radio';
$html .= self::textarea($name, $value, $default, $attribute, false);
}else{
foreach($param as $key=>$val){
$attribute[99999] = self::choose($key, $value, $default, 'checked');
$html .= "<label class=\"radio {$required} {$attribute[99999]}\">";
$html .= self::input($name, $key, null, 'radio', $attribute, false);
$html .= "<i></i>";
if(isset($val)){
$html .= "<ins>{$val}</ins>";
}
$html .= "</label>\n";
$i++;
}
}
}
if($code){
$html .= "</code>";
}
return $html;
}
/**
* checkbox多选项控件设置
*
* @param string $name
* @param string $value 选中该选项的值
* @param string $default 选中该选项的默认值
* @param array $param 选项控件的所属选项列表数组
* @param string $attribute 控件的各种属性数组(其中‘必填’的required属性为开启)
* boss-cms
*/
public static function checkbox($name, $value, $default=null, $param, $attribute=array(), $code=true){
$html = '';
if($code){
$html .= "<code class=\"checkbox\">";
}
if(isset($attribute['default'])){
unset($attribute['default']);
$html .= self::input($name, '' , null, 'hidden', null, false);
}
$i = 0;
$required = '';
if(isset($attribute['required'])){
unset($attribute['required']);
$required = 'required';
}
if($param){
foreach($param as $key=>$val){
$attribute[99999] = self::choose($key, $value, $default, 'checked');
$html .= "<label class=\"checkbox {$required} {$attribute[99999]}\">";
$html .= self::input(($name?$name.'[]':''), $key, null, 'checkbox', $attribute, false);
$html .= "<i></i>";
if(isset($val)){
$html .= "<ins>{$val}</ins>";
}
$html .= "</label>\n";
$i++;
}
}
if($code){
$html .= "</code>";
}
return $html;
}
/**
* select选项控件设置
*
* @param string $name
* @param string|array $value 选中该选项的值
* @param string|array $default 选中该选项的默认值
* @param array $param 选项控件的所属选项列表数组
* @param array $attribute 控件的各种属性数组(其中‘必填’的required属性为开启)
*/
public static function select($name, $value, $default=null, $param, $attribute=array('required'), $code=true){
$html = '';
if($code){
$readonly = defined('IS_INSIDE')&&array_search('readonly',$attribute)?'readonly':'';
$tight = preg_match('/^\w+$/',$attribute['tight'])?'tight':'';
$html .= "<code class=\"select {$readonly} {$tight}\">";
}
if(defined('IS_INSIDE')){
$li = '';
if(isset($param)){
if(isset($attribute['multiple']) || is_numeric(array_search('multiple',$attribute))){
$value = json::decode($value);
}
foreach($param as $key=>$val){
$on = self::choose($key, $value, $default)?' class="on"':'';
$li .= "<li".(strstr($key,P)?'':" value=\"".quotesFilter($key)."\"")."{$on}>{$val}</li>\n";
if($on){
$inpval = "value=\"".strip_tags($val)."\"";
}
}
}
if($attribute['width']){
$width = "width:".(strstr($attribute['width'],'%')?round(preg_replace('/[%\s]+/','',$attribute['width']))/100*768:$attribute['width'])."px;";
}
$height = $attribute['height']?"height:{$attribute['height']}px;":'';
if(isset($attribute['multiple']) || is_numeric(array_search('multiple',$attribute))){
$html .= "<div style=\"{$width}\"><dfn><u".($param&&count($param)==substr_count($li,'class="on"')?" class=\"on\"":'').">全选</u></dfn><ins><ul style=\"{$height}{$maxheight}\">{$li}</ul></ins></div>";
}else{
$placeholder = $attribute['placeholder']?"placeholder=\"{$attribute['placeholder']}\"":'';
$maxheight = $attribute['maxheight']?"max-height:{$attribute['maxheight']}px;":'';
$html .= "<dl style=\"{$width}\"><dt><input disabled {$placeholder} {$inpval}></dt><dd><ul style=\"{$height}{$maxheight}\">{$li}</ul></dd></dl>";
}
$html .= self::textarea($name, $value, $default, $attribute, false);
}else{
$html .= "<select ";
if($name){
$html .= " name=\"{$name}\"";
}
if(isset($attribute)){
foreach($attribute as $key=>$val){
$val = quotesFilter($val);
if(is_string($key)){
$html .= " {$key}=\"{$val}\"";
}else{
$html .= " {$val}";
}
}
}
$html .= ">";
if(preg_match('/^\w+$/',$attribute['tight'])){
$html .= "<option value=\"\">{$attribute['placeholder']}</option>";
}
if(isset($param)){
foreach($param as $key=>$val){
$html .= "<option value=\"".quotesFilter($key)."\" ".self::choose($key, $value, $default).">{$val}</option>\n";
}
}
$html .= "</select>";
}
if($code){
$html .= "</code>";
}
return $html;
}
/**
* textarea多行文本框控件设置
*
* @param string $name
* @param string|array $value 选中该选项的值
* @param string|array $default 选中该选项的默认值
* @param array $attribute 控件的各种属性数组(其中‘必填’的required属性为开启)
* BOOSCMS
*/
public static function textarea($name, $value, $default=null, $attribute=array('required'), $code=true){
$html = '';
if($code){
$html .= "<code class=\"textarea\">";
}
$html .= "<textarea ";
if($name){
$html .= " name=\"{$name}\"";
}
if(isset($attribute)){
foreach($attribute as $key=>$val){
$val = quotesFilter($val);
if(is_string($key)){
$html .= " {$key}=\"{$val}\"";
}else{
$html .= " {$val}";
}
}
}
$html .= " >";
$value = self::value($value, $default);
if(isset($value)){
$html .= preg_replace("/<\/(\s*)textarea/is",'</\\1textarea',$value);
}
$html .= "</textarea>";
if($code){
$html .= "</code>";
}
return $html;
}
}
?>