예제 #1
0
    public static Dictionary <int, Dictionary <string, object> > GetServiceCatsAndItsAttributes(int positionId, string filters = null, int userId = 0)
    {
        var rcats      = new Dictionary <int, Dictionary <string, object> >();
        var catsFilter = new List <int>();
        // Set if the catsFilter is the list of cats to be excluded from the total (value: true)
        // or is a list of unique cats to be returned (value: false)
        bool excludeCats = false;
        // This bool config set that only attributes related to the userId must be returned (query field 'UserChecked' == True)
        bool onlyUserChecked     = false;
        bool onlyBookingServices = false;

        List <string> filterList = new List <string>();

        // Reading filters:
        if (filters != null)
        {
            filterList.AddRange(filters.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
        }

        foreach (string filter in filterList)
        {
            switch (filter)
            {
            case "provider-services-without-virtual-cats":
            case "provider-services":
                //catsFilter.AddRange(new int[]{1, 2, 3, 4, 5, 7});
                //catsFilter.AddRange(new int[]{1, 2, 4, 5, 7});
                excludeCats = false;
                break;

            case "without-special-cats":
                catsFilter  = SpecialServicesAttCats;
                excludeCats = true;
                break;

            case "only-special-cats":
                catsFilter  = SpecialServicesAttCats;
                excludeCats = false;
                break;

            case "only-user-checked":
                onlyUserChecked = true;
                break;

            case "booking-services":
                onlyBookingServices = true;
                break;
            }
        }

        var sqlcat       = "exec GetServiceAttributeCategories @0, @1, @2, @3";
        var sqlattribute = "exec GetServiceAttributes @0, @1, @2, @3, @4, @5";

        using (var db = Database.Open("sqlloco"))
        {
            var catrow = db.Query(sqlcat, positionId, LcData.GetCurrentLanguageID(), LcData.GetCurrentCountryID(), onlyBookingServices);

            // Iterate the categories
            foreach (var cat in catrow)
            {
                // Apply filtering, if there are
                if (catsFilter.Count > 0 &&
                    (excludeCats && catsFilter.Contains(cat.ServiceAttributeCategoryID)
                     ||
                     !excludeCats && !catsFilter.Contains(cat.ServiceAttributeCategoryID)
                    ))
                {
                    continue;
                }
                // Copy data to a new structure
                var rcat = new Dictionary <string, object>()
                {
                    { "ServiceAttributeCategoryID", cat.ServiceAttributeCategoryID },
                    { "ServiceAttributeCategoryName", cat.ServiceCat },
                    { "ServiceAttributeCategoryDescription", cat.ServiceAttributeCategoryDescription },
                    { "RequiredInput", cat.RequiredInput },
                    { "SideBarCategory", cat.SideBarCategory }
                };
                // Getting attributes of the category
                rcat["ServiceAttributes"] = db.Query(sqlattribute,
                                                     positionId,
                                                     cat.ServiceAttributeCategoryID,
                                                     LcData.GetCurrentLanguageID(),
                                                     LcData.GetCurrentCountryID(),
                                                     (userId == 0 ? null : (object)userId),
                                                     onlyUserChecked);
                rcats.Add(cat.ServiceAttributeCategoryID, rcat);
            }


            /* SPECIAL CASES */
            if (filterList.Contains("provider-services") || filterList.Contains("only-special-cats"))
            {
                // Adding the extra tables Language Levels and Experience Levels as 'virtual' categories, using the same
                // fields name to be easy to implement
                // Returning a 'virtual' language levels category
                var rcat = new Dictionary <string, object>()
                {
                    { "ServiceAttributeCategoryID", ServiceAttCatIDLanguageLevel },
                    { "ServiceAttributeCategoryName", LcRessources.GetText("Language Level") },
                    { "ServiceAttributeCategoryDescription", LcRessources.GetText("Language Level Description") },
                    { "RequiredInput", false },
                    { "SideBarCategory", true }
                };
                var levelsIndex = new Dictionary <int, int>();
                var langlevels  = new List <object>();
                foreach (var level in LcData.GetLanguageLevels())
                {
                    langlevels.Add(new Dictionary <string, object> {
                        { "ServiceAttributeDescription", level.LanguageLevelDescription },
                        { "ServiceAttributeID", level.LanguageLevelID },
                        { "ServiceAttribute", level.LanguageLevelName },
                        { "UserChecked", false }
                    });
                    levelsIndex.Add(level.LanguageLevelID, langlevels.Count - 1);
                }
                rcat["ServiceAttributes"] = langlevels;
                if (userId > 0)
                {
                    rcat["LevelsIndex"]        = levelsIndex;
                    rcat["UserSelectedLevels"] = LcData.GetUserLanguageLevels(userId, positionId);
                }
                rcats[ServiceAttCatIDLanguageLevel] = rcat;

                // Returning a 'virtual' experience levels category
                rcat = new Dictionary <string, object>()
                {
                    { "ServiceAttributeCategoryID", ServiceAttCatIDExperienceLevel },
                    { "ServiceAttributeCategoryName", LcRessources.GetText("Experience Level") },
                    { "ServiceAttributeCategoryDescription", LcRessources.GetText("Experience Level Description") },
                    { "RequiredInput", false },
                    { "SideBarCategory", true }
                };
                var explevels = new List <object>();
                foreach (var level in GetExperienceLevels(userId, positionId))
                {
                    if (!onlyUserChecked || level.UserChecked)
                    {
                        explevels.Add(new Dictionary <string, object> {
                            { "ServiceAttributeDescription", level.ExperienceLevelDescription },
                            { "ServiceAttributeID", level.ExperienceLevelID },
                            { "ServiceAttribute", level.ExperienceLevelName },
                            { "UserChecked", level.UserChecked }
                        });
                    }
                }
                rcat["ServiceAttributes"]             = explevels;
                rcats[ServiceAttCatIDExperienceLevel] = rcat;
            }
        }
        return(rcats);
    }