Пример #1
0
        /// <summary>
        /// Sets the filters.
        /// </summary>
        private void SetFilters(bool setValues)
        {
            using (var rockContext = new RockContext())
            {
                string sessionKey       = string.Format("ConnectionSearch_{0}", this.BlockId);
                var    searchSelections = Session[sessionKey] as Dictionary <string, string>;
                setValues = setValues && searchSelections != null;

                var connectionType = new ConnectionTypeService(rockContext).Get(GetAttributeValue("ConnectionTypeId").AsInteger());

                if (connectionType != null)
                {
                    int entityTypeId    = new ConnectionOpportunity().TypeId;
                    var attributeOneKey = GetAttributeValue("AttributeOneKey");
                    var attributeTwoKey = GetAttributeValue("AttributeTwoKey");

                    if (!string.IsNullOrWhiteSpace(attributeOneKey))
                    {
                        if (!string.IsNullOrWhiteSpace(attributeTwoKey))
                        {
                            pnlAttributeOne.CssClass = "col-sm-6";
                        }
                        else
                        {
                            pnlAttributeOne.CssClass = "col-sm-12";
                        }

                        AttributeOne = AttributeCache.Get(new AttributeService(new RockContext()).Queryable()
                                                          .FirstOrDefault(a =>
                                                                          a.EntityTypeId == entityTypeId &&
                                                                          a.EntityTypeQualifierColumn.Equals("ConnectionTypeId", StringComparison.OrdinalIgnoreCase) &&
                                                                          a.EntityTypeQualifierValue.Equals(connectionType.Id.ToString()) &&
                                                                          a.Key == attributeOneKey));
                    }

                    if (!string.IsNullOrWhiteSpace(attributeTwoKey))
                    {
                        if (!string.IsNullOrWhiteSpace(attributeOneKey))
                        {
                            pnlAttributeTwo.CssClass = "col-sm-6";
                        }
                        else
                        {
                            pnlAttributeTwo.CssClass = "col-sm-12";
                        }

                        AttributeTwo = AttributeCache.Get(new AttributeService(new RockContext()).Queryable()
                                                          .FirstOrDefault(a =>
                                                                          a.EntityTypeId == entityTypeId &&
                                                                          a.EntityTypeQualifierColumn.Equals("ConnectionTypeId", StringComparison.OrdinalIgnoreCase) &&
                                                                          a.EntityTypeQualifierValue.Equals(connectionType.Id.ToString()) &&
                                                                          a.Key == attributeTwoKey));
                    }
                }

                if (AttributeOne != null)
                {
                    phAttributeOne.Controls.Clear();
                    AttributeOne.AddControl(phAttributeOne.Controls, string.Empty, string.Empty, true, true, false);
                    var control = phAttributeOne.Controls[0] as DropDownList;
                    if (control != null)
                    {
                        control.EnableViewState       = true;
                        control.AutoPostBack          = true;
                        control.SelectedIndexChanged += new EventHandler(ddlAttributeOne_SelectedIndexChanged);
                    }
                    else
                    {
                        var          theseControls = phAttributeOne.Controls[0] as DropDownList;
                        DropDownList ddl           = new DropDownList();
                        string[]     theseValues   = AttributeOne.QualifierValues.Values.ElementAt(0).Value.Split(',');

                        foreach (string nameValue in theseValues)
                        {
                            string[] nameAndValue = nameValue.Split(new char[] { '^' });

                            if (nameAndValue.Length == 2)
                            {
                                ddl.Items.Add(new ListItem(nameAndValue[1].Trim(), nameAndValue[0].Trim()));
                            }
                        }

                        ddl.Items.Insert(0, new ListItem(String.Empty, String.Empty));
                        ddl.SelectedIndex = 0;
                        ddl.CssClass      = "form-control";
                        var attributeOneLabel = new HtmlGenericContainer("label");
                        attributeOneLabel.InnerHtml = AttributeOne.Name;
                        attributeOneLabel.CssClass  = "control-label";

                        phAttributeOne.Controls.Clear();
                        phAttributeOne.Controls.Add(new Literal()
                        {
                            Text = "<div class='form-group rock-drop-down-list'>"
                        });
                        phAttributeOne.Controls.Add(attributeOneLabel);
                        phAttributeOne.Controls.Add(ddl);
                        phAttributeOne.Controls.Add(new Literal()
                        {
                            Text = "</div>"
                        });

                        var newDdl = phAttributeOne.Controls[2] as DropDownList;

                        newDdl.EnableViewState       = true;
                        newDdl.AutoPostBack          = true;
                        newDdl.SelectedIndexChanged += new EventHandler(ddlAttributeOne_SelectedIndexChanged);
                    }
                }

                if (AttributeTwo != null)
                {
                    phAttributeTwo.Controls.Clear();
                    AttributeTwo.AddControl(phAttributeTwo.Controls, string.Empty, string.Empty, true, true, false);
                    var control = phAttributeTwo.Controls[0] as DropDownList;
                    if (control != null)
                    {
                        control.EnableViewState       = true;
                        control.AutoPostBack          = true;
                        control.SelectedIndexChanged += new EventHandler(ddlAttributeTwo_SelectedIndexChanged);
                    }
                    else
                    {
                        var          theseControls = phAttributeTwo.Controls[0] as DropDownList;
                        DropDownList ddl           = new DropDownList();
                        string[]     theseValues   = AttributeTwo.QualifierValues.Values.ElementAt(0).Value.Split(',');

                        foreach (string nameValue in theseValues)
                        {
                            string[] nameAndValue = nameValue.Split(new char[] { '^' });

                            if (nameAndValue.Length == 2)
                            {
                                ddl.Items.Add(new ListItem(nameAndValue[1].Trim(), nameAndValue[0].Trim()));
                            }
                        }

                        ddl.Items.Insert(0, new ListItem(String.Empty, String.Empty));
                        ddl.SelectedIndex = 0;
                        ddl.CssClass      = "form-control";
                        var attributeTwoLabel = new HtmlGenericContainer("label");
                        attributeTwoLabel.InnerHtml = AttributeTwo.Name;
                        attributeTwoLabel.CssClass  = "control-label";

                        phAttributeTwo.Controls.Clear();
                        phAttributeTwo.Controls.Add(new Literal()
                        {
                            Text = "<div class='form-group rock-drop-down-list'>"
                        });
                        phAttributeTwo.Controls.Add(attributeTwoLabel);
                        phAttributeTwo.Controls.Add(ddl);
                        phAttributeTwo.Controls.Add(new Literal()
                        {
                            Text = "</div>"
                        });

                        var newDdl = phAttributeTwo.Controls[2] as DropDownList;

                        newDdl.EnableViewState       = true;
                        newDdl.AutoPostBack          = true;
                        newDdl.SelectedIndexChanged += new EventHandler(ddlAttributeTwo_SelectedIndexChanged);
                    }
                }
            }
        }
        /// <summary>
        /// Sets the filters.
        /// </summary>
        private void SetFilters(bool setValues)
        {
            using (var rockContext = new RockContext())
            {
                string sessionKey       = string.Format("ConnectionSearch_{0}", this.BlockId);
                var    searchSelections = Session[sessionKey] as Dictionary <string, string>;
                setValues = setValues && searchSelections != null;

                var connectionType = new ConnectionTypeService(rockContext).Get(GetAttributeValue(AttributeKey.ConnectionTypeId).AsInteger());

                if (!GetAttributeValue(AttributeKey.DisplayNameFilter).AsBoolean())
                {
                    tbSearchName.Visible = false;
                }

                if (GetAttributeValue(AttributeKey.DisplayCampusFilter).AsBoolean())
                {
                    cblCampus.Visible    = true;
                    cblCampus.DataSource = CampusCache.All(GetAttributeValue(AttributeKey.DisplayInactiveCampuses).AsBoolean());
                    cblCampus.DataBind();
                }
                else
                {
                    cblCampus.Visible = false;
                }

                if (setValues)
                {
                    if (searchSelections.ContainsKey("tbSearchName"))
                    {
                        tbSearchName.Text = searchSelections["tbSearchName"];
                    }
                    if (searchSelections.ContainsKey("cblCampus"))
                    {
                        var selectedItems = searchSelections["cblCampus"].SplitDelimitedValues().AsIntegerList();
                        cblCampus.SetValues(selectedItems);
                    }
                }
                else if (GetAttributeValue(AttributeKey.EnableCampusContext).AsBoolean())
                {
                    var campusEntityType = EntityTypeCache.Get("Rock.Model.Campus");
                    var contextCampus    = RockPage.GetCurrentContext(campusEntityType) as Campus;

                    if (contextCampus != null)
                    {
                        cblCampus.SetValue(contextCampus.Id.ToString());
                    }
                }

                if (GetAttributeValue(AttributeKey.DisplayAttributeFilters).AsBoolean())
                {
                    // Parse the attribute filters
                    AvailableAttributes = new List <AttributeCache>();
                    if (connectionType != null)
                    {
                        int entityTypeId = new ConnectionOpportunity().TypeId;
                        foreach (var attributeModel in new AttributeService(new RockContext()).Queryable()
                                 .Where(a =>
                                        a.EntityTypeId == entityTypeId &&
                                        a.EntityTypeQualifierColumn.Equals("ConnectionTypeId", StringComparison.OrdinalIgnoreCase) &&
                                        a.EntityTypeQualifierValue.Equals(connectionType.Id.ToString()) &&
                                        a.AllowSearch == true)
                                 .OrderBy(a => a.Order)
                                 .ThenBy(a => a.Name))
                        {
                            AvailableAttributes.Add(AttributeCache.Get(attributeModel));
                        }
                    }

                    // Clear the filter controls
                    phAttributeFilters.Controls.Clear();

                    if (AvailableAttributes != null)
                    {
                        foreach (var attribute in AvailableAttributes)
                        {
                            string controlId = "filter_" + attribute.Id.ToString();
                            var    control   = attribute.FieldType.Field.FilterControl(attribute.QualifierValues, controlId, false, Rock.Reporting.FilterMode.SimpleFilter);
                            if (control != null)
                            {
                                if (control is IRockControl)
                                {
                                    var rockControl = ( IRockControl )control;
                                    rockControl.Label = attribute.Name;
                                    rockControl.Help  = attribute.Description;
                                    phAttributeFilters.Controls.Add(control);
                                }
                                else
                                {
                                    var wrapper = new RockControlWrapper();
                                    wrapper.ID    = control.ID + "_wrapper";
                                    wrapper.Label = attribute.Name;
                                    wrapper.Controls.Add(control);
                                    phAttributeFilters.Controls.Add(wrapper);
                                }

                                if (setValues && searchSelections.ContainsKey(controlId))
                                {
                                    var values = searchSelections[controlId].FromJsonOrNull <List <string> >();
                                    attribute.FieldType.Field.SetFilterValues(control, attribute.QualifierValues, values);
                                }
                            }
                        }
                    }
                }
                else
                {
                    phAttributeFilters.Visible = false;
                }

                if (connectionType != null)
                {
                    int entityTypeId    = new ConnectionOpportunity().TypeId;
                    var attributeOneKey = GetAttributeValue(AttributeKey.AttributeOneKey);
                    var attributeTwoKey = GetAttributeValue(AttributeKey.AttributeTwoKey);

                    if (!string.IsNullOrWhiteSpace(attributeOneKey))
                    {
                        if (!string.IsNullOrWhiteSpace(attributeTwoKey))
                        {
                            pnlAttributeOne.CssClass = "col-sm-6";
                        }
                        else
                        {
                            pnlAttributeOne.CssClass = "col-sm-12";
                        }

                        AttributeOne = AttributeCache.Get(new AttributeService(new RockContext()).Queryable()
                                                          .FirstOrDefault(a =>
                                                                          a.EntityTypeId == entityTypeId &&
                                                                          a.EntityTypeQualifierColumn.Equals("ConnectionTypeId", StringComparison.OrdinalIgnoreCase) &&
                                                                          a.EntityTypeQualifierValue.Equals(connectionType.Id.ToString()) &&
                                                                          a.Key == attributeOneKey));
                    }

                    if (!string.IsNullOrWhiteSpace(attributeTwoKey))
                    {
                        if (!string.IsNullOrWhiteSpace(attributeOneKey))
                        {
                            pnlAttributeTwo.CssClass = "col-sm-6";
                        }
                        else
                        {
                            pnlAttributeTwo.CssClass = "col-sm-12";
                        }

                        AttributeTwo = AttributeCache.Get(new AttributeService(new RockContext()).Queryable()
                                                          .FirstOrDefault(a =>
                                                                          a.EntityTypeId == entityTypeId &&
                                                                          a.EntityTypeQualifierColumn.Equals("ConnectionTypeId", StringComparison.OrdinalIgnoreCase) &&
                                                                          a.EntityTypeQualifierValue.Equals(connectionType.Id.ToString()) &&
                                                                          a.Key == attributeTwoKey));
                    }
                }

                if (AttributeOne != null)
                {
                    phAttributeOne.Controls.Clear();
                    AttributeOne.AddControl(phAttributeOne.Controls, string.Empty, string.Empty, true, true, false);
                    var control = phAttributeOne.Controls[0] as DropDownList;
                    if (control != null)
                    {
                        control.EnableViewState       = true;
                        control.AutoPostBack          = true;
                        control.SelectedIndexChanged += new EventHandler(ddlAttributeOne_SelectedIndexChanged);
                    }
                    else
                    {
                        var          theseControls = phAttributeOne.Controls[0] as DropDownList;
                        DropDownList ddl           = new DropDownList();
                        string[]     theseValues   = AttributeOne.QualifierValues.Values.ElementAt(0).Value.Split(',');
                        if (theseValues.Count() == 1 && theseValues[0] == "rb")
                        {
                            theseValues = AttributeOne.QualifierValues.Values.ElementAt(1).Value.Split(',');
                        }
                        foreach (string nameValue in theseValues)
                        {
                            string[] nameAndValue = nameValue.Split(new char[] { '^' });

                            if (nameAndValue.Length == 2)
                            {
                                ddl.Items.Add(new ListItem(nameAndValue[1].Trim(), nameAndValue[0].Trim()));
                            }
                        }

                        ddl.Items.Insert(0, new ListItem(String.Empty, String.Empty));
                        ddl.SelectedIndex = 0;
                        ddl.CssClass      = "form-control";
                        var attributeOneLabel = new HtmlGenericContainer("label");
                        attributeOneLabel.InnerHtml = AttributeOne.Name;
                        attributeOneLabel.CssClass  = "control-label";

                        phAttributeOne.Controls.Clear();
                        phAttributeOne.Controls.Add(new Literal()
                        {
                            Text = "<div class='form-group rock-drop-down-list'>"
                        });
                        phAttributeOne.Controls.Add(attributeOneLabel);
                        phAttributeOne.Controls.Add(ddl);
                        phAttributeOne.Controls.Add(new Literal()
                        {
                            Text = "</div>"
                        });

                        var newDdl = phAttributeOne.Controls[2] as DropDownList;

                        newDdl.EnableViewState       = true;
                        newDdl.AutoPostBack          = true;
                        newDdl.SelectedIndexChanged += new EventHandler(ddlAttributeOne_SelectedIndexChanged);
                    }
                }

                if (AttributeTwo != null)
                {
                    phAttributeTwo.Controls.Clear();
                    AttributeTwo.AddControl(phAttributeTwo.Controls, string.Empty, string.Empty, true, true, false);
                    var control = phAttributeTwo.Controls[0] as DropDownList;
                    if (control != null)
                    {
                        control.EnableViewState       = true;
                        control.AutoPostBack          = true;
                        control.SelectedIndexChanged += new EventHandler(ddlAttributeTwo_SelectedIndexChanged);
                    }
                    else
                    {
                        var          theseControls = phAttributeTwo.Controls[0] as DropDownList;
                        DropDownList ddl           = new DropDownList();
                        string[]     theseValues   = AttributeTwo.QualifierValues.Values.ElementAt(0).Value.Split(',');
                        if (theseValues.Count() == 1 && theseValues[0] == "rb")
                        {
                            theseValues = AttributeTwo.QualifierValues.Values.ElementAt(1).Value.Split(',');
                        }

                        foreach (string nameValue in theseValues)
                        {
                            string[] nameAndValue = nameValue.Split(new char[] { '^' });

                            if (nameAndValue.Length == 2)
                            {
                                ddl.Items.Add(new ListItem(nameAndValue[1].Trim(), nameAndValue[0].Trim()));
                            }
                        }

                        ddl.Items.Insert(0, new ListItem(String.Empty, String.Empty));
                        ddl.SelectedIndex = 0;
                        ddl.CssClass      = "form-control";
                        var attributeTwoLabel = new HtmlGenericContainer("label");
                        attributeTwoLabel.InnerHtml = AttributeTwo.Name;
                        attributeTwoLabel.CssClass  = "control-label";

                        phAttributeTwo.Controls.Clear();
                        phAttributeTwo.Controls.Add(new Literal()
                        {
                            Text = "<div class='form-group rock-drop-down-list'>"
                        });
                        phAttributeTwo.Controls.Add(attributeTwoLabel);
                        phAttributeTwo.Controls.Add(ddl);
                        phAttributeTwo.Controls.Add(new Literal()
                        {
                            Text = "</div>"
                        });

                        var newDdl = phAttributeTwo.Controls[2] as DropDownList;

                        newDdl.EnableViewState       = true;
                        newDdl.AutoPostBack          = true;
                        newDdl.SelectedIndexChanged += new EventHandler(ddlAttributeTwo_SelectedIndexChanged);
                    }
                }
            }
        }