using System; using System.Collections; using System.Collections.Generic; using Zhaizj.Framework.Common.Resource; using Zhaizj.Framework.Serialization; using Zhaizj.Framework.Web; using Zhaizj.Framework.Web.Context; using Zhaizj.Framework.Web.Mvc; namespace Zhaizj.Framework.Config { /// /// ÅäÖÃ±íµ¥¹¤¾ß /// public class SettingFormTool { public static void BindTemplate( Template template, IList list, String categoryName, String actionUrl ) { template.Set( "settingCategory.Name", categoryName ); template.Set( "ActionUrl", actionUrl ); IBlock block = template.GetBlock( "list" ); foreach (ISetting setting in list) { block.Set( "setting.Id", setting.Id ); block.Set( "setting.Title", setting.Name ); SetInput( block, setting ); SetNote( block, setting ); block.Next(); } } private static void SetInput( IBlock block, ISetting setting ) { String lbl = "setting.inputControl"; String inputName = GetInputName( setting.Id ); String settingValue = setting.SettingValue; int width = 500; if (setting.DataType == SettingType.Int.ToString()) { block.Set( lbl, Html.TextInput( inputName, settingValue, "width:40px;" ) ); } else if (setting.DataType == SettingType.Bool.ToString()) { block.Set( lbl, Html.CheckBox( inputName, "", "1", Convert.ToBoolean( setting.SettingValue ) ) ); } else if (setting.DataType == SettingType.Droplist.ToString()) { block.Set( lbl, Html.DropList( getDropOptions(setting.Options), inputName, "Name", "Value", setting.ValueInt ) ); } else if (setting.DataType == SettingType.ShortText.ToString()) { block.Set( lbl, Html.TextInput( inputName, settingValue, "width:" + width + "px;" ) ); } else if (setting.DataType == SettingType.BigText.ToString()) { block.Set( lbl, Html.TextArea( inputName, settingValue, "width:" + width + "px;height:300px;" ) ); } else { block.Set( lbl, Html.TextArea( inputName, settingValue, "width:" + width + "px;height:50px;" ) ); } } private static PropertyCollection getDropOptions( String Options ) { PropertyCollection result = new PropertyCollection(); if (strUtil.IsNullOrEmpty( Options )) return result; Dictionary dic = JsonParser.Parse( Options ) as Dictionary; foreach (KeyValuePair pair in dic) { result.Add( new PropertyItem( pair.Key, cvt.ToInt( pair.Value ) ) ); } return result; } private static void SetNote( IBlock block, ISetting setting ) { if (strUtil.HasText( setting.Description )) { if ((setting.DataType == SettingType.Int.ToString()) || (setting.DataType == SettingType.Bool.ToString())) { block.Set( "setting.Note", "(" + setting.Description + ")" ); } else { block.Set( "setting.Note", "
(" + setting.Description + ")
" ); } } else { block.Set( "setting.Note", "" ); } } //------------------------------------------------------------------------------------ public static void UpdateSettings( IList list, MvcContext ctx ) { for (int i = 0; i < list.Count; i++) { ISettingValue s = list[i] as ISettingValue; String target = ctx.Post( GetInputName( s.Id ) ); if (strUtil.HasText( target )) { if (s.DataType == SettingType.Bool.ToString()) { updateSetting( s, cvt.ToBool( target ).ToString() ); } else if (!(s.DataType == SettingType.Int.ToString()) || cvt.IsInt( target )) { updateSetting( s, target ); } } } } private static void updateSetting( ISettingValue s, String val ) { if ((val != null) && (s.SettingValue.ToLower() != val.ToLower())) { s.SettingValue = val; s.Update( "SettingValue" ); } } private static String GetInputName( int settingId ) { return ("SettingValue" + settingId); } } }