Beispiel #1
0
        /// <summary>
        /// Saves a <see cref="Template"/>
        /// </summary>
        /// <param name="template"><see cref="Template"/> to save</param>
        /// <param name="userId"></param>
        public void SaveTemplate(ITemplate template, int userId = Constants.Security.SuperUserId)
        {
            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }

            if (string.IsNullOrWhiteSpace(template.Name) || template.Name.Length > 255)
            {
                throw new InvalidOperationException("Name cannot be null, empty, contain only white-space characters or be more than 255 characters in length.");
            }


            using (ICoreScope scope = ScopeProvider.CreateCoreScope())
            {
                EventMessages eventMessages      = EventMessagesFactory.Get();
                var           savingNotification = new TemplateSavingNotification(template, eventMessages);
                if (scope.Notifications.PublishCancelable(savingNotification))
                {
                    scope.Complete();
                    return;
                }

                _templateRepository.Save(template);

                scope.Notifications.Publish(new TemplateSavedNotification(template, eventMessages).WithStateFrom(savingNotification));

                Audit(AuditType.Save, userId, template.Id, UmbracoObjectTypes.Template.GetName());
                scope.Complete();
            }
        }
Beispiel #2
0
    /// <summary>
    ///     Saves a collection of <see cref="Template" /> objects
    /// </summary>
    /// <param name="templates">List of <see cref="Template" /> to save</param>
    /// <param name="userId">Optional id of the user</param>
    public void SaveTemplate(IEnumerable <ITemplate> templates, int userId = Constants.Security.SuperUserId)
    {
        ITemplate[] templatesA = templates.ToArray();
        using (ICoreScope scope = ScopeProvider.CreateCoreScope())
        {
            EventMessages eventMessages      = EventMessagesFactory.Get();
            var           savingNotification = new TemplateSavingNotification(templatesA, eventMessages);
            if (scope.Notifications.PublishCancelable(savingNotification))
            {
                scope.Complete();
                return;
            }

            foreach (ITemplate template in templatesA)
            {
                _templateRepository.Save(template);
            }

            scope.Notifications.Publish(
                new TemplateSavedNotification(templatesA, eventMessages).WithStateFrom(savingNotification));

            Audit(AuditType.Save, userId, -1, UmbracoObjectTypes.Template.GetName());
            scope.Complete();
        }
    }
 public void Handle(TemplateSavingNotification notification)
 {
     foreach (var item in notification.SavedEntities)
     {
         // tells the watcher this has been saved in umbraco.
         _templateWatcher.QueueChange(item.Alias);
     }
 }
Beispiel #4
0
        /// <summary>
        /// Used to check if a template is being created based on a document type, in this case we need to
        /// ensure the template markup is correct based on the model name of the document type
        /// </summary>
        public void Handle(TemplateSavingNotification notification)
        {
            if (_config.ModelsMode == ModelsMode.Nothing)
            {
                return;
            }

            // Don't do anything if we're not requested to create a template for a content type
            if (notification.CreateTemplateForContentType is false)
            {
                return;
            }

            // ensure we have the content type alias
            if (notification.ContentTypeAlias is null)
            {
                throw new InvalidOperationException("ContentTypeAlias was not found on the notification");
            }

            foreach (ITemplate template in notification.SavedEntities)
            {
                // if it is in fact a new entity (not been saved yet) and the "CreateTemplateForContentType" key
                // is found, then it means a new template is being created based on the creation of a document type
                if (!template.HasIdentity && string.IsNullOrWhiteSpace(template.Content))
                {
                    // ensure is safe and always pascal cased, per razor standard
                    // + this is how we get the default model name in Umbraco.ModelsBuilder.Umbraco.Application
                    var alias     = notification.ContentTypeAlias;
                    var name      = template.Name; // will be the name of the content type since we are creating
                    var className = UmbracoServices.GetClrName(_shortStringHelper, name, alias);

                    var modelNamespace = _config.ModelsNamespace;

                    // we do not support configuring this at the moment, so just let Umbraco use its default value
                    // var modelNamespaceAlias = ...;
                    var markup = _defaultViewContentProvider.GetDefaultFileContent(
                        modelClassName: className,
                        modelNamespace: modelNamespace /*,
                                                        * modelNamespaceAlias: modelNamespaceAlias*/);

                    // set the template content to the new markup
                    template.Content = markup;
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Creates a template for a content type
        /// </summary>
        /// <param name="contentTypeAlias"></param>
        /// <param name="contentTypeName"></param>
        /// <param name="userId"></param>
        /// <returns>
        /// The template created
        /// </returns>
        public Attempt <OperationResult <OperationResultType, ITemplate> > CreateTemplateForContentType(string contentTypeAlias, string contentTypeName, int userId = Constants.Security.SuperUserId)
        {
            var template = new Template(_shortStringHelper, contentTypeName,
                                        //NOTE: We are NOT passing in the content type alias here, we want to use it's name since we don't
                                        // want to save template file names as camelCase, the Template ctor will clean the alias as
                                        // `alias.ToCleanString(CleanStringType.UnderscoreAlias)` which has been the default.
                                        // This fixes: http://issues.umbraco.org/issue/U4-7953
                                        contentTypeName);

            EventMessages eventMessages = EventMessagesFactory.Get();

            if (contentTypeAlias != null && contentTypeAlias.Length > 255)
            {
                throw new InvalidOperationException("Name cannot be more than 255 characters in length.");
            }

            // check that the template hasn't been created on disk before creating the content type
            // if it exists, set the new template content to the existing file content
            string content = GetViewContent(contentTypeAlias);

            if (content != null)
            {
                template.Content = content;
            }

            using (IScope scope = ScopeProvider.CreateScope())
            {
                var savingEvent = new TemplateSavingNotification(template, eventMessages, true, contentTypeAlias);
                if (scope.Notifications.PublishCancelable(savingEvent))
                {
                    scope.Complete();
                    return(OperationResult.Attempt.Fail <OperationResultType, ITemplate>(OperationResultType.FailedCancelledByEvent, eventMessages, template));
                }

                _templateRepository.Save(template);
                scope.Notifications.Publish(new TemplateSavedNotification(template, eventMessages).WithStateFrom(savingEvent));

                Audit(AuditType.Save, userId, template.Id, ObjectTypes.GetName(UmbracoObjectTypes.Template));
                scope.Complete();
            }

            return(OperationResult.Attempt.Succeed <OperationResultType, ITemplate>(OperationResultType.Success, eventMessages, template));
        }