示例#1
0
        protected void OnItemSaved(object sender, EventArgs args)
        {
            var savedItem = Event.ExtractParameter(args, 0) as Item;

            if (savedItem == null)
            {
                return;
            }
            var validators = ValidationHelpers.GetValidatorCollection(savedItem);

            if (validators == null)
            {
                return;
            }
            var invalidFields = ValidationHelpers.AreFieldsValid(validators).Select(i => savedItem.Database.GetItem(i).Name).ToList();

            if (invalidFields.Any() && Sitecore.Context.Items[savedItem.ID.ToString() + savedItem.Language] == null)
            {
                Sitecore.Context.Items[savedItem.ID.ToString() + savedItem.Language] = true;
                try
                {
                    if (Sitecore.Context.ClientPage != null && Sitecore.Context.ClientPage.ClientResponse != null)
                    {
                        Sitecore.Context.ClientPage.ClientResponse.Alert(
                            $"{savedItem.DisplayName ?? savedItem.Name} has content entry errors in {savedItem.Language} on the following fields:" +
                            $" {Environment.NewLine}{string.Join(Environment.NewLine, invalidFields)}{Environment.NewLine}" +
                            "It will not be published in that language", "", "Publishing Blocked");
                    }
                }
                catch (NullReferenceException nullref)
                {
                    Sitecore.Diagnostics.Error.LogError("Context of OnItemSaved is incorrect, stacktrace is " + Environment.NewLine + nullref.StackTrace);
                }
            }
        }
        public override void Process(PublishItemContext context)
        {
            if (context == null)
            {
                return;
            }

            if (context.Aborted)
            {
                return;
            }

            var sourceItem = context.PublishHelper.GetSourceItem(context.ItemId);

            if (sourceItem == null)
            {
                return;
            }

            if (!sourceItem.Paths.IsContentItem)
            {
                return;
            }
            sourceItem.Fields.ReadAll();

            var validators = ValidationHelpers.GetValidatorCollection(sourceItem);

            if (validators == null)
            {
                return;
            }
            var invalidFields = ValidationHelpers.AreFieldsValid(validators).Select(i => sourceItem.Database.GetItem(i).Name).ToList();

            if (!invalidFields.Any())
            {
                return;
            }
            if (invalidFields.Any())
            {
                Log.Info(string.Format(
                             "{0}: Item '{1}' in '{4}' will not be publised to database '{2}' because '{3}' field values are not valid",
                             GetType().Name,
                             AuditFormatter.FormatItem(sourceItem),
                             context.PublishContext.PublishOptions.TargetDatabase,
                             string.Join(", ", invalidFields), sourceItem.Language.Name),
                         this);
            }

            context.Action = PublishAction.Skip;
        }
示例#3
0
        public override PipelineProcessorResponseValue ProcessRequest()
        {
            var processorResponseValue = new PipelineProcessorResponseValue();
            var items = base.RequestContext.GetSaveArgs().Items;
            var list  = ((IEnumerable <SaveArgs.SaveItem>)items).SelectMany <SaveArgs.SaveItem, SaveArgs.SaveField>((Func <SaveArgs.SaveItem, IEnumerable <SaveArgs.SaveField> >)(i => (IEnumerable <SaveArgs.SaveField>)i.Fields)).ToList <SaveArgs.SaveField>();
            var obj   = this.RequestContext.Item.Database.GetItem(items[0].ID, items[0].Language);

            if (obj == null || obj.Paths.IsMasterPart || StandardValuesManager.IsStandardValuesHolder(obj))
            {
                return(processorResponseValue);
            }
            var fields = WebUtility.GetFields(this.RequestContext.Item.Database, this.RequestContext.FieldValues);
            var fieldValidationErrorList = new List <FieldValidationError>();

            foreach (var pageEditorField in fields)
            {
                var field                = this.RequestContext.Item.Database.GetItem(pageEditorField.ItemID, pageEditorField.Language).Fields[pageEditorField.FieldID];
                var valueToValidate      = list.FirstOrDefault <SaveArgs.SaveField>((Func <SaveArgs.SaveField, bool>)(f => f.ID == field.ID))?.Value ?? field.Value;
                var fieldValidationError = this.ItemFieldValidator.GetFieldTypeValidationError(field, valueToValidate) ?? this.ItemFieldValidator.GetFieldRegexValidationError(field, valueToValidate);
                if (fieldValidationError != null)
                {
                    fieldValidationErrorList.Add(fieldValidationError);
                }
            }

            var validators = ValidationHelpers.GetValidatorCollection(obj);

            if (validators == null)
            {
                return(processorResponseValue);
            }
            var validationErrorsCustom =
                ValidationHelpers.AreFieldsValid(validators);

            if (validationErrorsCustom.Any())
            {
                var invalidFields = validationErrorsCustom.Select(i => obj.Database.GetItem(i).Name).ToList();

                processorResponseValue.ConfirmMessage = $"{obj.DisplayName ?? obj.Name} has content entry errors in {obj.Language} on the following fields: {Environment.NewLine}" +
                                                        $"{string.Join(Environment.NewLine, invalidFields)}{Environment.NewLine} " +
                                                        "It will not be published in that language";
            }
            processorResponseValue.Value = (object)fieldValidationErrorList;
            return(processorResponseValue);
        }