Ejemplo n.º 1
0
        public ActionResult Action(int?ID)
        {
            LanguageActionViewModel model = new LanguageActionViewModel();

            if (ID.HasValue)
            {
                var language = LanguagesService.Instance.GetLanguageByID(ID.Value);

                if (language == null)
                {
                    return(HttpNotFound());
                }

                model.ID        = language.ID;
                model.Name      = language.Name;
                model.ShortCode = language.ShortCode;
                model.IsEnabled = language.IsEnabled;
                model.IsDefault = language.IsDefault;
                model.IsRTL     = language.IsRTL;
                model.IconCode  = language.IconCode;
            }

            return(View(model));
        }
Ejemplo n.º 2
0
        public JsonResult Action(LanguageActionViewModel model)
        {
            JsonResult json = new JsonResult();

            try
            {
                if (model.ID > 0)
                {
                    var language = LanguagesService.Instance.GetLanguageByID(model.ID);

                    if (language == null)
                    {
                        throw new Exception("Dashboard.Languages.Action.Validation.LanguageNotFound".LocalizedString());
                    }

                    language.ID        = model.ID;
                    language.Name      = model.Name;
                    language.ShortCode = model.ShortCode;
                    language.IsEnabled = model.IsEnabled;

                    if (language.IsDefault && !model.IsDefault)
                    {
                        throw new Exception("Dashboard.Languages.Action.Validation.DefaultLanguageIsMust".LocalizedString());
                    }

                    var makeDefault = false;
                    if (model.IsDefault)
                    {
                        if (!language.IsDefault)
                        {
                            language.IsDefault = true;
                            makeDefault        = true;
                        }
                    }
                    else
                    {
                        language.IsDefault = false;
                    }

                    language.IsRTL    = model.IsRTL;
                    language.IconCode = model.IconCode;

                    if (!LanguagesService.Instance.UpdateLanguage(language, makeDefault))
                    {
                        throw new Exception("Dashboard.Languages.Action.Validation.UnableToUpdateLanguage".LocalizedString());
                    }
                }
                else
                {
                    var language = new Language
                    {
                        Name      = model.Name,
                        ShortCode = model.ShortCode,
                        IsEnabled = model.IsEnabled,
                        IsDefault = model.IsDefault,
                        IsRTL     = model.IsRTL,
                        IconCode  = model.IconCode
                    };

                    var makeDefault = false;
                    if (model.IsDefault)
                    {
                        language.IsDefault = true;
                        makeDefault        = true;
                    }
                    else
                    {
                        language.IsDefault = false;
                    }


                    if (!LanguagesService.Instance.AddLanguage(language, makeDefault))
                    {
                        throw new Exception("Dashboard.Languages.Action.Validation.UnableToCreateLanguage".LocalizedString());
                    }
                }

                var enabledLanguages = LanguagesService.Instance.GetLanguages(enabledLanguagesOnly: true);
                LanguagesHelper.LoadLanguages(enabledLanguages: enabledLanguages, defaultLanguage: enabledLanguages.FirstOrDefault(x => x.IsDefault));

                json.Data = new { Success = true };
            }
            catch (Exception ex)
            {
                json.Data = new { Success = false, Message = ex.Message };
            }

            return(json);
        }