/// <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();
}