|
发表于 2017-11-14 14:06:17
|
显示全部楼层
|阅读模式
来自 中国–河南–新乡
Discuz!为了界面统一,专门封装了一个表单生成函数showsetting(),使用改函数几乎可以生成任意web表单,本文就来介绍一下这个函数的用法:
定义文件:\source\function\function_admincp.php
函数定义:
- function showsetting($setname, $varname, $value, $type = 'radio', $disabled = '', $hidden = 0, $comment = '', $extra = '', $setid = '', $nofaq = false) {
-
- global $_G;
- $s = "\n";
- $check = array();
- $noborder = false;
- if(substr($disabled, 0, 8) == 'noborder') {
- $disabled = trim(substr($disabled, 8));
- $noborder = 'class="noborder" ';
- }
- $check['disabled'] = $disabled ? ($disabled == 'readonly' ? ' readonly' : ' disabled') : '';
- $check['disabledaltstyle'] = $disabled ? ', 1' : '';
-
-
- $nocomment = false;
-
-
- if(isset($_G['showsetting_multi'])) {
- $hidden = 0;
- if(is_array($varname)) {
- $varnameid = '_'.str_replace(array('[', ']'), '_', $varname[0]).'|'.$_G['showsetting_multi'];
- $varname[0] = preg_replace('/\w+new/', 'multinew['.$_G['showsetting_multi'].'][\\0]', $varname[0]);
- } else {
- $varnameid = '_'.str_replace(array('[', ']'), '_', $varname).'|'.$_G['showsetting_multi'];
- $varname = preg_replace('/\w+new/', 'multinew['.$_G['showsetting_multi'].'][\\0]', $varname);
- }
- } else {
- $varnameid = '';
- }
-
-
- if($type == 'radio') {
- $value ? $check['true'] = "checked" : $check['false'] = "checked";
- $value ? $check['false'] = '' : $check['true'] = '';
- $check['hidden1'] = $hidden ? ' onclick="$(\'hidden_'.$setname.'\').style.display = \'\';"' : '';
- $check['hidden0'] = $hidden ? ' onclick="$(\'hidden_'.$setname.'\').style.display = \'none\';"' : '';
- $onclick = $disabled && $disabled == 'readonly' ? ' onclick="return false"' : ($extra ? $extra : '');
- $s .= '<ul onmouseover="altStyle(this'.$check['disabledaltstyle'].');">'.
- '<li'.($check['true'] ? ' class="checked"' : '').'><input class="radio" type="radio"'.($varnameid ? ' id="_v1_'.$varnameid.'"' : '').' name="'.$varname.'" value="1" '.$check['true'].$check['hidden1'].$check['disabled'].$onclick.'> '.cplang('yes').'</li>'.
- '<li'.($check['false'] ? ' class="checked"' : '').'><input class="radio" type="radio"'.($varnameid ? ' id="_v0_'.$varnameid.'"' : '').' name="'.$varname.'" value="0" '.$check['false'].$check['hidden0'].$check['disabled'].$onclick.'> '.cplang('no').'</li>'.
- '</ul>';
- } elseif($type == 'text' || $type == 'password' || $type == 'number') {
- $s .= '<input name="'.$varname.'" value="'.dhtmlspecialchars($value).'" type="'.$type.'" class="txt" '.$check['disabled'].' '.$extra.' />';
- } elseif($type == 'htmltext') {
- $id .= 'html'.random(2);
- $s .= '<div id="'.$id.'">'.$value.'</div><input id="'.$id.'_v" name="'.$varname.'" value="'.dhtmlspecialchars($value).'" type="hidden" /><script type="text/javascript">sethtml(\''.$id.'\')</script>';
- } elseif($type == 'file') {
- $s .= '<input name="'.$varname.'" value="" type="file" class="txt uploadbtn marginbot" '.$check['disabled'].' '.$extra.' />';
- } elseif($type == 'filetext') {
- $defaulttype = $value ? 1 : 0;
- $id = 'file'.random(2);
- $s .= '<input id="'.$id.'_0" style="display:'.($defaulttype ? 'none' : '').'" name="'.($defaulttype ? 'TMP' : '').$varname.'" value="" type="file" class="txt uploadbtn marginbot" '.$check['disabled'].' '.$extra.' />'.
- '<input id="'.$id.'_1" style="display:'.(!$defaulttype ? 'none' : '').'" name="'.(!$defaulttype ? 'TMP' : '').$varname.'" value="'.dhtmlspecialchars($value).'" type="text" class="txt marginbot" '.$extra.' /><br />'.
- '<a id="'.$id.'_0a" style="'.(!$defaulttype ? 'font-weight:bold' : '').'" href="javascript:;" onclick="$(\''.$id.'_1a\').style.fontWeight = \'\';this.style.fontWeight = \'bold\';$(\''.$id.'_1\').name = \'TMP'.$varname.'\';$(\''.$id.'_0\').name = \''.$varname.'\';$(\''.$id.'_0\').style.display = \'\';$(\''.$id.'_1\').style.display = \'none\'">'.cplang('switch_upload').'</a> '.
- '<a id="'.$id.'_1a" style="'.($defaulttype ? 'font-weight:bold' : '').'" href="javascript:;" onclick="$(\''.$id.'_0a\').style.fontWeight = \'\';this.style.fontWeight = \'bold\';$(\''.$id.'_0\').name = \'TMP'.$varname.'\';$(\''.$id.'_1\').name = \''.$varname.'\';$(\''.$id.'_1\').style.display = \'\';$(\''.$id.'_0\').style.display = \'none\'">'.cplang('switch_url').'</a>';
- } elseif($type == 'textarea') {
- $readonly = $disabled ? 'readonly' : '';
- $s .= "<textarea $readonly rows="6" ".(!isset($_G['showsetting_multi']) ? "ondblclick="textareasize(this, 1)"" : '')." onkeyup="textareasize(this, 0)" onkeydown="textareakey(this, event)" name="$varname" id="$varname" cols="50" class="tarea" $extra>".dhtmlspecialchars($value)."</textarea>";
- } elseif($type == 'select') {
- $s .= '<select name="'.$varname[0].'" '.$extra.'>';
- foreach($varname[1] as $option) {
- if(!array_key_exists(0, $option)) {
- $option = array_values($option);
- }
- $selected = $option[0] == $value ? 'selected="selected"' : '';
- if(empty($option[2])) {
- $s .= "<option value="$option[0]" $selected>".$option[1]."</option>\n";
- } else {
- $s .= "<optgroup label="".$option[1].""></optgroup>\n";
- }
- }
- $s .= '</select>';
- } elseif($type == 'mradio' || $type == 'mradio2') {
- $nocomment = $type == 'mradio2' && !isset($_G['showsetting_multi']) ? true : false;
- $addstyle = $nocomment ? ' style="float: left; width: 18%"' : '';
- $ulstyle = $nocomment ? ' style="width: 790px"' : '';
- if(is_array($varname)) {
- $radiocheck = array($value => ' checked');
- $s .= '<ul'.(empty($varname[2]) ? ' class="nofloat"' : '').' onmouseover="altStyle(this'.$check['disabledaltstyle'].');"'.$ulstyle.'>';
- foreach($varname[1] as $varary) {
- if(is_array($varary) && !empty($varary)) {
- if(!array_key_exists(0, $varary)) {
- $varary = array_values($varary);
- }
- $onclick = '';
- if(!isset($_G['showsetting_multi']) && !empty($varary[2])) {
- foreach($varary[2] as $ctrlid => $display) {
- $onclick .= '$(\''.$ctrlid.'\').style.display = \''.$display.'\';';
- }
- }
- $onclick && $onclick = ' onclick="'.$onclick.'"';
- $s .= '<li'.($radiocheck[$varary[0]] ? ' class="checked"' : '').$addstyle.'><input class="radio" type="radio"'.($varnameid ? ' id="_v'.md5($varary[0]).'_'.$varnameid.'"' : '').' name="'.$varname[0].'" value="'.$varary[0].'"'.$radiocheck[$varary[0]].$check['disabled'].$onclick.'> '.$varary[1].'</li>';
- }
- }
- $s .= '</ul>';
- }
- } elseif($type == 'mcheckbox' || $type == 'mcheckbox2') {
- $nocomment = $type != 'mcheckbox2' && count($varname[1]) > 3 && !isset($_G['showsetting_multi']) ? true : false;
- $addstyle = $nocomment ? ' style="float: left;'.(empty($_G['showsetting_multirow']) ? ' width: 18%;overflow: hidden;' : '').'"' : '';
- $ulstyle = $nocomment && empty($_G['showsetting_multirow']) ? ' style="width: 790px"' : '';
- $s .= '<ul class="nofloat" onmouseover="altStyle(this'.$check['disabledaltstyle'].');"'.$ulstyle.'>';
- foreach($varname[1] as $varary) {
- if(is_array($varary) && !empty($varary)) {
- if(!array_key_exists(0, $varary)) {
- $varary = array_values($varary);
- }
- $onclick = !isset($_G['showsetting_multi']) && !empty($varary[2]) ? ' onclick="$(\''.$varary[2].'\').style.display = $(\''.$varary[2].'\').style.display == \'none\' ? \'\' : \'none\';"' : '';
- $checked = is_array($value) && in_array($varary[0], $value) ? ' checked' : '';
- $s .= '<li'.($checked ? ' class="checked"' : '').$addstyle.' title="'.dhtmlspecialchars($varary[1]).'"><input class="checkbox" type="checkbox"'.($varnameid ? ' id="_v'.md5($varary[0]).'_'.$varnameid.'"' : '').' name="'.$varname[0].'[]" value="'.$varary[0].'"'.$checked.$check['disabled'].$onclick.'> '.$varary[1].'</li>';
- }
- }
- $s .= '</ul>';
- } elseif($type == 'binmcheckbox') {
- $checkboxs = count($varname[1]);
- $value = sprintf('%0'.$checkboxs.'b', $value);$i = 1;
- $s .= '<ul class="nofloat" onmouseover="altStyle(this'.$check['disabledaltstyle'].');">';
- foreach($varname[1] as $key => $var) {
- if($var !== false) {
- $s .= '<li'.($value{$checkboxs - $i} ? ' class="checked"' : '').'><input class="checkbox" type="checkbox"'.($varnameid ? ' id="_v'.md5($i).'_'.$varnameid.'"' : '').' name="'.$varname[0].'['.$i.']" value="1"'.($value{$checkboxs - $i} ? ' checked' : '').' '.(!empty($varname[2][$key]) ? $varname[2][$key] : '').'> '.$var.'</li>';
- }
- $i++;
- }
- $s .= '</ul>';
- } elseif($type == 'omcheckbox') {
- $nocomment = count($varname[1]) > 3 ? true : false;
- $addstyle = $nocomment ? 'style="float: left; width: 18%"' : '';
- $ulstyle = $nocomment ? 'style="width: 790px"' : '';
- $s .= '<ul onmouseover="altStyle(this'.$check['disabledaltstyle'].');"'.(empty($varname[2]) ? ' class="nofloat"' : 'class="ckbox"').' '.$ulstyle.'>';
- foreach($varname[1] as $varary) {
- if(is_array($varary) && !empty($varary)) {
- $checked = is_array($value) && $value[$varary[0]] ? ' checked' : '';
- $s .= '<li'.($checked ? ' class="checked"' : '').' '.$addstyle.'><input class="checkbox" type="checkbox" name="'.$varname[0].'['.$varary[0].']" value="'.$varary[2].'"'.$checked.$check['disabled'].'> '.$varary[1].'</li>';
- }
- }
- $s .= '</ul>';
- } elseif($type == 'mselect') {
- $s .= '<select name="'.$varname[0].'" multiple="multiple" size="10" '.$extra.'>';
- foreach($varname[1] as $option) {
- if(!array_key_exists(0, $option)) {
- $option = array_values($option);
- }
- $selected = is_array($value) && in_array($option[0], $value) ? 'selected="selected"' : '';
- if(empty($option[2])) {
- $s .= "<option value="$option[0]" $selected>".$option[1]."</option>\n";
- } else {
- $s .= "<optgroup label="".$option[1].""></optgroup>\n";
- }
- }
- $s .= '</select>';
- } elseif($type == 'color') {
- global $stylestuff;
- $preview_varname = str_replace('[', '_', str_replace(']', '', $varname));
- $code = explode(' ', $value);
- $css = '';
- for($i = 0; $i <= 1; $i++) {
- if($code[$i] != '') {
- if($code[$i]{0} == '#') {
- $css .= strtoupper($code[$i]).' ';
- } elseif(preg_match('/^http:\/\//i', $code[$i])) {
- $css .= 'url(\''.$code[$i].'\') ';
- } else {
- $css .= 'url(\''.$stylestuff['imgdir']['subst'].'/'.$code[$i].'\') ';
- }
- }
- }
- $background = trim($css);
- $colorid = ++$GLOBALS['coloridcount'];
- $s .= "<input id="c{$colorid}_v" type="text" class="txt" style="float:left; width:210px;" value="$value" name="$varname" onchange="updatecolorpreview('c{$colorid}')">\n".
- "<input id="c$colorid" onclick="c{$colorid}_frame.location='static/image/admincp/getcolor.htm?c{$colorid}|c{$colorid}_v';showMenu({'ctrlid':'c$colorid'})" type="button" class="colorwd" value="" style="background: $background"><span id="c{$colorid}_menu" style="display: none"><iframe name="c{$colorid}_frame" src="" frameborder="0" width="210" height="148" scrolling="no"></iframe></span>\n$extra";
- } elseif($type == 'calendar') {
- $s .= "<input type="text" class="txt" name="$varname" value="".dhtmlspecialchars($value)."" onclick="showcalendar(event, this".($extra ? ', 1' : '').")">\n";
- } elseif(in_array($type, array('multiply', 'range', 'daterange'))) {
- $onclick = $type == 'daterange' ? ' onclick="showcalendar(event, this)"' : '';
- if(isset($_G['showsetting_multi'])) {
- $varname[1] = preg_replace('/\w+new/', 'multinew['.$_G['showsetting_multi'].'][\\0]', $varname[1]);
- }
- $s .= "<input type="text" class="txt" name="$varname[0]" value="".dhtmlspecialchars($value[0])."" style="width: 108px; margin-right: 5px;"$onclick>".($type == 'multiply' ? ' X ' : ' -- ')."<input type="text" class="txt" name="$varname[1]" value="".dhtmlspecialchars($value[1]).""class="txt" style="width: 108px; margin-left: 5px;"$onclick>";
- } else {
- $s .= $type;
- }
- $name = cplang($setname);
- $name .= $name && substr($name, -1) != ':' ? ':' : '';
- $name = $disabled ? '<span class="lightfont">'.$name.'</span>' : $name;
- $setid = !$setid ? substr(md5($setname), 0, 4) : $setid;
- $setid = isset($_G['showsetting_multi']) ? 'S'.$setid : $setid;
- if(!empty($_G['showsetting_multirow'])) {
- if(empty($_G['showsetting_multirow_n'])) {
- echo '<tr>';
- }
- echo '<td class="vtop rowform"><p class="td27m">'.$name.'</p>'.$s.'</td>';
- $_G['showsetting_multirow_n']++;
- if($_G['showsetting_multirow_n'] == 2) {
- if(empty($_G['showsetting_multirow_n'])) {
- echo '</tr>';
- }
- $_G['showsetting_multirow_n'] = 0;
- }
- return;
- }
- if(!isset($_G['showsetting_multi'])) {
- if(!$nofaq) {
- $faqurl = 'http://faq.comsenz.com?type=admin&ver='.$_G['setting']['version'].'&action='.rawurlencode($_GET['action']).'&operation='.rawurlencode($_GET['operation']).'&key='.rawurlencode($setname);
- showtablerow($noborder.'onmouseover="setfaq(this, \'faq'.$setid.'\')"', 'colspan="2" class="td27" s="1"', $name.'<a id="faq'.$setid.'" class="faq" title="'.cplang('setting_faq_title').'" href="'.$faqurl.'" target="_blank" style="display:none"> </a>');
- } else {
- showtablerow('', 'colspan="2" class="td27" s="1"', $name);
- }
- } else {
- if(empty($_G['showsetting_multijs'])) {
- $_G['setting_JS'] .= 'var ss = new Array();';
- $_G['showsetting_multijs'] = 1;
- }
- if($_G['showsetting_multi'] == 0) {
- showtablerow('', array('class="td27"'), array('<div id="D'.$setid.'"></div>'));
- $_G['setting_JS'] .= 'ss[\'D'.$setid.'\'] = new Array();';
- }
- $name = preg_replace("/\r\n|\n|\r/", '\n', addcslashes($name, "'\"));
- $_G['setting_JS'] .= 'ss[\'D'.$setid.'\'] += \'<div class="multicol">'.$name.'</div>\';';
- }
- if(!$nocomment && ($type != 'omcheckbox' || $varname[2] != 'isfloat')) {
- if(!isset($_G['showsetting_multi'])) {
- showtablerow('class="noborder" onmouseover="setfaq(this, \'faq'.$setid.'\')"', array('class="vtop rowform"', 'class="vtop tips2" s="1"'), array(
- $s,
- ($comment ? $comment : cplang($setname.'_comment', false)).($type == 'textarea' ? '<br />'.cplang('tips_textarea') : '').
- ($disabled ? '<br /><span class="smalltxt" style="color:#F00">'.cplang($setname.'_disabled', false).'</span>' : NULL)
- ));
- } else {
- if($_G['showsetting_multi'] == 0) {
- showtablerow('class="noborder"', array('class="vtop rowform" style="width:auto"'), array(
- '<div id="'.$setid.'"></div>'
- ));
- $_G['setting_JS'] .= 'ss[\''.$setid.'\'] = new Array();';
- }
- $s = preg_replace("/\r\n|\n|\r/", '\n', addcslashes($s, "'\"));
- $_G['setting_JS'] .= 'ss[\''.$setid.'\'] += \'<div class="multicol">'.$s.'</div>\';';
- }
- } else {
- showtablerow('class="noborder" onmouseover="setfaq(this, \'faq'.$setid.'\')"', array('colspan="2" class="vtop rowform"'), array($s));
- }
-
-
- if($hidden) {
- showtagheader('tbody', 'hidden_'.$setname, $value, 'sub');
- }
-
-
- }
复制代码
参数说明:
$setname - 指定输出标题,如:setting_basic_bbname, 自动匹配描述文字为:setting_basic_bbname_comment,comment形式文字可以在./source/language/lang_admincp.php语言包中添加
$varname - 指定表单的name值,如settingnew[bbname]
$value - 指定表单默认值\变量
$type - 表单样式
radio单选
text文本、password密码、number数字
file上传文件
filetext 上传文件或在线文件切换型表单
textarea 多行文本
select 选择框
mradio 高级单选模式
mcheckbox 高级多选模式
binmcheckbox 二进制数值多选模式
mselect 高级选择框模式
color 颜色选择
calendar 日期选择
multiply多表单型,daterange时间范围
其他未在上述样式中出现的$type均独立输出
$disabled - 是否不可修改
$hidden - 是否隐藏
$comment - 强制描述文字
$extra - 表单扩展属性
$setid - 用于拼接表单外层Div的id
使用方法举例:
- //以文本形式输出表单(站点名称 text):
- showsetting('setting_basic_bbname', 'settingnew[bbname]', $setting['bbname'], 'text');
- //以单选形式输出表单(显示授权信息链接 radio):
- showsetting('setting_basic_boardlicensed', 'settingnew[boardlicensed]', $setting['boardlicensed'], 'radio');
- //以多行文本形式输出表单(网站第三方统计代码 textarea):
- showsetting('setting_basic_stat', 'settingnew[statcode]', $setting['statcode'], 'textarea');
- //以高级单选形式输出表单(新用户注册验证 mradio)
- showsetting('setting_access_register_verify', array('settingnew[regverify]', array(
- array(0, $lang['none'], array('regverifyext' => 'none')),
- array(1, $lang['setting_access_register_verify_email'], array('regverifyext' => '')),
- array(2, $lang['setting_access_register_verify_manual'], array('regverifyext' => ''))
- )), $setting['regverify'], 'mradio');
- //以高级多选形式输出表单('允许新用户注册 mcheckbox)
- showsetting('setting_access_register_status', array('settingnew[regstatus]', array(
- array('open', $lang['setting_access_register_open']),
- array('invite', $lang['setting_access_register_invite'], 'showinvite'),
- $_G['setting']['connect']['allow'] ? array('connect', $lang['setting_access_register_connect'], 'showconnect') : array(),
- )), $regstatus, 'mcheckbox');
复制代码
|
|