/// <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; } if (!IsPostBack) { var canEdit = IsUserAuthorized(Authorization.EDIT); pnlDetails.Visible = true; pnlView.Visible = true; btnEdit.Visible = canEdit; ltTitle.Text = string.Format("{0} Attributes", entity.GetType().GetFriendlyTypeName()); } if (pnlEdit.Visible) { ShowEdit(false); } else { ShowDetails(); } }
/// <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); } } } }
/// <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 ); } } } }
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" }); } }