PageRenderTime 47ms CodeModel.GetById 17ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
C# | 190 lines | 128 code | 35 blank | 27 comment | 6 complexity | 4e76b54dd90fb4aa7b5a5ba2c72e2d88 MD5 | raw file
  1namespace admin.Settings
  2{
  3    using System;
  4    using System.Collections.Generic;
  5    using System.Web.UI.WebControls;
  6    using BlogEngine.Core;
  7    using BlogEngine.Core.Web.Extensions;
  8    using System.Web.Services;
  9    using BlogEngine.Core.Json;
 10    using App_Code;
 11
 12    public partial class Rules : System.Web.UI.Page
 13    {
 14        static protected ExtensionSettings Filters;
 15
 16        /// <summary>
 17        /// Usually filter implemented as extension and can be turned
 18        /// on and off. If it is not extension, defaulted to enabled.
 19        /// </summary>
 20        /// <param name="filter">Filter (extension) name</param>
 21        /// <returns>True if enabled</returns>
 22        public bool CustomFilterEnabled(string filter)
 23        {
 24            var ext = ExtensionManager.GetExtension(filter);
 25            return ext == null ? true : ext.Enabled;
 26        }
 27
 28        [WebMethod]
 29        public static List<JsonCustomFilter> GetCustomFilters()
 30        {
 31            if (!Security.IsAuthorizedTo(Rights.AccessAdminSettingsPages))
 32                return new List<JsonCustomFilter>();
 33
 34            var customFilters = JsonCustomFilterList.GetCustomFilters();
 35
 36            customFilters.Sort((f1, f2) => string.Compare(f1.Name, f2.Name));
 37
 38            return customFilters;
 39        }
 40
 41        protected void Page_Load(object sender, EventArgs e)
 42        {
 43            WebUtils.CheckRightsForAdminSettingsPage(false);
 44
 45            Filters = ExtensionManager.GetSettings("MetaExtension", "BeCommentFilters");
 46
 47            if (!IsPostBack)
 48            {
 49                BindFilters();
 50            }
 51
 52            Page.MaintainScrollPositionOnPostBack = true;
 53            Page.Title = Resources.labels.comments;
 54
 55            btnSave.Click += btnSave_Click;
 56            btnSave.Text = Resources.labels.saveSettings;
 57        }
 58
 59        protected void BindFilters()
 60        {
 61            gridFilters.DataKeyNames = new string[] { Filters.KeyField };
 62            gridFilters.DataSource = Filters.GetDataTable();
 63            gridFilters.DataBind();
 64
 65            // rules
 66            cbTrustAuthenticated.Checked = BlogSettings.Instance.TrustAuthenticatedUsers;
 67            ddWhiteListCount.SelectedValue = BlogSettings.Instance.CommentWhiteListCount.ToString();
 68            ddBlackListCount.SelectedValue = BlogSettings.Instance.CommentBlackListCount.ToString();
 69            cbReportMistakes.Checked = BlogSettings.Instance.CommentReportMistakes;
 70            cbBlockOnDelete.Checked = BlogSettings.Instance.BlockAuthorOnCommentDelete;
 71            cbAddIpToWhitelistFilterOnApproval.Checked = BlogSettings.Instance.AddIpToWhitelistFilterOnApproval;
 72            cbAddIpToBlacklistFilterOnRejection.Checked = BlogSettings.Instance.AddIpToBlacklistFilterOnRejection;
 73        }
 74
 75        protected void btnSave_Click(object sender, EventArgs e)
 76        {
 77            // rules
 78            BlogSettings.Instance.TrustAuthenticatedUsers = cbTrustAuthenticated.Checked;
 79            BlogSettings.Instance.CommentWhiteListCount = int.Parse(ddWhiteListCount.SelectedValue);
 80            BlogSettings.Instance.CommentBlackListCount = int.Parse(ddBlackListCount.SelectedValue);
 81
 82            BlogSettings.Instance.CommentReportMistakes = cbReportMistakes.Checked;
 83            BlogSettings.Instance.BlockAuthorOnCommentDelete = cbBlockOnDelete.Checked;
 84            BlogSettings.Instance.AddIpToWhitelistFilterOnApproval = cbAddIpToWhitelistFilterOnApproval.Checked;
 85            BlogSettings.Instance.AddIpToBlacklistFilterOnRejection = cbAddIpToBlacklistFilterOnRejection.Checked;
 86
 87            //-----------------------------------------------------------------------
 88            //  Persist settings
 89            //-----------------------------------------------------------------------
 90            BlogSettings.Instance.Save();
 91
 92            Response.Redirect(Request.RawUrl, true);
 93        }
 94
 95        protected void btnDelete_Click(object sender, EventArgs e)
 96        {
 97            ImageButton btn = (ImageButton)sender;
 98            GridViewRow grdRow = (GridViewRow)btn.Parent.Parent;
 99
100            int pageIdx = gridFilters.PageIndex;
101            int pageSize = gridFilters.PageSize;
102            int rowIndex = grdRow.RowIndex;
103
104            if (pageIdx > 0) rowIndex = pageIdx * pageSize + rowIndex;
105
106
107            foreach (ExtensionParameter par in Filters.Parameters)
108            {
109                par.DeleteValue(rowIndex);
110            }
111
112            ExtensionManager.SaveSettings("MetaExtension", Filters);
113            Response.Redirect(Request.RawUrl);
114        }
115
116        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
117        {
118            gridFilters.PageIndex = e.NewPageIndex;
119            BindFilters();
120        }
121
122        protected void btnAddFilter_Click(object sender, EventArgs e)
123        {
124            if (ValidateForm())
125            {
126                string id = Guid.NewGuid().ToString();
127                string[] f = new string[] { id, 
128                    ddAction.SelectedValue, 
129                    ddSubject.SelectedValue, 
130                    ddOperator.SelectedValue, 
131                    txtFilter.Text };
132
133                Filters.AddValues(f);
134                ExtensionManager.SaveSettings("MetaExtension", Filters);
135                Response.Redirect(Request.RawUrl);
136            }
137        }
138
139        protected bool ValidateForm()
140        {
141            if (string.IsNullOrEmpty(txtFilter.Text))
142            {
143                FilterValidation.InnerHtml = "Filter is a required field";
144                return false;
145            }
146
147            return true;
148        }
149
150        public static string ApprovedCnt(object total, object cought)
151        {
152            try
153            {
154                int t = int.Parse(total.ToString());
155                int c = int.Parse(cought.ToString());
156
157                int a = t - c;
158
159                return a.ToString();
160            }
161            catch (Exception)
162            {
163                return "";
164            }
165
166        }
167
168        protected void gridCustomFilters_RowCommand(object sender, GridViewCommandEventArgs e)
169        {
170            //string filterName = e.CommandArgument.ToString();
171
172            //if (!string.IsNullOrEmpty(filterName))
173            //{
174            //    // reset statistics for this filter
175            //    for (int i = 0; i < _customFilters.Parameters[0].Values.Count; i++)
176            //    {
177            //        if (_customFilters.Parameters[0].Values[i] == filterName)
178            //        {
179            //            _customFilters.Parameters[2].Values[i] = "0";
180            //            _customFilters.Parameters[3].Values[i] = "0";
181            //            _customFilters.Parameters[4].Values[i] = "0";
182            //        }
183            //    }
184
185            //    ExtensionManager.SaveSettings("MetaExtension", _customFilters);
186            //    Response.Redirect(Request.RawUrl);
187            //}
188        }
189    }
190}