namespace BlogEngine.Core
{
using System;
using System.Collections.Generic;
using System.Linq;
using BlogEngine.Core.Providers;
using BlogEngine.Core.Data.Models;
///
/// The author profile.
///
public class AuthorProfile : BusinessBase
{
#region Constants and Fields
///
/// The sync root.
///
private static readonly object SyncRoot = new object();
///
/// The profiles.
///
private static Dictionary> profiles;
///
/// The about me.
///
private string aboutMe;
///
/// The birthday.
///
private DateTime birthday;
///
/// The city town.
///
private string cityTown;
///
/// The company.
///
private string company;
///
/// The country.
///
private string country;
///
/// The display name.
///
private string displayName;
///
/// The email address.
///
private string emailAddress;
///
/// The first name.
///
private string firstName;
///
/// The is private.
///
private bool isprivate;
///
/// The last name.
///
private string lastName;
///
/// The middle name.
///
private string middleName;
///
/// The phone fax.
///
private string phoneFax;
///
/// The phone main.
///
private string phoneMain;
///
/// The phone mobile.
///
private string phoneMobile;
///
/// The photo url.
///
private string photoUrl;
///
/// The region state.
///
private string regionState;
#endregion
#region Constructors and Destructors
///
/// Initializes a new instance of the class.
///
public AuthorProfile()
{
}
///
/// Initializes a new instance of the class.
///
///
/// The username.
///
public AuthorProfile(string username)
{
this.Id = username;
}
static AuthorProfile()
{
Blog.Saved += (s, e) =>
{
if (e.Action == SaveAction.Delete)
{
Blog blog = s as Blog;
if (blog != null)
{
// remove deleted blog from static 'profiles'
if (profiles != null && profiles.ContainsKey(blog.Id))
profiles.Remove(blog.Id);
}
}
};
}
///
/// Gets an unsorted list of all pages.
///
public static List Profiles
{
get
{
if (profiles == null)
profiles = new Dictionary>();
if (!profiles.ContainsKey(Blog.CurrentInstance.Id))
profiles.Add(Blog.CurrentInstance.Id, BlogService.FillProfiles());
return profiles[Blog.CurrentInstance.Id];
}
}
#endregion
#region Properties
///
/// Gets or sets AboutMe.
///
public string AboutMe
{
get
{
return this.aboutMe;
}
set
{
base.SetValue("AboutMe", value, ref this.aboutMe);
}
}
///
/// Gets or sets Birthday.
///
public DateTime Birthday
{
get
{
return this.birthday;
}
set
{
base.SetValue("Birthday", value, ref this.birthday);
}
}
///
/// Gets or sets CityTown.
///
public string CityTown
{
get
{
return this.cityTown;
}
set
{
base.SetValue("CityTown", value, ref this.cityTown);
}
}
///
/// Gets or sets Company.
///
public string Company
{
get
{
return this.company;
}
set
{
base.SetValue("Company", value, ref this.company);
}
}
///
/// Gets or sets Country.
///
public string Country
{
get
{
return this.country;
}
set
{
base.SetValue("Country", value, ref this.country);
}
}
///
/// Gets or sets DisplayName.
///
public string DisplayName
{
get
{
return this.displayName;
}
set
{
base.SetValue("DisplayName", value, ref this.displayName);
}
}
///
/// Gets or sets EmailAddress.
///
public string EmailAddress
{
get
{
return this.emailAddress;
}
set
{
base.SetValue("EmailAddress", value, ref this.emailAddress);
}
}
///
/// Gets or sets FirstName.
///
public string FirstName
{
get
{
return this.firstName;
}
set
{
base.SetValue("FirstName", value, ref this.firstName);
}
}
///
/// Gets FullName.
///
public string FullName
{
get
{
return $"{FirstName} {MiddleName} {LastName}".Replace(" ", " ");
}
}
///
/// Gets or sets a value indicating whether Private.
///
public bool Private
{
get
{
return this.isprivate;
}
set
{
base.SetValue("Private", value, ref this.isprivate);
}
}
///
/// Gets or sets LastName.
///
public string LastName
{
get
{
return this.lastName;
}
set
{
base.SetValue("LastName", value, ref this.lastName);
}
}
///
/// Gets or sets MiddleName.
///
public string MiddleName
{
get
{
return this.middleName;
}
set
{
base.SetValue("MiddleName", value, ref this.middleName);
}
}
///
/// Gets or sets PhoneFax.
///
public string PhoneFax
{
get
{
return this.phoneFax;
}
set
{
base.SetValue("PhoneFax", value, ref this.phoneFax);
}
}
///
/// Gets or sets PhoneMain.
///
public string PhoneMain
{
get
{
return this.phoneMain;
}
set
{
base.SetValue("PhoneMain", value, ref this.phoneMain);
}
}
///
/// Gets or sets PhoneMobile.
///
public string PhoneMobile
{
get
{
return this.phoneMobile;
}
set
{
base.SetValue("PhoneMobile", value, ref this.phoneMobile);
}
}
///
/// Gets or sets PhotoURL.
///
public string PhotoUrl
{
get
{
return this.photoUrl;
}
set
{
base.SetValue("PhotoUrl", value, ref this.photoUrl);
}
}
///
/// Gets or sets RegionState.
///
public string RegionState
{
get
{
return this.regionState;
}
set
{
base.SetValue("RegionState", value, ref this.regionState);
}
}
///
/// Gets RelativeLink.
///
public string RelativeLink
{
get
{
return $"{Utils.RelativeWebRoot}author/{Id}{BlogConfig.FileExtension}";
}
}
///
/// Gets UserName.
///
public string UserName
{
get
{
return this.Id;
}
}
///
/// Custom fields
///
public Dictionary CustomFields
{
get
{
var profileFields = BlogService.Provider.FillCustomFields().Where(f =>
f.CustomType == "PROFILE" &&
f.ObjectId == Security.CurrentUser.Identity.Name).ToList();
if (profileFields == null || profileFields.Count < 1)
return null;
var fields = new Dictionary();
foreach (var item in profileFields)
{
fields.Add(item.Key, item);
}
return fields;
}
}
#endregion
#region Public Methods
///
/// Gets the profile.
///
/// The username.
/// The AuthorProfile.
public static AuthorProfile GetProfile(string username)
{
return
Profiles.Find(p => p.UserName.Equals(username, StringComparison.OrdinalIgnoreCase));
}
///
/// Gets profile by email address
///
/// The email address
/// Author profile
public static AuthorProfile GetProfileByEmail(string email)
{
return
Profiles.Find(p => String.IsNullOrWhiteSpace(p.EmailAddress) ? false : p.EmailAddress.Equals(email, StringComparison.OrdinalIgnoreCase));
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return this.FullName;
}
///
/// Returns the author profile as a JsonProfile object for json serialization.
///
///
///
public static Profile ToJson(string username)
{
var j = new Profile();
var p = Profiles.Find(ap => ap.UserName.Equals(username, StringComparison.OrdinalIgnoreCase));
if (p != null)
{
j.AboutMe = string.IsNullOrEmpty(p.AboutMe) ? "" : p.aboutMe;
j.Birthday = p.Birthday.ToShortDateString();
j.CityTown = string.IsNullOrEmpty(p.CityTown) ? "" : p.CityTown;
j.Country = string.IsNullOrEmpty(p.Country) ? "" : p.Country;
j.DisplayName = p.DisplayName;
j.EmailAddress = p.EmailAddress;
j.PhoneFax = string.IsNullOrEmpty(p.PhoneFax) ? "" : p.PhoneFax;
j.FirstName = string.IsNullOrEmpty(p.FirstName) ? "" : p.FirstName;
j.Private = p.Private;
j.LastName = string.IsNullOrEmpty(p.LastName) ? "" : p.LastName;
j.MiddleName = string.IsNullOrEmpty(p.MiddleName) ? "" : p.MiddleName;
j.PhoneMobile = string.IsNullOrEmpty(p.PhoneMobile) ? "" : p.PhoneMobile;
j.PhoneMain = string.IsNullOrEmpty(p.PhoneMain) ? "" : p.PhoneMain;
j.PhotoUrl = string.IsNullOrEmpty(p.PhotoUrl) ? "" : p.PhotoUrl;
j.RegionState = string.IsNullOrEmpty(p.RegionState) ? "" : p.RegionState;
}
else
{
j.AboutMe = "";
j.Birthday = "01/01/1900";
j.CityTown = "";
j.Country = "";
j.DisplayName = username;
j.EmailAddress = Utils.GetUserEmail(username);
j.PhoneFax = "";
j.FirstName = username;
j.Private = true;
j.LastName = "";
j.MiddleName = "";
j.PhoneMobile = "";
j.PhoneMain = "";
j.PhotoUrl = "";
j.RegionState = "";
}
return j;
}
///
/// Removes profile for a specific blog
///
/// Blog ID
public static void RemoveProfile(Guid blogId)
{
profiles.Remove(blogId);
}
#endregion
#region Methods
///
/// Datas the delete.
///
protected override void DataDelete()
{
BlogService.DeleteProfile(this);
if (Profiles.Contains(this))
{
Profiles.Remove(this);
}
}
///
/// Datas the insert.
///
protected override void DataInsert()
{
BlogService.InsertProfile(this);
if (this.New)
{
Profiles.Add(this);
}
}
///
/// Datas the select.
///
/// The AuthorProfile id.
/// The AuthorProfile.
protected override AuthorProfile DataSelect(string id)
{
return BlogService.SelectProfile(id);
}
///
/// Updates the data.
///
protected override void DataUpdate()
{
BlogService.UpdateProfile(this);
}
///
/// Validates based on rules.
///
protected override void ValidationRules()
{
this.AddRule(
"Id",
"Id must be set to the username of the user who the profile belongs to",
string.IsNullOrEmpty(this.Id));
}
#endregion
}
}