Esempio n. 1
0
        public static Control GetCandidateList(string partialName, string[] selectedPoliticians,
                                               string officeKeyOrStateCode, string[] currentCandidates,
                                               bool fullAlphaNameOnly = false)
        {
            var placeHolder = new PlaceHolder();
            var idPrefix    = fullAlphaNameOnly ? "addpolitician-" : "searchpolitician-";

            partialName = partialName.Trim();
            if (partialName.Length < 2)
            {
                return(placeHolder);
            }
            var alphaName         = partialName.StripAccents();
            var vowelStrippedName = partialName.StripVowels();

            // The officeKeyOrStateCode can be either empty/null, a single officeKey,
            // a single stateCode, or a comma separated list of stateCodes
            officeKeyOrStateCode = officeKeyOrStateCode ?? Empty;
            var stateCodes =
                officeKeyOrStateCode.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            if (stateCodes.Length == 1)
            {
                stateCodes[0] = Offices.GetStateCodeFromKey(stateCodes[0]);
            }

            var matches =
                GetSearchCandidates(partialName, currentCandidates, stateCodes, fullAlphaNameOnly)
                .Rows.Cast <DataRow>().OrderBy(row => row.LastName())
                .ThenBy(row => row.FirstName())
                .GroupBy(row => MatchCandidateQuality(alphaName, vowelStrippedName, row))
                .OrderBy(g => g.Key);

            foreach (var group in matches)
            {
                foreach (var politician in group)
                {
                    var selected = selectedPoliticians?.Contains(politician.PoliticianKey(),
                                                                 StringComparer.OrdinalIgnoreCase) == true;
                    var className = selected ? "selected" : null;
                    GetCandidateListItem(politician, idPrefix, selected).AddTo(placeHolder, className)
                    .Attributes.Add("sort-key",
                                    (politician.LastName() + " " + politician.FirstName() + " " +
                                     politician.MiddleName() + " " + politician.Nickname() + " " +
                                     politician.Suffix()).StripRedundantSpaces().ToLowerInvariant());
                }
            }
            return(placeHolder);
        }
Esempio n. 2
0
        public static HtmlGenericControl GetCandidateListItem(DataRow politician,
                                                              string idPrefix = null, bool noCache = false)
        {
            string OfficeDescription()
            {
                var officeKey = politician.LiveOfficeKey();

                if (IsNullOrWhiteSpace(officeKey))
                {
                    return(Empty);
                }

                var result = politician.OfficeLine1();

                if (!IsNullOrWhiteSpace(politician.OfficeLine2()) &&
                    politician.OfficeClass() != OfficeClass.USPresident)
                {
                    result += " " + politician.OfficeLine2();
                }
                if (politician.OfficeClass() != OfficeClass.USPresident)
                {
                    var stateCode  = Offices.GetStateCodeFromKey(officeKey);
                    var countyCode = Offices.GetCountyCodeFromKey(officeKey);
                    var localKey   = Offices.GetLocalKeyFromKey(officeKey);
                    if (IsNullOrWhiteSpace(countyCode))
                    {
                        result = StateCache.GetStateName(stateCode) + " " + result;
                    }
                    else if (IsNullOrWhiteSpace(localKey))
                    {
                        result = CountyCache.GetCountyName(stateCode, countyCode) + ", " +
                                 StateCache.GetStateName(stateCode) + " " + result;
                    }
                    else
                    {
                        result = CountyCache.GetCountyDescription(stateCode, localKey) + ", " +
                                 StateCache.GetStateName(stateCode) + ", " + politician.LocalDistrict() + " " +
                                 result;
                    }
                }
                return(politician.LivePoliticianStatus().GetOfficeStatusDescription() + result);
            }

            string AddressLine()
            {
                var result       = politician.PublicAddress();
                var cityStateZip = politician.PublicCityStateZip();

                if (!IsNullOrWhiteSpace(result) && !IsNullOrWhiteSpace(cityStateZip))
                {
                    result += ", ";
                }
                result += cityStateZip;
                return(result);
            }

            var div = new HtmlDiv();

            div.AddCssClasses("search-politician unselectable clearfix");
            if (!IsNullOrWhiteSpace(idPrefix))
            {
                div.ID = idPrefix + politician.PoliticianKey();
            }
            Report.CreatePoliticianImageTag(politician.PoliticianKey(), 35, noCache).AddTo(div);
            var text = FormatName(politician);

            if (!IsNullOrWhiteSpace(politician.PartyCode()))
            {
                text += " (" + politician.PartyCode() + ")";
            }
            new HtmlDiv {
                InnerHtml = text
            }.AddTo(div, "name");
            text = OfficeDescription();
            if (!IsNullOrWhiteSpace(text))
            {
                new HtmlDiv {
                    InnerHtml = text
                }
            }