예제 #1
0
        protected void ApproveFormBtn_Click(object sender, EventArgs e)
        {
            FormResult.Visible = false;
            //updating a form not, creating it
            if (Request.QueryString["pfid"] != null)
            {
                int           formId = int.Parse(Request.QueryString["pfid"]);
                Form          f      = FormUtil.GetForm(formId);
                Project       p      = ProjectUtil.GetProject(f.ProjectId);
                WorkflowModel w      = WorkflowUtil.GetWorkflow(p.WorkflowId);

                User user = (User)Session["User"];
                FormUtil.ApproveForm(formId, user.RoleId);
                Log.Info(user.Identity + " approved " + CompanyUtil.GetCompanyName(p.CompanyId) + "'s form " + f.FormName + " - " + p.Name);
                FormResult.CssClass = "success";
                FormResult.Text     = "Approved form " + f.FormName;
                FormResult.Visible  = true;

                //prep html for pdf generation
                HtmlDocument doc     = new HtmlDocument();
                string       pdfName = string.Format("{0} - {1} - {2}", w.WorkflowName, f.FormName, CompanyUtil.GetCompanyName(p.CompanyId));
                string       html    = formViewerData.Value;
                if (html.Contains("user-data"))
                {
                    html = html.Replace("user-data", "value");
                }
                if (html.Contains("\""))
                {
                    html = html.Replace("\"", "'");
                }
                doc.LoadHtml(html);
                doc.Save("PDFGen/" + CompanyUtil.GetCompanyName(p.CompanyId) + "_" + f.FormName + "_" + p.Name + ".html");

                //radiobtns
                foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//input[@type]"))
                {
                    HtmlAttribute type = link.Attributes["type"];
                    if (type.Value.Equals("radio"))
                    {
                        if (link.Attributes.Contains("checked"))
                        {
                        }
                        else
                        {
                            if (link.Attributes.Contains("id"))
                            {
                                string toDelId = link.Attributes["id"].Value;

                                foreach (HtmlNode label in doc.DocumentNode.SelectNodes("//label[@for]"))
                                {
                                    string forId = label.Attributes["for"].Value;
                                    if (forId.Equals(toDelId))
                                    {
                                        label.Remove();
                                    }
                                }
                            }
                        }
                        link.Attributes.Remove("value");
                    }
                }

                //text fields, dates, + similar
                foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//input[@value]"))
                {
                    HtmlAttribute value = link.Attributes["value"];
                    if (link.Attributes.Contains("placeholder"))
                    {
                        link.Attributes.Remove("placeholder");
                    }
                    string val = value.Value;
                    link.InnerHtml = val;
                    link.Attributes.Remove("value");
                }

                //text areas
                foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//textarea[@value]"))
                {
                    HtmlAttribute value = link.Attributes["value"];
                    if (link.Attributes.Contains("placeholder"))
                    {
                        link.Attributes.Remove("placeholder");
                    }
                    string val = value.Value;
                    link.InnerHtml = val;
                    link.Attributes.Remove("value");
                }

                //attached files
                if (f.FilePath.Length > 0)
                {
                    foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//input[@type]"))
                    {
                        HtmlAttribute type = link.Attributes["type"];
                        if (type.Value.Equals("file"))
                        {
                            string fileType = f.FilePath.Split('.')[1];
                            string fileName = string.Format("{0} {1} Attachment.{2}", CompanyUtil.GetCompanyName(p.CompanyId), f.FormName, fileType);
                            link.InnerHtml = "See " + fileName;
                        }
                    }
                }
                doc.Save("PDFGen/" + CompanyUtil.GetCompanyName(p.CompanyId) + "_" + f.FormName + "_" + p.Name + ".html");
                doc.Load("PDFGen/" + CompanyUtil.GetCompanyName(p.CompanyId) + "_" + f.FormName + "_" + p.Name + ".html");
                html = doc.Text;

                //pdf gen
                PDFGen.CreateHTMLPDF(html, pdfName);
                Response.Redirect("Forms.aspx?pfid=" + formId);
            }
        }