示例#1
0
        public ActionResult Create(InsertTipViewModel vm)
        {
            if (ModelState.IsValid)
            {
                vm.Tip.ContentHtml = vm.Tip.ContentHtml.GetSafeHtml(Constants.AllowedHtmlTags, Constants.AllowedHtmlAttributes);
                if (vm.Tip.ContentHtml.RemoveHtml() == string.Empty)
                {
                    vm = PrepareTipCategories(vm);
                    SetMessage(Messages.ContentIsEmpty, MessageType.Error);
                    return View(vm);
                }

                vm.Tip.ImageUrl = TipImageUrl(vm);
                if (vm.Tip.ImageUrl == null)
                {
                    vm = PrepareTipCategories(vm);
                    return View(vm);
                }

                vm.Tip.TitleUrl = vm.Tip.Title.ToUrlFriendly();
                vm.Tip.PostedById = CurrentUser.UserInfo.Id;

                var feedback = _tipRepository.InsertTip(vm.Tip);

                if (feedback.Success)
                {
                    bool isPointChanged = CurrentUser.UserInfo.Point != feedback.NewPoint;
                    CurrentUser.UserInfo.Point = feedback.NewPoint;
                    if (isPointChanged)
                    {
                        SetMessage(string.Format("Cảm ơn bạn đã đóng góp mẹo vặt mới. Bạn đã được cộng thêm {0} điểm.", ReferenceDataCache.PointConfigCollection.CreateTip), MessageType.Success);
                    }
                    else
                    {
                        SetMessage("Cảm ơn bạn đã đóng góp mẹo vặt mới. Tuy nhiên bạn không được cộng thêm điểm vì bạn đã đạt số điểm tối đa cho phép một ngày.", MessageType.Info);
                    }

                    return RedirectToAction("Details", new { id = feedback.Tip.Id, tipTitleUrl = feedback.Tip.TitleUrl });
                }

                // If update to DB unsuccessfully, delete new image
                if (!string.IsNullOrEmpty(vm.Tip.ImageUrl))
                {
                    _fileManager.DeleteSingleThumbnailImage(vm.Tip.ImageUrl);
                    vm.Tip.ImageUrl = null;
                }
                SetMessage(feedback.Message, MessageType.Error);
            }

            vm = PrepareTipCategories(vm);

            return View(vm);
        }
示例#2
0
        private string TipImageUrl(InsertTipViewModel vm)
        {
            string newUrl = null;
            if (vm.File == null)
            {
                string msg = "Ảnh đại diện là bắt buộc";
                SetMessage(msg, MessageType.Error);

                return null;
            }

            var uploadFeedback = _uploadHandler.UploadImage(vm.File);
            if (!uploadFeedback.Success)
            {
                string msg = uploadFeedback.Message;
                //string msg = string.Format("Chỉ hỗ trợ ảnh định dạng JPG và PNG. " +
                //      "Kích thước ảnh không được vượt quá {0}Mb. Nếu lỗi vẫn xảy ra bạn hãy thử lại vào lúc khác.",
                //      AppConfigs.UploadImageMaxSize);
                SetMessage(msg, MessageType.Error);

                return null;
            }

            //newUrl = string.Format("{0}{1}{2}", uploadFeedback.Data[0], Constants.ImageUrlsSeparator,
            //                              uploadFeedback.Data[1]);
            newUrl = string.Format("{0}", uploadFeedback.Data);
            return newUrl;
        }
示例#3
0
        private InsertTipViewModel PrepareTipCategories(InsertTipViewModel viewModel)
        {
            var tipCategories = ReferenceDataCache.TipCategoryCollection.ToList();

            if (viewModel == null)
            {
                viewModel = new InsertTipViewModel
                                {
                                    Tip = new Tip(),
                                    TipCategories = tipCategories
                                };

            }
            else
            {
                viewModel.TipCategories = tipCategories;
            }
            return viewModel;
        }
示例#4
0
        public ActionResult Edit(InsertTipViewModel vm)
        {
            if (ModelState.IsValid)
            {
                vm.Tip.ContentHtml = vm.Tip.ContentHtml.GetSafeHtml(Constants.AllowedHtmlTags, Constants.AllowedHtmlAttributes);
                if (vm.Tip.ContentHtml.RemoveHtml() == string.Empty)
                {
                    vm = PrepareTipCategories(vm);
                    SetMessage(Messages.ContentIsEmpty, MessageType.Error);
                    return View(vm);
                }

                if (vm.Tip.ImageUrl == null)
                {
                    vm.Tip.ImageUrl = TipImageUrl(vm);
                }

                if (vm.Tip.ImageUrl == null)
                {
                    vm = PrepareTipCategories(vm);
                    return View(vm);
                }

                vm.Tip.UpdatedById = CurrentUser.UserInfo.Id;

                var feedback = _tipRepository.UpdateTip(vm.Tip);

                if (feedback.Success)
                {
                    if (vm.OldImageUrl != null)
                    {
                        _fileManager.DeleteSingleThumbnailImage(vm.OldImageUrl);
                    }
                    return RedirectToAction("Details", new { id = feedback.Data.Id, tipTitleUrl = feedback.Data.TitleUrl });
                }

                    // If update to DB unsuccessfully, delete new image
                if (!string.IsNullOrEmpty(vm.Tip.ImageUrl))
                {
                    _fileManager.DeleteSingleThumbnailImage(vm.Tip.ImageUrl);
                    vm.Tip.ImageUrl = null;
                }

                SetMessage(feedback.Message, MessageType.Error);
            }

            vm = PrepareTipCategories(vm);
            return View(vm);
        }
示例#5
0
        public ViewResult Edit(int id)
        {
            InsertTipViewModel vm = null;
            var feedback = _tipRepository.GetTipForEditing(id);
            if (feedback.Success)
            {
                if (feedback.Data.PostedById != CurrentUser.UserInfo.Id && !SecurityHelper.CanEditContentsDirectly())
                {
                    SetMessage(Messages.EditTip_NotAllowed, MessageType.Error);
                    return View(vm);
                }

                vm = new InsertTipViewModel
                {
                    Tip = feedback.Data,
                };
                vm = PrepareTipCategories(vm);
                return View(vm);
            }
            SetMessage(feedback.Message, MessageType.Error);
            return View(vm);
        }
示例#6
0
        public ActionResult DeleteImage(int tipId = 0)
        {
            InsertTipViewModel vm = null;
            var feedback = _tipRepository.GetTipForEditing(tipId);
            if (feedback.Success)
            {
                if (feedback.Data.PostedById != CurrentUser.UserInfo.Id && !SecurityHelper.CanEditContentsDirectly())
                {
                    SetMessage(Messages.EditNews_NotAllowed, MessageType.Error);
                    return View(vm);
                }

                vm = new InsertTipViewModel
                {
                    Tip = feedback.Data,
                };
            }

            if (vm != null)
            {
                vm.OldImageUrl = vm.Tip.ImageUrl;
                vm.Tip.ImageUrl = null;
            }

            return PartialView("_UploadImage", vm);
        }