protected void ddlAddEditDocumentType_SelectedIndexChanged(object sender, EventArgs e) { if (tbDocumentName.Text.IsNotNullOrWhiteSpace() || ddlAddEditDocumentType.SelectedIndex == 0) { // If there is already a name or nothing is selected then do do anything. fuUploader.Visible = false; nbSelectDocumentType.Visible = true; return; } // Get the selected DocumentType from cache and update the BinaryFileTypeGuid in the FileUploader var documentTypeCache = DocumentTypeCache.Get(ddlAddEditDocumentType.SelectedValueAsInt() ?? 0); fuUploader.BinaryFileTypeGuid = new BinaryFileTypeService(new RockContext()).GetGuid(documentTypeCache.BinaryFileTypeId).Value; fuUploader.Visible = true; nbSelectDocumentType.Visible = false; string template = documentTypeCache.DefaultDocumentNameTemplate; if (template.IsNotNullOrWhiteSpace()) { // If there is a template then apply it var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson); tbDocumentName.Text = template.ResolveMergeFields(mergeFields); } }
/// <summary> /// Check if document type is valid for entity. /// </summary> /// <param name="entity">The entity.</param> /// <param name="documentType">The document type.</param> /// <returns></returns> private bool IsDocumentTypeValidForEntity(IEntity entity, DocumentTypeCache documentType) { bool isValid = true; // If the document does not have a qualifier column specified then allow it by default if (documentType.EntityTypeQualifierColumn.IsNotNullOrWhiteSpace()) { // Check that the EntityTypeQualifierColumn is a property for this entity, if not then remove it by default if (entity.GetType().GetProperty(documentType.EntityTypeQualifierColumn) == null) { isValid = false; } if (isValid) { // Get the value of the property specified in DocumentType.EntityTypeQualifierColumn from the current ContextEntity string entityPropVal = entity.GetPropertyValue(documentType.EntityTypeQualifierColumn).ToString(); // If the entity property values does not match DocumentType.EntityTypeQualifierValue then it should be removed. if (entityPropVal != documentType.EntityTypeQualifierValue) { isValid = false; } } } return(isValid); }
/// <summary> /// Get filtered document Types /// </summary> private IEnumerable <DocumentTypeCache> GetFilteredDocumentTypes() { var contextEntity = this.ContextEntity(); if (contextEntity == null) { return(new List <DocumentTypeCache>()); } var entityTypeId = contextEntity.TypeId; List <DocumentTypeCache> documentypesForContextEntityType = DocumentTypeCache.GetByEntity(entityTypeId, true); // Get the document types allowed from the block settings and only have those in the list of document types for the entity if (GetAttributeValue(AttributeKeys.DocumentTypes).IsNotNullOrWhiteSpace()) { var blockAttributeFilteredDocumentTypes = GetAttributeValue(AttributeKeys.DocumentTypes).Split(',').Select(int.Parse).ToList(); documentypesForContextEntityType = documentypesForContextEntityType.Where(d => blockAttributeFilteredDocumentTypes.Contains(d.Id)).ToList(); } // Remove document types from the list that do not match the EntityTypeQualifiers var accessDeniedForDocumentTypeList = new List <int>(); foreach (var documentType in documentypesForContextEntityType) { // Check System Security on the type if (!documentType.IsAuthorized(Authorization.VIEW, this.CurrentPerson)) { accessDeniedForDocumentTypeList.Add(documentType.Id); continue; } // If the document does not have a qualifier column specified then allow it by default if (documentType.EntityTypeQualifierColumn.IsNotNullOrWhiteSpace()) { // Check that the EntityTypeQualifierColumn is a property for this entity, if not then remove it by default if (contextEntity.GetType().GetProperty(documentType.EntityTypeQualifierColumn) == null) { accessDeniedForDocumentTypeList.Add(documentType.Id); continue; } // Get the value of the property specified in DocumentType.EntityTypeQualifierColumn from the current ContextEntity string entityPropVal = this.ContextEntity().GetPropertyValue(documentType.EntityTypeQualifierColumn).ToString(); // If the entity property values does not match DocumentType.EntityTypeQualifierValue then it should be removed. if (entityPropVal != documentType.EntityTypeQualifierValue) { accessDeniedForDocumentTypeList.Add(documentType.Id); } } } // Create the list of document types that are valid for this entity, satisfy EntityTypeQualifiers, and that the current user has rights to view return(documentypesForContextEntityType.Where(d => !accessDeniedForDocumentTypeList.Contains(d.Id))); }
/// <summary> /// Creates the control(s) necessary for prompting user for a new value /// </summary> /// <param name="configurationValues">The configuration values.</param> /// <param name="id"></param> /// <returns> /// The control /// </returns> public override Control EditControl(Dictionary <string, ConfigurationValue> configurationValues, string id) { ListControl editControl; var values = DocumentTypeCache.All().Select(v => new { v.Id, v.Name }).OrderBy(v => v.Name).ToList(); var allowMultiple = true; if (configurationValues != null && configurationValues.ContainsKey(ALLOW_MULTIPLE_KEY)) { allowMultiple = configurationValues[ALLOW_MULTIPLE_KEY].Value.AsBoolean(true); } if (!allowMultiple) { // Select single member editControl = new RockDropDownList { ID = id }; } else { // Select multiple members editControl = new RockListBox { ID = id, DisplayDropAsAbsolute = true }; } var items = new List <ListItem>(); foreach (var value in values) { items.Add(new ListItem(value.Name, value.Id.ToString())); } if (items.Count > 0) { if (editControl is RockListBox) { (editControl as RockListBox).Items.AddRange(items.ToArray()); } else { (editControl as RockDropDownList).Items.AddRange(items.ToArray()); } return(editControl); } return(null); }
/// <summary> /// Returns the field's current value(s) /// </summary> /// <param name="parentControl">The parent control.</param> /// <param name="value">Information about the value</param> /// <param name="configurationValues">The configuration values.</param> /// <param name="condensed">Flag indicating if the value should be condensed (i.e. for use in a grid column)</param> /// <returns></returns> public override string FormatValue(System.Web.UI.Control parentControl, string value, Dictionary <string, ConfigurationValue> configurationValues, bool condensed) { if (string.IsNullOrWhiteSpace(value)) { return(base.FormatValue(parentControl, value, configurationValues, condensed)); } // This is a list of IDs, we'll want it to be document type names instead var selectedValues = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList(); return(DocumentTypeCache.All() .Where(v => selectedValues.Contains(v.Id)) .Select(v => v.Name) .ToList() .AsDelimited(", "));; }
/// <summary> /// Creates the control(s) necessary for prompting user for a new value /// </summary> /// <param name="configurationValues">The configuration values.</param> /// <param name="id"></param> /// <returns> /// The control /// </returns> public override Control EditControl(Dictionary <string, ConfigurationValue> configurationValues, string id) { RockListBox editControl = new RockListBox { ID = id, DisplayDropAsAbsolute = true }; var values = DocumentTypeCache.All().Select(v => new { v.Id, v.Name }).OrderBy(v => v.Name).ToList(); foreach (var value in values) { editControl.Items.Add(new ListItem(value.Name, value.Id.ToString())); } if (editControl.Items.Count > 0) { return(editControl); } return(null); }
private List<DocumentData> GetBinaryFiles() { Guid binaryFileTypeGuid = binaryFileType != null ? binaryFileType.Guid : Guid.NewGuid(); RockContext context = new RockContext(); var binaryFileService = new BinaryFileService( context ); var attributeService = new AttributeService( context ); var attributeValueService = new AttributeValueService( context ); var personAliasService = new PersonAliasService( context ); var personService = new PersonService( context ); var documentService = new DocumentService( context ); // If the document type is not set if ( string.IsNullOrWhiteSpace(GetAttributeValue( "DocumentType" ) ) ) { return null; } int documentTypeId = DocumentTypeCache.Get( GetAttributeValue( "DocumentType" ).AsGuid() ).Id; var documentQuery = documentService.Queryable( "BinaryFile" ).Where( d => d.DocumentTypeId == documentTypeId ); // Add any query filters here string name = fBinaryFile.GetUserPreference( "File Name" ); if ( !string.IsNullOrWhiteSpace( name ) ) { documentQuery = documentQuery.Where( d => d.BinaryFile.FileName.Contains( name ) ); } var personQuery = personService.Queryable( true ); // Filter for a specific Person if ( ppPerson.SelectedValue.HasValue && ppPerson.SelectedValue.Value > 0 ) { string givingId = personService.Queryable().Where(a => a.Id == ppPerson.SelectedValue ).Select(p => p.GivingId ).FirstOrDefault(); personQuery = personQuery.Where( p => p.GivingId == givingId ); } var documents = documentQuery .Join( personQuery, obj => obj.EntityId, p => p.Id, ( obj, p ) => new { Document = obj, Person = p } ) .GroupBy(obj => obj.Document.BinaryFile.Id); List<DocumentData> list = documents.Select(d => new DocumentData() { BinaryFile = d.FirstOrDefault().Document.BinaryFile, People = d.Select( p => p.Person ).ToList() }).ToList(); List<Guid?> dataviews = GetAttributeValue( "PrintAndMail" ).SplitDelimitedValues().AsGuidOrNullList(); if ( list.Count() > 0 ) { if ( dataviews != null && dataviews.Count > 0 ) { var dataViewService = new DataViewService( context ); foreach ( var dataviewguid in dataviews ) { List<string> errorMessages = new List<string>(); list.FirstOrDefault().MailPersonIds.AddRange( dataViewService.Get( dataviewguid.Value ).GetQuery( null, null, out errorMessages ).OfType<Rock.Model.Person>().Select( p => p.Id ).ToList() ); } } } // Apply the Statement Delivery Preference filter if ( cbDeliveryPreference.SelectedValue == "Electronic" ) { list = list.Where( l => !l.MailPersonIds.Intersect( l.People.Select( p => p.Id ) ).Any() ).ToList(); } else if ( cbDeliveryPreference.SelectedValue == "Print & Mail" ) { list = list.Where( l => l.MailPersonIds.Intersect( l.People.Select( p => p.Id ) ).Any() ).ToList(); } var sortProperty = gBinaryFile.SortProperty; // Sort by Person Name if ( sortProperty != null && sortProperty.Property == "PersonNames" ) { if ( sortProperty.Direction == SortDirection.Ascending ) { list = list.OrderBy( l => l.People.FirstOrDefault().LastName ).ToList(); } else { list = list.OrderByDescending( l => l.People.FirstOrDefault().LastName ).ToList(); } } // Sort by Person Name else if ( sortProperty != null && sortProperty.Property == "GivingId" ) { if ( sortProperty.Direction == SortDirection.Ascending ) { list = list.OrderBy( l => l.People.FirstOrDefault().GivingId ).ToList(); } else { list = list.OrderByDescending( l => l.People.FirstOrDefault().GivingId ).ToList(); } } // Sort by Delivery Preference else if ( sortProperty != null && sortProperty.Property == "StatementDelivery" ) { if ( sortProperty.Direction == SortDirection.Ascending ) { list = list.OrderBy( l => l.StatementDelivery ).ToList(); } else { list = list.OrderByDescending( l => l.StatementDelivery ).ToList(); } } // Sort by LastModified else if ( sortProperty != null && sortProperty.Property == "ModifiedDateTime" ) { if ( sortProperty.Direction == SortDirection.Ascending ) { list = list.OrderBy( l => l.BinaryFile.ModifiedDateTime ).ToList(); } else { list = list.OrderByDescending( l => l.BinaryFile.ModifiedDateTime ).ToList(); } } // Sort by Giving Id else { if ( sortProperty == null || sortProperty.Direction == SortDirection.Ascending ) { list = list.OrderBy( d => d.BinaryFile.FileName ).ToList(); } else { list = list.OrderByDescending( d => d.BinaryFile.FileName ).ToList(); } } return list; }
/// <summary> /// Executes the specified workflow. /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="action">The action.</param> /// <param name="entity">The entity.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages) { errorMessages = new List <string>(); // Get the entity type EntityTypeCache entityType = null; var entityTypeGuid = GetAttributeValue(action, AttributeKey.EntityType).AsGuidOrNull(); if (entityTypeGuid.HasValue) { entityType = EntityTypeCache.Get(entityTypeGuid.Value); } if (entityType == null) { var message = $"Entity Type could not be found for selected value ('{entityTypeGuid.ToString()}')!"; errorMessages.Add(message); action.AddLogEntry(message, true); return(false); } var mergeFields = GetMergeFields(action); var _rockContext = new RockContext(); // Get the entity var entityTypeService = new EntityTypeService(_rockContext); IEntity entityObject = null; var entityIdGuidString = GetAttributeValue(action, AttributeKey.EntityIdOrGuid, true).ResolveMergeFields(mergeFields).Trim(); var entityId = entityIdGuidString.AsIntegerOrNull(); if (entityId.HasValue) { entityObject = entityTypeService.GetEntity(entityType.Id, entityId.Value); } else { var entityGuid = entityIdGuidString.AsGuidOrNull(); if (entityGuid.HasValue) { entityObject = entityTypeService.GetEntity(entityType.Id, entityGuid.Value); } } if (entityObject == null) { var value = GetActionAttributeValue(action, AttributeKey.EntityIdOrGuid); entityObject = action.GetEntityFromAttributeValue(value, rockContext); } if (entityObject == null) { var message = $"Entity could not be found for selected value ('{entityIdGuidString}')!"; errorMessages.Add(message); action.AddLogEntry(message, true); return(false); } var attributeFilteredDocumentType = GetAttributeValue(action, AttributeKey.DocumentType).Split(',').Select(int.Parse).FirstOrDefault(); var documentType = DocumentTypeCache.Get(attributeFilteredDocumentType); if (documentType == null) { var message = $"Document Type could not be found for selected value ('{attributeFilteredDocumentType}')!"; errorMessages.Add(message); action.AddLogEntry(message, true); return(false); } var documentypesForContextEntityType = DocumentTypeCache.GetByEntity(entityType.Id, true); if (!documentypesForContextEntityType.Any(d => attributeFilteredDocumentType == d.Id)) { var message = "The Document Type does not match the selected entity type."; errorMessages.Add(message); action.AddLogEntry(message, true); return(false); } var isDocumentTypeValid = IsDocumentTypeValidForEntity(entityObject, documentType); if (!isDocumentTypeValid) { var message = "The Document Type selected is not valid for the entity."; errorMessages.Add(message); action.AddLogEntry(message, true); return(false); } var binaryFile = new BinaryFileService(rockContext).Get(GetAttributeValue(action, AttributeKey.DocumentAttribute, true).AsGuid()); if (binaryFile == null) { action.AddLogEntry("The document to add to the entity was not found.", true); // returning true here to allow the action to run 'successfully' without a document. // This allows the action to be easily used when the document is optional without a bunch of action filter tests. return(true); } var documentName = GetAttributeValue(action, AttributeKey.DocumentName).ResolveMergeFields(mergeFields); if (documentName.IsNullOrWhiteSpace()) { documentName = Path.GetFileNameWithoutExtension(binaryFile.FileName); } var document = new Document(); document.Name = documentName; document.Description = GetAttributeValue(action, AttributeKey.DocumentDescription).ResolveMergeFields(mergeFields); document.EntityId = entityObject.Id; document.DocumentTypeId = documentType.Id; document.SetBinaryFile(binaryFile.Id, rockContext); if (!document.IsValidDocument(rockContext, out string errorMessage)) { errorMessages.Add(errorMessage); action.AddLogEntry(errorMessage, true); return(false); } var documentService = new DocumentService(rockContext); documentService.Add(document); rockContext.SaveChanges(); action.AddLogEntry("Added document to the Entity."); return(true); }
/// <summary> /// Updates any Cache Objects that are associated with this entity /// </summary> /// <param name="entityState">State of the entity.</param> /// <param name="dbContext">The database context.</param> public void UpdateCache( EntityState entityState, Rock.Data.DbContext dbContext ) { DocumentTypeCache.UpdateCachedEntity( this.Id, entityState ); DocumentTypeCache.RemoveEntityDocumentTypes(); }
/// <summary> /// Gets the cache object associated with this Entity /// </summary> /// <returns></returns> public IEntityCache GetCacheObject() { return DocumentTypeCache.Get( this.Id ); }