using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; using CMS.PortalControls; using CMS.SocialNetworking; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.UIControls; using CMS.CMSHelper; using CMS.PortalEngine; public partial class CMSWebParts_CommunityServices_GooglePlus_GooglePlusActivityFeed : CMSAbstractWebPart { #region "Properties" /// /// ID of the user or page (can be profile URL). /// public string FeedID { get { return HTMLHelper.StripTags(ValidationHelper.GetString(GetValue("FeedID"), string.Empty)); } set { SetValue("FeedID", value); } } /// /// Width of the web part in pixels. /// public int Width { get { return ValidationHelper.GetInteger(GetValue("Width"), 0); } set { SetValue("Width", value); } } /// /// Height of the web part in pixels. /// public int Height { get { return ValidationHelper.GetInteger(GetValue("Height"), 0); } set { SetValue("Height", value); } } /// /// Maximal number of activities to display. /// public int NumberOfActivities { get { return ValidationHelper.GetInteger(GetValue("NumberOfActivities"), 5); } set { SetValue("NumberOfActivities", value); } } /// /// Whether to show scrollbar. /// public bool Scrollbar { get { return ValidationHelper.GetBoolean(GetValue("Scrollbar"), false); } set { SetValue("Scrollbar", value); } } /// /// Shell background color. /// public string ShellBackgroundColor { get { return ValidationHelper.GetString(GetValue("ShellBackgroundColor"), string.Empty); } set { SetValue("ShellBackgroundColor", value); } } /// /// Shell text color. /// public string ShellTextColor { get { return ValidationHelper.GetString(GetValue("ShellTextColor"), string.Empty); } set { SetValue("ShellTextColor", value); } } /// /// Activity text color. /// public string ActivityTextColor { get { return ValidationHelper.GetString(GetValue("ActivityTextColor"), string.Empty); } set { SetValue("ActivityTextColor", value); } } /// /// Activity footer text color. /// public string ActivityFooterTextColor { get { return ValidationHelper.GetString(GetValue("ActivityFooterTextColor"), string.Empty); } set { SetValue("ActivityFooterTextColor", value); } } /// /// Border color. /// public string BorderColor { get { return ValidationHelper.GetString(GetValue("BorderColor"), string.Empty); } set { SetValue("BorderColor", value); } } #endregion #region "Methods" /// /// Content loaded event handler /// public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); } /// /// Initializes the control properties /// protected void SetupControl() { if (StopProcessing) { // Do not process } else { string apiKey = SettingsKeyProvider.GetStringValue(CMSContext.CurrentSiteName + ".CMSGooglePlusClientID"); string apiSecret = SettingsKeyProvider.GetStringValue(CMSContext.CurrentSiteName + ".CMSGooglePlusClientSecret"); string accessToken = SettingsKeyProvider.GetStringValue(CMSContext.CurrentSiteName + ".CMSGooglePlusAccessToken"); // Check Social networking DLL and settings if (!SiteManagerFunctions.CheckSocialNetworkingDLL()) { // Error label is displayed in Design mode when Dll is renamed ShowDesignErrorOrHide(GetString("socialnetworking.renameddll")); } else if (String.IsNullOrEmpty(apiKey) || String.IsNullOrEmpty(apiSecret) || String.IsNullOrEmpty(accessToken)) { // Error label is displayed in Design mode when missing settings string pathToSettings = SocialNetworkingHelper.GetPathToGooglePlusSettings(); ShowDesignErrorOrHide(String.Format(GetString("socialnetworking.googleplus.allsettingsmissing"), pathToSettings)); } else { // Process ID - it can be URL string showID = ExtractUserID(FeedID); GooglePlusPerson profile = null; // Fetch data from Google+ try { // Try to get person directly profile = GooglePlusProvider.GetProfileInfo(showID); } catch { try { // Try alternative approach (search for person) profile = GooglePlusProvider.FindPerson(showID); } catch { ShowDesignErrorOrHide(String.Format(GetString("sm.googleplus.nopeoplefound"), showID)); } } if (profile != null) { showID = profile.UserID; List activities = GooglePlusProvider.GetActivities(showID, NumberOfActivities); // Build code for plugin StringBuilder sb = new StringBuilder(); sb.Append("
"); // Build plugin header sb.Append("
"); sb.Append(""); sb.Append("
"); sb.Append(""); sb.Append("\"");"); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(profile.DisplayName); sb.Append(""); sb.Append("
"); sb.Append("
"); // Insert section separator sb.Append("
"); // Build plugin body sb.Append("
"); int counting = 1; foreach (GooglePlusActivity s in activities) { sb.Append("
"); sb.Append("
"); sb.Append(s.Content); sb.Append("
"); sb.Append("
"); sb.Append(s.Published); sb.Append("
"); sb.Append("
"); // Insert activity separator if (counting < activities.Count) { sb.Append("
"); counting++; } } sb.Append("
"); // Insert section separator sb.Append("
"); sb.Append("
"); // Build plugin footer sb.Append("
"); sb.Append(""); sb.Append("\"\""); sb.Append(""); sb.Append(""); sb.Append("
"); sb.Append("
"); // Close open divs and finish plugin sb.Append("
"); ltlPluginCode.Text = sb.ToString(); } else { ShowDesignErrorOrHide(String.Format(GetString("sm.googleplus.nopeoplefound"), showID)); } } } } /// /// Reloads the control data /// public override void ReloadData() { base.ReloadData(); SetupControl(); } /// /// Displays the provided error message if viewmode is non-LiveSite or hides the webpart if on LiveSite /// /// The error message. private void ShowDesignErrorOrHide(string message) { if (ViewMode != ViewModeEnum.LiveSite) { // Append a linebreak because of WP menu. lblError.Text = "
" + message; lblError.Visible = true; } else { Visible = false; } } /// /// Extracts user ID (or page ID) if input string is valid Google+ profile URL. /// /// Input - user ID or URL that contains user ID. private string ExtractUserID(string input) { string output = input; int indexEnd = input.LastIndexOfCSafe("/posts"); int indexStart = 0; // If input string is valid Google+ profile URL if (indexEnd > 0) { output = output.Remove(indexEnd); indexStart = output.LastIndexOfCSafe("/"); output = output.Substring(indexStart + 1); } return output; } #endregion }