static void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs <IContent> e)
        {
            // Check if the node has an override for the UnPublish date.
            // Need to check in the Publishing event as the URL is not assigned until now.
            try
            {
                var expiryRuleProvider = new ExpiryRulesFromUmbraco(UmbracoContext.Current.ContentCache);
                if (expiryRuleProvider.IsEnabled)
                {
                    // Create the rule evaluator
                    var ruleEvaluator = new ExpiryRuleEvaluator();

                    // Check if there is an override for this content element.
                    // If not, check that the unPublish date is within allowed date range.
                    foreach (var entity in e.PublishedEntities)
                    {
                        if (entity.Id == 0)
                        {
                            // Do a save to get the Id and other info
                            ApplicationContext.Current.Services.ContentService.Save(entity);
                        }

                        var result = ruleEvaluator.ApplyExpiryRules(DateTime.Now, expiryRuleProvider.DefaultMaximumExpiry, new DocumentTypeRuleMatcher(expiryRuleProvider.DocumentTypeRules), new PathRuleMatcher(expiryRuleProvider.PathRules), entity, new NodeUrlBuilder());

                        if (!String.IsNullOrEmpty(result.CancellationMessage))
                        {
                            e.CancelOperation(new EventMessage("Publish Failed", result.CancellationMessage, EventMessageType.Error));
                            return;
                        }

                        if (!String.IsNullOrEmpty(result.ExpireDateChangedMessage))
                        {
                            entity.ExpireDate = result.ExpireDate;
                            e.Messages.Add(new EventMessage("Warning", result.ExpireDateChangedMessage, EventMessageType.Warning));
                            return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error <ExpiryRulesEventHandler>("Error checking page expiry date.", ex);
                e.CancelOperation(new EventMessage("Publish Failed", ex.Message, EventMessageType.Error));
            }
        }
Example #2
0
        private void SetOrRemoveUnpublishDate(IPublishedContent publishedContent)
        {
            var contentService     = ApplicationContext.Current.Services.ContentService;
            var expiryRuleProvider = new ExpiryRulesFromUmbraco(UmbracoContext.ContentCache);
            var expiryRule         = new ExpiryRuleEvaluator().MatchExpiryRules(new DocumentTypeRuleMatcher(expiryRuleProvider.DocumentTypeRules), new PathRuleMatcher(expiryRuleProvider.PathRules), publishedContent);

            try
            {
                var unpublishDate = new ExpiryDateFromExamine(publishedContent.Id, ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"], new ExpiryDateMemoryCache(TimeSpan.FromHours(1))).ExpiryDate;
                if (unpublishDate.HasValue && expiryRule != null && !expiryRule.MaximumExpiry.HasValue)
                {
                    // Page should never expire
                    var node = contentService.GetById(publishedContent.Id);
                    node.ExpireDate = null;
                    contentService.SaveAndPublishWithStatus(node);
                }
                else if (!unpublishDate.HasValue && (expiryRule == null || expiryRule.MaximumExpiry.HasValue))
                {
                    var node = contentService.GetById(publishedContent.Id);
                    if (expiryRule != null && expiryRule.MaximumExpiry.HasValue)
                    {
                        node.ExpireDate = DateTime.Now.Add(expiryRule.MaximumExpiry.Value);
                        contentService.SaveAndPublishWithStatus(node);
                    }
                    else if (expiryRuleProvider.DefaultMaximumExpiry.HasValue)
                    {
                        node.ExpireDate = DateTime.Now.Add(expiryRuleProvider.DefaultMaximumExpiry.Value);
                        contentService.SaveAndPublishWithStatus(node);
                    }
                }
            }
            catch (Exception e)
            {
                e.ToExceptionless().Submit();
            }
        }