Beispiel #1
0
        public ActionResult Language(LanguageResourcesViewModel model)
        {
            var language = new LanguageManager(db);

            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(Request.Form["btnCreate"]))
                {
                    var languages = language.GetLanguages();
                    if (string.IsNullOrWhiteSpace(model.NewLanguageName))
                    {
                        ModelState.AddModelError("NewLanguageName",
                                                 AppGlobal.Language.GetText(this, "NameRequred", "This field is required."));
                    }
                    if (languages.Any(
                            x => x.DefaultText.Equals(model.NewLanguageName, StringComparison.CurrentCultureIgnoreCase)))
                    {
                        ModelState.AddModelError("NewLanguageName",
                                                 AppGlobal.Language.GetText(this, "IETFInUse", "Name in use."));
                    }
                    if (string.IsNullOrWhiteSpace(model.NewLanguageIETF))
                    {
                        ModelState.AddModelError("NewLanguageIETF",
                                                 AppGlobal.Language.GetText(this, "IETFRequred", "This field is required."));
                    }
                    if (languages.Any(
                            x => x.IETF.Equals(model.NewLanguageIETF, StringComparison.CurrentCultureIgnoreCase)))
                    {
                        ModelState.AddModelError("NewLanguageIETF",
                                                 AppGlobal.Language.GetText(this, "IETFInUse", "IETF code in use."));
                    }

                    model.NewLanguageSuccess = ModelState.IsValid;
                    if (ModelState.IsValid)
                    {
                        if (!string.IsNullOrEmpty(model.NewLanguageName) &&
                            !string.IsNullOrEmpty(model.NewLanguageIETF))
                        {
                            model.NewLanguageSuccess = language.CreateNewLanguage(model.NewLanguageName,
                                                                                  model.NewLanguageIETF);
                            if (model.NewLanguageSuccess)
                            {
                                model.NewLanguageMessage = AppGlobal.Language.GetText(this, "NewLanguageSuccess",
                                                                                      "New language created successfully");
                            }
                            else
                            {
                                model.NewLanguageMessage = AppGlobal.Language.GetText(this, "NewLanguageFailed",
                                                                                      "New language failed");
                            }
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(Request.Form["btnDownload"]))
                {
                    var data = language.GenerateCsvLanguageFileBytes(model.DownloadLanguageId);
                    return(File(data.Bytes, "text/csv", data.FileName));
                }
                else if (!string.IsNullOrEmpty(Request.Form["btnUpload"]))
                {
                    if (model.FileUpload != null)
                    {
                        var extension = Path.GetExtension(model.FileUpload.FileName);
                        if (extension != null && extension.ToLower() == ".csv")
                        {
                            try
                            {
                                model.UploadSuccess = language.ProcessUploadedCsvLanguageFile(model.UploadLanguageId,
                                                                                              model.FileUpload.InputStream);
                                if (model.UploadSuccess)
                                {
                                    model.UploadMessage = AppGlobal.Language.GetText(this, "LanguageUpdated",
                                                                                     "Language updated");
                                }
                                else
                                {
                                    model.UploadSuccess = false;
                                    model.UploadMessage = AppGlobal.Language.GetText(this, "LanguageUpdateFailed",
                                                                                     "Language update failed");
                                }
                            }
                            catch (CsvMissingFieldException e)
                            {
                                var field = e.Message.Substring(e.Message.IndexOf('\'') + 1);
                                field = field.Substring(0, field.IndexOf('\''));

                                model.UploadSuccess = false;
                                model.UploadMessage = String.Format(AppGlobal.Language.GetText(this, "MissingField",
                                                                                               "Required field '{0}' is missing from the CSV file"), field);
                            }
                        }
                        else
                        {
                            model.UploadSuccess = false;
                            model.UploadMessage = AppGlobal.Language.GetText(this, "InvalidLanguageFile",
                                                                             "Invalid language file");
                        }
                    }
                    else
                    {
                        model.UploadSuccess = false;
                        model.UploadMessage = AppGlobal.Language.GetText(this, "NoLanguageFile", "No language file");
                    }
                }
            }
            model.DownloadLanguageOptions = language.GetLanguageSelectList(model.DownloadLanguageId);
            model.UploadLanguageOptions   = language.GetLanguageSelectList(model.UploadLanguageId);

            return(View(model));
        }