Пример #1
0
        public void Update(IElementSettings settings, IUnitOfWork unitOfWork = null)
        {
            // Get HTML uploads that are being referenced by updated HTML
            HtmlSettings htmlSettings = (HtmlSettings)settings;

            htmlSettings.Html    = SanitizeHtml(htmlSettings.Html);
            htmlSettings.Uploads = GetHtmlUploads(htmlSettings);

            // Get previous HTML settings
            HtmlSettings previousHtmlSettings = (HtmlSettings)New(settings.TenantId);

            previousHtmlSettings.ElementId = settings.ElementId;
            _htmlRepository.Read(previousHtmlSettings, unitOfWork);

            // Get HTML uploads that are no longer used
            List <HtmlUpload>             uploadsToDelete = new List <HtmlUpload>();
            Dictionary <long, HtmlUpload> uploadsById     = htmlSettings.Uploads.GroupBy(u => u.HtmlUploadId).ToDictionary(u => u.Key, u => u.First());

            foreach (HtmlUpload previousHtmlUpload in previousHtmlSettings.Uploads)
            {
                if (!uploadsById.ContainsKey(previousHtmlUpload.HtmlUploadId))
                {
                    uploadsToDelete.Add(previousHtmlUpload);
                }
            }

            // Update HTML element
            _htmlRepository.Update(htmlSettings, unitOfWork);

            // Delete HTML uploads that are no longer used
            DeleteUploads(uploadsToDelete, unitOfWork);
        }
Пример #2
0
        public void Read(HtmlSettings settings, IUnitOfWork unitOfWork = null)
        {
            IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork);

            try
            {
                string sql = _sqlManager.GetSql("Sql.ReadHtml.sql");
                dbm.SetSQL(sql);
                dbm.AddParameter("@TenantId", FieldType.BigInt, settings.TenantId);
                dbm.AddParameter("@ElementId", FieldType.BigInt, settings.ElementId);
                dbm.ExecuteReader();
                dbm.Read();
                settings.Html    = (string)dbm.DataReaderValue("Html");
                settings.Uploads = new List <HtmlUpload>();
                dbm.Read();
                while (dbm.Read())
                {
                    settings.Uploads.Add(GetHtmlUploadFromDatabaseManager(dbm));
                }
            }
            finally
            {
                if (unitOfWork == null)
                {
                    dbm.Dispose();
                }
            }
        }
Пример #3
0
        private HtmlUploadCollection GetHtmlUploadCollection(HtmlSettings settings)
        {
            HtmlUploadCollection htmlUploadCollection = new HtmlUploadCollection();

            foreach (HtmlUpload htmlUpload in settings.Uploads)
            {
                htmlUploadCollection.Add(htmlUpload);
            }
            return(htmlUploadCollection);
        }
Пример #4
0
        private List <HtmlUpload> GetHtmlUploads(HtmlSettings settings)
        {
            // Construct empty list of uploads
            List <HtmlUpload> uploads = new List <HtmlUpload>();

            // Nothing to do if no HTML
            if (string.IsNullOrWhiteSpace(settings.Html))
            {
                return(uploads);
            }

            // Search for strings like "/elements/{elementid}/uploads/{uploadid}?t={ticks}" and extract all uploadids.
            string       html          = settings.Html;
            const string beginText     = "/elements/";
            const string endText       = "\"";
            const int    uploadIdIndex = 4;
            bool         finished      = false;

            while (!finished)
            {
                int uploadBeginIndex = html.IndexOf(beginText);
                finished = uploadBeginIndex < 0;
                if (finished)
                {
                    break;
                }
                int uploadEndIndex = html.IndexOf(endText, uploadBeginIndex + beginText.Length);
                finished = uploadEndIndex < 0;
                if (finished)
                {
                    break;
                }
                string   url          = html.Substring(uploadBeginIndex, uploadEndIndex - uploadBeginIndex);
                string[] urlParts     = url.Split('/');
                string   uploadIdText = urlParts[uploadIdIndex];
                int      queryIndex   = uploadIdText.IndexOf("?");
                if (queryIndex >= 0)
                {
                    uploadIdText = uploadIdText.Substring(0, queryIndex);
                }
                long htmlUploadId = Convert.ToInt64(uploadIdText);
                uploads.Add(new HtmlUpload {
                    TenantId = settings.TenantId, ElementId = settings.ElementId, HtmlUploadId = htmlUploadId
                });
                html = html.Substring(uploadEndIndex + 1);
            }

            // Return list of uploads
            return(uploads);
        }
Пример #5
0
        public void Delete(long tenantId, long elementId, IUnitOfWork unitOfWork = null)
        {
            // Get HTML settings so that we know what uploads to delete
            HtmlSettings htmlSettings = (HtmlSettings)New(tenantId);

            htmlSettings.ElementId = elementId;
            _htmlRepository.Read(htmlSettings, unitOfWork);

            // Delete HTML and upload records
            _htmlRepository.Delete(tenantId, elementId, unitOfWork);

            // Delete uploads
            DeleteUploads(htmlSettings.Uploads, unitOfWork);
        }
Пример #6
0
        public IElementContent GetContent(IElementSettings settings, IPageContext pageContext, IUnitOfWork unitOfWork = null)
        {
            // Construct element content
            HtmlContent htmlContent = new HtmlContent();

            htmlContent.PartialViewName = "Html";

            // Populate element content according to element settings
            HtmlSettings htmlSettings = (HtmlSettings)settings;

            htmlContent.FormattedHtml = FormatHtml(htmlSettings.Html);

            // Return resulting element content
            return(htmlContent);
        }
Пример #7
0
        public void Create(HtmlSettings settings, IUnitOfWork unitOfWork = null)
        {
            IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork);

            try
            {
                string sql = _sqlManager.GetSql("Sql.CreateHtml.sql");
                dbm.SetSQL(sql);
                dbm.AddParameter("@TenantId", FieldType.BigInt, settings.TenantId);
                dbm.AddParameter("@ElementId", FieldType.BigInt, settings.ElementId);
                dbm.AddParameter("@Html", FieldType.NVarChar, -1, settings.Html);
                dbm.ExecuteNonQuery();
            }
            finally
            {
                if (unitOfWork == null)
                {
                    dbm.Dispose();
                }
            }
        }
        public FormResult PostForm(Form form)
        {
            try
            {
                // Check permissions
                _authorizationService.AuthorizeUserForFunction(Functions.UpdatePageElements);

                // Get page and element identifiers
                string[] parts = form.Context.Split('|');
                long pageId = Convert.ToInt64(parts[0]);
                long elementId = Convert.ToInt64(parts[1]);

                // Get the HTML element service
                IAdvancedElementService elementService = (IAdvancedElementService)_elementFactory.GetElementService(FormId);

                // Get updated HTML settings
                HtmlSettings htmlSettings = (HtmlSettings)elementService.New(_authenticationService.TenantId);
                htmlSettings.ElementId = elementId;
                htmlSettings.Html = ((MultiLineTextField)form.Fields["html"]).Value;

                // Perform the update
                elementService.Update(htmlSettings);

                // Return form result with no errors
                return _formHelperService.GetFormResult();
            }
            catch (ValidationErrorException ex)
            {
                // Return form result containing errors
                return _formHelperService.GetFormResultWithValidationErrors(ex.Errors);
            }
            catch (Exception)
            {
                // Return form result containing unexpected error message
                return _formHelperService.GetFormResultWithErrorMessage(ApplicationResource.UnexpectedErrorMessage);
            }
        }
        public Form GetForm(string context)
        {
            // Check permissions
            _authorizationService.AuthorizeUserForFunction(Functions.UpdatePageElements);

            // Get page and element identifiers
            string[] parts = context.Split('|');
            long pageId = Convert.ToInt64(parts[0]);
            long elementId = Convert.ToInt64(parts[1]);

            // Get current HTML settings
            Guid elementTypeId = FormId;
            IAdvancedElementService elementService = (IAdvancedElementService)_elementFactory.GetElementService(elementTypeId);
            HtmlSettings htmlSettings = (HtmlSettings)elementService.New(_authenticationService.TenantId);
            htmlSettings.ElementId = elementId;
            elementService.Read(htmlSettings);

            // Construct form
            Form form = new Form { Fields = new Dictionary<string, IFormField>(), Id = FormId.ToString(), Context = context };
            form.Fields.Add("html", new MultiLineTextField
            {
                Name = "html",
                Label = ElementResource.HtmlHtmlLabel,
                Value = htmlSettings.Html,
                Rows = 15
            });
            form.Fields.Add("upload", new UploadField
            {
                Name = "upload",
                Label = ElementResource.HtmlUploadLabel
            });
            form.SubmitLabel = ElementResource.HtmlButtonLabel;

            // Return result
            return form;
        }
Пример #10
0
        public void Update(HtmlSettings settings, IUnitOfWork unitOfWork = null)
        {
            HtmlUploadCollection htmlUploadCollection = GetHtmlUploadCollection(settings);
            IUnitOfWork          localUnitOfWork      = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            try
            {
                IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork ?? localUnitOfWork);
                string           sql = _sqlManager.GetSql("Sql.UpdateHtml.sql");
                dbm.SetSQL(sql);
                dbm.AddParameter("@TenantId", FieldType.BigInt, settings.TenantId);
                dbm.AddParameter("@ElementId", FieldType.BigInt, settings.ElementId);
                dbm.AddParameter("@Html", FieldType.NVarChar, -1, settings.Html);
                dbm.AddTypedParameter("@HtmlUploads", FieldType.Structured, htmlUploadCollection.Count == 0 ? null : htmlUploadCollection, "element.HtmlUploadTableType");
                dbm.ExecuteNonQuery();
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
            }
            catch
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }
Пример #11
0
        public long PrepareImages(long tenantId, long elementId, CreateUploadModel model, IUnitOfWork unitOfWork = null)
        {
            // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            // Begin work
            try
            {
                // Check that uploaded content is valid image
                System.Drawing.Size imageSize = _htmlValidator.ValidatePrepareImages(tenantId, elementId, model);

                // Get HTML settings (does not have to be within unit of work)
                HtmlSettings htmlSettings = (HtmlSettings)New(tenantId);
                htmlSettings.ElementId = elementId;
                Read(htmlSettings);

                // Create thumbnail model
                byte[] thumbnailContent = model.Content;
                if (imageSize.Width > htmlSettings.ThumbnailImageWidth || imageSize.Height > htmlSettings.ThumbnailImageHeight)
                {
                    ResizeInfo thumbnailResizeInfo = new ResizeInfo
                    {
                        Width      = htmlSettings.ThumbnailImageWidth,
                        Height     = htmlSettings.ThumbnailImageHeight,
                        ResizeMode = htmlSettings.ThumbnailImageResizeMode
                    };
                    thumbnailContent = _imageAnalysisService.ResizeImage(model.Content, thumbnailResizeInfo);
                }
                CreateUploadModel thumbnailModel = new CreateUploadModel
                {
                    Content     = thumbnailContent,
                    ContentType = model.ContentType,
                    Name        = model.Name,
                    TenantId    = model.TenantId
                };

                // Create preview model
                byte[] previewContent = model.Content;
                if (imageSize.Width > htmlSettings.PreviewImageWidth || imageSize.Height > htmlSettings.PreviewImageHeight)
                {
                    ResizeInfo previewResizeInfo = new ResizeInfo
                    {
                        Width      = htmlSettings.PreviewImageWidth,
                        Height     = htmlSettings.PreviewImageHeight,
                        ResizeMode = htmlSettings.PreviewImageResizeMode
                    };
                    previewContent = _imageAnalysisService.ResizeImage(model.Content, previewResizeInfo);
                }
                CreateUploadModel previewModel = new CreateUploadModel
                {
                    Content     = previewContent,
                    ContentType = model.ContentType,
                    Name        = model.Name,
                    TenantId    = model.TenantId
                };

                // Create uncommitted uploads for thumbnail, preview and original image
                long thumbnailImageUploadId = _uploadService.Create(thumbnailModel, unitOfWork ?? localUnitOfWork);
                long previewImageUploadId   = _uploadService.Create(previewModel, unitOfWork ?? localUnitOfWork);
                long imageUploadId          = _uploadService.Create(model, unitOfWork ?? localUnitOfWork);

                // Commit uploads
                _uploadService.Commit(tenantId, thumbnailImageUploadId, GetHtmlUploadStorageHierarchy(elementId), unitOfWork ?? localUnitOfWork);
                _uploadService.Commit(tenantId, previewImageUploadId, GetHtmlUploadStorageHierarchy(elementId), unitOfWork ?? localUnitOfWork);
                _uploadService.Commit(tenantId, imageUploadId, GetHtmlUploadStorageHierarchy(elementId), unitOfWork ?? localUnitOfWork);

                // Create HTML image, recording upload IDs of newly created images
                HtmlUpload upload = new HtmlUpload
                {
                    TenantId               = tenantId,
                    ElementId              = elementId,
                    ImageTenantId          = tenantId,
                    ThumbnailImageUploadId = thumbnailImageUploadId,
                    PreviewImageUploadId   = previewImageUploadId,
                    ImageUploadId          = imageUploadId
                };
                long htmlImageId = _htmlRepository.CreateUpload(upload, unitOfWork ?? localUnitOfWork);

                // Commit work if local unit of work in place and return result
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
                return(htmlImageId);
            }
            catch (ValidationErrorException)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            catch (Exception ex)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }