Пример #1
0
        public override void DataBind()
        {
            dataSrc = this.Parent as CommentsDataSource;

            if (!_bound)
            {
                _bound = true;

                CommentsManager cMgr = new CommentsManager();

                Comments.Comments_Tables_View table = cMgr.GetTableDetails(TableName);
                if (table == null)
                {
                    CommentsSetup cs = new CommentsSetup();
                    if (MembersOnly)
                    {
                        cs.CreateCommentsTableWithMembers(TableName, RelateTo, 1);
                    }
                    else
                    {
                        cs.CreateCommentsTableNoMembers(TableName, RelateTo, 1);
                    }

                    table = cMgr.GetTableDetails(TableName);
                }

                //HtmlInputHidden RelationId = new HtmlInputHidden();
                //RelationId.ID = "RelationId";
                //RelationId.Value = "-2";
                //this.Controls.Add(RelationId);

                HtmlInputHidden ParentId = new HtmlInputHidden();
                ParentId.ID = "ParentId";
                if (this.dataSrc == null)
                {
                    ParentId.Value = "-1";
                }
                else
                {
                    ParentId.Value = dataSrc.ParentId.ToString();
                }
                this.Controls.Add(ParentId);


                if (!NoRelations)
                {
                    object relationValue = ControlUtils.GetBoundedDataField(this.NamingContainer, table.RelationField);

                    RelationId = (int)relationValue;
                }
                if (AjaxSubmit)
                {
                    AjaxResponse resp = new AjaxResponse();

                    if (this.IsPostBack)
                    {
                        resp.callBack = this.AjaxCallback;
                        this.Validate();
                        resp.valid = this.IsValid.Value;

                        if (resp.valid)
                        {
                            if (this.MembersOnly)
                            {
                                string text = WebContext.Request["CommentText"];
                                if (!String.IsNullOrWhiteSpace(text))
                                {
                                    int comment = cMgr.AddMemberComment(TableName,
                                                                        Int32.Parse(ParentId.Value), RelationId,
                                                                        WebContext.Request["CommentTitle"],
                                                                        text, WebContext.Profile.UserId, CommentType.Text);

                                    var dr = cMgr.GetTopComment(TableName, RelationId, Int32.Parse(ParentId.Value), "Order By CommentId Desc");

                                    MembersManager mMgr = new MembersManager();

                                    resp r = new resp();

                                    string mId = dr["MemberId"].ToString();

                                    if (!String.IsNullOrWhiteSpace(mId))
                                    {
                                        var member = mMgr.GetMember(Int32.Parse(mId));

                                        r.FirstName = member.FirstName;
                                        r.LastName  = member.LastName;
                                        r.Username  = member.UserName;
                                        r.Picture   = member.Picture;
                                    }

                                    r.CommentId   = dr["CommentId"].ToString();
                                    r.ParentId    = dr["ParentId"].ToString();
                                    r.RelationId  = dr["RelationId"].ToString();
                                    r.DateCreated = string.Format("{0:MMM dd, yyyy} at {0:h:mm:tt}", dr["DateCreated"]);
                                    r.Title       = dr["Title"].ToString();
                                    r.CommentText = dr["CommentText"].ToString();
                                    r.MemberId    = dr["MemberId"].ToString();
                                    r.TableName   = TableName;

                                    resp.data    = r;
                                    resp.message = "Text successfully added";
                                    resp.error   = null;
                                }
                                else
                                {
                                    resp.message = "Please write your text before submitting and try again.";
                                    resp.error   = "true";
                                }
                            }
                            else
                            {
                                NameValueCollection post = GetValues();
                                ContentManager      pMgr = new ContentManager();

                                CustomPage thisPage = this.Page as CustomPage;
                                if (thisPage == null)
                                {
                                    thisPage = new CustomPage();
                                }

                                if (String.IsNullOrEmpty(post["Name"]))
                                {
                                    Validated = false;
                                    ErrorContext.Add("validation-name", ContentManager.ErrorMsg("Name-Validation", thisPage.Language));
                                }
                                if (String.IsNullOrEmpty(post["Email"]) || !Validation.IsEmail(post["Email"]))
                                {
                                    Validated = false;
                                    ErrorContext.Add("validation-email", ContentManager.ErrorMsg("Email-Validation", thisPage.Language));
                                }
                                if (String.IsNullOrEmpty(post["Title"]))
                                {
                                    Validated = false;
                                    ErrorContext.Add("validation-comment", ContentManager.ErrorMsg("Comment-Validation", thisPage.Language));
                                }
                                if (String.IsNullOrEmpty(post["Comment"]))
                                {
                                    Validated = false;
                                    ErrorContext.Add("validation-comment", ContentManager.ErrorMsg("Comment-Validation", thisPage.Language));
                                }
                                if (Validated != null && Validated.Value)
                                {
                                    int commentId = -1;
                                    try
                                    {
                                        commentId = cMgr.AddNoMemberComment(TableName, -1,
                                                                            RelationId,
                                                                            post["Title"],
                                                                            post["Comment"], WebContext.Profile.UserGuid,
                                                                            post["Name"], post["Website"],
                                                                            post["Country"], WebContext.Request.ServerVariables["REMOTE_ADDR"],
                                                                            post["avatar"], CommentType.Text);
                                    }
                                    catch (Exception ex)
                                    {
                                        ErrorContext.Add(ex);
                                    }
                                    if (commentId > 0)
                                    {
                                        if (!String.IsNullOrEmpty(table.Email))
                                        {
                                            Config cfg  = new Config();
                                            Mail   mail = new Mail("Comments");
                                            mail.Subject = post["Name"] + " submitted a comment to " + table.TableName;

                                            NameValueCollection dic = new NameValueCollection();
                                            dic["Name"]    = post["Name"];
                                            dic["Email"]   = post["Email"];
                                            dic["Comment"] = StringUtils.PutBR(post["Comment"].ToString());

                                            if (!String.IsNullOrWhiteSpace(post["Website"]))
                                            {
                                                dic["Website"] = post["Website"];
                                            }

                                            ArrayList files = cMgr.AddFiles(TableName, commentId);

                                            StringBuilder sb = new StringBuilder();

                                            if (files.Count > 0)
                                            {
                                                sb.Append("<dl><dt>Files:</dt><dd>");
                                                foreach (string s in files)
                                                {
                                                    string temp = s;
                                                    if (temp.IndexOf("~") == 0)
                                                    {
                                                        temp = temp.Substring(1);
                                                    }

                                                    sb.Append(String.Format("<a href=\"http://{0}/{1}\">{2}</a>",
                                                                            WebTools.WebContext.ServerName,
                                                                            s.Replace("\\", "/").Substring(1),
                                                                            Path.GetFileName(temp)));
                                                    sb.Append("<br />");
                                                }
                                                sb.Append("</dd></dl>");
                                            }

                                            dic["Files"] = sb.ToString();
                                            sb.Clear();

                                            sb.Append(string.Format(@"<p>
	<a href=""http://{0}/Prv/proxies/comments/status.ashx?commentId={1}&table={2}&action=approve"">Approve</a>
	- <a href=""http://{0}/Prv/proxies/comments/status.ashx?commentId={1}&table={2}&action=edit"">Edit</a>
	- <a href=""http://{0}/Prv/proxies/comments/status.ashx?CommentId={1}&table={2}&action=delete"">Delete</a>
</p>", WebTools.WebContext.ServerName, commentId, table.TableName));

                                            dic["Permission"] = sb.ToString();

                                            mail.Data = dic;
                                            mail.To   = table.Email;

                                            /*
                                             * mail.From = cfg.GetKey("EmailsFrom");
                                             *
                                             * StringBuilder sb = new StringBuilder();
                                             *
                                             * sb.Append("<dl>");
                                             * sb.Append(String.Format("<dt>Name: </dt><dd>{0}</dd>", post["Name"]));
                                             * sb.Append(String.Format("<dt>Email: </dt><dd>{0}</dd>", post["Email"]));
                                             *
                                             * if(!String.IsNullOrWhiteSpace(post["Website"]))
                                             *      sb.Append(String.Format("<dt>Website: </dt><dd>{0}</dd>", post["Website"]));
                                             *
                                             *
                                             * ArrayList files = cMgr.AddFiles(TableName, commentId);
                                             *
                                             * if (files.Count > 0)
                                             * {
                                             *      sb.Append("<dt>Files:</dt><dd>");
                                             *      foreach (string s in files)
                                             *      {
                                             *              String.Format("<a href=\"{0}/{1}\">{2}</a>",
                                             *                      WebTools.WebContext.ServerName,
                                             *                      s.Replace("\\", "/"),
                                             *                      Path.GetFileName(s));
                                             *      }
                                             *      sb.Append("</dd>");
                                             * }
                                             *
                                             * sb.Append(String.Format("<dt>Comment: </dt><dd>{0}</dd>", StringUtils.PutBR(post["Comment"])));
                                             * sb.Append("</dl>");
                                             *
                                             * sb.Append(string.Format(@"<p>
                                             * <a href=""http://{0}/Prv/proxies/comments/status.ashx?commentId={1}&table={2}&action=approve"">Approve</a>
                                             * - <a href=""http://{0}/Prv/proxies/comments/status.ashx?commentId={1}&table={2}&action=edit"">Edit</a>
                                             * - <a href=""http://{0}/Prv/proxies/comments/status.ashx?CommentId={1}&table={2}&action=delete"">Delete</a>
                                             * </p>", WebTools.WebContext.ServerName, commentId, table.TableName));
                                             *
                                             * mail.Body = sb.ToString();
                                             * mail.Subject = post["Name"] + " submitted a comment to " + table.TableName;
                                             */
                                            mail.Send();
                                        }
                                        SuccessContext.Add("comment-submitted", ContentManager.Message("comment-submitted"));
                                        this.Visible = false;
                                    }
                                }
                            }
                        }
                        resp.WriteJson(true);
                    }
                }
                else
                {
                    if (this.MembersOnly)
                    {
                        string text = WebContext.Request["CommentText"];
                        if (!String.IsNullOrWhiteSpace(text))
                        {
                            cMgr.AddMemberComment(TableName,
                                                  Int32.Parse(ParentId.Value), RelationId,
                                                  WebContext.Request["CommentTitle"],
                                                  text, WebContext.Profile.UserId, CommentType.Text);

                            WebContext.Response.Redirect(WebContext.Request.RawUrl);
                        }
                    }
                    else
                    {
                        NameValueCollection post = GetValues();
                        ContentManager      pMgr = new ContentManager();

                        CustomPage thisPage = this.Page as CustomPage;
                        if (thisPage == null)
                        {
                            thisPage = new CustomPage();
                        }

                        if (String.IsNullOrEmpty(post["Name"]))
                        {
                            Validated = false;
                            ErrorContext.Add("validation-name", ContentManager.ErrorMsg("Name-Validation", thisPage.Language));
                        }
                        if (String.IsNullOrEmpty(post["Email"]) || !Validation.IsEmail(post["Email"]))
                        {
                            Validated = false;
                            ErrorContext.Add("validation-email", ContentManager.ErrorMsg("Email-Validation", thisPage.Language));
                        }
                        if (String.IsNullOrEmpty(post["Title"]))
                        {
                            Validated = false;
                            ErrorContext.Add("validation-comment", ContentManager.ErrorMsg("Comment-Validation", thisPage.Language));
                        }
                        if (String.IsNullOrEmpty(post["Comment"]))
                        {
                            Validated = false;
                            ErrorContext.Add("validation-comment", ContentManager.ErrorMsg("Comment-Validation", thisPage.Language));
                        }
                        if (Validated != null && Validated.Value)
                        {
                            int commentId = -1;
                            try
                            {
                                commentId = cMgr.AddNoMemberComment(TableName, -1,
                                                                    RelationId,
                                                                    post["Title"],
                                                                    post["Comment"], WebContext.Profile.UserGuid,
                                                                    post["Name"], post["Website"],
                                                                    post["Country"], WebContext.Request.ServerVariables["REMOTE_ADDR"],
                                                                    post["avatar"], CommentType.Text);
                            }
                            catch (Exception ex)
                            {
                                ErrorContext.Add(ex);
                            }
                            if (commentId > 0)
                            {
                                if (!String.IsNullOrEmpty(table.Email))
                                {
                                    Config cfg  = new Config();
                                    Mail   mail = new Mail("Comments");
                                    mail.Subject = post["Name"] + " submitted a comment to " + table.TableName;

                                    NameValueCollection dic = new NameValueCollection();
                                    dic["Name"]    = post["Name"];
                                    dic["Email"]   = post["Email"];
                                    dic["Comment"] = StringUtils.PutBR(post["Comment"].ToString());

                                    if (!String.IsNullOrWhiteSpace(post["Website"]))
                                    {
                                        dic["Website"] = post["Website"];
                                    }

                                    ArrayList files = cMgr.AddFiles(TableName, commentId);

                                    StringBuilder sb = new StringBuilder();

                                    if (files.Count > 0)
                                    {
                                        sb.Append("<dl><dt>Files:</dt><dd>");
                                        foreach (string s in files)
                                        {
                                            string temp = s;
                                            if (temp.IndexOf("~") == 0)
                                            {
                                                temp = temp.Substring(1);
                                            }

                                            sb.Append(String.Format("<a href=\"http://{0}/{1}\">{2}</a>",
                                                                    WebTools.WebContext.ServerName,
                                                                    s.Replace("\\", "/").Substring(1),
                                                                    Path.GetFileName(temp)));
                                            sb.Append("<br />");
                                        }
                                        sb.Append("</dd></dl>");
                                    }

                                    dic["Files"] = sb.ToString();
                                    sb.Clear();

                                    sb.Append(string.Format(@"<p>
								<a href=""http://{0}/Prv/proxies/comments/status.ashx?commentId={1}&table={2}&action=approve"">Approve</a>
								- <a href=""http://{0}/Prv/proxies/comments/status.ashx?commentId={1}&table={2}&action=edit"">Edit</a>
								- <a href=""http://{0}/Prv/proxies/comments/status.ashx?CommentId={1}&table={2}&action=delete"">Delete</a>
							</p>"                            , WebTools.WebContext.ServerName, commentId, table.TableName));

                                    dic["Permission"] = sb.ToString();

                                    mail.Data = dic;
                                    mail.To   = table.Email;

                                    /*
                                     * mail.From = cfg.GetKey("EmailsFrom");
                                     *
                                     * StringBuilder sb = new StringBuilder();
                                     *
                                     * sb.Append("<dl>");
                                     * sb.Append(String.Format("<dt>Name: </dt><dd>{0}</dd>", post["Name"]));
                                     * sb.Append(String.Format("<dt>Email: </dt><dd>{0}</dd>", post["Email"]));
                                     *
                                     * if(!String.IsNullOrWhiteSpace(post["Website"]))
                                     *      sb.Append(String.Format("<dt>Website: </dt><dd>{0}</dd>", post["Website"]));
                                     *
                                     *
                                     * ArrayList files = cMgr.AddFiles(TableName, commentId);
                                     *
                                     * if (files.Count > 0)
                                     * {
                                     *      sb.Append("<dt>Files:</dt><dd>");
                                     *      foreach (string s in files)
                                     *      {
                                     *              String.Format("<a href=\"{0}/{1}\">{2}</a>",
                                     *                      WebTools.WebContext.ServerName,
                                     *                      s.Replace("\\", "/"),
                                     *                      Path.GetFileName(s));
                                     *      }
                                     *      sb.Append("</dd>");
                                     * }
                                     *
                                     * sb.Append(String.Format("<dt>Comment: </dt><dd>{0}</dd>", StringUtils.PutBR(post["Comment"])));
                                     * sb.Append("</dl>");
                                     *
                                     * sb.Append(string.Format(@"<p>
                                     * <a href=""http://{0}/Prv/proxies/comments/status.ashx?commentId={1}&table={2}&action=approve"">Approve</a>
                                     * - <a href=""http://{0}/Prv/proxies/comments/status.ashx?commentId={1}&table={2}&action=edit"">Edit</a>
                                     * - <a href=""http://{0}/Prv/proxies/comments/status.ashx?CommentId={1}&table={2}&action=delete"">Delete</a>
                                     * </p>", WebTools.WebContext.ServerName, commentId, table.TableName));
                                     *
                                     * mail.Body = sb.ToString();
                                     * mail.Subject = post["Name"] + " submitted a comment to " + table.TableName;
                                     */
                                    mail.Send();
                                }
                                SuccessContext.Add("comment-submitted", ContentManager.Message("comment-submitted"));
                                this.Visible = false;
                            }
                        }
                    }
                }
            }
            base.DataBind();
        }