/// <summary>
    /// OnAfterSave event.
    /// </summary>
    private void ControlOnAfterSave(object sender, EventArgs eventArgs)
    {
        LinkedInPostInfo post = Control.EditedObject as LinkedInPostInfo;

        if (post == null)
        {
            return;
        }

        if (!post.LinkedInPostPostAfterDocumentPublish)
        {
            try
            {
                LinkedInPostInfoProvider.PublishLinkedInPost(post.LinkedInPostID);
            }
            catch (Exception ex)
            {
                EventLogProvider.LogWarning("Social marketing - LinkedIn post", "PUBLISHPOST", ex, SiteContext.CurrentSiteID,
                                            String.Format("An error occurred while publishing the LinkedIn post with ID {0}.", post.LinkedInPostID));
                Control.ShowError(Control.GetString("sm.linkedin.posts.msg.unknownerror"));
            }
        }

        // Invoke event to set the form's state properly
        ControlOnAfterDataLoad(sender, eventArgs);
    }
    /// <summary>
    /// Sets data retrieved from inner controls into new (or given) LinkedIn post object.
    /// </summary>
    /// <param name="post">LinkedIn post object that will be set from inner controls.</param>
    /// <returns>LinkedIn post containing data retrieved from inner controls.</returns>
    private LinkedInPostInfo SetControlDataIntoPost(LinkedInPostInfo post = null)
    {
        if (post == null)
        {
            post = new LinkedInPostInfo();
        }

        post.LinkedInPostLinkedInAccountID        = ValidationHelper.GetInteger(companySelector.Value, 0);
        post.LinkedInPostComment                  = txtPost.Text;
        post.LinkedInPostURLShortenerType         = (URLShortenerTypeEnum)urlShortenerSelector.Value;
        post.LinkedInPostPostAfterDocumentPublish = chkPostAfterDocumentPublish.Checked;
        post.LinkedInPostScheduledPublishDateTime = ValidationHelper.GetDateTime(publishDateTime.Value, DateTimeHelper.ZERO_TIME);
        post.LinkedInPostCampaignID               = ValidationHelper.GetInteger(campaingSelector.Value, 0);
        post.LinkedInPostSiteID          = SiteIdentifier;
        post.LinkedInPostIsCreatedByUser = true;

        return(post);
    }
    /// <summary>
    /// Loads post data into the inner controls.
    /// </summary>
    /// <param name="post">LinkedIn post that will be loaded.</param>
    private void LoadPostDataIntoControl(LinkedInPostInfo post)
    {
        companySelector.Value = post.LinkedInPostLinkedInAccountID;
        txtPost.Text          = post.LinkedInPostComment;

        if (post.LinkedInPostPostAfterDocumentPublish)
        {
            chkPostAfterDocumentPublish.Checked = true;
            publishDateTime.Enabled             = false;
        }
        publishDateTime.Value = post.LinkedInPostScheduledPublishDateTime;

        campaingSelector.Value     = post.LinkedInPostCampaignID;
        urlShortenerSelector.Value = post.LinkedInPostURLShortenerType;

        DisplayForm = true;
        ShowPostPublishState(post);
    }
    /// <summary>
    /// OnAfterDataLoad event
    /// </summary>
    private void ControlOnAfterDataLoad(object sender, EventArgs eventArgs)
    {
        LinkedInPostInfo post = Control.EditedObject as LinkedInPostInfo;

        if ((post != null) && (post.LinkedInPostID > 0))
        {
            string message = LinkedInPostInfoProvider.GetPostPublishStateMessage(post, MembershipContext.AuthenticatedUser, SiteContext.CurrentSite);
            if (post.IsFaulty)
            {
                Control.ShowError(message);
            }
            else
            {
                Control.ShowInformation(message);
            }

            // Disable control if post has already been published or is faulty
            Control.Enabled = !(post.IsPublished || post.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(LinkedInPostInfo post)
    {
        if (!chkPostToLinkedIn.Checked)
        {
            return(false);
        }

        DateTime?formScheduledPublishTime = ValidationHelper.GetDateTime(publishDateTime.Value, DateTimeHelper.ZERO_TIME);

        if (formScheduledPublishTime == DateTimeHelper.ZERO_TIME)
        {
            formScheduledPublishTime = null;
        }
        return(String.Equals(post.LinkedInPostComment, txtPost.Text, StringComparison.InvariantCulture) &&
               post.LinkedInPostScheduledPublishDateTime == formScheduledPublishTime &&
               post.LinkedInPostLinkedInAccountID == ValidationHelper.GetInteger(companySelector.Value, 0) &&
               post.LinkedInPostURLShortenerType == (URLShortenerTypeEnum)urlShortenerSelector.Value &&
               post.LinkedInPostPostAfterDocumentPublish == chkPostAfterDocumentPublish.Checked &&
               ValidationHelper.GetInteger(post.LinkedInPostCampaignID, 0) == ValidationHelper.GetInteger(campaingSelector.Value, 0) &&
               post.LinkedInPostSiteID == SiteIdentifier);
    }
    /// <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(LinkedInPostInfo post)
    {
        if (!chkPostToLinkedIn.Checked)
        {
            return(false);
        }

        DateTime?formScheduledPublishTime = ValidationHelper.GetDateTime(publishDateTime.Value, DateTimeHelper.ZERO_TIME);

        if (formScheduledPublishTime == DateTimeHelper.ZERO_TIME)
        {
            formScheduledPublishTime = null;
        }
        return(post.LinkedInPostComment.EqualsCSafe(txtPost.Text) &&
               post.LinkedInPostScheduledPublishDateTime == formScheduledPublishTime &&
               post.LinkedInPostLinkedInAccountID == ValidationHelper.GetInteger(companySelector.Value, 0) &&
               post.LinkedInPostURLShortenerType == (URLShortenerTypeEnum)ValidationHelper.GetInteger(urlShortenerSelector.Value, 0) &&
               post.LinkedInPostPostAfterDocumentPublish == chkPostAfterDocumentPublish.Checked &&
               ValidationHelper.GetInteger(post.LinkedInPostCampaignID, 0) == ValidationHelper.GetInteger(campaingSelector.Value, 0) &&
               post.LinkedInPostSiteID == SiteContext.CurrentSiteID);
    }
    /// <summary>
    /// OnBeforeSave event.
    /// </summary>
    private void Control_OnBeforeSave(object sender, EventArgs eventArgs)
    {
        LinkedInPostInfo post = Control.EditedObject as LinkedInPostInfo;

        if (post == null)
        {
            return;
        }

        if (post.LinkedInPostID <= 0)
        {
            // The post is being created, not edited
            return;
        }

        if (!LinkedInPostInfoProvider.TryCancelScheduledPublishLinkedInPost(post))
        {
            // The post has been published during user edit. Prevent the changes to take effect
            CancelPendingSave(Control.GetString("sm.linkedin.posts.msg.editforbidden"));
        }
    }
Esempio n. 8
0
    /// <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));

        // LinkedIn post
        case LinkedInPostInfo.OBJECT_TYPE:
            LinkedInPostInfo linkedInPost = LinkedInPostInfoProvider.GetLinkedInPostInfo(postId);
            return(LinkedInPostInfoProvider.GetPostPublishStateMessage(linkedInPost, 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>
    /// Shows given LinkedIn post's publish state.
    /// </summary>
    /// <param name="post">LinkedIn post.</param>
    private void ShowPostPublishState(LinkedInPostInfo post)
    {
        string message = LinkedInPostInfoProvider.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);
        }
        else
        {
            ShowInformation(message);
        }
    }
    /// <summary>
    /// Shows given LinkedIn post's publish state.
    /// </summary>
    /// <param name="post">LinkedIn post.</param>
    private void ShowPostPublishState(LinkedInPostInfo post)
    {
        string message = LinkedInPostInfoProvider.GetPostPublishStateMessage(post, MembershipContext.AuthenticatedUser, CurrentSite);

        if (String.IsNullOrEmpty(message))
        {
            return;
        }

        if (post.IsFaulty)
        {
            ShowPostStateError(message);
            ShowError(String.Format("<strong>{0}</strong>: {1}", GetString("sm.linkedin.autopost"), message));
        }
        else if (post.IsPublished)
        {
            ShowPostStateSuccess(message);
        }
        else
        {
            ShowPostStateInformation(message, MessageTypeEnum.Information);
        }
    }
    /// <summary>
    /// Shows given LinkedIn post's publish state.
    /// </summary>
    /// <param name="post">LinkedIn post.</param>
    private void ShowPostPublishState(LinkedInPostInfo post)
    {
        string message = LinkedInPostInfoProvider.GetPostPublishStateMessage(post, MembershipContext.AuthenticatedUser, CurrentSite);

        if (String.IsNullOrEmpty(message))
        {
            return;
        }

        if (post.IsFaulty)
        {
            ShowPostStateError(message);
            ShowError(String.Format("<strong>{0}</strong>: {1}", GetString("sm.linkedin.autopost"), message));
        }
        else if (post.IsPublished)
        {
            ShowPostStateSuccess(message);
        }
        else
        {
            ShowPostStateInformation(message, MessageTypeEnum.Information);
        }
    }
    /// <summary>
    /// Sets data retrieved from inner controls into new (or given) LinkedIn post object.
    /// </summary>
    /// <param name="post">LinkedIn post object that will be set from inner controls.</param>
    /// <returns>LinkedIn post containing data retrieved from inner controls.</returns>
    private LinkedInPostInfo SetControlDataIntoPost(LinkedInPostInfo post = null)
    {
        if (post == null)
        {
            post = new LinkedInPostInfo();
        }

        post.LinkedInPostLinkedInAccountID = ValidationHelper.GetInteger(companySelector.Value, 0);
        post.LinkedInPostComment = txtPost.Text;
        if (chkShortenUrls.Checked)
        {
            post.LinkedInPostURLShortenerType = (URLShortenerTypeEnum)ValidationHelper.GetInteger(urlShortenerSelector.Value, 0);
        }
        post.LinkedInPostPostAfterDocumentPublish = chkPostAfterDocumentPublish.Checked;
        post.LinkedInPostScheduledPublishDateTime = ValidationHelper.GetDateTime(publishDateTime.Value, DateTimeHelper.ZERO_TIME);
        post.LinkedInPostCampaignID = ValidationHelper.GetInteger(campaingSelector.Value, 0);
        post.LinkedInPostSiteID = SiteContext.CurrentSiteID;
        post.LinkedInPostIsCreatedByUser = true;

        return post;
    }
    /// <summary>
    /// Loads post data into the inner controls.
    /// </summary>
    /// <param name="post">LinkedIn post that will be loaded.</param>
    private void LoadPostDataIntoControl(LinkedInPostInfo post)
    {
        companySelector.Value = post.LinkedInPostLinkedInAccountID;
        txtPost.Text = post.LinkedInPostComment;

        if (post.LinkedInPostPostAfterDocumentPublish)
        {
            chkPostAfterDocumentPublish.Checked = true;
            publishDateTime.Enabled = false;
        }
        publishDateTime.Value = post.LinkedInPostScheduledPublishDateTime;

        campaingSelector.Value = post.LinkedInPostCampaignID;
        if (PostInfo.LinkedInPostURLShortenerType != URLShortenerTypeEnum.None)
        {
            chkShortenUrls.Checked = true;
            urlShortenerSelector.Value = (int)post.LinkedInPostURLShortenerType;
        }

        DisplayForm = true;
        ShowPostPublishState(post);
    }
    /// <summary>
    /// Form OnBeforeDataRetrieval.
    /// </summary>
    private void Form_OnBeforeDataRetrieval(object sender, EventArgs eventArgs)
    {
        if (!HasUserModifyPermission())
        {
            return;
        }

        if (chkPostToLinkedIn.Checked)
        {
            if ((PostInfo == null) || PostInfo.IsEditable)
            {
                // Post does not exist or can be modified
                PostInfo = SetControlDataIntoPost(PostInfo);
                LinkedInPostInfoProvider.SetLinkedInPostInfo(PostInfo);
            }
            else if (!FormDataEqualsPostInfo(PostInfo))
            {
                mPublishedWhileEditing = true;
            }
        }
        else
        {
            // Checkbox post to LinkedIn is not checked
            if (PostInfo != null)
            {
                if (PostInfo.IsEditable)
                {
                    // Existing post has to be deleted
                    LinkedInPostInfoProvider.DeleteLinkedInPostInfo(PostInfo);
                    PostInfo = null;
                    InitializeControls();
                }
                else
                {
                    mPublishedWhileEditing = 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(LinkedInPostInfo post)
    {
        if (!chkPostToLinkedIn.Checked)
        {
            return false;
        }

        DateTime? formScheduledPublishTime = ValidationHelper.GetDateTime(publishDateTime.Value, DateTimeHelper.ZERO_TIME);
        if (formScheduledPublishTime == DateTimeHelper.ZERO_TIME)
        {
            formScheduledPublishTime = null;
        }
        return post.LinkedInPostComment.EqualsCSafe(txtPost.Text)
            && post.LinkedInPostScheduledPublishDateTime == formScheduledPublishTime
            && post.LinkedInPostLinkedInAccountID == ValidationHelper.GetInteger(companySelector.Value, 0)
            && post.LinkedInPostURLShortenerType == (URLShortenerTypeEnum)ValidationHelper.GetInteger(urlShortenerSelector.Value, 0)
            && post.LinkedInPostPostAfterDocumentPublish == chkPostAfterDocumentPublish.Checked
            && ValidationHelper.GetInteger(post.LinkedInPostCampaignID, 0) == ValidationHelper.GetInteger(campaingSelector.Value, 0)
            && post.LinkedInPostSiteID == SiteContext.CurrentSiteID;
    }