/// <summary> /// Creates Facebook post. Called when the "Create post" button is pressed. /// </summary> private bool CreateFacebookPost() { // Get an page to which the post is tied FacebookAccountInfo page = FacebookAccountInfoProvider.GetFacebookAccountInfo("MyNewPage", SiteContext.CurrentSiteID); if (page == null) { throw new Exception("[FacebookApiExamples.CreateFacebookPost]: Page 'My new page' wasn't found."); } // Create new Facebook post object FacebookPostInfo post = new FacebookPostInfo(); // Set the properties post.FacebookPostFacebookAccountID = page.FacebookAccountID; post.FacebookPostSiteID = SiteContext.CurrentSiteID; post.FacebookPostText = "Sample post text."; // Should the post be scheduled instead of directly posted? post.FacebookPostScheduledPublishDateTime = DateTime.Now + TimeSpan.FromMinutes(5); // Is the post tied to a document? post.FacebookPostDocumentGUID = null; // Save the Facebook post into DB FacebookPostInfoProvider.SetFacebookPostInfo(post); return(true); }
/// <summary> /// Gets and updates Facebook post. Called when the "Get and update post" button is pressed. /// Expects the CreateFacebookPost method to be run first. /// </summary> private bool GetAndUpdateFacebookPost() { // Get an page to which the post is tied FacebookAccountInfo page = FacebookAccountInfoProvider.GetFacebookAccountInfo("MyNewPage", SiteContext.CurrentSiteID); if (page == null) { throw new Exception("[FacebookApiExamples.GetAndUpdateFacebookPost]: Page 'My new page' wasn't found."); } // Get the Facebook post from DB FacebookPostInfo post = FacebookPostInfoProvider.GetFacebookPostInfosByAccountId(page.FacebookAccountID).FirstOrDefault(); if (post != null) { // Update the properties post.FacebookPostText = post.FacebookPostText + " Edited."; // Save the changes into DB FacebookPostInfoProvider.SetFacebookPostInfo(post); return(true); } return(false); }
/// <summary> /// OnAfterSave event. /// </summary> private void ControlOnAfterSave(object sender, EventArgs eventArgs) { FacebookPostInfo facebookPost = Control.EditedObject as FacebookPostInfo; if (facebookPost == null) { return; } if (!facebookPost.FacebookPostPostAfterDocumentPublish) { try { FacebookPostInfoProvider.PublishFacebookPost(facebookPost.FacebookPostID); } catch (Exception ex) { EventLogProvider.LogWarning("Social marketing - Facebook post", "PUBLISHPOST", ex, SiteContext.CurrentSiteID, String.Format("An error occurred while publishing the Facebook post with ID {0}.", facebookPost.FacebookPostID)); Control.ShowError(Control.GetString("sm.facebook.posts.msg.unknownerror")); } } // Invoke event to set the form's state properly ControlOnAfterDataLoad(sender, eventArgs); }
/// <summary> /// Gets a link control that opens post details dialog when clicked. Post's content is used as link text. /// </summary> /// <param name="postId">Facebook post identifier.</param> private object GetFacebookPostDetailLink(int postId) { FacebookPostInfo post = FacebookPostInfoProvider.GetFacebookPostInfo(postId); if (LicenseKeyInfoProvider.IsFeatureAvailable(FeatureEnum.SocialMarketingInsights)) { string dialogUrl = String.Format(mFacebookPostDetailsUrlFormat, post.FacebookPostID); return(String.Format("<a href=\"{0}\" onclick=\"modalDialog('{0}', 'PostDetails', 480, 570); return false;\" title=\"{1}\">{2}</a>", dialogUrl, ResHelper.GetString("sm.facebook.posts.detail.tooltip"), HTMLHelper.HTMLEncode(TextHelper.LimitLength(post.FacebookPostText, 80)) )); } return(HTMLHelper.HTMLEncode(TextHelper.LimitLength(post.FacebookPostText, 80))); }
/// <summary> /// Gets localized message describing post state. /// </summary> /// <param name="postId">Post id.</param> private object GetPostState(int postId) { switch (Control.ObjectType) { // Facebook post case FacebookPostInfo.OBJECT_TYPE: FacebookPostInfo facebookPost = FacebookPostInfoProvider.GetFacebookPostInfo(postId); return(FacebookPostInfoProvider.GetPostPublishStateMessage(facebookPost, MembershipContext.AuthenticatedUser, SiteContext.CurrentSite, true)); // Twitter post case TwitterPostInfo.OBJECT_TYPE: TwitterPostInfo twitterPost = TwitterPostInfoProvider.GetTwitterPostInfo(postId); return(TwitterPostInfoProvider.GetPostPublishStateMessage(twitterPost, MembershipContext.AuthenticatedUser, SiteContext.CurrentSite, true)); } return(String.Empty); }
/// <summary> /// Loads post data into the inner controls. /// </summary> /// <param name="post">Facebook post that will be loaded.</param> private void LoadPostDataIntoControl(FacebookPostInfo post) { pageSelector.Value = post.FacebookPostFacebookAccountID; txtPost.Text = post.FacebookPostText; if (post.FacebookPostPostAfterDocumentPublish) { chkPostAfterDocumentPublish.Checked = true; publishDateTime.Enabled = false; } publishDateTime.Value = post.FacebookPostScheduledPublishDateTime; campaingSelector.Value = post.FacebookPostCampaignID; urlShortenerSelector.Value = post.FacebookPostURLShortenerType; DisplayForm = true; ShowPostPublishState(post); }
/// <summary> /// Sets data retrieved from inner controls into new (or given) Facebook post object. /// </summary> /// <param name="post">Facebook post object that will be set from inner controls.</param> /// <returns>Facebook post containing data retrieved from inner controls.</returns> private FacebookPostInfo SetControlDataIntoPost(FacebookPostInfo post = null) { if (post == null) { post = new FacebookPostInfo(); } post.FacebookPostFacebookAccountID = ValidationHelper.GetInteger(pageSelector.Value, 0); post.FacebookPostText = txtPost.Text; post.FacebookPostURLShortenerType = (URLShortenerTypeEnum)urlShortenerSelector.Value; post.FacebookPostPostAfterDocumentPublish = chkPostAfterDocumentPublish.Checked; post.FacebookPostScheduledPublishDateTime = ValidationHelper.GetDateTime(publishDateTime.Value, DateTimeHelper.ZERO_TIME); post.FacebookPostCampaignID = ValidationHelper.GetInteger(campaingSelector.Value, 0); post.FacebookPostSiteID = SiteIdentifier; post.FacebookPostIsCreatedByUser = true; return(post); }
/// <summary> /// OnAfterDataLoad event /// </summary> private void ControlOnAfterDataLoad(object sender, EventArgs eventArgs) { FacebookPostInfo facebookPost = Control.EditedObject as FacebookPostInfo; if ((facebookPost != null) && (facebookPost.FacebookPostID > 0)) { string message = FacebookPostInfoProvider.GetPostPublishStateMessage(facebookPost, MembershipContext.AuthenticatedUser, SiteContext.CurrentSite); if (facebookPost.IsFaulty) { Control.ShowError(message); } else { Control.ShowInformation(message); } // Disable control if post has already been published or is faulty Control.Enabled = !(facebookPost.IsPublished || facebookPost.IsFaulty); } }
/// <summary> /// Indicates whether data in the form are equals to the given post info or not. /// </summary> /// <param name="post">Post that is compared with form data.</param> /// <returns>True if form data are equals to given post, false otherwise.</returns> private bool FormDataEqualsPostInfo(FacebookPostInfo post) { if (!chkPostToFacebook.Checked) { return(false); } DateTime?formScheduledPublishTime = ValidationHelper.GetDateTime(publishDateTime.Value, DateTimeHelper.ZERO_TIME); if (formScheduledPublishTime == DateTimeHelper.ZERO_TIME) { formScheduledPublishTime = null; } return(post.FacebookPostText.EqualsCSafe(txtPost.Text) && post.FacebookPostScheduledPublishDateTime == formScheduledPublishTime && post.FacebookPostFacebookAccountID == ValidationHelper.GetInteger(pageSelector.Value, 0) && post.FacebookPostURLShortenerType == (URLShortenerTypeEnum)urlShortenerSelector.Value && post.FacebookPostPostAfterDocumentPublish == chkPostAfterDocumentPublish.Checked && ValidationHelper.GetInteger(post.FacebookPostCampaignID, 0) == ValidationHelper.GetInteger(campaingSelector.Value, 0) && post.FacebookPostSiteID == SiteIdentifier); }
/// <summary> /// OnBeforeSave event. /// </summary> private void Control_OnBeforeSave(object sender, EventArgs eventArgs) { FacebookPostInfo facebookPost = Control.EditedObject as FacebookPostInfo; if (facebookPost == null) { return; } if (facebookPost.FacebookPostID <= 0) { // The post is being created, not edited return; } if (!FacebookPostInfoProvider.TryCancelScheduledPublishFacebookPost(facebookPost)) { // The post has been published during user edit. Prevent the changes to take effect CancelPendingSave(Control.GetString("sm.facebook.posts.msg.editforbidden")); } }
/// <summary> /// Publishes a facebook post. /// </summary> private bool PublishPostToFacebook() { // Get an page to which the post is tied FacebookAccountInfo page = FacebookAccountInfoProvider.GetFacebookAccountInfo("MyNewPage", SiteContext.CurrentSiteID); if (page == null) { throw new Exception("[FacebookApiExamples.PublishPostToFacebook]: Page 'My new page' wasn't found."); } // Get the Facebook post from DB FacebookPostInfo post = FacebookPostInfoProvider.GetFacebookPostInfosByAccountId(page.FacebookAccountID).FirstOrDefault(); if (post == null) { throw new Exception("[FacebookApiExamples.PublishPostToFacebook]: No post has been created via these api Examples, or they have been deleted."); } // Publish the post. The post is scheduled to be published if its FacebookPostScheduledPublishDateTime is set in the future. FacebookPostInfoProvider.PublishFacebookPost(post.FacebookPostID); return(true); }
/// <summary> /// Shows given Facebook post's publish state. /// </summary> /// <param name="post">Facebook post.</param> private void ShowPostPublishState(FacebookPostInfo post) { string message = FacebookPostInfoProvider.GetPostPublishStateMessage(post, MembershipContext.AuthenticatedUser, CurrentSite); if (String.IsNullOrEmpty(message)) { return; } if (post.IsFaulty) { ShowPostStateError(message); ShowError(String.Format("<strong>{0}</strong>: {1}", GetString("sm.facebook.autopost"), message)); } else if (post.IsPublished) { ShowPostStateSuccess(message); } else { ShowPostStateInformation(message, MessageTypeEnum.Information); } }
/// <summary> /// Shows given Facebook post's publish state. /// </summary> /// <param name="post">Facebook post.</param> private void ShowPostPublishState(FacebookPostInfo post) { string message = FacebookPostInfoProvider.GetPostPublishStateMessage(post, MembershipContext.AuthenticatedUser, CurrentSite); if (String.IsNullOrEmpty(message)) { return; } if (post.IsFaulty) { var errorMessage = FormatErrorMessage(message); ShowError(errorMessage); } else if (post.IsPublished) { ShowConfirmation(message, true); } else { ShowInformation(message); } }
/// <summary> /// Loads data from post info object into the dialog. /// </summary> /// <param name="post">Facebook post info object.</param> private void LoadPostIntoDialog(FacebookPostInfo post) { if ((post == null) || (post.FacebookPostSiteID != SiteContext.CurrentSiteID)) { pnlPostDetail.Visible = false; ShowError(ResHelper.GetString("sm.facebook.posts.msg.postnotexist")); return; } lblPostStatus.Text = FacebookPostInfoProvider.GetPostPublishStateMessage(post, MembershipContext.AuthenticatedUser, SiteContext.CurrentSite, true); lblPeopleReachedValue.Text = ValidationHelper.GetString(post.FacebookPostInsightPeopleReached, String.Empty); if (post.FacebookPostCampaignID.HasValue) { CampaignInfo campaign = CampaignInfoProvider.GetCampaignInfo(post.FacebookPostCampaignID.Value); if (campaign != null) { lblCampaign.Text = HTMLHelper.HTMLEncode(campaign.CampaignDisplayName); } } else { pnlCampaign.Visible = false; } lblPostText.Text = GetPostTextHTML(post.FacebookPostText); lblPostLikesValue.Text = ValidationHelper.GetString(post.FacebookPostInsightLikesFromPage, String.Empty); lblPostCommentsValue.Text = ValidationHelper.GetString(post.FacebookPostInsightCommentsFromPage, String.Empty); lblPostSharesValue.Text = ValidationHelper.GetString(post.FacebookPostInsightSharesFromPage, String.Empty); lblTotalLikesValue.Text = ValidationHelper.GetString(post.FacebookPostInsightLikesTotal, String.Empty); lblTotalCommentsValue.Text = ValidationHelper.GetString(post.FacebookPostInsightCommentsTotal, String.Empty); lblHidePostValue.Text = ValidationHelper.GetString(post.FacebookPostInsightNegativeHidePost, String.Empty); lblHideAllPostsValue.Text = ValidationHelper.GetString(post.FacebookPostInsightNegativeHideAllPosts, String.Empty); lblReportSpamValue.Text = ValidationHelper.GetString(post.FacebookPostInsightNegativeReportSpam, String.Empty); lblUnlikePageValue.Text = ValidationHelper.GetString(post.FacebookPostInsightNegativeUnlikePage, String.Empty); }
/// <summary> /// Loads data from post info object into the dialog. /// </summary> /// <param name="post">Facebook post info object.</param> private void LoadPostIntoDialog(FacebookPostInfo post) { if ((post == null) || (post.FacebookPostSiteID != SiteContext.CurrentSiteID)) { pnlPostDetail.Visible = false; ShowError(GetString("sm.facebook.posts.msg.postnotexist")); return; } lblPostStatus.Text = FacebookPostInfoProvider.GetPostPublishStateMessage(post, MembershipContext.AuthenticatedUser, SiteContext.CurrentSite, true); lblPeopleReachedValue.Text = ValidationHelper.GetString(post.FacebookPostInsightPeopleReached, String.Empty); if (post.FacebookPostCampaignID.HasValue) { CampaignInfo campaign = CampaignInfo.Provider.Get(post.FacebookPostCampaignID.Value); if (campaign != null) { lblCampaign.Text = HTMLHelper.HTMLEncode(campaign.CampaignDisplayName); } } else { pnlCampaign.Visible = false; } lblPostText.Text = GetPostTextHTML(post.FacebookPostText); lblPostLikesValue.Text = ValidationHelper.GetString(post.FacebookPostInsightLikesFromPage, String.Empty); lblPostCommentsValue.Text = ValidationHelper.GetString(post.FacebookPostInsightCommentsFromPage, String.Empty); lblPostSharesValue.Text = ValidationHelper.GetString(post.FacebookPostInsightSharesFromPage, String.Empty); lblTotalLikesValue.Text = ValidationHelper.GetString(post.FacebookPostInsightLikesTotal, String.Empty); lblTotalCommentsValue.Text = ValidationHelper.GetString(post.FacebookPostInsightCommentsTotal, String.Empty); lblHidePostValue.Text = ValidationHelper.GetString(post.FacebookPostInsightNegativeHidePost, String.Empty); lblHideAllPostsValue.Text = ValidationHelper.GetString(post.FacebookPostInsightNegativeHideAllPosts, String.Empty); lblReportSpamValue.Text = ValidationHelper.GetString(post.FacebookPostInsightNegativeReportSpam, String.Empty); lblUnlikePageValue.Text = ValidationHelper.GetString(post.FacebookPostInsightNegativeUnlikePage, String.Empty); }
/// <summary> /// Sets data retrieved from inner controls into new (or given) Facebook post object. /// </summary> /// <param name="post">Facebook post object that will be set from inner controls.</param> /// <returns>Facebook post containing data retrieved from inner controls.</returns> private FacebookPostInfo SetControlDataIntoPost(FacebookPostInfo post = null) { if (post == null) { post = new FacebookPostInfo(); } post.FacebookPostFacebookAccountID = ValidationHelper.GetInteger(pageSelector.Value, 0); post.FacebookPostText = txtPost.Text; post.FacebookPostURLShortenerType = (URLShortenerTypeEnum)urlShortenerSelector.Value; post.FacebookPostPostAfterDocumentPublish = chkPostAfterDocumentPublish.Checked; post.FacebookPostScheduledPublishDateTime = ValidationHelper.GetDateTime(publishDateTime.Value, DateTimeHelper.ZERO_TIME); post.FacebookPostCampaignID = ValidationHelper.GetInteger(campaingSelector.Value, 0); post.FacebookPostSiteID = SiteIdentifier; post.FacebookPostIsCreatedByUser = true; return post; }
/// <summary> /// Form OnBeforeDataRetrieval. /// </summary> private void Form_OnBeforeDataRetrieval(object sender, EventArgs eventArgs) { if (!HasUserModifyPermission) { return; } if (chkPostToFacebook.Checked) { if ((PostInfo == null) || PostInfo.IsEditable) { // Post does not exist or can be modified PostInfo = SetControlDataIntoPost(PostInfo); FacebookPostInfoProvider.SetFacebookPostInfo(PostInfo); } else if (!FormDataEqualsPostInfo(PostInfo)) { mPublishedWhileEditing = true; } } else { // Checkbox post to Facebook is not checked if (PostInfo != null) { if (PostInfo.IsEditable) { // Existing post has to be deleted FacebookPostInfoProvider.DeleteFacebookPostInfo(PostInfo); PostInfo = null; InitializeControls(); } else { mPublishedWhileEditing = true; } } else if (mBackCompatibilityValue.HasValue) { // Form has to be cleaned up from old value mBackCompatibilityValue = null; InitializeControls(); } } }
/// <summary> /// Creates Facebook post. Called when the "Create post" button is pressed. /// </summary> private bool CreateFacebookPost() { // Get an page to which the post is tied FacebookAccountInfo page = FacebookAccountInfoProvider.GetFacebookAccountInfo("MyNewPage", SiteContext.CurrentSiteID); if (page == null) { throw new Exception("[FacebookApiExamples.CreateFacebookPost]: Page 'My new page' wasn't found."); } // Create new Facebook post object FacebookPostInfo post = new FacebookPostInfo(); // Set the properties post.FacebookPostFacebookAccountID = page.FacebookAccountID; post.FacebookPostSiteID = SiteContext.CurrentSiteID; post.FacebookPostText = "Sample post text."; // Should the post be scheduled instead of directly posted? post.FacebookPostScheduledPublishDateTime = DateTime.Now + TimeSpan.FromMinutes(5); // Is the post tied to a document? post.FacebookPostDocumentGUID = null; // Save the Facebook post into DB FacebookPostInfoProvider.SetFacebookPostInfo(post); return true; }
/// <summary> /// Indicates whether data in the form are equals to the given post info or not. /// </summary> /// <param name="post">Post that is compared with form data.</param> /// <returns>True if form data are equals to given post, false otherwise.</returns> private bool FormDataEqualsPostInfo(FacebookPostInfo post) { if (!chkPostToFacebook.Checked) { return false; } DateTime? formScheduledPublishTime = ValidationHelper.GetDateTime(publishDateTime.Value, DateTimeHelper.ZERO_TIME); if (formScheduledPublishTime == DateTimeHelper.ZERO_TIME) { formScheduledPublishTime = null; } return post.FacebookPostText.EqualsCSafe(txtPost.Text) && post.FacebookPostScheduledPublishDateTime == formScheduledPublishTime && post.FacebookPostFacebookAccountID == ValidationHelper.GetInteger(pageSelector.Value, 0) && post.FacebookPostURLShortenerType == (URLShortenerTypeEnum)urlShortenerSelector.Value && post.FacebookPostPostAfterDocumentPublish == chkPostAfterDocumentPublish.Checked && ValidationHelper.GetInteger(post.FacebookPostCampaignID, 0) == ValidationHelper.GetInteger(campaingSelector.Value, 0) && post.FacebookPostSiteID == SiteIdentifier; }