Пример #1
0
        /// <summary>
        /// Saves the server widget options.
        /// </summary>
        /// <param name="widget">The widget.</param>
        /// <param name="requestOptions">The request options.</param>
        /// <returns> List of saved widget options </returns>
        private IList<ContentOption> SaveServerWidgetOptions(ServerControlWidget widget, IList<ContentOptionDto> requestOptions)
        {
            var options = new List<ContentOption>();

            if (requestOptions != null && requestOptions.Count > 0)
            {
                foreach (var requestOption in requestOptions)
                {
                    var option = new ContentOption { Key = requestOption.Key, DefaultValue = requestOption.DefaultValue, Type = requestOption.Type, Content = widget };

                    options.Add(option);
                    Repository.Save(option);
                }
            }

            return options;
        }
Пример #2
0
        /// <summary>
        /// Creates the server control widget.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        /// Created widget entity
        /// </returns>
        /// <exception cref="CmsApiValidationException"></exception>
        /// <exception cref="CmsApiException"></exception>
        /// <exception cref="BetterCms.Core.Exceptions.Api.CmsApiException"></exception>
        public ServerControlWidget CreateServerControlWidget(CreateServerControlWidgetRequest request)
        {
            ValidateRequest(request);

            try
            {
                UnitOfWork.BeginTransaction();

                var serverWidget = new ServerControlWidget
                {
                    Name = request.Name,
                    Url = request.WidgetPath,
                    PreviewUrl = request.PreviewUrl
                };

                if (request.CategoryId.HasValue && !request.CategoryId.Value.HasDefaultValue())
                {
                    serverWidget.Category = Repository.AsProxy<Category>(request.CategoryId.Value);
                }

                var service = Resolve<IContentService>();
                var widgetToSave = (ServerControlWidget)service.SaveContentWithStatusUpdate(serverWidget, ContentStatus.Published);

                Repository.Save(widgetToSave);

                if (request.Options != null && request.Options.Count > 0)
                {
                    SaveServerWidgetOptions(widgetToSave, request.Options);
                }

                UnitOfWork.Commit();

                // Notify
                Events.OnWidgetCreated(widgetToSave);

                return widgetToSave;
            }
            catch (CmsApiValidationException inner)
            {
                var message = string.Format("Failed to create server control widget.");
                Logger.Error(message, inner);
                throw;
            }
            catch (Exception inner)
            {
                var message = string.Format("Failed to create server control widget.");
                Logger.Error(message, inner);
                throw new CmsApiException(message, inner);
            }
        }