PageRenderTime 40ms CodeModel.GetById 6ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 1ms

/BlogEngine/BlogEngine.NET/admin/Settings/Main.aspx.cs

#
C# | 205 lines | 154 code | 14 blank | 37 comment | 5 complexity | 63270b9f2803a3e22acaa7471c039249 MD5 | raw file
  1namespace admin.Settings
  2{
  3    using System;
  4    using System.IO;
  5    using System.Web;
  6    using System.Web.UI.WebControls;
  7    using System.Globalization;
  8    using System.Web.Services;
  9    using Resources;
 10    using BlogEngine.Core;
 11    using BlogEngine.Core.Json;
 12    using System.Web.Security;
 13    using System.Linq;
 14    using App_Code;
 15    using Page = System.Web.UI.Page;
 16
 17    public partial class Main : Page
 18    {
 19        /// <summary>
 20        /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event to initialize the page.
 21        /// </summary>
 22        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
 23        protected override void OnInit(EventArgs e)
 24        {
 25            WebUtils.CheckRightsForAdminSettingsPage(false);
 26
 27            BindCultures();
 28            BindRoles();
 29            BindSettings();
 30
 31            Page.MaintainScrollPositionOnPostBack = true;
 32            Page.Title = labels.settings;
 33
 34            base.OnInit(e);
 35        }
 36
 37        private void BindRoles()
 38        {
 39            ddlSelfRegistrationInitialRole.AppendDataBoundItems = true;
 40            ddlSelfRegistrationInitialRole.DataSource = Roles.GetAllRoles().Where(r => !r.Equals(BlogConfig.AnonymousRole, StringComparison.OrdinalIgnoreCase));
 41            ddlSelfRegistrationInitialRole.DataBind();
 42        }
 43
 44        /// <summary>
 45        /// The bind settings.
 46        /// </summary>
 47        private void BindSettings()
 48        {
 49            // -----------------------------------------------------------------------
 50            // Bind Basic settings
 51            // -----------------------------------------------------------------------
 52            txtName.Text = BlogSettings.Instance.Name;
 53            txtDescription.Text = BlogSettings.Instance.Description;
 54            txtPostsPerPage.Text = BlogSettings.Instance.PostsPerPage.ToString();
 55            cbShowRelatedPosts.Checked = BlogSettings.Instance.EnableRelatedPosts;
 56            txtThemeCookieName.Text = BlogSettings.Instance.ThemeCookieName;
 57            cbUseBlogNameInPageTitles.Checked = BlogSettings.Instance.UseBlogNameInPageTitles;
 58            cbEnableRating.Checked = BlogSettings.Instance.EnableRating;
 59            cbShowDescriptionInPostList.Checked = BlogSettings.Instance.ShowDescriptionInPostList;
 60            txtDescriptionCharacters.Text = BlogSettings.Instance.DescriptionCharacters.ToString();
 61            cbShowDescriptionInPostListForPostsByTagOrCategory.Checked =
 62                BlogSettings.Instance.ShowDescriptionInPostListForPostsByTagOrCategory;
 63            txtDescriptionCharactersForPostsByTagOrCategory.Text =
 64                BlogSettings.Instance.DescriptionCharactersForPostsByTagOrCategory.ToString();
 65            cbTimeStampPostLinks.Checked = BlogSettings.Instance.TimeStampPostLinks;
 66            ddlCulture.SelectedValue = BlogSettings.Instance.Culture;
 67            txtTimeZone.Text = BlogSettings.Instance.Timezone.ToString();
 68            cbShowPostNavigation.Checked = BlogSettings.Instance.ShowPostNavigation;
 69            cbEnablePasswordReset.Checked = BlogSettings.Instance.EnablePasswordReset;
 70            cbEnableSelfRegistration.Checked = BlogSettings.Instance.EnableSelfRegistration;
 71            Utils.SelectListItemByValue(ddlSelfRegistrationInitialRole, BlogSettings.Instance.SelfRegistrationInitialRole);
 72        }
 73
 74        /// <summary>
 75        /// The bind cultures.
 76        /// </summary>
 77        private void BindCultures()
 78        {
 79            if (File.Exists(Path.Combine(HttpRuntime.AppDomainAppPath, "PrecompiledApp.config")))
 80            {
 81                var precompiledDir = HttpRuntime.BinDirectory;
 82                var translations = Directory.GetFiles(
 83                    precompiledDir, "App_GlobalResources.resources.dll", SearchOption.AllDirectories);
 84                foreach (var translation in translations)
 85                {
 86                    var path = Path.GetDirectoryName(translation);
 87                    if (path == null)
 88                    {
 89                        continue;
 90                    }
 91
 92                    var resourceDir = path.Remove(0, precompiledDir.Length);
 93                    if (String.IsNullOrEmpty(resourceDir))
 94                    {
 95                        continue;
 96                    }
 97
 98                    var info = CultureInfo.GetCultureInfoByIetfLanguageTag(resourceDir);
 99                    ddlCulture.Items.Add(new ListItem(info.NativeName, resourceDir));
100                }
101            }
102            else
103            {
104                var path = Server.MapPath(string.Format("{0}App_GlobalResources/", Utils.ApplicationRelativeWebRoot));
105                foreach (var file in Directory.GetFiles(path, "labels.*.resx"))
106                {
107                    var index = file.LastIndexOf(Path.DirectorySeparatorChar) + 1;
108                    var filename = file.Substring(index);
109                    filename = filename.Replace("labels.", string.Empty).Replace(".resx", string.Empty);
110                    var info = CultureInfo.GetCultureInfoByIetfLanguageTag(filename);
111                    ddlCulture.Items.Add(new ListItem(info.NativeName, filename));
112                }
113            }
114        }
115
116        /// <summary>
117        /// Save settings
118        /// </summary>
119        /// <param name="name">Blog name</param>
120        /// <param name="desc">Description</param>
121        /// <param name="postsPerPage">Number of posts per page</param>
122        /// <param name="theme">Active theme</param>
123        /// <param name="mobileTheme">Mobile theme</param>
124        /// <param name="themeCookieName">Cookie name to persist theme within session</param>
125        /// <param name="useBlogNameInPageTitles">Whether use blog name in page titles</param>
126        /// <param name="enableRelatedPosts">Enable related posts</param>
127        /// <param name="enableRating">Enable rating</param>
128        /// <param name="showDescriptionInPostList">Show description in post list</param>
129        /// <param name="descriptionCharacters">Number of characters in description</param>
130        /// <param name="showDescriptionInPostListForPostsByTagOrCategory">Shwo descripton for posts by tag or category</param>
131        /// <param name="descriptionCharactersForPostsByTagOrCategory">Description characters</param>
132        /// <param name="timeStampPostLinks">Time stamp post links</param>
133        /// <param name="showPostNavigation">Show post navigation</param>
134        /// <param name="culture">Culture</param>
135        /// <param name="timezone">Time zone</param>
136        /// <param name="enablePasswordReset">Enable password resets</param>
137        /// <param name="enableSelfRegistration">Enable self registration</param>
138        /// <param name="selfRegistrationInitialRole">Self registration initial role</param>
139        /// <returns></returns>
140        [WebMethod]
141        public static JsonResponse Save(string name, 
142			string desc,
143			string postsPerPage,
144			string themeCookieName,
145			string useBlogNameInPageTitles,
146			string enableRelatedPosts,
147			string enableRating,
148			string showDescriptionInPostList,
149			string descriptionCharacters,
150			string showDescriptionInPostListForPostsByTagOrCategory,
151			string descriptionCharactersForPostsByTagOrCategory,
152			string timeStampPostLinks,
153			string showPostNavigation,
154			string culture,
155			string timezone,
156            string enablePasswordReset,
157			string enableSelfRegistration,
158            string selfRegistrationInitialRole)
159        {
160            var response = new JsonResponse {Success = false};
161
162            if (!WebUtils.CheckRightsForAdminSettingsPage(true))
163            {
164                response.Message = "Not authorized";
165                return response;
166            }
167
168            try
169            {
170                BlogSettings.Instance.Name = name;
171                BlogSettings.Instance.Description = desc;
172				BlogSettings.Instance.PostsPerPage = int.Parse(postsPerPage);
173				BlogSettings.Instance.ThemeCookieName = themeCookieName;
174				BlogSettings.Instance.UseBlogNameInPageTitles = bool.Parse(useBlogNameInPageTitles);
175				BlogSettings.Instance.EnableRelatedPosts = bool.Parse(enableRelatedPosts);
176				BlogSettings.Instance.EnableRating = bool.Parse(enableRating);
177				BlogSettings.Instance.ShowDescriptionInPostList = bool.Parse(showDescriptionInPostList);
178				BlogSettings.Instance.DescriptionCharacters = int.Parse(descriptionCharacters);
179				BlogSettings.Instance.ShowDescriptionInPostListForPostsByTagOrCategory =
180					bool.Parse(showDescriptionInPostListForPostsByTagOrCategory);
181				BlogSettings.Instance.DescriptionCharactersForPostsByTagOrCategory =
182					int.Parse(descriptionCharactersForPostsByTagOrCategory);
183				BlogSettings.Instance.TimeStampPostLinks = bool.Parse(timeStampPostLinks);
184				BlogSettings.Instance.ShowPostNavigation = bool.Parse(showPostNavigation);
185				BlogSettings.Instance.Culture = culture;
186				BlogSettings.Instance.Timezone = double.Parse(timezone);
187                BlogSettings.Instance.EnablePasswordReset = bool.Parse(enablePasswordReset);
188				BlogSettings.Instance.EnableSelfRegistration = bool.Parse(enableSelfRegistration);
189                BlogSettings.Instance.SelfRegistrationInitialRole = selfRegistrationInitialRole;
190
191                BlogSettings.Instance.Save();
192            }
193            catch (Exception ex)
194            {
195                Utils.Log(string.Format("admin.Settings.Main.Save(): {0}", ex.Message));
196                response.Message = string.Format("Could not save settings: {0}", ex.Message);
197                return response;
198            }
199
200            response.Success = true;
201            response.Message = "Settings saved";
202            return response;
203        }
204    }
205}