コード例 #1
0
        public ActionResult edit(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" }) == 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  optionTypeId    = Convert.ToInt32(collection["txtId"]);
            string optionTypeTitle = collection["txtTitle"];
            string google_name     = collection["selectGoogleName"];

            string[] optionIds    = collection.GetValues("optionId");
            string[] optionTitles = collection.GetValues("optionTitle");
            string[] optionSuffix = collection.GetValues("optionSuffix");

            // Get the default admin language id
            Int32 adminLanguageId = currentDomain.back_end_language;

            // Get translated texts
            KeyStringList tt = StaticText.GetAll(adminLanguageId, "id", "ASC");

            // Get the option type
            OptionType optionType = OptionType.GetOneById(optionTypeId, adminLanguageId);

            // Check if the option type exists
            if (optionType == null)
            {
                // Create the option type
                optionType = new OptionType();
            }

            // Update values
            optionType.title       = optionTypeTitle;
            optionType.google_name = google_name;

            // Count the options
            Int32 optionCount = optionIds != null ? optionIds.Length : 0;

            // Create the list of options
            List <Option> options = new List <Option>(optionCount);

            // Add all options to the list
            for (int i = 0; i < optionCount; i++)
            {
                if (optionTitles[i] != string.Empty)
                {
                    // Create a option
                    Option option = new Option();
                    option.id    = Convert.ToInt32(optionIds[i]);
                    option.title = optionTitles[i];
                    option.product_code_suffix = optionSuffix[i];
                    option.sort_order          = Convert.ToInt16(i);
                    option.option_type_id      = optionType.id;

                    // Add the option to the list
                    options.Add(option);
                }
            }

            // Create a error message
            string errorMessage = string.Empty;

            // Check for errors in the option type
            if (optionType.title.Length > 200)
            {
                errorMessage += "&#149; " + String.Format(tt.Get("error_field_length"), tt.Get("title"), "100") + "<br/>";
            }
            if (optionType.google_name.Length > 50)
            {
                errorMessage += "&#149; " + String.Format(tt.Get("error_field_length"), tt.Get("google_name"), "50") + "<br/>";
            }

            // Check for errors in options
            foreach (Option option in options)
            {
                if (option.title.Length > 50)
                {
                    errorMessage += "&#149; " + String.Format(tt.Get("error_field_length"), option.title, "50") + "<br/>";
                }
                if (option.product_code_suffix.Length > 10)
                {
                    errorMessage += "&#149; " + String.Format(tt.Get("error_field_length"), option.product_code_suffix, "10") + "<br/>";
                }
            }

            // Check if there is errors
            if (errorMessage == string.Empty)
            {
                // Check if we should add or update the option
                if (optionType.id == 0)
                {
                    // Add the option
                    AddOption(optionType, options, adminLanguageId);
                }
                else
                {
                    // Update the option
                    UpdateOption(optionType, options, adminLanguageId);
                }

                // Update the option count
                OptionType.UpdateCount(optionType.id);

                // Redirect the user to the list
                return(Redirect("/admin_options" + returnUrl));
            }
            else
            {
                // Set form values
                ViewBag.ErrorMessage    = errorMessage;
                ViewBag.OptionType      = optionType;
                ViewBag.Options         = options;
                ViewBag.TranslatedTexts = tt;
                ViewBag.ReturnUrl       = returnUrl;

                // Return the edit view
                return(View("edit"));
            }
        } // End of the edit method