Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes an issue with search indexing and timezones #60

Merged
merged 9 commits into from
Sep 5, 2020
45 changes: 31 additions & 14 deletions AnnouncementsEdit.ascx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Framework;
using DotNetNuke.Framework.JavaScriptLibraries;
using DotNetNuke.Modules.Announcements.Components.Business;
Expand All @@ -38,6 +40,7 @@
using DotNetNuke.Modules.Announcements.MVP.Presenters;
using DotNetNuke.Modules.Announcements.MVP.Views;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Web.Client;
using DotNetNuke.Web.Client.ClientResourceManagement;
using DotNetNuke.Web.Mvp;
Expand Down Expand Up @@ -68,7 +71,6 @@ public partial class AnnouncementsEdit : ModuleView<AnnouncementsEditModel>, IAn
override protected void OnInit(EventArgs e)
{
base.OnInit(e);

JavaScript.RequestRegistration(CommonJs.DnnPlugins);

ClientResourceManager.RegisterStyleSheet(Page, Globals.ApplicationPath + "/DesktopModules/Announcements/AnnouncementsEdit.css", FileOrder.Css.ModuleCss);
Expand Down Expand Up @@ -142,7 +144,7 @@ private void CmdUpdateClick(object sender, EventArgs e)
ModuleID = ModuleContext.ModuleId,
PortalID = ModuleContext.PortalId,
CreatedByUserID = ModuleContext.PortalSettings.UserId,
CreatedOnDate = DateTime.Now
CreatedOnDate = DateTime.UtcNow
};
}
else
Expand All @@ -155,11 +157,11 @@ private void CmdUpdateClick(object sender, EventArgs e)
announcement.ImageSource = urlImage.FilePath;
announcement.Description = teDescription.Text;
announcement.URL = ctlURL.Url;
announcement.PublishDate = GetDateTimeValue(publishDate, publishTime, DateTime.Now);
announcement.PublishDate = GetDateTimeValue(publishDate, publishTime, DateTime.UtcNow);
announcement.ExpireDate = GetDateTimeValue(expireDate, expireTime);
announcement.LastModifiedByUserID = ModuleContext.PortalSettings.UserId;
announcement.LastModifiedOnDate = DateTime.Now;
if (txtViewOrder.Text != "")
announcement.LastModifiedOnDate = DateTime.UtcNow;
if (!string.IsNullOrWhiteSpace(txtViewOrder.Text))
{
announcement.ViewOrder = Convert.ToInt32(txtViewOrder.Text);
}
Expand All @@ -172,7 +174,6 @@ private void CmdUpdateClick(object sender, EventArgs e)

// redirect back to page
Response.Redirect(ReturnURL, true);

}
}
catch (Exception exc)
Expand All @@ -198,20 +199,29 @@ private void BindForm()
if ((!Null.IsNull(Model.AnnouncementInfo.PublishDate)) &&
(Model.AnnouncementInfo.PublishDate != (DateTime)SqlDateTime.Null))
{
publishDate.SelectedDate = Model.AnnouncementInfo.PublishDate;
publishTime.SelectedDate = Model.AnnouncementInfo.PublishDate;
var portalDateTime = TimeZoneInfo.ConvertTimeFromUtc(Model.AnnouncementInfo.PublishDate.Value, ModuleContext.PortalSettings.TimeZone);
publishDate.SelectedDate = portalDateTime;
publishTime.SelectedDate = portalDateTime;
}
if ((!Null.IsNull(Model.AnnouncementInfo.ExpireDate)) &&
(Model.AnnouncementInfo.ExpireDate != (DateTime)SqlDateTime.Null))
{
expireDate.SelectedDate = Model.AnnouncementInfo.ExpireDate;
expireTime.SelectedDate = Model.AnnouncementInfo.ExpireDate;
var portalDateTime = TimeZoneInfo.ConvertTimeFromUtc(Model.AnnouncementInfo.ExpireDate.Value, ModuleContext.PortalSettings.TimeZone);
expireDate.SelectedDate = portalDateTime;
expireTime.SelectedDate = portalDateTime;
}

ctlAudit.CreatedDate = Model.AnnouncementInfo.CreatedOnDate.ToString(CultureInfo.InvariantCulture);
var user = UserController.Instance.GetCurrentUserInfo();
var userPreferredCulture = CultureInfo.InvariantCulture;
if (user != null && !string.IsNullOrWhiteSpace(user.Profile.PreferredLocale))
{
userPreferredCulture = new CultureInfo(user.Profile.PreferredLocale);
}

ctlAudit.CreatedDate = TimeZoneInfo.ConvertTimeFromUtc(Model.AnnouncementInfo.CreatedOnDate, ModuleContext.PortalSettings.TimeZone).ToString(userPreferredCulture);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be utc or portal time for the audit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it is in the portal preferred timezone but formatted in the current user culture, makes sense or I misenterpreted your comment ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, looks good

ctlAudit.CreatedByUser = Model.AnnouncementInfo.CreatedByUserID.ToString(CultureInfo.InvariantCulture);
ctlAudit.LastModifiedByUser = Model.AnnouncementInfo.LastModifiedByUserID.ToString(CultureInfo.InvariantCulture);
ctlAudit.LastModifiedDate = Model.AnnouncementInfo.LastModifiedOnDate.ToString(CultureInfo.InvariantCulture);
ctlAudit.LastModifiedDate = TimeZoneInfo.ConvertTimeFromUtc(Model.AnnouncementInfo.LastModifiedOnDate, ModuleContext.PortalSettings.TimeZone).ToString(userPreferredCulture);
ctlTracking.URL = Model.AnnouncementInfo.URL;
ctlTracking.ModuleID = ModuleContext.ModuleId;
}
Expand Down Expand Up @@ -274,11 +284,18 @@ private string ReturnURL
{
resultValue = dnnDatePicker.SelectedDate;
}

if ((dnnTimePicker.SelectedTime != null) && (resultValue.HasValue))
{
resultValue = resultValue.Value.Add((TimeSpan)dnnTimePicker.SelectedTime);
}
return resultValue;

if (resultValue.HasValue)
{
return TimeZoneInfo.ConvertTimeToUtc(resultValue.Value, ModuleContext.PortalSettings.TimeZone);
}

return null;
}

private DateTime? GetDateTimeValue(DnnDatePicker dnnDatePicker, DnnTimePicker dnnTimePicker, DateTime defaultValue)
Expand All @@ -296,4 +313,4 @@ private string ReturnURL

}

}
}
2 changes: 1 addition & 1 deletion AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
// </auto-generated>
//------------------------------------------------------------------------------

[assembly: System.Reflection.AssemblyVersion("07.02.03.00")]
[assembly: System.Reflection.AssemblyVersion("07.02.04.00")]


40 changes: 36 additions & 4 deletions Components/Business/AnnouncementInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,17 @@ public AnnouncementInfo()
public int ModuleID { get; set; }
public string Title { get; set; }
public string URL { get; set; }

/// <summary>
/// Gets or sets the UTC expiration date and time.
/// </summary>
public DateTime? ExpireDate { get; set; }
public string Description { get; set; }
public int ViewOrder { get; set; }

/// <summary>
/// Gets or sets the UTC publish date and time.
/// </summary>
public DateTime? PublishDate { get; set; }
public string ImageSource { get; set; }
public int PortalID { get; set; }
Expand All @@ -100,10 +108,18 @@ public AnnouncementInfo()
public bool IsEditable { get; set; }
[Browsable(false), XmlIgnore]
public int CreatedByUserID { get; set; }

/// <summary>
/// Gets or sets the UTC creation date.
/// </summary>
[Browsable(false), XmlIgnore]
public DateTime CreatedOnDate { get; set; }
[Browsable(false), XmlIgnore]
public int LastModifiedByUserID { get; set; }

/// <summary>
/// Gets or sets the UTC last modification date.
/// </summary>
[Browsable(false), XmlIgnore]
public DateTime LastModifiedOnDate { get; set; }

Expand Down Expand Up @@ -355,6 +371,7 @@ public void WriteXml(XmlWriter writer)
public string GetProperty(string strPropertyName, string strFormat, CultureInfo formatProvider, UserInfo AccessingUser, Scope AccessLevel, ref bool PropertyNotFound)
{
PortalSettings portalSettings = PortalController.Instance.GetCurrentPortalSettings();
var userInfo = UserController.Instance.GetCurrentUserInfo();
string outputFormat = strFormat == string.Empty ? "D" : strFormat;
switch (strPropertyName.ToLowerInvariant())
{
Expand Down Expand Up @@ -445,13 +462,13 @@ public string GetProperty(string strPropertyName, string strFormat, CultureInfo
return NewWindow ? "_blank" : "_self";
case "createddate":
case "createdondate":
return (CreatedOnDate.ToString(outputFormat, formatProvider));
return FormatDisplayDateTime(CreatedOnDate, userInfo, portalSettings, outputFormat, formatProvider);
case "lastmodifiedondate":
return (LastModifiedOnDate.ToString(outputFormat, formatProvider));
return FormatDisplayDateTime(LastModifiedOnDate, userInfo, portalSettings, outputFormat, formatProvider);
case "publishdate":
return PublishDate.HasValue ? (PublishDate.Value.ToString(outputFormat, formatProvider)) : "";
return PublishDate.HasValue ? FormatDisplayDateTime(PublishDate.Value, userInfo, portalSettings, outputFormat, formatProvider) : "";
case "expiredate":
return ExpireDate.HasValue ? (ExpireDate.Value.ToString(outputFormat, formatProvider)) : "";
return ExpireDate.HasValue ? FormatDisplayDateTime(ExpireDate.Value, userInfo, portalSettings, outputFormat, formatProvider) : "";
case "more":
return Localization.GetString("More.Text", _localResourceFile);
case "readmore":
Expand Down Expand Up @@ -484,6 +501,21 @@ public CacheLevel Cacheability
}

#endregion

private static string FormatDisplayDateTime(DateTime dateTime, UserInfo userInfo, PortalSettings portalSettings, string outputFormat, CultureInfo formatProvider)
{
if (userInfo != null)
{
return userInfo.LocalTime(dateTime).ToString(outputFormat, formatProvider);
}

if (portalSettings.TimeZone != null)
{
return TimeZoneInfo.ConvertTimeFromUtc(dateTime, portalSettings.TimeZone).ToString(outputFormat, formatProvider);
}

return (dateTime.ToString(outputFormat, formatProvider));
}
}
}

Loading