예제 #1
0
        public ActionResult edit_rating(FormCollection collection)
        {
            // Make sure that the user is signed in
            Administrator user = Administrator.GetSignedInAdministrator();

            // Get the current domain
            Domain domain = Tools.GetCurrentDomain();

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

            // Check if the post request is valid
            if (user == null || collection == null)
            {
                return RedirectToAction("login", "user");
            }

            // Get the form data
            Int32 post_id = Convert.ToInt32(collection["hiddenPostId"]);
            Int32 language_id = Convert.ToInt32(collection["hiddenLanguageId"]);
            decimal userVote = 0;
            decimal.TryParse(collection["userVote"], NumberStyles.Any, CultureInfo.InvariantCulture, out userVote);

            // Get the post
            Post post = Post.GetOneById(post_id, language_id);

            // Try to get a saved rating
            PostRating postRating = PostRating.GetOneById(post_id, user.id, language_id);

            // Add or update the rating
            if (postRating != null && postRating.administrator_id == user.id)
            {
                // Update values
                postRating.rating_date = DateTime.UtcNow;
                postRating.rating = userVote;

                // Update the rating
                PostRating.Update(postRating);
            }
            else
            {
                // Create a new rating
                postRating = new PostRating();

                // Update values
                postRating.post_id = post_id;
                postRating.administrator_id = user.id;
                postRating.language_id = language_id;
                postRating.rating_date = DateTime.UtcNow;
                postRating.rating = userVote;

                // Add the rating
                PostRating.Add(postRating);
            }

            // Send a email to the administrator of the website
            string subject = tt.Get("rating") + " - " + domain.website_name;
            string message = tt.Get("post") + ": " + postRating.post_id.ToString() + "<br />"
                + tt.Get("language") + ": " + postRating.language_id.ToString() + "<br />"
                + tt.Get("user_name") + ": " + user.admin_user_name + "<br />" 
                + tt.Get("rating") + ": " + postRating.rating.ToString();
            Tools.SendEmailToHost("", subject, message);

            // Update the rating for the post
            Post.UpdateRating(postRating.post_id, postRating.language_id);

            // Redirect the user to the post
            return Redirect("/home/post/" + post.page_name + "#comments");

        } // End of the edit_rating method
        public ActionResult edit(FormCollection collection)
        {
            // Get the current domain
            Domain currentDomain = Tools.GetCurrentDomain();
            ViewBag.CurrentDomain = currentDomain;

            // Get all the form values
            Int32 post_id = Convert.ToInt32(collection["hiddenPostId"]);
            Int32 administrator_id = Convert.ToInt32(collection["hiddenAdministratorId"]);
            Int32 language_id = Convert.ToInt32(collection["hiddenLanguageId"]);
            decimal rating = 0;
            decimal.TryParse(collection["userVote"].Replace(",", "."), NumberStyles.Any, CultureInfo.InvariantCulture, out rating);
            string returnUrl = collection["returnUrl"];

            // Get query parameters
            ViewBag.QueryParams = new QueryParams(returnUrl);

            // Get the signed in administrator
            Administrator administrator = Administrator.GetSignedInAdministrator();

            // Get the post rating
            PostRating postRating = PostRating.GetOneById(post_id, administrator_id, language_id);

            // Check if the administrator is authorized
            if (Administrator.IsAuthorized(new string[] { "Administrator", "Editor" }) == true)
            {
                ViewBag.AdminSession = true;
            }
            else if (administrator != null && administrator.admin_role == "Author" && 
                (postRating == null || postRating.administrator_id == administrator.id))
            {
                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");
            }

            // Update the post rating
            if (postRating != null)
            {
                // Update the rating for the post
                postRating.rating = rating;
                PostRating.Update(postRating);

                // Update the rating sum for the post
                Post.UpdateRating(post_id, language_id);
            }

            // Redirect the user to the list
            return Redirect(returnUrl);

        } // End of the edit method