Beispiel #1
0
    /// <summary>
    /// Initialize resources, holders, controls etc.
    /// </summary>
    protected void Initialize()
    {
        #region "Captcha"

        if ((ForumContext.CurrentForum.ForumUseCAPTCHA) && (ForumContext.CurrentState != ForumStateEnum.EditPost))
        {
            // Do not generate security every time
            SecurityCode1.AlwaysGenerate        = false;
            SecurityCode1.KeepCodeAutomatically = false;
        }
        else
        {
            plcCaptcha.Visible = false;
        }

        #endregion

        #region "Settings of HTML editor"

        // Set HTML editor properties
        htmlTemplateBody.AutoDetectLanguage    = false;
        htmlTemplateBody.DefaultLanguage       = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
        htmlTemplateBody.EditorAreaCSS         = "";
        htmlTemplateBody.ToolbarSet            = "Forum";
        htmlTemplateBody.DisableObjectResizing = true; // Disable image resizing
        htmlTemplateBody.RemovePlugin("contextmenu");  // Disable context menu
        htmlTemplateBody.IsLiveSite = this.IsLiveSite;
        htmlTemplateBody.MediaDialogConfig.UseFullURL = true;
        htmlTemplateBody.LinkDialogConfig.UseFullURL  = true;

        #endregion

        #region "Resource strings"

        // Resources
        rfvSubject.ErrorMessage  = GetString("Forums_WebInterface_ForumNewPost.subjectErrorMsg");
        lblText.Text             = GetString("Forums_WebInterface_ForumNewPost.text");
        rfvText.ErrorMessage     = GetString("Forums_WebInterface_ForumNewPost.textErrorMsg");
        rfvUserName.ErrorMessage = GetString("Forums_WebInterface_ForumNewPost.usernameErrorMsg");
        btnOk.Text            = GetString("general.ok");
        btnCancel.Text        = GetString("general.cancel");
        btnPreview.Text       = GetString("Forums_WebInterface_ForumNewPost.Preview");
        lblSubscribe.Text     = GetString("Forums_WebInterface_ForumNewPost.Subscription");
        lblSignature.Text     = GetString("Forums_WebInterface_ForumNewPost.Signature");
        rfvEmail.ErrorMessage = GetString("Forums_WebInterface_ForumNewPost.emailErrorMsg");
        lblCaptcha.Text       = GetString("Forums_WebInterface_ForumNewPost.captcha");
        lblAttachFile.Text    = GetString("For.NewPost.Attach");
        lblNickName.Text      = GetString("Forums_WebInterface_ForumNewPost.NickName");

        // Regular expression to validate email (e-mail is not required)
        rfvEmail.ValidationExpression = @"^([\w0-9_\-\+]+(\.[\w0-9_\-\+]+)*@[\w0-9_-]+(\.[\w0-9_-]+)+)*$";

        // WAI validation
        lblCaptcha.AssociatedControlClientID = SecurityCode1.InputClientID;

        #endregion

        #region "Controls visibility"

        ForumInfo fi = ForumContext.CurrentForum;

        // Hide or display html editor/ text area
        if (fi.ForumHTMLEditor)
        {
            ucBBEditor.Visible = false;
            rfvText.Enabled    = false;

            // Define customizable shortcuts
            Hashtable keystrokes = new Hashtable()
            {
                { "link", "CKEDITOR.CTRL + 76 /*L*/" },
                { "bold", "CKEDITOR.CTRL + 66 /*B*/" },
                { "italic", "CKEDITOR.CTRL + 73 /*I*/" },
                { "underline", "CKEDITOR.CTRL + 85 /*U*/" }
            };

            if (!fi.ForumEnableURL)
            {
                htmlTemplateBody.RemoveButton("InsertUrl");
                if (!fi.ForumEnableAdvancedURL)
                {
                    // Remove the keyborad shortcut for the link insertion
                    keystrokes.Remove("link");
                }
            }
            if (!fi.ForumEnableAdvancedURL)
            {
                htmlTemplateBody.RemoveButton("InsertLink");
            }
            if (!fi.ForumEnableImage)
            {
                htmlTemplateBody.RemoveButton("InsertImage");
            }
            if (!fi.ForumEnableAdvancedImage)
            {
                htmlTemplateBody.RemoveButton("InsertImageOrMedia");
            }
            if (!fi.ForumEnableQuote)
            {
                htmlTemplateBody.RemoveButton("InsertQuote");
            }
            if (!fi.ForumEnableFontBold)
            {
                htmlTemplateBody.RemoveButton("Bold");
                keystrokes.Remove("bold");
            }
            if (!fi.ForumEnableFontItalics)
            {
                htmlTemplateBody.RemoveButton("Italic");
                keystrokes.Remove("italic");
            }
            if (!fi.ForumEnableFontUnderline)
            {
                htmlTemplateBody.RemoveButton("Underline");
                keystrokes.Remove("underline");
            }
            if (!fi.ForumEnableFontStrike)
            {
                htmlTemplateBody.RemoveButton("Strike");
            }
            if (!fi.ForumEnableFontColor)
            {
                htmlTemplateBody.RemoveButton("TextColor");
                htmlTemplateBody.RemoveButton("BGColor");
            }

            // Generate keystrokes string for the CK Editor
            StringBuilder sb     = new StringBuilder("[ [ CKEDITOR.ALT + 121 /*F10*/, 'toolbarFocus' ], [ CKEDITOR.ALT + 122 /*F11*/, 'elementsPathFocus' ], [ CKEDITOR.CTRL + 90 /*Z*/, 'undo' ], [ CKEDITOR.CTRL + 89 /*Y*/, 'redo' ], [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 /*Z*/, 'redo' ], [ CKEDITOR.ALT + ( CKEDITOR.env.ie || CKEDITOR.env.webkit ? 189 : 109 ) /*-*/, 'toolbarCollapse' ], [ CKEDITOR.ALT + 48 /*0*/, 'a11yHelp' ]");
            string        format = ", [ {0}, '{1}' ]";

            foreach (DictionaryEntry entry in keystrokes)
            {
                sb.Append(String.Format(format, entry.Value, entry.Key));
            }

            sb.Append("]");
            htmlTemplateBody.Keystrokes = sb.ToString();
        }
        else
        {
            ucBBEditor.IsLiveSite        = this.IsLiveSite;
            ucBBEditor.ShowImage         = fi.ForumEnableImage;
            ucBBEditor.ShowQuote         = fi.ForumEnableQuote;
            ucBBEditor.ShowURL           = fi.ForumEnableURL;
            ucBBEditor.ShowBold          = fi.ForumEnableFontBold;
            ucBBEditor.ShowItalic        = fi.ForumEnableFontItalics;
            ucBBEditor.ShowUnderline     = fi.ForumEnableFontUnderline;
            ucBBEditor.ShowStrike        = fi.ForumEnableFontStrike;
            ucBBEditor.ShowColor         = fi.ForumEnableFontColor;
            ucBBEditor.ShowCode          = fi.ForumEnableCodeSnippet;
            ucBBEditor.ShowAdvancedImage = fi.ForumEnableAdvancedImage;
            ucBBEditor.ShowAdvancedURL   = fi.ForumEnableAdvancedURL;
            htmlTemplateBody.Visible     = false;
        }

        if ((fi.ForumModerated) && (!ForumContext.UserIsModerator(fi.ForumID, this.CommunityGroupID)))
        {
            pnlInfo.Visible = true;
            lblInfo.Text    = GetString("forums.requiremoderation");
        }

        if ((CMSContext.CurrentUser.IsPublic()) || (!CheckPermission("AttachFiles", fi.AllowAttachFiles, fi.ForumGroupID, fi.ForumID)) || (fi.ForumModerated && !ForumContext.UserIsModerator(fi.ForumID, this.CommunityGroupID)))
        {
            plcAttachFile.Visible = false;
        }

        // If user can choose thread type and this is not reply, show the options
        if ((fi.ForumType == 0) && (ForumContext.CurrentReplyThread == null))
        {
            // Only thread can be set
            if ((ForumContext.CurrentState != ForumStateEnum.EditPost) || (ForumContext.CurrentPost.PostLevel == 0))
            {
                plcThreadType.Visible = true;
            }
        }

        // Hide or display subscription checkbox with dependence
        // on allow subscription property value and security
        if ((!AllowSubscription) || (!CheckPermission("Subscribe", fi.AllowSubscribe, fi.ForumGroupID, fi.ForumID)))
        {
            SubscribeHolder.Visible = false;
        }

        // Display signature if is allowed
        if (!AllowSignature)
        {
            plcSignature.Visible = false;
        }

        bool newThread = (ForumContext.CurrentReplyThread == null);

        // Display username textbox if is change name allowed or label with user name
        if (fi.ForumAllowChangeName || CMSContext.CurrentUser.IsPublic() || ((ForumContext.CurrentForum != null) && (ForumContext.UserIsModerator(ForumContext.CurrentForum.ForumID, ForumContext.CommunityGroupID))))
        {
            if (!RequestHelper.IsPostBack())
            {
                // Do not show 'public' for unauthenticated user
                if (!CMSContext.CurrentUser.IsPublic())
                {
                    txtUserName.Text = UserName;
                }
            }
            plcNickName.Visible = false;
        }
        else
        {
            if (ForumContext.CurrentMode != ForumMode.Edit)
            {
                lblNickNameValue.Text = HTMLHelper.HTMLEncode(UserName);
            }
            else
            {
                lblNickNameValue.Text = HTMLHelper.HTMLEncode(ForumContext.CurrentPost.PostUserName);
            }
            plcUserName.Visible = false;
        }

        // Prefill user email and reset the security code
        if (!RequestHelper.IsPostBack())
        {
            txtEmail.Text = CMSContext.CurrentUser.Email;
            SecurityCode1.GenerateNew();
        }

        if (ForumContext.CurrentReplyThread != null)
        {
            string replyPrefix = GetString("forums.replyprefix");
            if (!ForumContext.CurrentReplyThread.PostSubject.StartsWith(replyPrefix))
            {
                txtSubject.Text = replyPrefix + ForumContext.CurrentReplyThread.PostSubject;
                txtSubject.Text = TextHelper.LimitLength(txtSubject.Text, POST_SUBJECT_LENGTH, "");
            }
            else
            {
                txtSubject.Text = ForumContext.CurrentReplyThread.PostSubject;
            }
            txtSubject.Text = txtSubject.Text;


            // New post - check max level for subscribcribtion
            if (ForumContext.CurrentReplyThread.PostLevel >= ForumPostInfoProvider.MaxPostLevel - 1)
            {
                SubscribeHolder.Visible = false;
            }
        }
        // Edit post - check max level for subscribcribtion
        else if ((ForumContext.CurrentPost != null) && (ForumContext.CurrentPost.PostLevel >= ForumPostInfoProvider.MaxPostLevel))
        {
            SubscribeHolder.Visible = false;
        }


        // Hide subscription if not enabled
        if (!this.EnableSubscription)
        {
            SubscribeHolder.Visible = false;
        }

        #endregion

        #region "Post Data"

        if (!RequestHelper.IsPostBack())
        {
            // Check whether current state is edit
            if (ForumContext.CurrentState == ForumStateEnum.EditPost)
            {
                txtEmail.Text     = ForumContext.CurrentPost.PostUserMail;
                txtSignature.Text = ForumContext.CurrentPost.PostUserSignature;
                txtSubject.Text   = ForumContext.CurrentPost.PostSubject;
                txtUserName.Text  = ForumContext.CurrentPost.PostUserName;

                SetPostText(ForumContext.CurrentPost.PostText);


                radTypeDiscussion.Checked = true;

                if (ForumContext.CurrentPost.PostType == 1)
                {
                    radTypeQuestion.Checked = true;
                }
            }
            else if ((ForumContext.CurrentMode == ForumMode.Quote) && (ForumContext.CurrentReplyThread != null))
            {
                // Indicates whether wysiwyg editor is used
                bool isHtml = ForumContext.CurrentForum.ForumHTMLEditor;
                // Keeps post user name
                string userName = ForumContext.CurrentReplyThread.PostUserName;

                // Encode username for wysiwyg editor
                if (isHtml)
                {
                    userName = HTMLHelper.HTMLEncode(userName);
                }

                SetPostText(DiscussionMacroHelper.GetQuote(userName, ForumContext.CurrentReplyThread.PostText));

                // Set new line after
                if (isHtml)
                {
                    htmlTemplateBody.ResolvedValue += "<br /><br />";
                }
                else
                {
                    ucBBEditor.Text += "\n";
                }
            }
        }

        #endregion
    }