protected string GetSubjectInfo(PrivateMessage pm)
 {
     string result = string.Empty;
     string subjectInfo = Server.HtmlEncode(pm.Subject);
     result = string.Format("<a href=\"{0}ViewPM.aspx?PM={1}\">{2}</a>", CommonHelper.GetStoreLocation(), pm.PrivateMessageID, subjectInfo);
     return result;
 }
        protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                string subject = txtSubject.Text.Trim();
                string message = txtMessageBBCode.Text.Trim();

                var customer = this.CustomerService.GetCustomerById(this.CustomerId);
                if (customer != null)
                {
                    var pm = new PrivateMessage()
                    {
                        FromUserId = NopContext.Current.User.CustomerId,
                        ToUserId = customer.CustomerId,
                        Subject = subject,
                        Text = message,
                        CreatedOn = DateTime.UtcNow
                    };
                    this.ForumService.InsertPrivateMessage(pm);
                }
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }
        protected string GetSubjectInfo(PrivateMessage pm)
        {
            string result = string.Empty;
            string subjectInfo = string.Empty;
            if (pm.IsRead)
            {
                subjectInfo = Server.HtmlEncode(pm.Subject);
            }
            else
            {
                subjectInfo = string.Format("<b>{0}</b>", Server.HtmlEncode(pm.Subject));
            }

            result = string.Format("<a href=\"{0}viewpm.aspx?pm={1}\">{2}</a>", CommonHelper.GetStoreLocation(), pm.PrivateMessageId, subjectInfo);
            return result;
        }
        /// <summary>
        /// Replaces a message template tokens
        /// </summary>
        /// <param name="privateMessage">Private message</param>
        /// <param name="template">Template</param>
        /// <returns>New template</returns>
        private string ReplaceMessageTemplateTokens(PrivateMessage privateMessage,
            string template)
        {
            var tokens = new NameValueCollection();
            tokens.Add("Store.Name", IoC.Resolve<ISettingManager>().StoreName);
            tokens.Add("Store.URL", IoC.Resolve<ISettingManager>().StoreUrl);
            tokens.Add("Store.Email", this.DefaultEmailAccount.Email);

            tokens.Add("PrivateMessage.Subject", HttpUtility.HtmlEncode(privateMessage.Subject));
            tokens.Add("PrivateMessage.Text", privateMessage.FormatPrivateMessageText());

            foreach (string token in tokens.Keys)
            {
                template = Replace(template, String.Format(@"%{0}%", token), tokens[token]);
            }

            return template;
        }
        /// <summary>
        /// Sends a private message notification
        /// </summary>
        /// <param name="privateMessage">Private message</param>
        /// <param name="languageId">Message language identifier</param>
        /// <returns>Queued email identifier</returns>
        public int SendPrivateMessageNotification(PrivateMessage privateMessage, int languageId)
        {
            if (privateMessage == null)
                throw new ArgumentNullException("privateMessage");

            string templateName = "Customer.NewPM";
            var localizedMessageTemplate = this.GetLocalizedMessageTemplate(templateName, languageId);
            if (localizedMessageTemplate == null || !localizedMessageTemplate.IsActive)
                return 0;

            var emailAccount = localizedMessageTemplate.EmailAccount;

            string subject = ReplaceMessageTemplateTokens(privateMessage, localizedMessageTemplate.Subject);
            string body = ReplaceMessageTemplateTokens(privateMessage, localizedMessageTemplate.Body);
            string bcc = localizedMessageTemplate.BccEmailAddresses;
            var from = new MailAddress(emailAccount.Email, emailAccount.DisplayName);

            var recipient = privateMessage.ToUser;
            if (recipient == null)
                return 0;

            var to = new MailAddress(recipient.Email, recipient.FullName);
            var queuedEmail = InsertQueuedEmail(5, from, to, string.Empty, bcc, subject, body,
                DateTime.UtcNow, 0, null, emailAccount.EmailAccountId);
            return queuedEmail.QueuedEmailId;
        }
        private static PrivateMessage DBMapping(DBPrivateMessage dbItem)
        {
            if (dbItem == null)
                return null;

            PrivateMessage item = new PrivateMessage();
            item.PrivateMessageID = dbItem.PrivateMessageID;
            item.FromUserID = dbItem.FromUserID;
            item.ToUserID = dbItem.ToUserID;
            item.Subject = dbItem.Subject;
            item.Text = dbItem.Text;
            item.IsRead = dbItem.IsRead;
            item.IsDeletedByAuthor = dbItem.IsDeletedByAuthor;
            item.IsDeletedByRecipient = dbItem.IsDeletedByRecipient;
            item.CreatedOn = dbItem.CreatedOn;

            return item;
        }
        protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                string subject = txtSubject.Text.Trim();
                string message = txtMessageBBCode.Text.Trim();

                if (String.IsNullOrEmpty(subject))
                    throw new NopException(GetLocaleResourceString("PrivateMessages.SubjectCannotBeEmpty"));

                if (String.IsNullOrEmpty(message))
                    throw new NopException(GetLocaleResourceString("PrivateMessages.MessageCannotBeEmpty"));

                Customer toCustomer = null;
                var replyToPM = this.ForumService.GetPrivateMessageById(this.ReplyToMessageId);
                if (replyToPM != null)
                {
                    if (replyToPM.ToUserId == NopContext.Current.User.CustomerId || replyToPM.FromUserId == NopContext.Current.User.CustomerId)
                    {
                        toCustomer = replyToPM.FromUser;
                    }
                    else
                    {
                        Response.Redirect(CommonHelper.GetStoreLocation() + "privatemessages.aspx");
                    }
                }
                else
                {
                    toCustomer = this.CustomerService.GetCustomerById(this.ToCustomerId);
                }

                if (toCustomer == null || toCustomer.IsGuest)
                {
                    Response.Redirect(CommonHelper.GetStoreLocation() + "privatemessages.aspx");
                }

                var pm = new PrivateMessage()
                {
                    FromUserId = NopContext.Current.User.CustomerId,
                    ToUserId = toCustomer.CustomerId,
                    Subject = subject,
                    Text = message,
                    CreatedOn = DateTime.UtcNow
                };
                this.ForumService.InsertPrivateMessage(pm);

                Response.Redirect(CommonHelper.GetStoreLocation() + "privatemessages.aspx?tab=sent");
            }
            catch (Exception exc)
            {
                pnlError.Visible = true;
                lErrorMessage.Text = Server.HtmlEncode(exc.Message);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Updates the private message
        /// </summary>
        /// <param name="privateMessage">Private message</param>
        public void UpdatePrivateMessage(PrivateMessage privateMessage)
        {
            if (privateMessage == null)
                throw new ArgumentNullException("privateMessage");

            privateMessage.Subject = CommonHelper.EnsureNotNull(privateMessage.Subject);
            privateMessage.Subject = privateMessage.Subject.Trim();
            if (String.IsNullOrEmpty(privateMessage.Subject))
                throw new NopException("Subject cannot be empty");
            if (this.PMSubjectMaxLength > 0)
            {
                if (privateMessage.Subject.Length > this.PMSubjectMaxLength)
                    privateMessage.Subject = privateMessage.Subject.Substring(0, this.PMSubjectMaxLength);
            }

            privateMessage.Text = CommonHelper.EnsureNotNull(privateMessage.Text);
            privateMessage.Text = privateMessage.Text.Trim();
            if (String.IsNullOrEmpty(privateMessage.Text))
                throw new NopException("Text cannot be empty");
            if (this.PMTextMaxLength > 0)
            {
                if (privateMessage.Text.Length > this.PMTextMaxLength)
                    privateMessage.Text = privateMessage.Text.Substring(0, this.PMTextMaxLength);
            }

            privateMessage.Subject = CommonHelper.EnsureMaximumLength(privateMessage.Subject, 450);

            if (privateMessage.IsDeletedByAuthor && privateMessage.IsDeletedByRecipient)
            {
                DeletePrivateMessage(privateMessage.PrivateMessageId);
            }
            else
            {

                if (!_context.IsAttached(privateMessage))
                    _context.PrivateMessages.Attach(privateMessage);

                _context.SaveChanges();
            }
        }
Beispiel #9
0
        /// <summary>
        /// Inserts a private message
        /// </summary>
        /// <param name="privateMessage">Private message</param>
        public void InsertPrivateMessage(PrivateMessage privateMessage)
        {
            if (privateMessage == null)
                throw new ArgumentNullException("privateMessage");

            privateMessage.Subject = CommonHelper.EnsureNotNull(privateMessage.Subject);
            privateMessage.Subject = privateMessage.Subject.Trim();
            if (String.IsNullOrEmpty(privateMessage.Subject))
                throw new NopException("Subject cannot be empty");
            if (this.PMSubjectMaxLength > 0)
            {
                if (privateMessage.Subject.Length > this.PMSubjectMaxLength)
                    privateMessage.Subject = privateMessage.Subject.Substring(0, this.PMSubjectMaxLength);
            }

            privateMessage.Text = CommonHelper.EnsureNotNull(privateMessage.Text);
            privateMessage.Text = privateMessage.Text.Trim();
            if (String.IsNullOrEmpty(privateMessage.Text))
                throw new NopException("Text cannot be empty");

            privateMessage.Subject = CommonHelper.EnsureMaximumLength(privateMessage.Subject, 450);

            if (this.PMTextMaxLength > 0)
            {
                if (privateMessage.Text.Length > this.PMTextMaxLength)
                    privateMessage.Text = privateMessage.Text.Substring(0, this.PMTextMaxLength);
            }

            Customer customerTo = IoC.Resolve<ICustomerService>().GetCustomerById(privateMessage.ToUserId);
            if (customerTo == null)
                throw new NopException("Recipient could not be loaded");

            _context.PrivateMessages.AddObject(privateMessage);
            _context.SaveChanges();

            //UI notification
            customerTo.NotifiedAboutNewPrivateMessages = false;
            //Email notification
            if (this.NotifyAboutPrivateMessages)
            {
                IoC.Resolve<IMessageService>().SendPrivateMessageNotification(privateMessage, NopContext.Current.WorkingLanguage.LanguageId);
            }
        }