PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/admin/Tracking/referrers.aspx.cs

#
C# | 295 lines | 189 code | 37 blank | 69 comment | 15 complexity | 2a85ddf9aac620525db1f539fda1adf2 MD5 | raw file
  1// --------------------------------------------------------------------------------------------------------------------
  2// <summary>
  3//   The admin pages referrers.
  4// </summary>
  5// --------------------------------------------------------------------------------------------------------------------
  6
  7namespace Admin.Tracking
  8{
  9    using System;
 10    using System.Collections.Generic;
 11    using System.Data;
 12    using System.Linq;
 13    using System.Web.UI.WebControls;
 14    using System.Web.Services;
 15    using System.Threading;
 16    using App_Code;
 17    using BlogEngine.Core;
 18    using BlogEngine.Core.Json;
 19
 20    using Resources;
 21
 22    using Page = System.Web.UI.Page;
 23
 24    /// <summary>
 25    /// The admin pages referrers.
 26    /// </summary>
 27    public partial class Referrers : Page
 28    {
 29        #region Methods
 30
 31        /// <summary>
 32        /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event to initialize the page.
 33        /// </summary>
 34        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
 35        protected override void OnInit(EventArgs e)
 36        {
 37            WebUtils.CheckRightsForAdminSettingsPage(false);
 38
 39            infoPanel.Visible = false;
 40            if (BlogSettings.Instance.EnableReferrerTracking)
 41            {
 42                this.BindDays();
 43                this.BindReferrers();
 44            }
 45            else
 46            {
 47                this.ddlDays.Enabled = false;
 48                infoPanel.InnerText = "Referrers disabled. If you want to enable referrer tracking open settings from the link in the top right corner of the page.";
 49                infoPanel.Visible = true;
 50                referrersPanel.Visible = false;
 51                possibleSmapPanel.Visible = false;
 52            }
 53
 54            this.txtNumberOfDays.Text = BlogSettings.Instance.NumberOfReferrerDays.ToString();
 55            this.cbEnableReferrers.Checked = BlogSettings.Instance.EnableReferrerTracking;
 56
 57            this.ddlDays.SelectedIndexChanged += this.DdlDaysSelectedIndexChanged;
 58            this.Page.Title = labels.referrers;
 59
 60            base.OnInit(e);
 61        }
 62
 63        /// <summary>
 64        /// The bind days.
 65        /// </summary>
 66        private void BindDays()
 67        {
 68            var sortedDates = new List<DateTime>(Referrer.ReferrersByDay.Keys);
 69
 70            sortedDates.Sort((firstPair, nextPair) => firstPair.CompareTo(nextPair) * -1);
 71
 72            this.ddlDays.DataSource = sortedDates;
 73            this.ddlDays.DataBind();
 74            foreach (var item in
 75                this.ddlDays.Items.Cast<ListItem>().Where(item => item.Text == DateTime.Today.ToShortDateString()))
 76            {
 77                item.Selected = true;
 78            }
 79        }
 80
 81        /// <summary>
 82        /// The bind referrers.
 83        /// </summary>
 84        private void BindReferrers()
 85        {
 86            if (!(this.ddlDays.SelectedIndex >= 0 & Referrer.Referrers.Count > 0))
 87            {
 88                infoPanel.InnerText = "There are no referrers.";
 89                infoPanel.Visible = true;
 90                referrersPanel.Visible = false;
 91                possibleSmapPanel.Visible = false;
 92                return;
 93            }
 94
 95            var referrers = Referrer.ReferrersByDay[DateTime.Parse(this.ddlDays.SelectedItem.Text)];
 96            if (referrers == null)
 97            {
 98                return;
 99            }
100
101            var table = new DataTable();
102            table.Columns.Add("url", typeof(string));
103            table.Columns.Add("shortUrl", typeof(string));
104            table.Columns.Add("target", typeof(string));
105            table.Columns.Add("shortTarget", typeof(string));
106            table.Columns.Add("hits", typeof(int));
107
108            var spamTable = table.Clone();
109
110            foreach (var refer in referrers)
111            {
112                DataRow tableRow;
113                if (refer.PossibleSpam)
114                {
115                    tableRow = spamTable.NewRow();
116                    this.PopulateRow(tableRow, refer);
117                    spamTable.Rows.Add(tableRow);
118                }
119                else
120                {
121                    tableRow = table.NewRow();
122                    this.PopulateRow(tableRow, refer);
123                    table.Rows.Add(tableRow);
124                }
125            }
126
127            BindTable(table, this.grid);
128            BindTable(spamTable, this.spamGrid);
129        }
130
131        /// <summary>
132        /// Binds the table.
133        /// </summary>
134        /// <param name="table">
135        /// The table.
136        /// </param>
137        /// <param name="gridview">
138        /// The gridview.
139        /// </param>
140        private static void BindTable(DataTable table, GridView gridview)
141        {
142            var total = table.Compute("sum(hits)", null).ToString();
143
144            var view = new DataView(table) { Sort = "hits desc" };
145
146            gridview.DataSource = view;
147            gridview.DataBind();
148
149            if (gridview.Rows.Count > 0)
150            {
151                gridview.FooterRow.Cells[0].Text = labels.Referrers_BindTable_Total;
152                gridview.FooterRow.Cells[gridview.FooterRow.Cells.Count - 1].Text = total;
153            }
154            else
155            {
156            }
157
158            PaintRows(gridview, 3);
159        }
160
161        /// <summary>
162        /// Makes the short URL.
163        /// </summary>
164        /// <param name="url">The text to shorten.</param>
165        /// <returns>The shortened text.</returns>
166        private string MakeShortUrl(string url)
167        {
168            return url.Length > 150
169                       ? string.Format("{0}...", url.Substring(0, 150))
170                       : this.Server.HtmlEncode(url.Replace("http://", string.Empty).Replace("www.", string.Empty));
171        }
172
173        /// <summary>
174        /// Paints the background color of the alternate rows
175        ///     in the gridview.
176        /// </summary>
177        /// <param name="gridview">
178        /// The gridview.
179        /// </param>
180        /// <param name="alternateRows">
181        /// The alternate Rows.
182        /// </param>
183        private static void PaintRows(GridView gridview, int alternateRows)
184        {
185            if (gridview.Rows.Count == 0)
186            {
187                return;
188            }
189
190            var count = 0;
191            for (var i = 0; i < gridview.Controls[0].Controls.Count - 1; i++)
192            {
193                if (count > alternateRows)
194                {
195                    ((WebControl)gridview.Controls[0].Controls[i]).CssClass = "alt";
196                }
197
198                count++;
199
200                if (count == alternateRows + alternateRows + 1)
201                {
202                    count = 1;
203                }
204            }
205        }
206
207        /// <summary>
208        /// Populates the row.
209        /// </summary>
210        /// <param name="tableRow">
211        /// The table row.
212        /// </param>
213        /// <param name="refer">
214        /// The refer.
215        /// </param>
216        private void PopulateRow(DataRow tableRow, Referrer refer)
217        {
218            tableRow["url"] = this.Server.HtmlEncode(refer.ReferrerUrl.ToString());
219            tableRow["shortUrl"] = this.MakeShortUrl(refer.ReferrerUrl.ToString());
220            tableRow["target"] = this.Server.HtmlEncode(refer.Url.ToString());
221            tableRow["shortTarget"] = this.MakeShortUrl(refer.Url.ToString());
222            tableRow["hits"] = refer.Count;
223        }
224
225
226        [WebMethod]
227        public static JsonResponse Save(string enable, string days)
228        {
229            var response = new JsonResponse { Success = false };
230
231            if (!WebUtils.CheckRightsForAdminSettingsPage(true))
232            {
233                response.Message = "Not authorized";
234                return response;
235            }
236
237            try
238            {
239                BlogSettings.Instance.EnableReferrerTracking = bool.Parse(enable);
240                BlogSettings.Instance.NumberOfReferrerDays = int.Parse(days);
241                BlogSettings.Instance.Save();
242                response.Success = true;
243                response.Message = string.Format("Settings saved");
244                return response;
245            }
246            catch (Exception ex)
247            {
248                Utils.Log(string.Format("admin.Tracking.referrers.Save(): {0}", ex.Message));
249                response.Message = string.Format("Could not save settings: {0}", ex.Message);
250                return response;
251            }
252        }
253
254        /// <summary>
255        /// Handles the CheckedChanged event of the cbEnableReferrers control.
256        /// </summary>
257        /// <param name="sender">
258        /// The source of the event.
259        /// </param>
260        /// <param name="e">
261        /// The <see cref="System.EventArgs"/> instance containing the event data.
262        /// </param>
263        private void CbEnableReferrersCheckedChanged(object sender, EventArgs e)
264        {
265            if (this.cbEnableReferrers.Checked)
266            {
267                this.BindDays();
268                this.BindReferrers();
269            }
270            else
271            {
272                this.ddlDays.Enabled = false;
273            }
274
275            BlogSettings.Instance.EnableReferrerTracking = this.cbEnableReferrers.Checked;
276            BlogSettings.Instance.Save();
277        }
278
279        /// <summary>
280        /// Handles the SelectedIndexChanged event of the ddlDays control.
281        /// </summary>
282        /// <param name="sender">
283        /// The source of the event.
284        /// </param>
285        /// <param name="e">
286        /// The <see cref="System.EventArgs"/> instance containing the event data.
287        /// </param>
288        private void DdlDaysSelectedIndexChanged(object sender, EventArgs e)
289        {
290            this.BindReferrers();
291        }
292
293        #endregion
294    }
295}