Ejemplo n.º 1
0
    protected void forwardSend_Click(object sender, EventArgs e)
    {
        SortedList noPrivilegeList = new SortedList();
        SortedList noEmailAddressList = new SortedList();
        SortedList wrongEmailAddressList = new SortedList();
        ArrayList allExternalUsers = new ArrayList();

        ISubjectFetchProvider subjectProvider = (new SubjectProviderFactory()).GetSubjectFetchProvider();
        ArrayList userList = new ArrayList();
        IList users = new ArrayList();
        const string MatchEmailPattern =
            @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
           + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
           + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
           + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

        try
        {
            StringBuilder mailtoUsers = new StringBuilder();
            MailToUser[] jsonObjectArray = WebUtility.DeserializeAjaxResult("[" + hideMailto.Value + "]", typeof(MailToUser[])) as MailToUser[];
            String[] externalUsers = this.txtMailto.Value.Trim().Split(';'); //外部收件者
            foreach (string emailAddress in externalUsers)
            {
                if (externalUsers.Length==0)
                {
                    break;
                }
                string address = emailAddress.Trim();

                if (address.Length == 0)
                {
                    noEmailAddressList.Add(address, address);
                }
                else
                {
                    if (Regex.IsMatch(address, MatchEmailPattern))
                    {
                        if (allExternalUsers.BinarySearch(address) < 0)
                        {
                            allExternalUsers.Add(address);
                        }
                    }
                    else
                    {
                        wrongEmailAddressList.Add(address, address);
                    }
                }
            }

            foreach (MailToUser mailtoUser in jsonObjectArray)
            {
                users.Clear();
                switch (Convert.ToInt32(mailtoUser.SubjectType))
                {
                    case 0:
                        if (!string.IsNullOrEmpty(mailtoUser.EmailAddress))
                        {
                            if (Regex.IsMatch(mailtoUser.EmailAddress, MatchEmailPattern))
                            {
                                userList.Add(mailtoUser);
                            }
                            else
                            {
                                wrongEmailAddressList.Add(mailtoUser.SubjectId, mailtoUser.DisplayName);
                            }
                        }
                        else
                        {
                            noEmailAddressList.Add(mailtoUser.SubjectId, mailtoUser.DisplayName);
                        }
                        break;
                    case 1:
                        users = subjectProvider.GetUsersInOU(mailtoUser.SubjectId);
                        break;
                    case 2:
                        users = subjectProvider.GetUsersInGroup(mailtoUser.SubjectId);
                        break;
                    case 3:
                        users = subjectProvider.GetUsersInRole(mailtoUser.SubjectId);
                        break;
                    default:
                        break;
                }

                if (users != null && users.Count > 0)
                {
                    foreach (User user in users)
                    {
                        if (!string.IsNullOrEmpty(user.EmailAddress))
                        {
                            if (Regex.IsMatch(user.EmailAddress, MatchEmailPattern))
                            {
                                userList.Add(ConvertUserToMailToUser(user));
                            }
                            else
                            {
                                wrongEmailAddressList.Add(user.SubjectId, user.DisplayName);
                            }
                        }
                        else
                        {
                            noEmailAddressList.Add(mailtoUser.SubjectId, mailtoUser.DisplayName);
                        }
                    }
                }
            }
            ArrayList uniList = new ArrayList();
            if (userList.Count > 0)
            {
                int i;
                MailToUser preUser = (MailToUser)userList[0];
                uniList.Add(preUser);
                ComparerUtility comp = new ComparerUtility();
                comp.ObjectType = (typeof(MailToUser));
                comp.SortingOrder = (int)ComparerUtility.SortOrder.Ascending;
                comp.SortProperty = "SubjectId";
                MailToUser[] userAry = userList.ToArray(typeof(MailToUser)) as MailToUser[];
                Array.Sort(userAry, comp);

                //移除重複的,因為上面有先做過排序
                for (i = 1; i < userAry.Length; i++)
                {
                    if (preUser.SubjectId != ((MailToUser)userAry[i]).SubjectId)
                    {
                        preUser = (MailToUser)userAry[i];
                        uniList.Add(preUser);
                    }
                }
            }
            foreach (MailToUser mailtoUser in uniList)
            {
                if (!allowSendToNoPrivilegeUser)
                {
                    KBUserDetailInfo checkkbuser;
                    checkkbuser = kbuserService.GetKBUserDetailBySubjectId(mailtoUser.SubjectId);

                    if (PrivilegeUtility.HasPrivilege(PrivilegeType.DocumentPrivilege, docId, checkkbuser, DocumentOperationAttributes.DOC_READ_DOCUMENT))
                    {
                        mailtoUsers.Append(mailtoUser.DisplayName).Append("<").Append(mailtoUser.EmailAddress).Append(">;");
                    }
                    else
                    {
                        noPrivilegeList.Add(mailtoUser.SubjectId, mailtoUser.DisplayName);
                    }
                }
                else
                {
                    mailtoUsers.Append(mailtoUser.DisplayName).Append("<").Append(mailtoUser.EmailAddress).Append(">;");
                }
            }
            string mailto = mailtoUsers.ToString();
            if (allowSendToNoPrivilegeUser)
            {
                if (allExternalUsers.Count>0)
                {
                    mailto += string.Join(";", ((string[])allExternalUsers.ToArray(typeof(string))));
                }
            }

            //如果最後還是有 email 要寄送的話
            if (mailto.Length > 0)
            {
                string subject = "[" + i18n.GetMessage("m858") + "] " + this.txtSubject.Value;
                string message = this.txtAreaBody.Value.Replace(" ", "&nbsp;").Replace("\r\n", "<br />");
                string mailCC = string.Empty;
                string mailBCC = string.Empty;

                if (sendToMe.Checked)
                {
                    if (!string.IsNullOrEmpty(currentUser.EmailAddress))
                    {
                        if (Regex.IsMatch(currentUser.EmailAddress, MatchEmailPattern))
                        {
                            mailCC = currentUser.DisplayName + "<" + currentUser.EmailAddress + ">";
                        }
                        else
                        {
                            wrongEmailAddressList.Add(currentUser.SubjectId, currentUser.DisplayName);
                        }
                    }
                    else
                    {
                        noEmailAddressList.Add(currentUser.SubjectId, currentUser.DisplayName);
                    }
                }
                //建立樣本引擎實體
                VelocityEngine velocity = new VelocityEngine();
                //設定引擎的一些屬性
                ExtendedProperties props = new ExtendedProperties();
                //設定樣版被載入的來源位置
                props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
                ArrayList resourceLocators = new ArrayList();
                resourceLocators.Add(".");
                resourceLocators.Add(System.Configuration.ConfigurationManager.AppSettings["TemplatePath"]);
                props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, resourceLocators);
                //設定讀寫樣版的 Encoding
                props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
                props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
                //將設定的屬性塞給引擎
                velocity.Init(props);

                Template detailTemplate = velocity.GetTemplate(System.Configuration.ConfigurationManager.AppSettings["DocumentDetailTemplateFileName"]);
                Template masterTemplate = velocity.GetTemplate(System.Configuration.ConfigurationManager.AppSettings["ForwardMasterTemplateFileName"]);
                VelocityContext context = new VelocityContext();
                StringWriter detailWriter = new StringWriter();
                StringWriter masterWriter = new StringWriter();
                string Attachs = string.Empty;
                ArrayList list = new ArrayList();
                string photoPath = System.Configuration.ConfigurationManager.AppSettings["photoPath"];
                KBUserInfo kbuser = GetKBUser(doc.VersionCreatorId);
                string personalPhotoFilenName = kbuser.AvatarPath;

                if (personalPhotoFilenName == null || personalPhotoFilenName.Trim().Length == 0)
                {
                    personalPhotoFilenName = "default.jpg";
                }
                string uppath = Server.MapPath(Request.ApplicationPath + photoPath + personalPhotoFilenName);
                context.Put("UserImage", kbuser.AvatarPath);
                Attachs = uppath;
                string folderPath = string.Empty;
                FolderInfo[] fiArray = folderService.GetFolderPath(currentUser, documentService.GetParentFolders(currentUser, doc.DocumentId)[0].FolderId);

                for (int i = 0; i < fiArray.Length; i++)
                {
                    if (!fiArray[i].DisplayName.Equals(GSS.Vitals.KnowledgeBase.SpecialFolder.ROOT_FOLDER))
                    {
                        if (fiArray[i].DisplayName.Equals(GSS.Vitals.KnowledgeBase.SpecialFolder.PUBLIC_FOLDER))
                        {
                            folderPath += i18n.GetMessage("m82");
                        }
                        else
                        {
                            folderPath += fiArray[i].DisplayName;
                        }
                        if (i < fiArray.Length - 1)
                        {
                            folderPath += " / ";
                        }
                    }
                }
                context.Put("Author", kbuser.DisplayName.Trim());
                context.Put("Modify", i18n.GetMessage("m152")); //修改於
                TimeSpan ts2 = WebUtility.GetCurrentUserUTCOffset(currentUser);
                DateTime dt2 = doc.VersionCreationDatetime.Add(ts2);

                context.Put("ModifyDateTime", dt2.ToString());
                context.Put("DocumentClass", i18n.GetMessage("m273"));
                context.Put("DocumentClassName", doc.DocumentClass.ClassName.Trim());

                context.Put("FolderPath", folderPath);
                context.Put("Link", System.Configuration.ConfigurationManager.AppSettings["lambdaSite"] + "readdocument.aspx?documentid=" + doc.DocumentId);
                context.Put("VersionTitle", doc.VersionTitle.Trim());
                //render 文件欄位

                for (int i = 0; i < doc.DocumentAttributes.Length; i++)
                {
                    if (doc.DocumentAttributes[i].AttributeType != GSS.Vitals.KnowledgeBase.AttributeType.TITLE)
                    {
                        if (doc.DocumentAttributes[i].Value != string.Empty)
                        {
                            GSS.Vitals.KnowledgeBase.UIField.FieldType ft = GSS.Vitals.KnowledgeBase.UIField.ParseUIFieldType(doc.DocumentAttributes[i].UIFieldInfo);

                            if (ft == GSS.Vitals.KnowledgeBase.UIField.FieldType.CALENDAR)
                            {
                                DateTime dt = KBUtility.ParseISO8601SimpleString(doc.DocumentAttributes[i].Value);
                                dt = dt.Add(WebUtility.GetCurrentUserUTCOffset(currentUser));
                                list.Add(new InfoItem(doc.DocumentAttributes[i].DisplayName, dt.ToString("yyyy/MM/dd")));
                            }
                            else if (ft == GSS.Vitals.KnowledgeBase.UIField.FieldType.DATETIME)
                            {
                                DateTime dt = KBUtility.ParseISO8601SimpleString(doc.DocumentAttributes[i].Value);
                                dt = dt.Add(WebUtility.GetCurrentUserUTCOffset(currentUser));
                                list.Add(new InfoItem(doc.DocumentAttributes[i].DisplayName, dt.ToString("yyyy/MM/dd HH:mm:ss")));
                            }
                            else if (ft == GSS.Vitals.KnowledgeBase.UIField.FieldType.RICHTEXT)
                            {
                                list.Add(new InfoItem(doc.DocumentAttributes[i].DisplayName, doc.DocumentAttributes[i].Value));
                            }
                            else
                            {
                                list.Add(new InfoItem(doc.DocumentAttributes[i].DisplayName, WebUtility.crlf2br(doc.DocumentAttributes[i].Value)));
                            }
                        }
                    }
                }
                context.Put("Items", list);
                detailTemplate.Merge(context, detailWriter);

                //將Detail加入到master當中
                context.Put("DocSections", detailWriter.GetStringBuilder().ToString());
                string header = String.Format(i18n.GetMessage("m862"), currentUser.DisplayName, "<a href='" + System.Configuration.ConfigurationManager.AppSettings["lambdaSite"] + "'>" +
                                        System.Configuration.ConfigurationManager.AppSettings["SystemName"] + "</a>");
                context.Put("Header", header);
                context.Put("Message", i18n.GetMessage("m863") + ":<br/><br/>" + message);
                masterTemplate.Merge(context, masterWriter);
                mailService.SendMail(mailService.FromMailAddress, mailto, mailBCC, mailCC, subject, masterWriter.GetStringBuilder().ToString(), Attachs);
                forwardService.SetForwardRecord((KBUserInfo)currentUser, mailto, mailCC, subject, masterWriter.GetStringBuilder().ToString(), message);
                this.txtMailto.Value = string.Empty;
                this.txtAreaBody.Value = string.Empty;
                this.sendToMe.Checked = false;
            }

            if (noEmailAddressList.Count > 0 || wrongEmailAddressList.Count > 0 || noPrivilegeList.Count > 0)
            {
                Table a = makeTable(noPrivilegeList, i18n.GetMessage("m478"));
                Table b = makeTable(noEmailAddressList, i18n.GetMessage("m926"));
                Table c = makeTable(wrongEmailAddressList, i18n.GetMessage("m927"));
                renderForwardResult(new Table[] { a, b, c });
            }
            else
            {
                forwardResultPanel.Visible = false;
            }
        }
        catch (Exception ex)
        {
            log4net.LogManager.GetLogger("MAIL").Error(ex.Message, ex);
            renderForwardResult(ex);
        }
    }
Ejemplo n.º 2
0
    private void drawTable(TagCloudElement[] elements)
    {
        if (elements != null && elements.Length > 0)
        {
            StringBuilder sb = new StringBuilder();

            ComparerUtility comparer = new ComparerUtility();
            comparer.ObjectType = typeof(TagCloudElement);
            comparer.SortingOrder = (int)ComparerUtility.SortOrder.Ascending;
            comparer.SortProperty = "TagName";
            Array.Sort(elements, comparer);

            foreach (TagCloudElement tce in elements)
            {
                sb.Append("<a title='" + tce.TagName + "' ");
                sb.Append("class='").Append(getFontSize(tce.Importance)).Append(" ");
                sb.Append(getFontColor(tce.Recency)).Append("' ");
                if (IsSafari)
                {
                    string s = tce.TagName.Replace(" ", "%20").Replace("\"", "\\\"");

                    sb.Append("href=javascript:document.location=\"searchbytag.aspx?tag=\"+lambdaEncode(\"");
                    sb.Append(s);
                    sb.Append("\");>");
                }
                else
                {
                    string s = tce.TagName.Replace(" ", "%20").Replace("\"", "\\\"");

                    sb.Append("href=javascript:document.location=\"searchbytag.aspx?tag=\"+lambdaEncode(\"");
                    sb.Append(s);
                    sb.Append("\");>");
                }
                if (tce.IsDedicated)
                {
                    sb.Append("*").Append(tce.TagName);
                }
                else
                {
                    sb.Append(tce.TagName);
                }
                sb.Append("</a> ");
            }
            div = sb.ToString();
        }
    }