public GetByBlockIdAndEntityValueAndVersion ( int blockId, string entityValue, int version ) : HtmlContent | ||
blockId | int | A |
entityValue | string | A |
version | int | A |
Результат | HtmlContent |
/// <summary> /// Handles the Click event of the lbSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void lbSave_Click( object sender, EventArgs e ) { bool supportVersioning = GetAttributeValue( "SupportVersions" ).AsBoolean(); bool requireApproval = GetAttributeValue( "RequireApproval" ).AsBoolean(); var rockContext = new RockContext(); HtmlContentService htmlContentService = new HtmlContentService( rockContext ); // get settings string entityValue = EntityValue(); // get current content int version = hfVersion.ValueAsInt(); HtmlContent htmlContent = htmlContentService.GetByBlockIdAndEntityValueAndVersion( this.BlockId, entityValue, version ); // get the content depending on which mode we are in (codeeditor or ckeditor) string newContent = ceHtml.Visible ? ceHtml.Text : htmlEditor.Text; //// create a new record only in the following situations: //// - this is the first time this htmlcontent block got content (new block and edited for the first time) //// - the content was changed, versioning is enabled, and OverwriteVersion is not checked // if the existing content changed, and the overwrite option was not checked, create a new version if (htmlContent != null) { // Editing existing content. Check if content has changed if (htmlContent.Content != newContent) { // The content has changed (different than database). Check if versioning is enabled if (supportVersioning && !cbOverwriteVersion.Checked) { //// versioning is enabled, and they didn't choose to overwrite //// set to null so that we'll create a new record htmlContent = null; } } } // if a record doesn't exist then create one if ( htmlContent == null ) { htmlContent = new HtmlContent(); htmlContent.BlockId = this.BlockId; htmlContent.EntityValue = entityValue; if ( supportVersioning ) { int? maxVersion = GetMaxVersionOfHtmlContent(); htmlContent.Version = maxVersion.HasValue ? maxVersion.Value + 1 : 1; } else { htmlContent.Version = 1; } htmlContentService.Add( htmlContent ); } htmlContent.StartDateTime = drpDateRange.LowerValue; htmlContent.ExpireDateTime = drpDateRange.UpperValue; bool currentUserCanApprove = IsUserAuthorized( "Approve" ); if ( !requireApproval ) { // if this block doesn't require Approval, mark it as approved htmlContent.IsApproved = true; htmlContent.ApprovedByPersonAliasId = CurrentPersonAliasId; htmlContent.ApprovedDateTime = RockDateTime.Now; } else { // since this content requires Approval, mark it as approved ONLY if the current user can approve // and they set the hfApprovalStatus flag to true. if ( currentUserCanApprove && hfApprovalStatus.Value.AsBoolean() ) { htmlContent.IsApproved = true; htmlContent.ApprovedByPersonAliasId = CurrentPersonAliasId; htmlContent.ApprovedDateTime = RockDateTime.Now; } else { // if the content has changed if ( htmlContent.Content != newContent ) { nbApprovalRequired.Visible = true; htmlContent.IsApproved = false; } } } htmlContent.Content = newContent; rockContext.SaveChanges(); // flush cache content HtmlContentService.FlushCachedContent( htmlContent.BlockId, htmlContent.EntityValue ); ShowView(); }
/// <summary> /// Handles the Click event of the lbSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void lbSave_Click( object sender, EventArgs e ) { bool supportVersioning = GetAttributeValue( "SupportVersions" ).AsBoolean(); bool requireApproval = GetAttributeValue( "RequireApproval" ).AsBoolean(); HtmlContentService htmlContentService = new HtmlContentService(); // get settings string entityValue = EntityValue(); // get current content int version = hfVersion.ValueAsInt(); HtmlContent htmlContent = htmlContentService.GetByBlockIdAndEntityValueAndVersion( this.BlockId, entityValue, version ); // if the existing content changed, and the overwrite option was not checked, create a new version string newContent = ceHtml.Visible ? ceHtml.Text : htmlEditor.Text; if ( htmlContent != null && supportVersioning && htmlContent.Content != newContent && !cbOverwriteVersion.Checked ) { htmlContent = null; } // if a record doesn't exist then create one if ( htmlContent == null ) { htmlContent = new HtmlContent(); htmlContent.BlockId = this.BlockId; htmlContent.EntityValue = entityValue; if ( supportVersioning ) { int? maxVersion = GetMaxVersionOfHtmlContent(); htmlContent.Version = maxVersion.HasValue ? maxVersion.Value + 1 : 1; } else { htmlContent.Version = 1; } htmlContentService.Add( htmlContent, CurrentPersonId ); } htmlContent.StartDateTime = drpDateRange.LowerValue; htmlContent.ExpireDateTime = drpDateRange.UpperValue; if ( !requireApproval || IsUserAuthorized( "Approve" ) ) { htmlContent.IsApproved = !requireApproval || hfApprovalStatus.Value.AsBoolean(); if ( htmlContent.IsApproved ) { int personId = hfApprovalStatusPersonId.ValueAsInt(); if ( personId > 0 ) { htmlContent.ApprovedByPersonId = personId; htmlContent.ApprovedDateTime = DateTime.Now; } } } htmlContent.LastModifiedPersonId = this.CurrentPersonId; htmlContent.LastModifiedDateTime = DateTime.Now; htmlContent.Content = newContent; if ( htmlContentService.Save( htmlContent, CurrentPersonId ) ) { // flush cache content this.FlushCacheItem( entityValue ); // force the updatepanel for the view to update since we have two update panels and we only want the View to update when it is edited and Saved upnlHtmlContent.Update(); ShowView(); } else { // TODO: service.ErrorMessages; } }
/// <summary> /// Handles the Click event of the lbSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void lbSave_Click( object sender, EventArgs e ) { bool supportVersioning = GetAttributeValue( "SupportVersions" ).AsBoolean(); bool requireApproval = GetAttributeValue( "RequireApproval" ).AsBoolean(); var rockContext = new RockContext(); HtmlContentService htmlContentService = new HtmlContentService( rockContext ); // get settings string entityValue = EntityValue(); // get current content int version = hfVersion.ValueAsInt(); HtmlContent htmlContent = htmlContentService.GetByBlockIdAndEntityValueAndVersion( this.BlockId, entityValue, version ); // get the content depending on which mode we are in (codeeditor or ckeditor) string newContent = ceHtml.Visible ? ceHtml.Text : htmlEditor.Text; // check if the new content is valid // NOTE: This is a limited check that will only warn of invalid HTML the first // time a user clicks the save button. Any errors encountered on the second runthrough // are assumed to be intentional. HtmlDocument doc = new HtmlDocument(); doc.LoadHtml( newContent ); if ( doc.ParseErrors.Count() > 0 && !nbInvalidHtml.Visible ) { var reasons = doc.ParseErrors.Select( r => r.Reason ).ToList(); StringBuilder sb = new StringBuilder(); sb.AppendLine( "Warning: The HTML has the following errors:<ul>" ); foreach ( var reason in reasons ) { sb.AppendLine( String.Format( "<li>{0}</li>", reason.EncodeHtml() ) ); } sb.AppendLine( "</ul> <br/> If you wish to save anyway, click the save button again." ); nbInvalidHtml.Text = sb.ToString(); nbInvalidHtml.Visible = true; return; } //// create a new record only in the following situations: //// - this is the first time this htmlcontent block got content (new block and edited for the first time) //// - the content was changed, versioning is enabled, and OverwriteVersion is not checked // if the existing content changed, and the overwrite option was not checked, create a new version if (htmlContent != null) { // Editing existing content. Check if content has changed if (htmlContent.Content != newContent) { // The content has changed (different than database). Check if versioning is enabled if (supportVersioning && !cbOverwriteVersion.Checked) { //// versioning is enabled, and they didn't choose to overwrite //// set to null so that we'll create a new record htmlContent = null; } } } // if a record doesn't exist then create one if ( htmlContent == null ) { htmlContent = new HtmlContent(); htmlContent.BlockId = this.BlockId; htmlContent.EntityValue = entityValue; if ( supportVersioning ) { int? maxVersion = GetMaxVersionOfHtmlContent(); htmlContent.Version = maxVersion.HasValue ? maxVersion.Value + 1 : 1; } else { htmlContent.Version = 1; } htmlContentService.Add( htmlContent ); } htmlContent.StartDateTime = drpDateRange.LowerValue; htmlContent.ExpireDateTime = drpDateRange.UpperValue; bool currentUserCanApprove = IsUserAuthorized( "Approve" ); if ( !requireApproval ) { // if this block doesn't require Approval, mark it as approved htmlContent.IsApproved = true; htmlContent.ApprovedByPersonAliasId = CurrentPersonAliasId; htmlContent.ApprovedDateTime = RockDateTime.Now; } else { // since this content requires Approval, mark it as approved ONLY if the current user can approve // and they set the hfApprovalStatus flag to true. if ( currentUserCanApprove && hfApprovalStatus.Value.AsBoolean() ) { htmlContent.IsApproved = true; htmlContent.ApprovedByPersonAliasId = CurrentPersonAliasId; htmlContent.ApprovedDateTime = RockDateTime.Now; } else { // if the content has changed if ( htmlContent.Content != newContent ) { nbApprovalRequired.Visible = true; htmlContent.IsApproved = false; } } } htmlContent.Content = newContent; rockContext.SaveChanges(); // flush cache content HtmlContentService.FlushCachedContent( htmlContent.BlockId, htmlContent.EntityValue ); ShowView(); }