Example #1
0
        /// <summary>
        ///     Search through all resources for a language by page and search terms.
        ///     Search either by key or value.
        /// </summary>
        /// <param name="searchByKey">True means serach the keys else search the values</param>
        /// <param name="languageId"></param>
        /// <param name="p"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        private async Task <ActionResult> GetLanguageResources(bool searchByKey, Guid languageId, int?p, string search)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    var errors = (from key in ModelState.Keys
                                  select ModelState[key]
                                  into state
                                  where state.Errors.Any()
                                  select state.Errors.First().ErrorMessage).ToList();
                    ShowErrors(errors);
                }
                else
                {
                    var language  = LocalizationService.Get(languageId);
                    var pageIndex = p ?? 1;

                    // Get all the resources or just the ones that match the search
                    var allResources = string.IsNullOrWhiteSpace(search)
                        ? await LocalizationService.GetAllValues(language.Id, pageIndex,
                                                                 ForumConfiguration.Instance.AdminListPageSize)
                        : searchByKey
                            ? await LocalizationService.SearchResourceKeys(language.Id, search,
                                                                           pageIndex,
                                                                           ForumConfiguration.Instance.AdminListPageSize)
                            : await LocalizationService.SearchResourceValues(language.Id, search,
                                                                             pageIndex,
                                                                             ForumConfiguration.Instance.AdminListPageSize);

                    var models = allResources.Select(resource => new LocaleResourceViewModel
                    {
                        Id                = resource.Id,
                        ResourceKeyId     = resource.LocaleResourceKey.Id,
                        LocaleResourceKey = resource.LocaleResourceKey.Name,
                        ResourceValue     = resource.ResourceValue
                    }).ToList();

                    var resourceListModel = new LanguageListResourcesViewModel
                    {
                        LanguageId      = language.Id,
                        LanguageName    = language.Name,
                        LocaleResources = models,
                        PageIndex       = pageIndex,
                        TotalCount      = allResources.TotalCount,
                        Search          = search,
                        TotalPages      = allResources.TotalPages
                    };

                    return(View("ListValues", resourceListModel));
                }
            }
            catch (Exception ex)
            {
                ShowError(ex.Message);
            }

            // Default ie error
            return(RedirectToAction("Index"));
        }