private void SetFormValues(Dictionary <string, string> body)
        {
            if (_workflow != null && _actionType != null)
            {
                var form = _actionType.WorkflowForm;

                var values = new Dictionary <int, string>();
                foreach (var formAttribute in form.FormAttributes.OrderBy(a => a.Order))
                {
                    if (formAttribute.IsVisible && !formAttribute.IsReadOnly)
                    {
                        var attribute = AttributeCache.Read(formAttribute.AttributeId);

                        if (attribute != null && body.ContainsKey(attribute.Key))
                        {
                            IHasAttributes item = null;
                            if (attribute.EntityTypeId == _workflow.TypeId)
                            {
                                item = _workflow;
                            }
                            else if (attribute.EntityTypeId == _activity.TypeId)
                            {
                                item = _activity;
                            }

                            if (item != null)
                            {
                                var convertedValue = attribute.FieldType.Field.DecodeAttributeValue(attribute, body[attribute.Key]);
                                item.SetAttributeValue(attribute.Key, convertedValue);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Gets the custom settings control. The returned control will be added to the parent automatically.
        /// </summary>
        /// <param name="attributeEntity">The attribute entity.</param>
        /// <param name="parent">The parent control that will eventually contain the returned control.</param>
        /// <returns>
        /// A control that contains all the custom UI.
        /// </returns>
        public override Control GetCustomSettingsControl(IHasAttributes attributeEntity, Control parent)
        {
            var control = parent.LoadControl(UserControlPath);

            control.ClientIDMode = ClientIDMode.AutoID;

            return(control);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the client attribute values.
        /// </summary>
        /// <returns></returns>
        private List <ClientAttributeValueViewModel> GetClientAttributeValues(IHasAttributes entity)
        {
            var categoryGuids = GetCategoryGuids();

            return(entity.GetClientAttributeValues(RequestContext.CurrentPerson)
                   .Where(av => categoryGuids.Count == 0 || categoryGuids.Any(g => av.Categories.FirstOrDefault(c => c.Guid == g) != null))
                   .ToList());
        }
        /// <summary>
        /// Update the entity with values from the custom UI.
        /// </summary>
        /// <param name="attributeEntity">The attribute entity.</param>
        /// <param name="control">The control returned by the GetCustomSettingsControl() method.</param>
        /// <param name="rockContext">The rock context to use when accessing the database.</param>
        /// <exception cref="InvalidOperationException">Custom settings user control does not implement {nameof( IRockCustomSettingsUserControl )}</exception>
        /// <remarks>
        /// Do not save the entity, it will be automatically saved later. This call will be made inside
        /// a SQL transaction for the passed rockContext. If you need to make changes to the database
        /// do so on this context so they can be rolled back if something fails during the final save.
        /// </remarks>
        public override void WriteSettingsToEntity(IHasAttributes attributeEntity, Control control, RockContext rockContext)
        {
            if (!(control is IRockCustomSettingsUserControl customSettingsControl))
            {
                throw new InvalidOperationException($"Custom settings user control does not implement {nameof( IRockCustomSettingsUserControl )}");
            }

            customSettingsControl.WriteSettingsToEntity(attributeEntity, rockContext);
        }
        /// <summary>
        /// Retrieves a property value of an attribute of a given type (non-reference)
        /// </summary>
        /// <typeparam name="Y">The type of the property to return</typeparam>
        /// <param name="o">The object to check</param>
        /// <param name="t">The type of the attribute to search for</param>
        /// <param name="PropertyName">The name of the property to retrieve the value for</param>
        /// <param name="Default">If the property is not found, this is the default to return in place of null</param>
        /// <returns>Either the casted property, or default</returns>
        public static Y AttributeStruct <Y>(this IHasAttributes o, Type t, string PropertyName, Y Default) where Y : struct
        {
            if (!o.HasAttribute(t) || !o.Attribute(t).HasProperty(PropertyName))
            {
                return(Default);
            }

            return(o.Attribute(t).GetProperty(PropertyName).GetValue <Y>());
        }
        /// <summary>
        /// Gets an attribute of a given type and returns a nullable? of its value
        /// </summary>
        /// <typeparam name="X">The type of the attribute</typeparam>
        /// <typeparam name="Y">The type of the value to return</typeparam>
        /// <param name="o">The object to check</param>
        /// <param name="PropertyName">The property name to look for</param>
        /// <returns>A nullable struct representation of its value</returns>
        public static Y?AttributeNullable <X, Y>(this IHasAttributes o, string PropertyName) where Y : struct
        {
            if (!o.HasAttribute <X>() || !o.Attribute <X>().HasProperty(PropertyName))
            {
                return(null);
            }

            return(o.Attribute <X>().GetProperty(PropertyName).GetValue <Y>());
        }
        /// <summary>
        /// Returns an attribute instance of a specified type
        /// </summary>
        /// <param name="o">Type of the object to check for attributes</param>
        /// <param name="t">The type of the attribute to check for</param>
        /// <returns>The attribute, or null if not found</returns>
        public static IMetaObject Attribute(this IHasAttributes o, Type t)
        {
            if (o is null)
            {
                throw new ArgumentNullException(nameof(o));
            }

            return(o.Attributes.FirstOrDefault(a => a.Type.FullName == t.FullName)?.Instance);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets the attributes keys that are valid given our category configuration.
        /// </summary>
        /// <returns></returns>
        private List <string> GetCategoryAttributeKeys(IHasAttributes entity)
        {
            var categories = GetCategoryGuids();

            return(entity.Attributes.Values
                   .Where(a => a.Categories.Any(c => categories.Contains(c.Guid)))
                   .Select(a => a.Key)
                   .ToList());
        }
        /// <summary>
        /// Gets an attribute of a given type and then casts and returns a specified value
        /// </summary>
        /// <typeparam name="X">The type of the attribute</typeparam>
        /// <typeparam name="Y">The type of the value to return</typeparam>
        /// <param name="o">The object to check</param>
        /// <param name="PropertyName">The property name to look for</param>
        /// <param name="Default">Default to return if property or attribute does not exist</param>
        /// <returns>Either the casted property, or default</returns>
        public static Y AttributeRef <X, Y>(this IHasAttributes o, string PropertyName, Y Default) where Y : class
        {
            if (!o.HasAttribute <X>() || !o.Attribute <X>().HasProperty(PropertyName))
            {
                return(Default);
            }

            return(o.Attribute <X>().GetProperty(PropertyName).GetValue <Y>() ?? Default);
        }
        /// <summary>
        /// Update the custom UI to reflect the current settings found in the entity.
        /// </summary>
        /// <param name="attributeEntity">The attribute entity.</param>
        /// <param name="control">The control returned by GetCustomSettingsControl().</param>
        /// <exception cref="InvalidOperationException">Custom settings user control does not implement {nameof( IRockCustomSettingsUserControl )}</exception>
        public override void ReadSettingsFromEntity(IHasAttributes attributeEntity, Control control)
        {
            if (!(control is IRockCustomSettingsUserControl customSettingsControl))
            {
                throw new InvalidOperationException($"Custom settings user control does not implement {nameof( IRockCustomSettingsUserControl )}");
            }

            customSettingsControl.ReadSettingsFromEntity(attributeEntity);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Gets the row value.
        /// </summary>
        /// <param name="row">The row.</param>
        /// <param name="condensed">if set to <c>true</c> [condensed].</param>
        /// <param name="formatAsHtml">if set to <c>true</c> [format as HTML].</param>
        /// <returns></returns>
        private object GetRowValue(GridViewRow row, bool condensed, bool formatAsHtml)
        {
            // First try to get an IHasAttributes from the grid's object list
            IHasAttributes dataItem = GetAttributeObject(row);

            if (dataItem == null)
            {
                // If unsuccessful, check to see if row has attributes
                dataItem = row.DataItem as IHasAttributes;
            }

            if (dataItem != null)
            {
                if (dataItem.Attributes == null)
                {
                    dataItem.LoadAttributes();
                }

                AttributeCache attrib   = null;
                string         rawValue = string.Empty;

                bool exists = dataItem.Attributes.ContainsKey(this.DataField);
                if (exists)
                {
                    attrib   = dataItem.Attributes[this.DataField];
                    rawValue = dataItem.GetAttributeValue(this.DataField);
                }
                else
                {
                    if (AttributeId.HasValue)
                    {
                        attrib = dataItem.Attributes.Where(a => a.Value.Id == AttributeId.Value).Select(a => a.Value).FirstOrDefault();
                        if (attrib != null)
                        {
                            exists   = true;
                            rawValue = dataItem.GetAttributeValue(attrib.Key);
                        }
                    }
                }

                if (exists)
                {
                    if (formatAsHtml)
                    {
                        string resultHtml = attrib.FieldType.Field.FormatValueAsHtml(null, rawValue, attrib.QualifierValues, condensed);
                        return(new HtmlString(resultHtml ?? string.Empty));
                    }
                    else
                    {
                        string result = attrib.FieldType.Field.FormatValue(null, rawValue, attrib.QualifierValues, condensed);
                        return(result ?? string.Empty);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Saves the attribute values.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="personId">The person id.</param>
 public static void SaveAttributeValues(IHasAttributes model, int?personId)
 {
     foreach (var category in model.Attributes)
     {
         foreach (var attribute in category.Value)
         {
             SaveAttributeValues(model, attribute, model.AttributeValues[attribute.Key].Value, personId);
         }
     }
 }
Exemplo n.º 13
0
 /// <summary>
 /// Saves the specified attribute values to the database.
 /// </summary>
 /// <param name="entity">The entity.</param>
 /// <param name="keys">The attribute keys.</param>
 /// <param name="rockContext">The rock context.</param>
 public static void SaveAttributeValues(this IHasAttributes entity, IEnumerable <string> keys, RockContext rockContext = null)
 {
     foreach (var key in keys)
     {
         if (entity.AttributeValues.ContainsKey(key))
         {
             Attribute.Helper.SaveAttributeValue(entity, entity.Attributes[key], entity.AttributeValues[key].Value, rockContext);
         }
     }
 }
        /// <summary>
        /// Shows the read-only attribute values.
        /// </summary>
        protected void ShowDetails()
        {
            IHasAttributes entity = ContextEntity() as IHasAttributes;

            if (entity != null)
            {
                phAttributes.Controls.Clear();
                Rock.Attribute.Helper.AddDisplayControls(entity, phAttributes, null, false, false);
            }
        }
        /// <summary>
        /// Shows the edit attributes controls.
        /// </summary>
        /// <param name="setValues">If true then the values are set from the database, otherwise the postback values will be used.</param>
        protected void ShowEdit(bool setValues)
        {
            IHasAttributes entity = ContextEntity() as IHasAttributes;

            if (entity != null)
            {
                phEditAttributes.Controls.Clear();
                Rock.Attribute.Helper.AddEditControls(entity, phEditAttributes, true, BlockValidationGroup);
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Gets the display HTML.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="parentControl">The parent control.</param>
        /// <param name="exclude">The exclude.</param>
        /// <param name="supressOrdering">if set to <c>true</c> supresses reording (LoadAttributes() may perform custom ordering as is the case for group member attributes).</param>
        public static void AddDisplayControls(IHasAttributes item, Control parentControl, List <string> exclude = null, bool supressOrdering = false)
        {
            exclude = exclude ?? new List <string>();
            string result = string.Empty;

            if (item.Attributes != null)
            {
                AddDisplayControls(item, GetAttributeCategories(item, false, supressOrdering), parentControl, exclude);
            }
        }
Exemplo n.º 17
0
        private bool HandleAttribute(IAttribute attribute, IHasAttributes item, MetadataContextStack contextStack, List <IDom> newList, ref IDom lastPart)
        {
            var value = Helper.GetBestMetadata <CodeFirstMetadata>(attribute, contextStack);
            var targetHasStructuredDocs = item as IHasStructuredDocumentation;

            if (value != null && targetHasStructuredDocs != null && !string.IsNullOrWhiteSpace(value.XmlCommentString))
            {
                // targetHasStructuredDocs.StructuredDocumentation = value.XmlCommentString ;
            }
            return(false);
        }
Exemplo n.º 18
0
        public EditAttributes(IHasAttributes <T> attributeContainer)
        {
            InitializeComponent();

            _attributeContainer               = attributeContainer;
            _editAttributesControl            = new Controls.EditAttributes <T>();
            _editAttributesControl.Attributes = _attributeContainer.Attributes;
            _editAttributesControl.Dock       = DockStyle.Fill;

            editAttributesPanel.Controls.Add(_editAttributesControl);
        }
        /// <summary>
        /// Handles the Click event of the 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 btnSave_Click(object sender, EventArgs e)
        {
            pnlView.Visible = true;
            pnlEdit.Visible = false;

            IHasAttributes entity = ContextEntity() as IHasAttributes;

            Rock.Attribute.Helper.GetEditValues(phEditAttributes, entity);
            entity.SaveAttributeValues();

            ShowDetails();
        }
        /// <summary>
        /// Initialize basic information about the page structure and setup the default content.
        /// </summary>
        /// <param name="sender">Object that is generating this event.</param>
        /// <param name="e">Arguments that describe this event.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            IHasAttributes entity = ContextEntity() as IHasAttributes;

            if (entity == null || entity.Attributes.Count == 0)
            {
                return;
            }
            else
            {
                ShowDetails();
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// Update the custom UI to reflect the current settings found in the entity.
        /// </summary>
        /// <param name="attributeEntity">The attribute entity.</param>
        public void ReadSettingsFromEntity(IHasAttributes attributeEntity)
        {
            var mobileBlock        = ( BlockCache )attributeEntity;
            var additionalSettings = mobileBlock.AdditionalSettings.FromJsonOrNull <AdditionalBlockSettings>() ?? new AdditionalBlockSettings();

            nbCacheDuration.Text          = additionalSettings.CacheDuration.ToString();
            cbProcessLavaOnServer.Checked = additionalSettings.ProcessLavaOnServer;
            cbProcessLavaOnClient.Checked = additionalSettings.ProcessLavaOnClient;
            cbShowOnTablet.Checked        = additionalSettings.ShowOnTablet;
            cbShowOnPhone.Checked         = additionalSettings.ShowOnPhone;
            cbRequiresNetwork.Checked     = additionalSettings.RequiresNetwork;
            ceNoNetworkContent.Text       = additionalSettings.NoNetworkContent;
        }
Exemplo n.º 22
0
        private void ApplyAuraEffect(IHasAttributes target, AuraEffect auraEffect)
        {
            var attributeEffect = auraEffect.AttributeEffect;
            var attributeName   = attributeEffect.AffectedAttributeName;

            if (!target.HasAttribute(attributeName))
            {
                return;
            }

            target.GetAttribute(attributeName).AddAttributeEffect(attributeEffect);
            _affectedAuraTargets.Add(target);
        }
Exemplo n.º 23
0
 /// <summary>
 /// Saves the attribute values.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="rockContext">The rock context.</param>
 /// <remarks>
 /// If a rockContext value is included, this method will save any previous changes made to the context
 /// </remarks>
 public static void SaveAttributeValues(IHasAttributes model, RockContext rockContext = null)
 {
     if (model != null && model.Attributes != null && model.AttributeValues != null)
     {
         foreach (var attribute in model.Attributes)
         {
             if (model.AttributeValues.ContainsKey(attribute.Key))
             {
                 SaveAttributeValue(model, attribute.Value, model.AttributeValues[attribute.Key].Value, rockContext);
             }
         }
     }
 }
Exemplo n.º 24
0
 /// <summary>
 /// Adds edit controls for each of the item's attributes
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="parentControl">The parent control.</param>
 /// <param name="setValue">if set to <c>true</c> [set value].</param>
 /// <param name="validationGroup">The validation group.</param>
 /// <param name="exclude">List of attribute names not to render</param>
 /// <param name="supressOrdering">if set to <c>true</c> supresses reording (LoadAttributes() may perform custom ordering as is the case for group member attributes).</param>
 public static void AddEditControls(IHasAttributes item, Control parentControl, bool setValue, string validationGroup, List <string> exclude, bool supressOrdering = false)
 {
     if (item.Attributes != null)
     {
         foreach (var attributeCategory in GetAttributeCategories(item, false, false, supressOrdering))
         {
             AddEditControls(
                 attributeCategory.Category != null ? attributeCategory.Category.Name : string.Empty,
                 attributeCategory.Attributes.Select(a => a.Key).ToList(),
                 item, parentControl, validationGroup, setValue, exclude);
         }
     }
 }
Exemplo n.º 25
0
        /// <summary>
        /// Builds the attribute fields.
        /// </summary>
        /// <param name="entity">The entity whose attribute values are going to be edited.</param>
        /// <param name="attributes">The attributes to be displayed.</param>
        /// <param name="postbackParameters">The postback parameters to request, key is node name and value is property path.</param>
        /// <param name="includeHeader">if set to <c>true</c> [include header].</param>
        /// <param name="person">If not null then security will be enforced for this person.</param>
        /// <returns>
        /// A XAML string that contains any attribute fields as well as the header text.
        /// </returns>
        public static string GetEditAttributesXaml(IHasAttributes entity, List <AttributeCache> attributes = null, Dictionary <string, string> postbackParameters = null, bool includeHeader = true, Person person = null)
        {
            if (entity.Attributes == null)
            {
                entity.LoadAttributes();
            }

            attributes = attributes ?? entity.Attributes.Values.ToList();

            if (!attributes.Any())
            {
                return(string.Empty);
            }

            var sb = new StringBuilder();

            sb.AppendLine("<StackLayout Spacing=\"0\">");

            if (includeHeader)
            {
                sb.AppendLine("<Label Text=\"Attributes\" StyleClass=\"h1\" />");
                sb.AppendLine("<BoxView Color=\"#888\" HeightRequest=\"1\" Margin=\"0 0 12 0\" />");
            }

            foreach (var attribute in attributes)
            {
                var label = attribute.AbbreviatedName.IsNotNullOrWhiteSpace() ? attribute.AbbreviatedName : attribute.Name;

                if (person != null && !attribute.IsAuthorized(Authorization.EDIT, person))
                {
                    if (person == null || attribute.IsAuthorized(Authorization.VIEW, person))
                    {
                        sb.AppendLine(GetReadOnlyFieldXaml(label, ""));
                    }
                }
                else
                {
                    var fieldName           = $"attribute_{attribute.Id}";
                    var configurationValues = attribute.QualifierValues
                                              .ToDictionary(a => a.Key, a => a.Value.Value)
                                              .ToJson();

                    sb.AppendLine(GetSingleFieldXaml($"<Rock:AttributeValueEditor x:Name=\"{fieldName}\" Label=\"{label}\" IsRequired=\"{attribute.IsRequired}\" FieldType=\"{attribute.FieldType.Class}\" ConfigurationValues=\"{{}}{configurationValues.EncodeXml( true )}\" Value=\"{entity.GetAttributeValue( attribute.Key ).EncodeXml( true )}\" />"));
                    postbackParameters.Add(fieldName, "Value");
                }
            }

            sb.AppendLine("</StackLayout>");

            return(sb.ToString());
        }
Exemplo n.º 26
0
        public virtual HttpResponseMessage SetAttributeValue(int id, string attributeKey, string attributeValue)
        {
            T model;

            if (!Service.TryGet(id, out model))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            CheckCanEdit(model);

            IHasAttributes modelWithAttributes = model as IHasAttributes;

            if (modelWithAttributes != null)
            {
                using (var rockContext = new RockContext())
                {
                    modelWithAttributes.LoadAttributes(rockContext);
                    Rock.Web.Cache.AttributeCache attributeCache = modelWithAttributes.Attributes.ContainsKey(attributeKey) ? modelWithAttributes.Attributes[attributeKey] : null;

                    if (attributeCache != null)
                    {
                        if (!attributeCache.IsAuthorized(Rock.Security.Authorization.EDIT, this.GetPerson()))
                        {
                            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden)
                            {
                                ReasonPhrase = string.Format("Not authorized to edit {0} on {1}", modelWithAttributes.GetType().GetFriendlyTypeName(), attributeKey)
                            });
                        }

                        Rock.Attribute.Helper.SaveAttributeValue(modelWithAttributes, attributeCache, attributeValue, rockContext);
                        var response = ControllerContext.Request.CreateResponse(HttpStatusCode.Accepted, modelWithAttributes.Id);
                        return(response);
                    }
                    else
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                        {
                            ReasonPhrase = string.Format("{0} does not have a {1} attribute", modelWithAttributes.GetType().GetFriendlyTypeName(), attributeKey)
                        });
                    }
                }
            }
            else
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    ReasonPhrase = "specified item does not have attributes"
                });
            }
        }
        private void HandleAttributes(IHasAttributes newItemHasAttributes, MetadataContextStack contextStack)
        {
            if (newItemHasAttributes == null)
            {
                return;
            }
            var attributes   = newItemHasAttributes.Attributes;
            var xfAttributes = attributes.Where(x => x.Name.StartsWith("_xf_.")).ToList();

            foreach (var attribute in xfAttributes)
            {
                var name = attribute.Name.SubstringAfter("_xf_.");
            }
        }
Exemplo n.º 28
0
        private void AddAttribute <T>(IHasAttributes <T> container, string key, string value = null)
            where T : IProjectSchemaElement
        {
            Attribute <T> attribute = container.Attributes.SingleOrDefault(o => string.Equals(key, o.Key, StringComparison.InvariantCultureIgnoreCase));

            if (attribute == null)
            {
                container.Attributes.Add(new Attribute <T>(key, value));
            }
            else
            {
                attribute.Value = value;
            }
        }
        private bool HandleAttribute(IAttribute attribute, IHasAttributes item, MetadataContextStack contextStack, List <IDom> newList, ref IDom lastPart)
        {
            if (item == null)
            {
                return(false);
            }
            var value = Helper.GetBestMetadata <IHasAttributes>(attribute, contextStack);

            if (value != null)
            {
                item.Attributes.AddOrMoveAttributeRange(value.Attributes);
            }
            return(false);
        }
Exemplo n.º 30
0
        /// <summary>
        /// Gets the attribute categories.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="onlyIncludeGridColumns">if set to <c>true</c> will only include those attributes with the option to display in grid set to true</param>
        /// <param name="allowMultiple">if set to <c>true</c> returns the attribute in each of it's categories, if false, only returns attribut in first category.</param>
        /// <param name="supressOrdering">if set to <c>true</c> supresses reording (LoadAttributes() may perform custom ordering as is the case for group member attributes).</param>
        /// <returns></returns>
        public static List <AttributeCategory> GetAttributeCategories(IHasAttributes entity, bool onlyIncludeGridColumns = false, bool allowMultiple = false, bool supressOrdering = false)
        {
            if (entity != null)
            {
                var attributes = entity.Attributes.Select(a => a.Value);
                if (!supressOrdering)
                {
                    attributes = attributes.OrderBy(t => t.Order).ThenBy(t => t.Name);
                }

                return(GetAttributeCategories(attributes.ToList(), onlyIncludeGridColumns, allowMultiple));
            }

            return(null);
        }
Exemplo n.º 31
0
        /// <summary>
        /// Gets the edit values.
        /// </summary>
        /// <param name="parentControl">The parent control.</param>
        /// <param name="item">The item.</param>
        public static void GetEditValues( Control parentControl, IHasAttributes item )
        {
            if ( item.Attributes != null )
            {
                foreach ( var attribute in item.Attributes )
                {
                    Control control = parentControl.FindControl( string.Format( "attribute_field_{0}", attribute.Value.Id ) );
                    if ( control != null )
                    {
                        var value = new AttributeValue();

                        // Creating a brand new AttributeValue and setting its Value property.
                        // The Value prop's setter then queries the AttributeCache passing in the AttributeId, which is 0
                        // The AttributeCache.Read method returns null
                        value.Value = attribute.Value.FieldType.Field.GetEditValue( control, attribute.Value.QualifierValues );
                        item.AttributeValues[attribute.Key] = value;
                    }
                }
            }
        }
Exemplo n.º 32
0
        /// <summary>
        /// Copies the attributes from one entity to another
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="target">The target.</param>
        public static void CopyAttributes( IHasAttributes source, IHasAttributes target )
        {
            // Copy Attributes
            if ( source.Attributes != null )
            {
                target.Attributes = new Dictionary<string, Web.Cache.AttributeCache>();
                foreach ( var item in source.Attributes )
                {
                    target.Attributes.Add( item.Key, item.Value );
                }
            }
            else
            {
                target.Attributes = null;
            }

            // Copy Attribute Values
            if ( source.AttributeValues != null )
            {
                target.AttributeValues = new Dictionary<string, List<Model.AttributeValue>>();
                foreach ( var item in source.AttributeValues )
                {
                    var list = new List<Model.AttributeValue>();
                    foreach ( var value in item.Value )
                    {
                        var attributeValue = new Model.AttributeValue();
                        attributeValue.IsSystem = value.IsSystem;
                        attributeValue.AttributeId = value.AttributeId;
                        attributeValue.EntityId = value.EntityId;
                        attributeValue.Order = value.Order;
                        attributeValue.Value = value.Value;
                        attributeValue.Id = value.Id;
                        attributeValue.Guid = value.Guid;
                        list.Add( attributeValue );
                    }
                    target.AttributeValues.Add( item.Key, list );
                }
            }
            else
            {
                target.AttributeValues = null;
            }

        }
Exemplo n.º 33
0
 /// <summary>
 /// Displays the edit attributes.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="displayedAttributeGuids">The displayed attribute guids.</param>
 /// <param name="phAttributes">The ph attributes.</param>
 /// <param name="pnlAttributes">The PNL attributes.</param>
 private void DisplayEditAttributes( IHasAttributes item, List<Guid> displayedAttributeGuids, PlaceHolder phAttributes, Panel pnlAttributes, bool setValue )
 {
     phAttributes.Controls.Clear();
     item.LoadAttributes();
     var excludedAttributeList = item.Attributes.Where( a => !displayedAttributeGuids.Contains( a.Value.Guid ) ).Select( a => a.Value.Key ).ToList();
     if ( item.Attributes != null && item.Attributes.Any() && displayedAttributeGuids.Any() )
     {
         pnlAttributes.Visible = true;
         Rock.Attribute.Helper.AddEditControls( item, phAttributes, setValue, BlockValidationGroup, excludedAttributeList, false, 2 );
     }
     else
     {
         pnlAttributes.Visible = false;
     }
 }
Exemplo n.º 34
0
        /// <summary>
        /// Adds the edit controls.
        /// </summary>
        /// <param name="category">The category.</param>
        /// <param name="attributeKeys">The attribute keys.</param>
        /// <param name="item">The item.</param>
        /// <param name="parentControl">The parent control.</param>
        /// <param name="validationGroup">The validation group.</param>
        /// <param name="setValue">if set to <c>true</c> [set value].</param>
        /// <param name="exclude">The exclude.</param>
        public static void AddEditControls( string category, List<string> attributeKeys, IHasAttributes item, Control parentControl, string validationGroup, bool setValue, List<string> exclude )
        {
            HtmlGenericControl fieldSet = new HtmlGenericControl( "fieldset" );
            parentControl.Controls.Add( fieldSet );
            fieldSet.Controls.Clear();

            if ( !string.IsNullOrEmpty( category ) )
            {
                HtmlGenericControl legend = new HtmlGenericControl( "h4" );
                fieldSet.Controls.Add( legend );
                legend.Controls.Clear();
                legend.InnerText = category.Trim();
            }

            foreach ( string key in attributeKeys )
            {
                var attribute = item.Attributes[key];

                if ( !exclude.Contains( attribute.Name ) )
                {
                    // Add the control for editing the attribute value
                    attribute.AddControl( fieldSet.Controls, item.AttributeValues[attribute.Key].Value, validationGroup, setValue, true );
                }
            }
        }
Exemplo n.º 35
0
        /// <summary>
        /// Adds the indexable attributes.
        /// </summary>
        /// <param name="indexModel">The index model.</param>
        /// <param name="sourceModel">The source model.</param>
        protected static void AddIndexableAttributes( IndexModelBase indexModel, IHasAttributes sourceModel )
        {
            sourceModel.LoadAttributes();

            foreach ( var attributeValue in sourceModel.AttributeValues )
            {
                // check that the attribute is marked as IsIndexEnabled
                var attribute = AttributeCache.Read(attributeValue.Value.AttributeId);

                if ( attribute.IsIndexEnabled )
                {

                    var key = attributeValue.Key;

                    // remove invalid characters
                    key = key.Replace( ".", "_" );
                    key = key.Replace( ",", "_" );
                    key = key.Replace( "#", "_" );
                    key = key.Replace( "*", "_" );
                    key = key.StartsWith( "_" ) ? key.Substring( 1 ) : key;

                    indexModel[key] = attributeValue.Value.ValueFormatted;
                }
            }
        }
Exemplo n.º 36
0
        /// <summary>
        /// Saves an attribute value.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="newValue">The new value.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <remarks>
        /// If a rockContext value is included, this method will save any previous changes made to the context
        /// </remarks>
        public static void SaveAttributeValue( IHasAttributes model, Rock.Web.Cache.AttributeCache attribute, string newValue, RockContext rockContext = null )
        {
            if ( model != null && attribute != null )
            {
                rockContext = rockContext ?? new RockContext();
                var attributeValueService = new Model.AttributeValueService( rockContext );

                var attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, model.Id );
                if ( attributeValue == null )
                {
                    if ( newValue == null )
                    {
                        return;
                    }

                    attributeValue = new Rock.Model.AttributeValue();
                    attributeValue.AttributeId = attribute.Id;
                    attributeValue.EntityId = model.Id;
                    attributeValueService.Add( attributeValue );
                }

                attributeValue.Value = newValue;

                rockContext.SaveChanges();

                if ( model.AttributeValues != null && model.AttributeValues.ContainsKey( attribute.Key ) )
                {
                    model.AttributeValues[attribute.Key] = attributeValue.Clone( false ) as Rock.Model.AttributeValue;
                }
            }
        }
Exemplo n.º 37
0
 /// <summary>
 /// Adds edit controls for each of the item's attributes
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="parentControl">The parent control.</param>
 /// <param name="setValue">if set to <c>true</c> [set value].</param>
 /// <param name="validationGroup">The validation group.</param>
 /// <param name="supressOrdering">if set to <c>true</c> supresses reording (LoadAttributes() may perform custom ordering as is the case for group member attributes).</param>
 public static void AddEditControls( IHasAttributes item, Control parentControl, bool setValue, string validationGroup = "", bool supressOrdering = false )
 {
     AddEditControls( item, parentControl, setValue, validationGroup, new List<string>(), supressOrdering );
 }
Exemplo n.º 38
0
 /// <summary>
 /// Adds the edit controls.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="parentControl">The parent control.</param>
 /// <param name="setValue">if set to <c>true</c> [set value].</param>
 /// <param name="validationGroup">The validation group.</param>
 /// <param name="numberOfColumns">The number of columns.</param>
 /// <param name="exclude">The exclude.</param>
 /// <param name="supressOrdering">if set to <c>true</c> [supress ordering].</param>
 public static void AddEditControls( IHasAttributes item, Control parentControl, bool setValue, string validationGroup, List<string> exclude, bool supressOrdering, int? numberOfColumns = null )
 {
     if ( item != null && item.Attributes != null )
     {
         foreach ( var attributeCategory in GetAttributeCategories( item, false, false, supressOrdering ) )
         {
             if ( attributeCategory.Attributes.Where( a => !exclude.Contains( a.Name ) && !exclude.Contains( a.Key ) ).Select( a => a.Key ).Count() > 0 )
             {
                 AddEditControls(
                     attributeCategory.Category != null ? attributeCategory.Category.Name : string.Empty,
                     attributeCategory.Attributes.Select( a => a.Key ).ToList(),
                     item, parentControl, validationGroup, setValue, exclude, numberOfColumns );
             }
         }
     }
 }
Exemplo n.º 39
0
 /// <summary>
 /// Gets the values entered into the edit controls generated by the <see cref="GetEditControls"/> method and sets the <see cref="P:IHasAttributes.AttributeValues"/> of 
 /// the <see cref="IHasAttributes"/> object
 /// </summary>
 /// <param name="parentControl">The parent control.</param>
 /// <param name="item">The item.</param>
 public static void GetEditValues( Control parentControl, IHasAttributes item )
 {
     if ( item.Attributes != null )
         foreach ( var category in item.Attributes )
             foreach ( var attribute in category.Value )
             {
                 Control control = parentControl.FindControl( string.Format( "attribute-field-{0}", attribute.Id.ToString() ) );
                 if ( control != null )
                     item.AttributeValues[attribute.Key] = new KeyValuePair<string, string>( attribute.Name, attribute.FieldType.Field.ReadValue( control ) );
             }
 }
Exemplo n.º 40
0
        /// <summary>
        /// Saves an attribute value.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="value">The value.</param>
        /// <param name="personId">The person id.</param>
        public static void SaveAttributeValue( IHasAttributes model, Rock.Web.Cache.Attribute attribute, string newValue, int? personId )
        {
            Core.AttributeValueService attributeValueService = new Core.AttributeValueService();

            var attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, model.Id ).FirstOrDefault();
            if ( attributeValue == null )
            {
                attributeValue = new Rock.Core.AttributeValue();
                attributeValue.AttributeId = attribute.Id;
                attributeValue.EntityId = model.Id;
                attributeValue.Order = 0;
                attributeValueService.Add( attributeValue, personId );
            }

            attributeValue.Value = newValue;

            attributeValueService.Save( attributeValue, personId );

            model.AttributeValues[attribute.Key] =
                new KeyValuePair<string, List<Rock.Core.DTO.AttributeValue>>(
                    attribute.Name,
                    new List<Rock.Core.DTO.AttributeValue>() { attributeValue.DataTransferObject } );
        }
Exemplo n.º 41
0
 /// <summary>
 /// Saves the attribute values.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="personId">The person id.</param>
 public static void SaveAttributeValues( IHasAttributes model, int? personId )
 {
     foreach ( var category in model.Attributes )
         foreach ( var attribute in category.Value )
             SaveAttributeValues( model, attribute, model.AttributeValues[attribute.Key].Value, personId );
 }
Exemplo n.º 42
0
 /// <summary>
 /// Gets the values entered into the edit controls generated by the <see cref="GetEditControls"/> method and sets the <see cref="P:IHasAttributes.AttributeValues"/> of 
 /// the <see cref="IHasAttributes"/> object
 /// </summary>
 /// <param name="parentControl">The parent control.</param>
 /// <param name="item">The item.</param>
 public static void GetEditValues( Control parentControl, IHasAttributes item )
 {
     if ( item.Attributes != null )
         foreach ( var category in item.Attributes )
             foreach ( var attribute in category.Value )
             {
                 Control control = parentControl.FindControl( string.Format( "attribute_field_{0}", attribute.Id.ToString() ) );
                 if ( control != null )
                 {
                     Rock.Core.DTO.AttributeValue value = new Core.DTO.AttributeValue();
                     value.Value = attribute.FieldType.Field.ReadValue( control );
                     item.AttributeValues[attribute.Key] = new KeyValuePair<string, List<Rock.Core.DTO.AttributeValue>>( attribute.Name, new List<Rock.Core.DTO.AttributeValue>() { value } );
                 }
             }
 }
Exemplo n.º 43
0
 public static void AddEditControls( IHasAttributes item, Control parentControl, bool setValue)
 {
     AddEditControls( item, parentControl, setValue, "", new List<string>(), false );
 }
Exemplo n.º 44
0
        /// <summary>
        /// Adds the edit controls.
        /// </summary>
        /// <param name="category">The category.</param>
        /// <param name="attributeKeys">The attribute keys.</param>
        /// <param name="item">The item.</param>
        /// <param name="parentControl">The parent control.</param>
        /// <param name="validationGroup">The validation group.</param>
        /// <param name="setValue">if set to <c>true</c> [set value].</param>
        /// <param name="exclude">The exclude.</param>
        /// <param name="numberOfColumns">The number of columns.</param>
        public static void AddEditControls( string category, List<string> attributeKeys, IHasAttributes item, Control parentControl, string validationGroup, bool setValue, List<string> exclude, int? numberOfColumns )
        {
            // ensure valid number of columns
            if ( numberOfColumns.HasValue && numberOfColumns.Value > 12)
            {
                numberOfColumns = 12;
            }

            HtmlGenericControl fieldSet;
            if ( parentControl is DynamicControlsPanel )
            {
                fieldSet = new DynamicControlsHtmlGenericControl( "fieldset" );
            }
            else
            {
                fieldSet = new HtmlGenericControl( "fieldset" );
            }

            parentControl.Controls.Add( fieldSet );
            fieldSet.Controls.Clear();

            if ( !string.IsNullOrEmpty( category ) )
            {
                HtmlGenericControl legend = new HtmlGenericControl( "h4" );

                if ( numberOfColumns.HasValue )
                {
                    HtmlGenericControl row = new HtmlGenericControl( "div" );
                    row.AddCssClass( "row" );
                    fieldSet.Controls.Add( row );

                    HtmlGenericControl col = new HtmlGenericControl( "div" );
                    col.AddCssClass( "col-md-12" );
                    row.Controls.Add( col );

                    col.Controls.Add( legend );
                }
                else
                {
                    fieldSet.Controls.Add( legend );
                }

                legend.Controls.Clear();
                legend.InnerText = category.Trim();
            }

            HtmlGenericControl attributeRow = new HtmlGenericControl( "div" );
            if ( numberOfColumns.HasValue )
            {
                fieldSet.Controls.Add( attributeRow );
                attributeRow.AddCssClass( "row" );
            }

            foreach ( string key in attributeKeys )
            {
                var attribute = item.Attributes[key];

                if ( !exclude.Contains( attribute.Name ) && !exclude.Contains( attribute.Key ) )
                {
                    // Add the control for editing the attribute value

                    if ( numberOfColumns.HasValue )
                    {
                        int colSize = (int)Math.Ceiling((double)12 / numberOfColumns.Value);

                        HtmlGenericControl attributeCol = new HtmlGenericControl( "div" );
                        attributeRow.Controls.Add( attributeCol );
                        attributeCol.AddCssClass( string.Format( "col-md-{0}", colSize ) );
                        attribute.AddControl( attributeCol.Controls, item.AttributeValues[attribute.Key].Value, validationGroup, setValue, true );
                    }
                    else
                    {
                        attribute.AddControl( fieldSet.Controls, item.AttributeValues[attribute.Key].Value, validationGroup, setValue, true );
                    }
                }
            }
        }
Exemplo n.º 45
0
        /// <summary>
        /// Gets the attribute categories.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="onlyIncludeGridColumns">if set to <c>true</c> will only include those attributes with the option to display in grid set to true</param>
        /// <param name="allowMultiple">if set to <c>true</c> returns the attribute in each of it's categories, if false, only returns attribut in first category.</param>
        /// <param name="supressOrdering">if set to <c>true</c> supresses reording (LoadAttributes() may perform custom ordering as is the case for group member attributes).</param>
        /// <returns></returns>
        public static List<AttributeCategory> GetAttributeCategories( IHasAttributes entity, bool onlyIncludeGridColumns = false, bool allowMultiple = false, bool supressOrdering = false)
        {
            if ( entity != null )
            {
                var attributes = entity.Attributes.Select( a => a.Value );
                if ( !supressOrdering )
                {
                    attributes = attributes.OrderBy( t => t.Order ).ThenBy( t => t.Name );
                }

                return GetAttributeCategories( attributes.ToList(), onlyIncludeGridColumns, allowMultiple );
            }

            return null;
        }
Exemplo n.º 46
0
        /// <summary>
        /// Adds the display controls.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="attributeCategories">The attribute categories.</param>
        /// <param name="parentControl">The parent control.</param>
        /// <param name="exclude">The exclude.</param>
        /// <param name="showHeading">if set to <c>true</c> [show heading].</param>
        public static void AddDisplayControls( IHasAttributes item, List<AttributeCategory> attributeCategories, Control parentControl, List<string> exclude = null, bool showHeading = true )
        {
            foreach ( var attributeCategory in attributeCategories )
            {

                if ( showHeading )
                {
                    HtmlGenericControl header = new HtmlGenericControl( "h4" );

                    string categoryName = attributeCategory.Category != null ? attributeCategory.Category.Name : string.Empty;

                    header.InnerText = string.IsNullOrWhiteSpace( categoryName ) ? item.GetType().GetFriendlyTypeName() + " Attributes" : categoryName.Trim();
                    parentControl.Controls.Add( header );
                }

                HtmlGenericControl dl = new HtmlGenericControl( "dl" );
                parentControl.Controls.Add( dl );

                foreach ( var attribute in attributeCategory.Attributes )
                {
                    if ( exclude == null || !exclude.Contains( attribute.Name ) )
                    {
                        HtmlGenericControl dt = new HtmlGenericControl( "dt" );
                        dt.InnerText = attribute.Name;
                        dl.Controls.Add( dt );

                        HtmlGenericControl dd = new HtmlGenericControl( "dd" );

                        string value = attribute.DefaultValue;
                        if (item.AttributeValues.ContainsKey(attribute.Key) && item.AttributeValues[attribute.Key] != null )
                        {
                            value = item.AttributeValues[attribute.Key].Value;
                        }

                        string controlHtml = attribute.FieldType.Field.FormatValueAsHtml( parentControl, value, attribute.QualifierValues );
                        
                        if ( string.IsNullOrWhiteSpace( controlHtml ) )
                        {
                            controlHtml = None.TextHtml;
                        }

                        dd.InnerHtml = controlHtml;
                        dl.Controls.Add( dd );
                    }
                }
            }
        }
Exemplo n.º 47
0
 /// <summary>
 /// Saves the attribute values.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="rockContext">The rock context.</param>
 /// <remarks>
 /// If a rockContext value is included, this method will save any previous changes made to the context
 /// </remarks>
 public static void SaveAttributeValues( IHasAttributes model, RockContext rockContext = null )
 {
     if ( model != null && model.Attributes != null && model.AttributeValues != null )
     {
         foreach ( var attribute in model.Attributes )
         {
             if ( model.AttributeValues.ContainsKey( attribute.Key ) )
             {
                 SaveAttributeValue( model, attribute.Value, model.AttributeValues[attribute.Key].Value, rockContext );
             }
         }
     }
 }
Exemplo n.º 48
0
 /// <summary>
 /// Adds the edit controls.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="parentControl">The parent control.</param>
 /// <param name="setValue">if set to <c>true</c> [set value].</param>
 /// <param name="validationGroup">The validation group.</param>
 /// <param name="numberOfColumns">The number of columns.</param>
 public static void AddEditControls( IHasAttributes item, Control parentControl, bool setValue, string validationGroup, int? numberOfColumns )
 {
     AddEditControls( item, parentControl, setValue, validationGroup, new List<string>(), false, numberOfColumns );
 }
Exemplo n.º 49
0
        /// <summary>
        /// Copies the attributes from one entity to another
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="target">The target.</param>
        public static void CopyAttributes( IHasAttributes source, IHasAttributes target )
        {
            if ( source != null && target != null )
            {
                // Copy Attributes
                if ( source.Attributes != null )
                {
                    target.Attributes = new Dictionary<string, Web.Cache.AttributeCache>();
                    foreach ( var item in source.Attributes )
                    {
                        target.Attributes.Add( item.Key, item.Value );
                    }
                }
                else
                {
                    target.Attributes = null;
                }

                // Copy Attribute Values
                if ( source.AttributeValues != null )
                {
                    target.AttributeValues = new Dictionary<string, Model.AttributeValue>();
                    foreach ( var item in source.AttributeValues )
                    {
                        var value = item.Value;
                        if ( value != null )
                        {
                            var attributeValue = new Model.AttributeValue();
                            attributeValue.IsSystem = value.IsSystem;
                            attributeValue.AttributeId = value.AttributeId;
                            attributeValue.EntityId = value.EntityId;
                            attributeValue.Value = value.Value;
                            attributeValue.Id = value.Id;
                            attributeValue.Guid = value.Guid;
                            target.AttributeValues.Add( item.Key, attributeValue );
                        }
                        else
                        {
                            target.AttributeValues.Add( item.Key, null );
                        }
                    }
                }
                else
                {
                    target.AttributeValues = null;
                }
            }
        }
Exemplo n.º 50
0
        /// <summary>
        /// Saves an attribute value.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="newValue">The new value.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <remarks>
        /// If a rockContext value is included, this method will save any previous changes made to the context
        /// </remarks>
        public static void SaveAttributeValue( IHasAttributes model, Rock.Web.Cache.AttributeCache attribute, string newValue, RockContext rockContext = null )
        {
            rockContext = rockContext ?? new RockContext();

            var attributeValueService = new Model.AttributeValueService( rockContext );

            var attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, model.Id ).FirstOrDefault();
            if ( attributeValue == null )
            {
                if ( newValue == null )
                {
                    return;
                }

                attributeValue = new Rock.Model.AttributeValue();
                attributeValue.AttributeId = attribute.Id;
                attributeValue.EntityId = model.Id;
                attributeValue.Order = 0;
                attributeValueService.Add( attributeValue );
            }

            attributeValue.Value = newValue;

            rockContext.SaveChanges();

            model.AttributeValues[attribute.Key] = new List<Rock.Model.AttributeValue>() { attributeValue.Clone( false ) as Rock.Model.AttributeValue };

        }
Exemplo n.º 51
0
 /// <summary>
 /// Adds edit controls for each of the item's attributes
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="parentControl">The parent control.</param>
 /// <param name="setValue">if set to <c>true</c> [set value].</param>
 /// <param name="validationGroup">The validation group.</param>
 /// <param name="exclude">List of attribute names not to render</param>
 /// <param name="supressOrdering">if set to <c>true</c> supresses reording (LoadAttributes() may perform custom ordering as is the case for group member attributes).</param>
 public static void AddEditControls( IHasAttributes item, Control parentControl, bool setValue, string validationGroup, List<string> exclude, bool supressOrdering = false )
 {
     if ( item.Attributes != null )
     {
         foreach ( var attributeCategory in GetAttributeCategories( item, false, false, supressOrdering ) )
         {
             AddEditControls(
                 attributeCategory.Category != null ? attributeCategory.Category.Name : string.Empty,
                 attributeCategory.Attributes.Select( a => a.Key ).ToList(),
                 item, parentControl, validationGroup, setValue, exclude );
         }
     }
 }
Exemplo n.º 52
0
        /// <summary>
        /// Saves an attribute value.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="newValues">The new values.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <remarks>
        /// If a rockContext value is included, this method will save any previous changes made to the context
        /// </remarks>
        public static void SaveAttributeValues( IHasAttributes model, Rock.Web.Cache.AttributeCache attribute, List<Rock.Model.AttributeValue> newValues, RockContext rockContext = null )
        {
            rockContext = rockContext ?? new RockContext();
            var attributeValueService = new Model.AttributeValueService( rockContext );

            var attributeValues = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, model.Id ).ToList();
            int i = 0;

            while ( i < attributeValues.Count || i < newValues.Count )
            {
                Rock.Model.AttributeValue attributeValue;

                if ( i < attributeValues.Count )
                {
                    attributeValue = attributeValues[i];
                }
                else
                {
                    attributeValue = new Rock.Model.AttributeValue();
                    attributeValue.AttributeId = attribute.Id;
                    attributeValue.EntityId = model.Id;
                    attributeValue.Order = i;
                    attributeValueService.Add( attributeValue );
                }

                if ( i >= newValues.Count )
                    attributeValueService.Delete( attributeValue );
                else
                {
                    if ( attributeValue.Value != newValues[i].Value )
                        attributeValue.Value = newValues[i].Value;
                    newValues[i] = attributeValue.Clone( false ) as Rock.Model.AttributeValue;
                }

                i++;
            }

            rockContext.SaveChanges();

            model.AttributeValues[attribute.Key] = newValues;
        }
Exemplo n.º 53
0
        /// <summary>
        /// Gets the display HTML.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="parentControl">The parent control.</param>
        /// <param name="exclude">The exclude.</param>
        /// <param name="supressOrdering">if set to <c>true</c> supresses reording (LoadAttributes() may perform custom ordering as is the case for group member attributes).</param>
        public static void AddDisplayControls( IHasAttributes item, Control parentControl, List<string> exclude = null, bool supressOrdering = false )
        {
            exclude = exclude ?? new List<string>();
            string result = string.Empty;

            if ( item.Attributes != null )
            {
                AddDisplayControls(item, GetAttributeCategories(item, false, supressOrdering), parentControl, exclude);
            }
        }
Exemplo n.º 54
0
 /// <summary>
 /// Adds edit controls for each of the item's attributes
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="parentControl">The parent control.</param>
 /// <param name="setValue">if set to <c>true</c> [set value].</param>
 /// <param name="validationGroup">The validation group.</param>
 /// <param name="exclude">List of attribute names not to render</param>
 /// <param name="supressOrdering">if set to <c>true</c> supresses reording (LoadAttributes() may perform custom ordering as is the case for group member attributes).</param>
 public static void AddEditControls( IHasAttributes item, Control parentControl, bool setValue, string validationGroup, List<string> exclude, bool supressOrdering = false )
 {
     AddEditControls( item, parentControl, setValue, validationGroup, exclude, supressOrdering, null );
 }
Exemplo n.º 55
0
        /// <summary>
        /// Saves an attribute value.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="value">The value.</param>
        /// <param name="personId">The person id.</param>
        public static void SaveAttributeValues( IHasAttributes model, Rock.Web.Cache.Attribute attribute, List<Rock.Core.DTO.AttributeValue> newValues, int? personId )
        {
            Core.AttributeValueService attributeValueService = new Core.AttributeValueService();

            var attributeValues = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, model.Id ).ToList();
            int i = 0;

            while ( i < attributeValues.Count || i < newValues.Count )
            {
                Rock.Core.AttributeValue attributeValue;

                if ( i < attributeValues.Count )
                {
                    attributeValue = attributeValues[i];
                }
                else
                {
                    attributeValue = new Rock.Core.AttributeValue();
                    attributeValue.AttributeId = attribute.Id;
                    attributeValue.EntityId = model.Id;
                    attributeValue.Order = i;
                    attributeValueService.Add( attributeValue, personId );
                }

                if ( i >= newValues.Count )
                    attributeValueService.Delete( attributeValue, personId );
                else
                {
                    if ( attributeValue.Value != newValues[i].Value )
                        attributeValue.Value = newValues[i].Value;
                    newValues[i] = attributeValue.DataTransferObject;
                }

                attributeValueService.Save( attributeValue, personId );

                i++;
            }

            model.AttributeValues[attribute.Key] =
                new KeyValuePair<string, List<Rock.Core.DTO.AttributeValue>>( attribute.Name, newValues );
        }
Exemplo n.º 56
0
 /// <summary>
 /// Saves the attribute values.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="rockContext">The rock context.</param>
 /// <remarks>
 /// If a rockContext value is included, this method will save any previous changes made to the context
 /// </remarks>
 public static void SaveAttributeValues( IHasAttributes model, RockContext rockContext = null )
 {
     foreach ( var attribute in model.Attributes )
     {
         SaveAttributeValues( model, attribute.Value, model.AttributeValues[attribute.Key], rockContext );
     }
 }
Exemplo n.º 57
0
        /// <summary>
        /// Saves an attribute value.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="value">The value.</param>
        /// <param name="personId">The person id.</param>
        public static void SaveAttributeValue( IHasAttributes model, Rock.Web.Cache.Attribute attribute, string value, int? personId )
        {
            Core.AttributeValueService attributeValueService = new Core.AttributeValueService();
            Core.AttributeValue attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, model.Id );

            if ( attributeValue == null )
            {
                attributeValue = new Rock.Core.AttributeValue();
                attributeValueService.Add( attributeValue, personId );
            }

            attributeValue.AttributeId = attribute.Id;
            attributeValue.EntityId = model.Id;
            attributeValue.Value = value;

            attributeValueService.Save( attributeValue, personId );

            model.AttributeValues[attribute.Key] = new KeyValuePair<string, string>( attribute.Name, value );
        }
Exemplo n.º 58
0
 /// <summary>
 /// Sets any missing required field error indicators.
 /// </summary>
 /// <param name="parentControl">The parent control.</param>
 /// <param name="item">The item.</param>
 public static void SetErrorIndicators( Control parentControl, IHasAttributes item )
 {
     if ( item.Attributes != null )
         foreach ( var category in item.Attributes )
             foreach ( var attribute in category.Value )
             {
                 if ( attribute.Required )
                 {
                     HtmlGenericControl dl = parentControl.FindControl( string.Format( "attribute_{0}", attribute.Id ) ) as HtmlGenericControl;
                     RequiredFieldValidator rfv = parentControl.FindControl( string.Format( "attribute_rfv_{0}", attribute.Id ) ) as RequiredFieldValidator;
                     if ( dl != null && rfv != null )
                         dl.Attributes["class"] = rfv.IsValid ? "" : "error";
                 }
             }
 }
Exemplo n.º 59
0
 /// <summary>
 /// Adds the edit controls.
 /// </summary>
 /// <param name="category">The category.</param>
 /// <param name="attributeKeys">The attribute keys.</param>
 /// <param name="item">The item.</param>
 /// <param name="parentControl">The parent control.</param>
 /// <param name="validationGroup">The validation group.</param>
 /// <param name="setValue">if set to <c>true</c> [set value].</param>
 /// <param name="exclude">The exclude.</param>
 public static void AddEditControls( string category, List<string> attributeKeys, IHasAttributes item, Control parentControl, string validationGroup, bool setValue, List<string> exclude )
 {
     AddEditControls( category, attributeKeys, item, parentControl, validationGroup, setValue, exclude, null );
 }
Exemplo n.º 60
0
        /// <summary>
        /// Helper method to generate a list of <![CDATA[<li>]]> tags that contain the appropriate html edit
        /// control returned by each attribute's <see cref="Rock.FieldTypes.IFieldType"/>
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="setValue">if set to <c>true</c> set the edit control's value based on the attribute value.</param>
        /// <returns></returns>
        public static List<HtmlGenericControl> GetEditControls( IHasAttributes item, bool setValue )
        {
            List<HtmlGenericControl> controls = new List<HtmlGenericControl>();

            if ( item.Attributes != null )
                foreach(var category in item.Attributes)
                {
                    HtmlGenericControl fieldSet = new HtmlGenericControl("fieldset");
                    controls.Add(fieldSet);

                    HtmlGenericControl legend = new HtmlGenericControl("legend");
                    fieldSet.Controls.Add(legend);
                    legend.InnerText = category.Key.Trim() != string.Empty ? category.Key.Trim() : "Attributes";

                    foreach ( Rock.Web.Cache.Attribute attribute in category.Value )
                    {
                        HtmlGenericControl dl = new HtmlGenericControl( "dl" );
                        dl.ID = string.Format( "attribute_{0}", attribute.Id );
                        dl.Attributes.Add( "attribute-key", attribute.Key );
                        dl.ClientIDMode = ClientIDMode.AutoID;
                        fieldSet.Controls.Add( dl );

                        HtmlGenericControl dt = new HtmlGenericControl( "dt" );
                        dl.Controls.Add( dt );

                        Label lbl = new Label();
                        lbl.ClientIDMode = ClientIDMode.AutoID;
                        lbl.Text = attribute.Name;
                        lbl.AssociatedControlID = string.Format( "attribute_field_{0}", attribute.Id );
                        if ( attribute.Required )
                            lbl.Attributes.Add( "class", "required" );
                        dt.Controls.Add( lbl );

                        HtmlGenericControl dd = new HtmlGenericControl( "dd" );
                        dl.Controls.Add( dd );

                        Control attributeControl = attribute.CreateControl( item.AttributeValues[attribute.Key].Value[0].Value, setValue );
                        attributeControl.ID = string.Format( "attribute_field_{0}", attribute.Id );
                        attributeControl.ClientIDMode = ClientIDMode.AutoID;
                        dd.Controls.Add( attributeControl );

                        if ( attribute.Required )
                        {
                            RequiredFieldValidator rfv = new RequiredFieldValidator();
                            dd.Controls.Add( rfv );
                            rfv.ControlToValidate = attributeControl.ID;
                            rfv.ID = string.Format( "attribute_rfv_{0}", attribute.Id );
                            rfv.ErrorMessage = string.Format( "{0} is Required", attribute.Name );
                            rfv.Display = ValidatorDisplay.None;

                            if (!setValue && !rfv.IsValid)
                                dl.Attributes.Add( "class", "error" );
                        }

                        if ( !string.IsNullOrEmpty( attribute.Description ) )
                        {
                            HtmlAnchor a = new HtmlAnchor();
                            a.ClientIDMode = ClientIDMode.AutoID;
                            a.Attributes.Add( "class", "help-tip" );
                            a.HRef = "#";
                            a.InnerHtml = "help<span>" + attribute.Description + "</span>";

                            dd.Controls.Add( a );
                        }
                    }
                }

            return controls;
        }