예제 #1
0
        /// <summary>
        /// Gets the mobile attribute values.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="attributes">The attributes.</param>
        /// <returns></returns>
        public static Dictionary <string, MobileAttributeValue> GetMobileAttributeValues(IHasAttributes entity, IEnumerable <AttributeCache> attributes)
        {
            var mobileAttributeValues = new Dictionary <string, MobileAttributeValue>();

            if (entity.Attributes == null)
            {
                entity.LoadAttributes();
            }

            foreach (var attribute in attributes)
            {
                var value          = entity.GetAttributeValue(attribute.Key);
                var formattedValue = entity.AttributeValues.ContainsKey(attribute.Key) ? entity.AttributeValues[attribute.Key].ValueFormatted : attribute.DefaultValueAsFormatted;

                var mobileAttributeValue = new MobileAttributeValue
                {
                    Value          = value,
                    FormattedValue = formattedValue
                };

                mobileAttributeValues.AddOrReplace(attribute.Key, mobileAttributeValue);
            }

            return(mobileAttributeValues);
        }
예제 #2
0
        /// <summary>
        /// Gets the row value.
        /// </summary>
        /// <param name="row">The row.</param>
        /// <returns></returns>
        private object GetRowValue(GridViewRow row)
        {
            // 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();
                }

                bool exists = dataItem.Attributes.ContainsKey(this.DataField);
                if (exists)
                {
                    var    attrib     = dataItem.Attributes[this.DataField];
                    string rawValue   = dataItem.GetAttributeValue(this.DataField);
                    string resultHtml = attrib.FieldType.Field.FormatValueAsHtml(null, rawValue, attrib.QualifierValues, Condensed);
                    return(new HtmlString(resultHtml ?? string.Empty));
                }
            }

            return(null);
        }
예제 #3
0
파일: CreateLabels.cs 프로젝트: sjison/Rock
        /// <summary>
        /// Gets the labels for an item (person, grouptype, group, location).
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="existingLabels">The existing labels.</param>
        /// <returns></returns>
        public virtual List <KioskLabel> GetLabels(IHasAttributes item, List <KioskLabel> existingLabels)
        {
            var labels = new List <KioskLabel>(existingLabels);

            if (item.Attributes == null)
            {
                item.LoadAttributes();
            }

            foreach (var attribute in item.Attributes.OrderBy(a => a.Value.Order))
            {
                if (attribute.Value.FieldType.Class == typeof(Rock.Field.Types.LabelFieldType).FullName)
                {
                    Guid?binaryFileGuid = item.GetAttributeValue(attribute.Key).AsGuidOrNull();
                    if (binaryFileGuid != null)
                    {
                        if (!labels.Any(l => l.Guid == binaryFileGuid.Value))
                        {
                            var labelCache = KioskLabel.Read(binaryFileGuid.Value);
                            labelCache.Order = attribute.Value.Order;
                            if (labelCache != null && (
                                    labelCache.LabelType == KioskLabelType.Family ||
                                    labelCache.LabelType == KioskLabelType.Person ||
                                    labelCache.LabelType == KioskLabelType.Location))
                            {
                                labels.Add(labelCache);
                            }
                        }
                    }
                }
            }

            return(labels);
        }
예제 #4
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);
        }
예제 #5
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());
        }
예제 #6
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"
                });
            }
        }
예제 #7
0
        /// <summary>
        /// Updates the editable attributes in the given entity. This method should be called
        /// to update the attributes with the postback data received from a prevoius
        /// GetEditAttributeXaml() call.
        /// </summary>
        /// <param name="entity">The entity whose attribute values will be updated.</param>
        /// <param name="postbackData">The postback data.</param>
        /// <param name="attributes">If not null, updating will be limited to these attributes.</param>
        /// <param name="person">If not null then security will be enforced for this person.</param>
        public static void UpdateEditAttributeValues(IHasAttributes entity, Dictionary <string, object> postbackData, List <AttributeCache> attributes = null, Person person = null)
        {
            if (entity.Attributes == null)
            {
                entity.LoadAttributes();
            }

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

            foreach (var attribute in attributes)
            {
                if (person != null && !attribute.IsAuthorized(Authorization.EDIT, person))
                {
                    continue;
                }

                var keyName = $"attribute_{attribute.Id}";
                if (postbackData.ContainsKey(keyName))
                {
                    entity.SetAttributeValue(attribute.Key, postbackData[keyName].ToStringSafe());
                }
            }
        }
예제 #8
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;
                }
            }
        }
예제 #9
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 (attrib?.FieldType?.Field is BooleanFieldType)
                    {
                        if (this.ItemStyle.HorizontalAlign != HorizontalAlign.Center)
                        {
                            this.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                        }

                        var boolValue = rawValue.AsBoolean();
                        return(boolValue ? "<i class=\"fa fa-check\"></i>" : string.Empty);
                    }

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

            return(null);
        }
예제 #10
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;
                }
            }
        }
        /// <summary>
        /// Shows the read-only attribute values.
        /// </summary>
        protected void ShowDetails()
        {
            var loadAttributeValue = GetAttributeValue("LoadAttributeValue").AsBoolean(false);

            ltTitle.Text = GetAttributeValue("PanelHeading");
            btnSave.Text = GetAttributeValue("SaveButtonText");

            IHasAttributes entity = ContextEntity() as IHasAttributes;

            if (entity != null && CurrentPerson != null)
            {
                var rockContext = new RockContext();

                if (entity.Attributes == null)
                {
                    entity.LoadAttributes();
                }

                if (CurrentPerson.Attributes == null)
                {
                    CurrentPerson.LoadAttributes();
                }

                var sourceAttributeKey   = GetAttributeValue("SourceEntityAttributeKey");
                var sourceAttributeValue = entity.GetAttributeValue(sourceAttributeKey);
                var attributeService     = new AttributeService(rockContext);
                var targetAttribute      = attributeService.GetByGuids(new List <Guid>()
                {
                    sourceAttributeValue.AsGuid()
                }).ToList().FirstOrDefault();

                fsAttributes.Controls.Clear();

                string validationGroup = string.Format("vgAttributeValues_{0}", this.BlockId);
                btnSave.ValidationGroup = validationGroup;

                var    attribute      = AttributeCache.Get(targetAttribute);
                string attributeValue = CurrentPerson.GetAttributeValue(attribute.Key);
                string formattedValue = string.Empty;

                if (GetAttributeValue("CheckboxMode").AsBoolean(false))
                {
                    var      checkboxText = GetAttributeValue("CheckboxText");
                    CheckBox checkbox     = new CheckBox();
                    checkbox.ID   = "SimpleCheckBox";
                    checkbox.Text = String.IsNullOrWhiteSpace(checkboxText) ? attribute.Name : checkboxText;

                    if (GetAttributeValue("InstantSave").AsBoolean(false))
                    {
                        checkbox.AutoPostBack = true;

                        checkbox.CheckedChanged += new EventHandler(this.Check_Change);
                        btnSave.Visible          = false;
                    }

                    if (loadAttributeValue && !string.IsNullOrWhiteSpace(attributeValue) && attributeValue.AsBoolean(true))
                    {
                        checkbox.Checked = true;
                    }

                    fsAttributes.Controls.Add(checkbox);
                }
                else
                {
                    attribute.AddControl(fsAttributes.Controls, attributeValue, validationGroup, loadAttributeValue, true);
                }

                pnlDetails.Visible = true;
            }
        }
        private string GetFileUrlOrNull(IHasAttributes item, string attributeKey)
        {
            item.LoadAttributes();

            return(_binaryFileService.Get(item.GetAttributeValue(attributeKey).AsGuid())?.Path);
        }
예제 #13
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;
     }
 }