public HttpResponseMessage add(Campaign post) { // Check for errors if (post == null) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The post is null")); } else if (Language.MasterPostExists(post.language_id) == false) { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The language does not exist")); } // Make sure that the data is valid post.name = AnnytabDataValidation.TruncateString(post.name, 50); post.category_name = AnnytabDataValidation.TruncateString(post.category_name, 50); post.image_name = AnnytabDataValidation.TruncateString(post.image_name, 100); post.link_url = AnnytabDataValidation.TruncateString(post.link_url, 200); // Add the post Campaign.Add(post); // Return the success response return(Request.CreateResponse <string>(HttpStatusCode.OK, "The post has been added")); } // End of the add method
public ActionResult edit(FormCollection collection) { // Get the current domain Domain currentDomain = Tools.GetCurrentDomain(); ViewBag.CurrentDomain = currentDomain; // Get the return url string returnUrl = collection["returnUrl"]; ViewBag.QueryParams = new QueryParams(returnUrl); // Check if the administrator is authorized if (Administrator.IsAuthorized(new string[] { "Administrator", "Editor" }) == 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 all the form values Int32 id = Convert.ToInt32(collection["txtId"]); Int32 languageId = Convert.ToInt32(collection["selectLanguage"]); string name = collection["txtName"]; string url = collection["txtUrl"]; string categoryName = collection["selectCategory"]; bool inactive = Convert.ToBoolean(collection["cbInactive"]); HttpPostedFileBase campaignImage = Request.Files["campaignImage"]; // Get the default admin language id Int32 adminLanguageId = currentDomain.back_end_language; // Get translated texts KeyStringList tt = StaticText.GetAll(adminLanguageId, "id", "ASC"); // Get the campaign Campaign campaign = Campaign.GetOneById(id); bool postExists = true; // Check if the campaign exists if (campaign == null) { // Create an empty campaign campaign = new Campaign(); postExists = false; } // Update values campaign.name = name; campaign.language_id = languageId; campaign.category_name = categoryName; campaign.link_url = url; campaign.inactive = inactive; // Get the image name string image_name = ""; if (campaignImage.ContentLength > 0) { image_name = System.IO.Path.GetFileName(campaignImage.FileName); } // Create a error message string errorMessage = string.Empty; // Check for errors in the campaign if(campaign.language_id == 0) { errorMessage += "• " + String.Format(tt.Get("error_select_value"), tt.Get("language").ToLower()) + "<br/>"; } if (campaign.name.Length > 50) { errorMessage += "• " + String.Format(tt.Get("error_field_length"), tt.Get("name"), "50") + "<br/>"; } if (campaign.link_url.Length > 200) { errorMessage += "• " + String.Format(tt.Get("error_field_length"), tt.Get("url"), "200") + "<br/>"; } if (image_name.Length > 100) { errorMessage += "• " + String.Format(tt.Get("error_field_length"), tt.Get("file_name"), "100") + "<br/>"; } // Check if there is errors if (errorMessage == string.Empty) { // Update the image if(campaignImage.ContentLength > 0) { UpdateCampaignImage(campaign, campaignImage); } // Check if we should add or update the campaign if (postExists == false) { // Add the campaign Campaign.Add(campaign); } else { // Update the campaign Campaign.Update(campaign); } // Redirect the user to the list return Redirect("/admin_campaigns" + returnUrl); } else { // Set form values ViewBag.ErrorMessage = errorMessage; ViewBag.TranslatedTexts = tt; ViewBag.Languages = Language.GetAll(currentDomain.back_end_language, "id", "ASC"); ViewBag.Campaign = campaign; ViewBag.ReturnUrl = returnUrl; // Return the edit view return View("edit"); } } // End of the edit method