Exemple #1
0
        /// <summary>
        /// Send expiry warning emails to users (Web Authors) of pages about to expire
        /// </summary>
        /// <param name="emailTo">
        /// who to send the email to
        /// </param>
        /// <param name="userPages">
        /// User details and list of expiring pages for this user
        /// </param>
        public void UserPageExpiryEmail(string emailTo, UserPagesModel userPages)
        {
            var siteUri = ConfigurationManager.AppSettings["SiteUri"];

            const string subject = "ACTION: Your website pages expire in under 14 days";
            var          body    = new StringBuilder();

            body.AppendLine("<p>Your website pages will expire within the next two weeks. After this they will no longer be available to the public. The dates for each page are given below.</p>");
            body.AppendLine("<p>Note, you should always use Google Chrome to update your pages. If your default web browser is Internet Explorer, you will need to right-click and copy and paste the links below into Chrome instead.</p>");
            body.AppendLine("<p>After you’ve logged in, click on each page below and:</p>");
            body.AppendLine("<ul>");
            body.AppendLine("<li>check they are up to date</li>");
            body.AppendLine("<li>check the information is still needed</li>");
            body.AppendLine("<li>go to Properties tab and use the calendar to set a new date in the 'Unpublish at' box</li>");
            body.AppendLine("<li>then click 'Save and publish'.</li>");
            body.AppendLine("</ul>");
            body.AppendLine("<p>For details on updating your pages, see <a href=\"" + ConfigurationManager.AppSettings["WebAuthorsGuidanceUrl"] + "\">Guidance for web authors</a>.</p>");

            var otherTitle       = "Expiring Pages:";
            var warningDate      = DateTime.Now.AddDays(2);
            var lastWarningPages = userPages.Pages.Where(d => d.ExpiryDate <= warningDate).ToList();

            if (lastWarningPages.Any())
            {
                body.AppendLine("<strong>Pages Expiring Tomorrow:</strong>");
                body.AppendLine("<ol>");
                foreach (var page in lastWarningPages)
                {
                    var linkUrl = string.Format("{0}#/content/content/edit/{1}", siteUri, page.PageId);
                    body.Append("<li>");
                    body.AppendFormat("<a href=\"{0}\">{1}</a> (expires {2}, {3})", linkUrl, page.PageName, page.ExpiryDate.ToLongDateString(), page.ExpiryDate.ToShortTimeString());
                    body.AppendFormat("<br/>{0}", page.PageUrl);
                    body.Append("</li>");
                }
                body.AppendLine("</ol>");

                otherTitle = "Other Pages:";
            }

            // Process remaining pages
            var nonWarningPages = userPages.Pages.Where(d => d.ExpiryDate > warningDate).ToList();

            if (nonWarningPages.Any())
            {
                body.AppendFormatLine("<strong>{0}</strong>", otherTitle);
                body.AppendLine("<ol>");
                foreach (var page in nonWarningPages)
                {
                    var linkUrl = string.Format("{0}#/content/content/edit/{1}", siteUri, page.PageId);
                    body.Append("<li>");
                    body.AppendFormat("<a href=\"{0}\">{1}</a> (expires {2}, {3})", linkUrl, page.PageName, page.ExpiryDate.ToLongDateString(), page.ExpiryDate.ToShortTimeString());
                    body.AppendFormat("<br/>{0}", page.PageUrl);
                    body.Append("</li>");
                }
                body.AppendLine("</ol>");
            }

            SmtpSendEmail(emailTo, subject, body.ToString());
        }
Exemple #2
0
        /// <summary>
        /// Send page Expiry email, one per Web Author, listing all expiring pages
        /// </summary>
        /// <param name="userPages">
        /// Array of expiring pages
        /// </param>
        private void SendEmail(UserPagesModel userPages)
        {
            var emailTo = userPages.User.EmailAddress;

            // If "ForceEmailTo" is set, send all emails there instead (for Testing)
            if (!string.IsNullOrEmpty(_forceSendTo))
            {
                emailTo = _forceSendTo;
            }

            _emailService.UserPageExpiryEmail(emailTo, userPages);
        }
        /// <summary>
        /// Get a list of expiring pages, collated by User
        /// </summary>
        /// <param name="noOfDaysFrom">
        /// How many days to look forward
        /// </param>
        /// <returns>
        /// List Users with expiring pages they are responsible for
        /// </returns>
        public IList <UserPagesModel> GetExpiringNodesByUser(int noOfDaysFrom)
        {
            // Get all content at the root
            var rootnodes = _contentService.GetRootContent();
            // Create a list to store expiring content
            List <IContent> expiringNodes = new List <IContent>();

            // for each content node at the root
            foreach (var node in rootnodes)
            {
                // if the node is expiring within the declared period, add it to the list
                if (node.ExpireDate > DateTime.Now && node.ExpireDate < DateTime.Now.AddDays(noOfDaysFrom))
                {
                    expiringNodes.Add(node);
                }
                // get the root nodes children that are expiring within the declared period.
                var descendants = node.Descendants().Where(nn => nn.ExpireDate > DateTime.Now && nn.ExpireDate < DateTime.Now.AddDays(noOfDaysFrom)).OrderBy(nn => nn.ExpireDate);
                foreach (var child in descendants)
                {
                    // add each one to the list
                    expiringNodes.Add(child);
                }
            }
            // once done, order by expire date.
            expiringNodes.OrderBy(nn => nn.ExpireDate);

            // For each page:
            IList <UserPagesModel> userPages = new List <UserPagesModel>();

            // Create a WebStaff record. Use -1 as an Id as there won't be a valid Umbraco user with that value.
            var webStaff     = new UserPagesModel();
            var webstaffUser = new UmbracoUserModel
            {
                UserId       = -1,
                UserName     = "******",
                FullName     = "Web Staff",
                EmailAddress = "*****@*****.**"
            };

            webStaff.User = webstaffUser;
            userPages.Add(webStaff);

            var helper = new UmbracoHelper(UmbracoContext.Current);

            foreach (var expiringNode in expiringNodes)
            {
                // this should not happen, but just in case...
                if (expiringNode.ExpireDate == null)
                {
                    continue;
                }

                var userPage = new UserPageModel
                {
                    PageId     = expiringNode.Id,
                    PageName   = expiringNode.Name,
                    PagePath   = expiringNode.Path,
                    PageUrl    = helper.NiceUrl(expiringNode.Id),
                    ExpiryDate = (DateTime)expiringNode.ExpireDate
                };

                // Get Web Authors with permission
                // if no permissions at all, then there will be only one element which will contain a "-"
                // If only the default permission then there will only be one element which will contain "F" (Browse Node)
                var perms =
                    _contentService.GetPermissionsForEntity(expiringNode)
                    .Where(
                        x =>
                        x.AssignedPermissions.Count() > 1 ||
                        (x.AssignedPermissions[0] != "-" && x.AssignedPermissions[0] != "F"));

                var nodeAuthors = perms as IList <EntityPermission> ?? perms.ToList();

                // if no Web Authors, add this page to the WebStaff list
                if (!nodeAuthors.Any())
                {
                    userPages.Where(p => p.User.UserId == -1).ForEach(u => u.Pages.Add(userPage));
                    continue;
                }

                // if all Authors of a page are disabled, add page to the webStaff list
                List <EntityPermission> disabledUsers = new List <EntityPermission>();
                foreach (var user in nodeAuthors)
                {
                    var tempUser = _userService.GetUserById(user.UserId);
                    if (!tempUser.IsApproved)
                    {
                        disabledUsers.Add(user);
                    }
                }
                if (disabledUsers.Count == nodeAuthors.Count)
                {
                    userPages.Where(p => p.User.UserId == -1).ForEach(u => u.Pages.Add(userPage));
                    continue;
                }

                // Add the current page to each user that has edit rights
                foreach (var author in nodeAuthors)
                {
                    var user = userPages.FirstOrDefault(f => f.User.UserId == author.UserId);

                    // Create a User record if one does not yet exist
                    if (user == null)
                    {
                        var pUser = _userService.GetUserById(author.UserId);

                        // Check that this author is not Disabled / Locked Out
                        // If they are, end this loop and move onto the next author
                        if (!pUser.IsApproved)
                        {
                            continue;
                        }

                        var p = new UmbracoUserModel
                        {
                            UserId       = author.UserId,
                            UserName     = pUser.Username,
                            FullName     = pUser.Name,
                            EmailAddress = pUser.Email
                        };

                        user = new UserPagesModel {
                            User = p
                        };
                        userPages.Add(user);
                    }

                    // Assign the current page (outer loop) to this author
                    userPages.Where(p => p.User.UserId == user.User.UserId).ForEach(u => u.Pages.Add(userPage));
                }
            }

            // Return a list of users to email, along with the page details
            return(userPages);
        }