Ejemplo n.º 1
0
        public ActionResult Delete(string[] ids)
        {
            try
            {
                if (ids.Length != 0)
                {
                    var attributeValues =
                        from id in ids
                        select _attributeValueService.GetById(int.Parse(id));

                    _attributeValueService.BatchDelete(attributeValues);
                }
            }
            catch (Exception exception1)
            {
                var exception = exception1;
                LogText.Log(string.Concat("District.Delete: ", exception.Message));
            }
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public ActionResult Create(PostViewModel model, string returnUrl)
        {
            ActionResult action;

            try
            {
                if (!ModelState.IsValid)
                {
                    var messages = String.Join(Environment.NewLine, ModelState.Values.SelectMany(v => v.Errors)
                                               .Select(v => v.ErrorMessage + " " + v.Exception));
                    ModelState.AddModelError("", messages);
                    return(View(model));
                }

                var titleNonAccent = model.Title.NonAccent();
                var bySeoUrl       = _postService.GetListSeoUrl(titleNonAccent);

                model.SeoUrl = model.Title.NonAccent();
                if (bySeoUrl.Any(x => x.Id != model.Id))
                {
                    var postViewModel = model;
                    postViewModel.SeoUrl = string.Concat(postViewModel.SeoUrl, "-", bySeoUrl.Count());
                }

                var folderName = Utils.FolderName(model.Title);
                if (model.Image != null && model.Image.ContentLength > 0)
                {
                    var fileNameOriginal = Path.GetFileNameWithoutExtension(model.Image.FileName);
                    var fileExtension    = Path.GetExtension(model.Image.FileName);

                    var fileName1 = fileNameOriginal.FileNameFormat(fileExtension);
                    var fileName2 = fileNameOriginal.FileNameFormat(fileExtension);
                    var fileName3 = fileNameOriginal.FileNameFormat(fileExtension);

                    _imagePlugin.CropAndResizeImage(model.Image, $"{Contains.PostFolder}{folderName}/", fileName1, ImageSize.WithBigSize, ImageSize.HeightBigSize, true);
                    _imagePlugin.CropAndResizeImage(model.Image, $"{Contains.PostFolder}{folderName}/", fileName2, ImageSize.WithMediumSize, ImageSize.HeightMediumSize, true);
                    _imagePlugin.CropAndResizeImage(model.Image, $"{Contains.PostFolder}{folderName}/", fileName3, ImageSize.WithSmallSize, ImageSize.HeightSmallSize, true);

                    model.ImageBigSize    = $"{Contains.PostFolder}{folderName}/{fileName1}";
                    model.ImageMediumSize = $"{Contains.PostFolder}{folderName}/{fileName2}";
                    model.ImageSmallSize  = $"{Contains.PostFolder}{folderName}/{fileName3}";
                }

                var menuId = model.MenuId;
                var i      = 0;
                if (menuId.GetValueOrDefault() > i && menuId.HasValue)
                {
                    var menuLinkService = _menuLinkService;
                    menuId = model.MenuId;
                    var byId = menuLinkService.GetById(menuId.Value);
                    model.VirtualCatUrl     = byId.VirtualSeoUrl;
                    model.VirtualCategoryId = byId.VirtualId;
                }

                //Gallery image
                var files         = Request.Files;
                var galleryImages = new List <GalleryImage>();
                if (files.Count > 0)
                {
                    var count   = files.Count - 1;
                    var num     = 0;
                    var allKeys = files.AllKeys;
                    for (i = 0; i < allKeys.Length; i++)
                    {
                        var str7 = allKeys[i];
                        if (num <= count)
                        {
                            if (!str7.Equals("Image"))
                            {
                                var str8 = str7.Replace("[]", "");
                                var item = files[num];
                                if (item.ContentLength > 0)
                                {
                                    var item1 = Request[str8];
                                    var galleryImageViewModel = new GalleryImageViewModel
                                    {
                                        PostId           = model.Id,
                                        AttributeValueId = int.Parse(str8)
                                    };

                                    var fileNameOriginal = Path.GetFileNameWithoutExtension(item.FileName);
                                    var fileExtension    = Path.GetExtension(item.FileName);

                                    var fileName1 = $"attr.{ fileNameOriginal}".FileNameFormat(fileExtension);
                                    var fileName2 = $"attr.{ fileNameOriginal}".FileNameFormat(fileExtension);

                                    _imagePlugin.CropAndResizeImage(item, $"{Contains.PostFolder}{folderName}/", fileName1, ImageSize.WithBigSize, ImageSize.HeightBigSize, true);
                                    _imagePlugin.CropAndResizeImage(item, $"{Contains.PostFolder}{folderName}/", fileName2, ImageSize.WithThumbnailSize, ImageSize.HeightThumbnailSize, true);

                                    galleryImageViewModel.ImageBig       = $"{Contains.PostFolder}{folderName}/{fileName1}";
                                    galleryImageViewModel.ImageThumbnail = $"{Contains.PostFolder}{folderName}/{fileName2}";

                                    galleryImageViewModel.OrderDisplay = num;
                                    galleryImageViewModel.Status       = 1;
                                    galleryImageViewModel.Title        = model.Title;
                                    galleryImageViewModel.Price        = double.Parse(item1);

                                    galleryImages.Add(Mapper.Map <GalleryImage>(galleryImageViewModel));
                                }
                                num++;
                            }
                            else
                            {
                                num++;
                            }
                        }
                    }
                }

                //Attribute
                var attributeValues = new List <AttributeValue>();
                var item2           = Request["Values"];
                if (!string.IsNullOrEmpty(item2))
                {
                    foreach (var list in item2.Split(',').ToList())
                    {
                        var num1 = int.Parse(list);
                        attributeValues.Add(_attributeValueService.GetById(num1));
                    }
                }

                var modelMap = Mapper.Map <PostViewModel, Post>(model);
                if (galleryImages.IsAny())
                {
                    modelMap.GalleryImages = galleryImages;
                }
                if (attributeValues.IsAny())
                {
                    modelMap.AttributeValues = attributeValues;
                }

                _postService.Create(modelMap);

                //Update Localized
                foreach (var localized in model.Locales)
                {
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.Title, localized.Title, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.ProductCode, localized.ProductCode, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.ShortDesc, localized.ShortDesc, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.Description, localized.Description, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.TechInfo, localized.TechInfo, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.SeoUrl, localized.SeoUrl, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.MetaTitle, localized.MetaTitle, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.MetaKeywords, localized.MetaKeywords, localized.LanguageId);
                    _localizedPropertyService.SaveLocalizedValue(modelMap, x => x.MetaDescription, localized.MetaDescription, localized.LanguageId);
                }

                Response.Cookies.Add(new HttpCookie("system_message", string.Format(MessageUI.CreateSuccess, FormUI.Post)));
                if (!Url.IsLocalUrl(returnUrl) || returnUrl.Length <= 1 || !returnUrl.StartsWith("/") || returnUrl.StartsWith("//") || returnUrl.StartsWith("/\\"))
                {
                    action = RedirectToAction("Index");
                }
                else
                {
                    action = Redirect(returnUrl);
                }
                //else
                //{
                //	base.ModelState.AddModelError("", "Mã sản phẩm đã tồn tại.");
                //	action = base.View(model);
                //}
            }
            catch (Exception ex)
            {
                ExtentionUtils.Log(string.Concat("Post.Create: ", ex.Message));
                ModelState.AddModelError("", ex.Message);

                return(View(model));
            }
            return(action);
        }