public HttpResponseMessage add(Option post, Int32 languageId = 0) { // Check for errors if (post == null) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The post is null")); } else if (Language.MasterPostExists(languageId) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The language does not exist")); } else if (OptionType.MasterPostExists(post.option_type_id) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The option type does not exist")); } // Make sure that the data is valid post.title = AnnytabDataValidation.TruncateString(post.title, 50); post.product_code_suffix = AnnytabDataValidation.TruncateString(post.product_code_suffix, 10); // Add the post Int64 insertId = Option.AddMasterPost(post); post.id = Convert.ToInt32(insertId); Option.AddLanguagePost(post, languageId); // Return the success response return(Request.CreateResponse <string>(HttpStatusCode.OK, "The post has been added")); } // End of the add method
} // End of the AddOption method /// <summary> /// Update the option in the database /// </summary> /// <param name="optionType">A reference to a option type</param> /// <param name="options">A list of options</param> /// <param name="languageId">A language id</param> private void UpdateOption(OptionType optionType, List <Option> options, Int32 languageId) { // Update the option type OptionType.UpdateMasterPost(optionType); OptionType.UpdateLanguagePost(optionType, languageId); // Get all the saved options List <Option> savedOptions = Option.GetByOptionTypeId(optionType.id, languageId); // Update or add options foreach (Option option in options) { // Get the saved option Option savedOption = Option.GetOneById(option.id, languageId); if (savedOption != null) { Option.UpdateMasterPost(option); Option.UpdateLanguagePost(option, languageId); } else { long insertId = Option.AddMasterPost(option); option.id = Convert.ToInt32(insertId); Option.AddLanguagePost(option, languageId); } } // Delete options foreach (Option savedOption in savedOptions) { // A boolean to indicate if the id is found bool idFound = false; // Loop all the input options foreach (Option option in options) { // Id has been found if (savedOption.id == option.id) { idFound = true; } } // Delete the id if has not been found if (idFound == false) { Option.DeleteOnId(savedOption.id); } } } // End of the Update Option method
} // End of the delete method #endregion #region Helper methods /// <summary> /// Add the option to the database /// </summary> /// <param name="optionType">A reference to a option type</param> /// <param name="options">A list of options</param> /// <param name="languageId">A language id</param> private void AddOption(OptionType optionType, List <Option> options, Int32 languageId) { // Save the option type long insertId = OptionType.AddMasterPost(optionType); optionType.id = Convert.ToInt32(insertId); OptionType.AddLanguagePost(optionType, languageId); // Save all the options foreach (Option option in options) { option.option_type_id = optionType.id; insertId = Option.AddMasterPost(option); option.id = Convert.ToInt32(insertId); Option.AddLanguagePost(option, languageId); } } // End of the AddOption method
public HttpResponseMessage translate(Option post, Int32 languageId = 0) { // Check for errors if (post == null) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The post is null")); } else if (Language.MasterPostExists(languageId) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The language does not exist")); } else if (Option.MasterPostExists(post.id) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The option does not exist")); } // Make sure that the data is valid post.title = AnnytabDataValidation.TruncateString(post.title, 50); post.product_code_suffix = AnnytabDataValidation.TruncateString(post.product_code_suffix, 10); // Get the post Option savedPost = Option.GetOneById(post.id, languageId); // Check if we should add or update the post if (savedPost == null) { Option.AddLanguagePost(post, languageId); } else { Option.UpdateLanguagePost(post, languageId); } // Return the success response return(Request.CreateResponse <string>(HttpStatusCode.OK, "The translate was successful")); } // End of the translate method
public ActionResult translate(FormCollection collection) { // Get the current domain Domain currentDomain = Tools.GetCurrentDomain(); ViewBag.CurrentDomain = currentDomain; // Get query parameters string returnUrl = collection["returnUrl"]; ViewBag.QueryParams = new QueryParams(returnUrl); // Check if the administrator is authorized if (Administrator.IsAuthorized(new string[] { "Administrator", "Editor", "Translator" }) == true) { ViewBag.AdminSession = true; } else if (Administrator.IsAuthorized(Administrator.GetAllAdminRoles()) == true) { ViewBag.AdminSession = true; ViewBag.AdminErrorCode = 1; ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC"); return(View("index")); } else { // Redirect the user to the start page return(RedirectToAction("index", "admin_login")); } // Get the admin default language Int32 adminLanguageId = currentDomain.back_end_language; // Get translated texts KeyStringList tt = StaticText.GetAll(adminLanguageId, "id", "ASC"); // Get all the form values Int32 translationLanguageId = Convert.ToInt32(collection["selectLanguage"]); Int32 optionTypeId = Convert.ToInt32(collection["hiddenOptionTypeId"]); string optionTypeTitle = collection["txtTranslatedTitle"]; string[] optionIds = collection.GetValues("optionId"); string[] optionTitles = collection.GetValues("optionTranslatedTitle"); // Create the translated option type OptionType translatedOptionType = new OptionType(); translatedOptionType.id = optionTypeId; translatedOptionType.title = optionTypeTitle; // Create a list of translated options Int32 optionCount = optionIds != null ? optionIds.Length : 0; List <Option> translatedOptions = new List <Option>(optionCount); for (int i = 0; i < optionCount; i++) { // Create a new option Option translatedOption = new Option(); translatedOption.id = Convert.ToInt32(optionIds[i]); translatedOption.title = optionTitles[i]; translatedOption.option_type_id = optionTypeId; // Add the translated option translatedOptions.Add(translatedOption); } // Create a error message string errorMessage = string.Empty; // Check the option type title if (optionTypeTitle.Length > 200) { errorMessage += "• " + String.Format(tt.Get("error_field_length"), tt.Get("title"), "200") + "<br/>"; } // Check for errors in options for (int i = 0; i < optionCount; i++) { if (optionTitles[i].Length > 50) { errorMessage += "• " + String.Format(tt.Get("error_field_length"), optionTitles[i], "50") + "<br/>"; } } // Check if there is errors if (errorMessage == string.Empty) { // Get the saved option type OptionType optionType = OptionType.GetOneById(optionTypeId, translationLanguageId); if (optionType == null) { // Add a new translated option type OptionType.AddLanguagePost(translatedOptionType, translationLanguageId); } else { // Update the translated option type optionType.title = translatedOptionType.title; OptionType.UpdateLanguagePost(optionType, translationLanguageId); } // Translate options for (int i = 0; i < translatedOptions.Count; i++) { // Get the option Option option = Option.GetOneById(translatedOptions[i].id, translationLanguageId); if (option == null) { // Add the translated option Option.AddLanguagePost(translatedOptions[i], translationLanguageId); } else { // Update the option option.title = translatedOptions[i].title; Option.UpdateLanguagePost(option, translationLanguageId); } } // Redirect the user to the list return(Redirect("/admin_options" + returnUrl)); } else { // Set form values ViewBag.LanguageId = translationLanguageId; ViewBag.Languages = Language.GetAll(adminLanguageId, "name", "ASC"); ViewBag.StandardOptionType = OptionType.GetOneById(optionTypeId, adminLanguageId); ViewBag.StandardOptions = Option.GetByOptionTypeId(optionTypeId, adminLanguageId); ViewBag.TranslatedOptionType = translatedOptionType; ViewBag.TranslatedOptions = translatedOptions; ViewBag.ErrorMessage = errorMessage; ViewBag.TranslatedTexts = tt; ViewBag.ReturnUrl = returnUrl; // Return the translate view return(View("translate")); } } // End of the translate method