Beispiel #1
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            RockContext rockContext = new RockContext();

            rockContext.WrapTransaction(() =>
            {
                WidgityTypeService widgityTypeService = new WidgityTypeService(rockContext);
                AttributeService attributeService     = new AttributeService(rockContext);
                WidgityType widgityType = GetWidgityType(widgityTypeService);

                if (widgityType.Id == 0)
                {
                    widgityTypeService.Add(widgityType);
                }

                widgityType.Name                = tbName.Text;
                widgityType.Description         = tbDescription.Text;
                widgityType.EnabledLavaCommands = lcCommands.SelectedValue;
                widgityType.Icon                = tbIcon.Text;
                widgityType.Markdown            = ceMarkup.Text;
                widgityType.HasItems            = cbHasItems.Checked;
                var _ = widgityType.EntityTypes.ToList(); //Attach the entity types to context
                widgityType.EntityTypes = new EntityTypeService(rockContext).GetByIds(lbEntityTypes.SelectedValuesAsInt).ToList();
                widgityType.CategoryId  = pCategory.SelectedValueAsId();
                rockContext.SaveChanges();

                //Widgity Attributes
                var widgityEntityTypeId = EntityTypeCache.Get(typeof(Widgity)).Id;

                var actualAttributes = attributeService.Queryable()
                                       .Where(a => a.EntityTypeId == widgityEntityTypeId && a.EntityTypeQualifierValue == widgityType.Id.ToString())
                                       .OrderBy(a => a.Order)
                                       .ToList();

                //Remove deleted attributes
                foreach (var attribute in actualAttributes)
                {
                    if (!WidgityAttributes.Where(a => a.Guid == attribute.Guid).Any())
                    {
                        attributeService.Delete(attribute);
                    }
                }

                //Update db from viewstate
                foreach (var attribute in WidgityAttributes)
                {
                    if (attribute.Id == 0)
                    {
                        attribute.Order = WidgityAttributes.IndexOf(attribute);
                        attribute.EntityTypeQualifierValue = widgityType.Id.ToString();
                        attributeService.Add(attribute);
                    }
                    else
                    {
                        var trackedAttribute = actualAttributes.Where(a => a.Guid == attribute.Guid).FirstOrDefault();
                        trackedAttribute.CopyPropertiesFrom(attribute);
                        foreach (var qualifier in trackedAttribute.AttributeQualifiers)
                        {
                            var value = attribute.AttributeQualifiers.Where(q => q.Key == qualifier.Key).FirstOrDefault();
                            if (value != null)
                            {
                                qualifier.Value = value.Value;
                            }
                        }
                        trackedAttribute.Order = WidgityAttributes.IndexOf(attribute);
                    }
                }
                rockContext.SaveChanges();


                //Widgity Item Attributes
                var widgityItemEntityTypeId = EntityTypeCache.Get(typeof(WidgityItem)).Id;

                var actualItemAttributes = attributeService.Queryable()
                                           .Where(a => a.EntityTypeId == widgityItemEntityTypeId && a.EntityTypeQualifierValue == widgityType.Id.ToString())
                                           .OrderBy(a => a.Order)
                                           .ToList();

                //Remove deleted attributes
                foreach (var attribute in actualItemAttributes)
                {
                    if (!WidgityItemAttributes.Where(a => a.Guid == attribute.Guid).Any())
                    {
                        attributeService.Delete(attribute);
                    }
                }

                //Update db from viewstate
                foreach (var attribute in WidgityItemAttributes)
                {
                    if (attribute.Id == 0)
                    {
                        attribute.Order = WidgityItemAttributes.IndexOf(attribute);
                        attribute.EntityTypeQualifierValue = widgityType.Id.ToString();
                        attributeService.Add(attribute);
                    }
                    else
                    {
                        var trackedAttribute = actualItemAttributes.Where(a => a.Guid == attribute.Guid).FirstOrDefault();
                        trackedAttribute.CopyPropertiesFrom(attribute);
                        foreach (var qualifier in trackedAttribute.AttributeQualifiers)
                        {
                            var value = attribute.AttributeQualifiers.Where(q => q.Key == qualifier.Key).FirstOrDefault();
                            if (value != null)
                            {
                                qualifier.Value = value.Value;
                            }
                        }
                        trackedAttribute.Order = WidgityItemAttributes.IndexOf(attribute);
                    }
                }
                rockContext.SaveChanges();
            });
            WidgityTypeCache.Clear();
            WidgityCache.Clear();
            WidgityItemCache.Clear();
            NavigateToParentPage();
        }
Beispiel #2
0
        private void ShowWidgityTypes()
        {
            var widgityTypes = WidgityTypeCache.All()
                               .Where(wt => wt.EntityTypes.Select(e => e.Id).Contains(EntityTypeId)).ToList();

            if (!widgityTypes.Any())
            {
                NotificationBox notification = new NotificationBox
                {
                    NotificationBoxType = NotificationBoxType.Warning,
                    Text = "There are no widgity types for this entity type: " + EntityTypeCache.Get(EntityTypeId)?.FriendlyName
                };
                pnlMenu.Controls.Add(notification);
                return;
            }

            var categories = widgityTypes
                             .Where(wt => wt.Category != null)
                             .Select(wt => wt.Category)
                             .DistinctBy(c => c.Id)
                             .ToList();

            foreach (var category in categories)
            {
                PanelWidget panelWidget = new PanelWidget
                {
                    ID    = this.ID + "_pwCategory_" + category.Id.ToString(),
                    Title = category.Name
                };
                pnlMenu.Controls.Add(panelWidget);

                var dragContainer = new Panel
                {
                    ID       = this.ID + "_pnlWidgityContainer_" + category.Id.ToString(),
                    CssClass = "widgitySource"
                };
                panelWidget.Controls.Add(dragContainer);

                var categoryTypes = widgityTypes.Where(wt => wt.CategoryId == category.Id).ToList();

                foreach (var widgityType in categoryTypes)
                {
                    HtmlGenericContainer item = new HtmlGenericContainer("div");
                    item.InnerHtml = string.Format("<i class='{0}'></i><br />{1}",
                                                   widgityType.Icon,
                                                   widgityType.Name);
                    item.Attributes.Add("data-component-id", widgityType.Id.ToString());
                    item.CssClass = "btn btn-default btn-block";
                    dragContainer.Controls.Add(item);
                }
            }

            var noCategoryTypes = widgityTypes.Where(wt => wt.Category == null).ToList();

            if (noCategoryTypes.Any())
            {
                var dragContainerOther = new Panel
                {
                    ID       = this.ID + "_pnlWidgityContainer_other",
                    CssClass = "widgitySource"
                };

                if (categories.Any())
                {
                    PanelWidget panelWidgetOther = new PanelWidget
                    {
                        ID    = this.ID + "_pwCategory_Other",
                        Title = "Other"
                    };
                    pnlMenu.Controls.Add(panelWidgetOther);
                    panelWidgetOther.Controls.Add(dragContainerOther);
                }
                else
                {
                    pnlMenu.Controls.Add(dragContainerOther);
                }

                foreach (var widgityType in noCategoryTypes)
                {
                    HtmlGenericContainer item = new HtmlGenericContainer("div")
                    {
                        InnerHtml = string.Format("<i class='{0}'></i><br />{1}",
                                                  widgityType.Icon,
                                                  widgityType.Name)
                    };
                    item.Attributes.Add("data-component-id", widgityType.Id.ToString());
                    dragContainerOther.Controls.Add(item);
                }
            }

            if (ShowPublishButtons)
            {
                HtmlGenericContainer hr = new HtmlGenericContainer("hr");
                pnlMenu.Controls.Add(hr);

                BootstrapButton btnSave = new BootstrapButton
                {
                    CssClass        = "btn btn-primary",
                    Text            = "Publish",
                    ID              = this.ID + "_btnSave",
                    ValidationGroup = this.ID + "ValidationGroup"
                };
                pnlMenu.Controls.Add(btnSave);
                btnSave.Click += BtnSave_Click;

                LinkButton btnCancel = new LinkButton
                {
                    ID       = this.ID + "_btnCancel",
                    Text     = "Cancel",
                    CssClass = "btn btn-link"
                };
                pnlMenu.Controls.Add(btnCancel);
                btnCancel.Click += BtnCancel_Click;
            }
        }
Beispiel #3
0
        private void ShowWidigtyEdit(bool setValues)
        {
            var widgityGuid = CurrentEditWidgity.Value;
            var widgity     = Widgities.Where(w => w.Guid == widgityGuid).FirstOrDefault();
            var widgityType = WidgityTypeCache.Get(widgity.WidgityTypeId);

            Literal ltName = new Literal
            {
                Text = string.Format("<h3>{0}</h3>", widgityType.Name)
            };

            pnlMenu.Controls.Add(ltName);

            phAttributesEdit = new PlaceHolder
            {
                ID = this.ID + "_phAttributesEdit"
            };
            pnlMenu.Controls.Add(phAttributesEdit);
            Rock.Attribute.Helper.AddEditControls(widgity, phAttributesEdit, setValues, this.ID + "ValidationGroup");

            if (widgityType.HasItems)
            {
                BuildWidigityItemAttibutes(widgity, setValues);
            }

            HtmlGenericContainer hr = new HtmlGenericContainer("hr");

            pnlMenu.Controls.Add(hr);

            BootstrapButton btnEditSave = new BootstrapButton
            {
                CssClass        = "btn btn-primary",
                Text            = "Save",
                ID              = this.ID + "_btnEditSave",
                ValidationGroup = this.ID + "ValidationGroup"
            };

            pnlMenu.Controls.Add(btnEditSave);
            btnEditSave.Click += BtnEditSave_Click;

            LinkButton btnEditCancel = new LinkButton
            {
                ID               = this.ID + "_btnEditCancel",
                Text             = "Cancel",
                CssClass         = "btn btn-link",
                CausesValidation = false
            };

            pnlMenu.Controls.Add(btnEditCancel);
            btnEditCancel.Click += BtnEditCancel_Click;

            LinkButton btnDeleteWidgity = new LinkButton
            {
                ID               = this.ID + "_btnDeleteWidgity",
                Text             = "Delete",
                CssClass         = "btn btn-delete pull-right",
                CausesValidation = false
            };

            pnlMenu.Controls.Add(btnDeleteWidgity);
            btnDeleteWidgity.Click += BtnDeleteWidgity_Click;
        }
Beispiel #4
0
        private void BuildWidigityItemAttibutes(Widgity widgity, bool setValues)
        {
            pnlItems = new Panel();
            pnlMenu.Controls.Add(pnlItems);

            var widgityType = WidgityTypeCache.Get(widgity.WidgityTypeId);

            if (widgityType.HasItems)
            {
                pnlItems.CssClass = "widgityItemControls";
                var widgityItems = WidgityItems[widgity.Guid];
                if (widgityItems.Any())
                {
                    foreach (var widgityItem in widgityItems)
                    {
                        Panel panel = new Panel();
                        pnlItems.Controls.Add(panel);
                        panel.Attributes["data-component-id"] = widgityItem.Guid.ToString();

                        PanelWidget panelWidget = new PanelWidget
                        {
                            ID = string.Format("pnlItem_{0}", widgityItem.Guid),
                        };

                        panel.Controls.Add(panelWidget);

                        if (ExpandedPanels != null && ExpandedPanels.Contains(widgityItem.Guid))
                        {
                            panelWidget.Expanded = true;
                        }

                        if (widgityItem.AttributeValues.Any())
                        {
                            panelWidget.Title = "<a class='btn btn-xs btn-link ui-sortable-handle'><i class='fa fa-bars ui-sortable-handle'></i></a> " +
                                                widgityItem.AttributeValues.FirstOrDefault().Value.ValueFormatted;
                        }
                        else
                        {
                            panelWidget.Title = "<a class='btn btn-xs btn-link ui-sortable-handle'><i class='fa fa-bars ui-sortable-handle'></i></a> ";
                        }

                        HiddenField hiddenField = new HiddenField
                        {
                            ID    = string.Format("hfItem_{0}", widgityItem.Guid),
                            Value = widgityItem.Guid.ToString()
                        };
                        panelWidget.Controls.Add(hiddenField);

                        PlaceHolder phItemAttributes = new PlaceHolder
                        {
                            ID = string.Format("phItem_{0}", widgityItem.Guid)
                        };
                        panelWidget.Controls.Add(phItemAttributes);

                        LinkButton linkButton = new LinkButton()
                        {
                            ID       = string.Format("btnRemove_{0}", widgityItem.Guid),
                            CssClass = "btn btn-danger btn-xs",
                            Text     = "Remove Item"
                        };

                        panelWidget.Controls.Add(linkButton);

                        linkButton.Click += (s, e) => { DeleteWidgityItem(widgityItem.Guid); };

                        Rock.Attribute.Helper.AddEditControls(widgityItem, phItemAttributes, setValues, this.ID + "ValidationGroup");
                    }
                }
                else
                {
                    pnlItems.Controls.Add(new Literal
                    {
                        Text = "<i>No items. Click Add Item to add a new item to this widgity.</i>"
                    });
                }

                LinkButton btnAddButton = new LinkButton
                {
                    ID               = this.ID + "_AddItem",
                    Text             = "Add Item",
                    CssClass         = "btn btn-default btn-xs",
                    CausesValidation = false
                };
                pnlMenu.Controls.Add(btnAddButton);
                btnAddButton.Click += BtnAddButton_Click;
            }
        }
Beispiel #5
0
        private void ShowWidgities(bool setValues)
        {
            EnsureChildControls();
            pnlContent.Controls.Clear();

            if (Mode == WidgityMode.Edit)
            {
                pnlMenu.Visible = true;
                ShowEdit(setValues);
            }
            else
            {
                pnlMenu.Visible = false;
                pnlContent.Style["box-shadow"] = "";
                pnlContent.CssClass            = "";
            }

            RockPage rockPage = null;

            if (HttpContext.Current != null)
            {
                rockPage = HttpContext.Current.Handler as RockPage;
            }

            var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(rockPage);

            mergeFields.Add("CurrentPage", PageCache.Get(rockPage.PageId));

            foreach (var widgity in Widgities)
            {
                var panel = new Panel();
                pnlContent.Controls.Add(panel);
                if (Mode == WidgityMode.Edit)
                {
                    if (!CurrentEditWidgity.HasValue || (CurrentEditWidgity.HasValue && CurrentEditWidgity == widgity.Guid))
                    {
                        panel.Attributes["data-component-id"] = widgity.Guid.ToString();
                        panel.CssClass = "widgityContent";

                        Panel pencilPanel = new Panel
                        {
                            CssClass = "widgity-edit"
                        };
                        panel.Controls.Add(pencilPanel);

                        LinkButton link = new LinkButton
                        {
                            ID       = string.Format("lbEdit{0}", widgity.Guid),
                            Text     = "<i class='fa fa-pencil'></i>",
                            CssClass = "widgity-edit-pencil"
                        };
                        link.Click += (s, e) => { EditWidgity(widgity.Guid); };
                        pencilPanel.Controls.Add(link);
                        if (CurrentEditWidgity == widgity.Guid)
                        {
                            panel.AddCssClass("active-edit");
                        }
                    }
                }

                Panel displayPanel = new Panel();
                displayPanel.Style.Add("width", "100%");
                panel.Controls.Add(displayPanel);

                mergeFields["Widgity"]      = widgity;
                mergeFields["WidgityItems"] = WidgityItems[widgity.Guid];
                mergeFields["uniqueid"]     = widgity.Guid.ToString().Split('-').First();
                var widgityType = WidgityTypeCache.Get(widgity.WidgityTypeId);
                var output      = new Literal
                {
                    Text = widgityType.Markdown.ResolveMergeFields(mergeFields, widgityType.EnabledLavaCommands)
                };
                displayPanel.Controls.Add(output);
            }
        }