public string AddNewCategory(string CatValue)
        {
            string id;

            using (Db db = new Db())
            {
                if (db.categories.Any(x => x.Name == CatValue))
                {
                    return("titletaken");
                }
                Categories newcat = new Categories();
                newcat.Name = CatValue;
                string newlink;
                newlink = CatValue.ToLower();
                Translitor translitor = new Translitor();
                foreach (KeyValuePair <string, string> pair in translitor._words)
                {
                    newlink = newlink.Replace(pair.Key, pair.Value);
                }
                newcat.Sorting = 100;
                newcat.Slug    = newlink;
                //newcat.Id = id;
                db.categories.Add(newcat);
                db.SaveChanges();
                id = newcat.Id.ToString();
            }
            return(id);
            //return View();
        }
        public ActionResult AddPage(PageViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            using (Db db = new Db())
            {
                string link;

                ShopPages shopPages = new ShopPages();
                shopPages.Title = model.Title;

                if (string.IsNullOrWhiteSpace(model.Link))
                {
                    link = model.Title.ToLower();
                    Translitor translitor = new Translitor();
                    foreach (KeyValuePair <string, string> pair in translitor._words)
                    {
                        link = link.Replace(pair.Key, pair.Value);
                    }
                }
                else
                {
                    //link = model.Link.ToLower().Replace(" ", "-").Replace("а", "a").Replace("б", "b")
                    //     .Replace("в", "v").Replace("г", "g").Replace("д", "d")
                    //    .Replace("е", "e").Replace("ж", "zh").Replace("з", "z").Replace("и", "i").Replace("й", "y").Replace("к", "k")
                    //    .Replace("л", "l").Replace("м", "m").Replace("н", "n").Replace("о", "o").Replace("п", "p").Replace("р", "r")
                    //    .Replace("с", "s").Replace("т", "t").Replace("у", "u").Replace("ф", "f").Replace("х", "h").Replace("ц", "c")
                    //    .Replace("ч", "ch").Replace("ш", "sh").Replace("ю", "u").Replace("я", "ya").Replace("ь", "").Replace("ъ", "");

                    //link = model.Link.Replace(" ", "-").ToLower();
                    link = model.Link.ToLower();
                    Translitor translitor = new Translitor();
                    foreach (KeyValuePair <string, string> pair in translitor._words)
                    {
                        link = link.Replace(pair.Key, pair.Value);
                    }
                }
                if (db.shoppages.Any(x => x.Title == model.Title) || db.shoppages.Any(x => x.Link == model.Link))
                {
                    ModelState.AddModelError(" ", "Такое название страницы или ссылка уже существуют ");
                    return(View(model));
                }
                shopPages.Content = model.Content;

                shopPages.Link       = link;
                shopPages.HasSidebar = model.HasSidebar;
                shopPages.Sorting    = 100;

                db.shoppages.Add(shopPages);
                db.SaveChanges();
            }
            TempData["SM"] = "Успешно добавлено";
            return(RedirectToAction("AddPage"));
        }
Exemplo n.º 3
0
        //[HttpPost("UploadFiles")]
        public IActionResult AddPost(ArticleViewModel art, IFormFile SomeFile)
        {
            using (Db db = new Db())
            {
                if (!ModelState.IsValid)
                {
                    return(View(art));
                }
                string   postlink;
                Articles newart = new Articles();
                newart.Title = art.Title;
                if (string.IsNullOrWhiteSpace(art.Slug))
                {
                    postlink = art.Title.ToLower();
                    Translitor translitor = new Translitor();
                    foreach (KeyValuePair <string, string> pair in translitor.words)
                    {
                        postlink = postlink.Replace(pair.Key, pair.Value);
                    }
                }
                else
                {
                    postlink = art.Slug.ToLower();
                    Translitor translitor = new Translitor();
                    foreach (KeyValuePair <string, string> pair in translitor.words)
                    {
                        postlink = postlink.Replace(pair.Key, pair.Value);
                    }
                }
                if (db.article.Any(x => x.Title == art.Title) || db.article.Any(x => x.Slug == art.Slug))
                {
                    ModelState.AddModelError(" ", "Такое название поста или ссылка уже существуют ");
                    return(View(art));
                }
                //newart.Urlimg = art.Urlimg;
                newart.Slug    = postlink;
                newart.Content = art.Content;
                newart.Sorting = 100;
                //Add post's picture
                var picblog = SomeFile;
                if (picblog != null)
                {
                    var    fileName    = Path.Combine(he.WebRootPath + "\\images\\", Path.GetFileName(picblog.FileName));
                    string picturelink = "/images/" + SomeFile.FileName;
                    picblog.CopyTo(new FileStream(fileName, FileMode.Create));
                    ViewData["filelocation"] = fileName;
                    newart.Urlimg            = picturelink;
                }

                db.article.Add(newart);
                db.SaveChanges();
            }
            TempData["SM"] = "Успешно добавлено";
            return(RedirectToAction("AddPost"));
        }
 public string ChangeCategory(string newmytextcat, int id)
 {
     using (Db db = new Db())
     {
         Categories changecat = db.categories.Find(id);
         if (db.categories.Any(x => x.Name == newmytextcat))
         {
             return("titletaken");
         }
         changecat.Name = newmytextcat;
         string newslug;
         newslug = newmytextcat.ToLower();
         Translitor translitor = new Translitor();
         foreach (KeyValuePair <string, string> pair in translitor._words)
         {
             newslug = newslug.Replace(pair.Key, pair.Value);
         }
         changecat.Slug = newslug;
         db.SaveChanges();
     }
     return(newmytextcat);
 }
        public ActionResult EditProduct(ProductViewModel productViewModel, HttpPostedFileBase file)
        {
            int id = productViewModel.Id;

            using (Db db = new Db())
            {
                productViewModel.Categories = new SelectList(db.categories.ToList(), "Id", "Name");

                //return View(productViewModel);
            }
            productViewModel.MyPictures = Directory.EnumerateFiles(Server.MapPath("~/Content/images/Upload/Products/" + id + "/Gallery/Thumbs"))
                                          .Select(y => Path.GetFileName(y));
            if (!ModelState.IsValid)
            {
                return(View(productViewModel));
            }
            using (Db db = new Db())
            {
                if (db.product.Where(x => x.Id != id).Any(x => x.Name == productViewModel.Name) || db.product.Where(x => x.Id != id).Any(x => x.Slug == productViewModel.Slug))
                {
                    productViewModel.Categories = new SelectList(db.categories.ToList(), "Id", "Name");

                    ModelState.AddModelError("Такое название товара или ссылка уже существуют ", " ");
                    return(View(productViewModel));
                }
            }



            using (Db db = new Db())
            {
                Products changeprod = db.product.Find(id);
                changeprod.Name = productViewModel.Name;
                string slugprod;
                if (string.IsNullOrWhiteSpace(productViewModel.Slug))
                {
                    slugprod = productViewModel.Name.ToLower();
                    Translitor translitor = new Translitor();
                    foreach (KeyValuePair <string, string> pair in translitor._words)
                    {
                        slugprod = slugprod.Replace(pair.Key, pair.Value);
                    }
                }
                else
                {
                    slugprod = productViewModel.Slug.ToLower();
                    Translitor translitor = new Translitor();
                    foreach (KeyValuePair <string, string> pair in translitor._words)
                    {
                        slugprod = slugprod.Replace(pair.Key, pair.Value);
                    }
                }

                changeprod.Slug        = slugprod;
                changeprod.Price       = productViewModel.Price;
                changeprod.Description = productViewModel.Description;
                changeprod.CategoryId  = productViewModel.CategoryId;
                //changeprod.ImageString = productViewModel.ImageString;
                Categories newcat = db.categories.FirstOrDefault(x => x.Id == productViewModel.CategoryId);
                changeprod.CategoryName = newcat.Name;

                db.SaveChanges();
                //id = newpr.Id;
            }
            TempData["EditProd"] = "Товар изменен";
            #region Update photo
            if (file != null && file.ContentLength > 0)
            {
                string extention = file.ContentType.ToLower();
                if (extention != "image/jpg" &&
                    extention != "image/png" &&
                    extention != "image/jpeg" &&
                    extention != "image/gif" &&
                    extention != "image/x-png" &&
                    extention != "image/pjpeg")
                {
                    using (Db db = new Db())
                    {
                        //productViewModel.Categories = new SelectList(db.categories.ToList(), "Id", "Name");
                        ModelState.AddModelError(" ", "Неверный формат картинки");
                        return(View(productViewModel));
                    }
                }
                var imagedirectory = new DirectoryInfo(string.Format("{0}Content\\images\\Upload", Server.MapPath(@"\")));
                //var imagedirectory = new DirectoryInfo();


                //var namepicture1 = Path.Combine(imagedirectory.ToString(), "Products");
                var namepicture1 = Path.Combine(imagedirectory.ToString(), "Products\\" + id.ToString());
                var namepicture2 = Path.Combine(imagedirectory.ToString(), "Products\\" + id.ToString() + "\\Thumbs");
                //var namepicture4 = Path.Combine(imagedirectory.ToString(), "Products\\" + id.ToString() + "\\Gallery");
                //var namepicture5 = Path.Combine(imagedirectory.ToString(), "Products\\" + id.ToString() + "\\Gallery\\Thumbs");

                //Delete old file from directory
                DirectoryInfo fordel1 = new DirectoryInfo(namepicture1);
                DirectoryInfo fordel2 = new DirectoryInfo(namepicture2);
                foreach (FileInfo file1 in fordel1.GetFiles())
                {
                    file1.Delete();
                }
                foreach (FileInfo file2 in fordel2.GetFiles())
                {
                    file2.Delete();
                }
                //Save image name
                string filename = file.FileName;
                using (Db db = new Db())
                {
                    Products prodedit = db.product.Find(id);
                    prodedit.ImageString = filename;
                    db.SaveChanges();
                }
                var path1 = string.Format("{0}\\{1}", namepicture1, filename);
                var path2 = string.Format("{0}\\{1}", namepicture2, filename);
                file.SaveAs(path1);
                WebImage webImage = new WebImage(file.InputStream);
                webImage.Resize(200, 300);
                webImage.Save(path2);
            }
            #endregion
            return(RedirectToAction("EditProduct"));
            //return RedirectToAction("Products");
        }
        public ActionResult AddNewProduct(ProductViewModel newprod, HttpPostedFileBase newfile)
        //public ActionResult AddNewProduct(ProductViewModel newprod, )
        {
            if (!ModelState.IsValid)
            {
                using (Db db = new Db())
                {
                    newprod.Categories = new SelectList(db.categories.ToList(), "Id", "Name");
                    return(View(newprod));
                }
            }
            int id;

            using (Db db = new Db())
            {
                Products newpr = new Products();
                newpr.Name = newprod.Name;
                string linkprod;
                if (string.IsNullOrWhiteSpace(newprod.Slug))
                {
                    linkprod = newprod.Name.ToLower();
                    Translitor translitor = new Translitor();
                    foreach (KeyValuePair <string, string> pair in translitor._words)
                    {
                        linkprod = linkprod.Replace(pair.Key, pair.Value);
                    }
                }
                else
                {
                    linkprod = newprod.Slug.ToLower();
                    Translitor translitor = new Translitor();
                    foreach (KeyValuePair <string, string> pair in translitor._words)
                    {
                        linkprod = linkprod.Replace(pair.Key, pair.Value);
                    }
                }
                if (db.product.Any(x => x.Name == newprod.Name) || db.product.Any(x => x.Slug == newprod.Slug))
                {
                    newprod.Categories = new SelectList(db.categories.ToList(), "Id", "Name");

                    ModelState.AddModelError("Такое название страницы или ссылка уже существуют ", " ");
                    return(View(newprod));
                }
                newpr.Slug        = linkprod;
                newpr.Price       = newprod.Price;
                newpr.Description = newprod.Description;
                newpr.CategoryId  = newprod.CategoryId;
                Categories newcat = db.categories.FirstOrDefault(x => x.Id == newprod.CategoryId);
                newpr.CategoryName = newcat.Name;
                db.product.Add(newpr);
                db.SaveChanges();
                id = newpr.Id;
            }
            TempData["SMM"] = "Успешно добавлено";
            #region UploadPhoto
            //Directory for photos
            var imagedirectory = new DirectoryInfo(string.Format("{0}Content\\images\\Upload", Server.MapPath(@"\")));
            //var imagedirectory = new DirectoryInfo();


            var namepicture1 = Path.Combine(imagedirectory.ToString(), "Products");
            var namepicture2 = Path.Combine(imagedirectory.ToString(), "Products\\" + id.ToString());
            var namepicture3 = Path.Combine(imagedirectory.ToString(), "Products\\" + id.ToString() + "\\Thumbs");
            var namepicture4 = Path.Combine(imagedirectory.ToString(), "Products\\" + id.ToString() + "\\Gallery");
            var namepicture5 = Path.Combine(imagedirectory.ToString(), "Products\\" + id.ToString() + "\\Gallery\\Thumbs");
            if (!Directory.Exists(namepicture1))
            {
                Directory.CreateDirectory(namepicture1);
            }
            if (!Directory.Exists(namepicture2))
            {
                Directory.CreateDirectory(namepicture2);
            }
            if (!Directory.Exists(namepicture3))
            {
                Directory.CreateDirectory(namepicture3);
            }
            if (!Directory.Exists(namepicture4))
            {
                Directory.CreateDirectory(namepicture4);
            }
            if (!Directory.Exists(namepicture5))
            {
                Directory.CreateDirectory(namepicture5);
            }
            //Checking upload picture
            if (newfile != null && newfile.ContentLength > 0)
            {
                //Get extention of file
                var ext = newfile.ContentType.ToLower();
                //check the extention
                if (ext != "image/jpg" &&
                    ext != "image/png" &&
                    ext != "image/jpeg" &&
                    ext != "image/gif" &&
                    ext != "image/x-png" &&
                    ext != "image/pjpeg")
                {
                    using (Db db = new Db())
                    {
                        newprod.Categories = new SelectList(db.categories.ToList(), "Id", "Name");
                        ModelState.AddModelError(" ", "Неверный формат картинки");
                        return(View(newprod));
                    }
                }
                //Init name of picture
                string imgName = newfile.FileName;
                using (Db db = new Db())
                {
                    Products newpr = db.product.Find(id);
                    newpr.ImageString = imgName;
                    db.SaveChanges();
                }
                //Set unique filestring and extention
                var path  = string.Format("{0}\\{1}", namepicture2, imgName);
                var path2 = string.Format("{0}\\{1}", namepicture3, imgName);
                //var path3 = string.Format("{0}\\{1}", namepicture3, imgName);
                //var path4 = string.Format("{0}\\{1}", namepicture4, imgName);
                //var path5 = string.Format("{0}\\{1}", namepicture5, imgName);
                newfile.SaveAs(path);
                WebImage img = new WebImage(newfile.InputStream);
                img.Resize(200, 300);
                img.Save(path2);
            }
            #endregion
            TempData["Editprod"] = "Изменено";
            return(RedirectToAction("AddNewProduct"));
        }
        public Builder_Translation(string input, Builder_Locales locales)
        {
            localeStrings = locales;
            original      = input;
            translated    = input;
            //TODO - cut out comments
            foreach (NamesTranslated nt in locales.names_translated)
            {
                if (!string.IsNullOrWhiteSpace(nt.locale) && !string.IsNullOrWhiteSpace(nt.translated))
                {
                    //int ind = translated.IndexOf(nt.locale);
                    translated = translated.Replace(nt.locale, nt.translated);
                }
            }

            //Parse letter by letter, skip strings
            int    quoteIndex = 0;
            string output     = "";

            while (quoteIndex < translated.Length)
            {
                bool breakLoop = false;
                if (translated[quoteIndex] == '\"')
                {
                    if (quoteIndex == 0 || translated[quoteIndex - 1] != '\\')
                    {
                        //We have a quote start, find end
                        int quoteStart = quoteIndex;
                        ++quoteIndex;
                        while (!(translated[quoteIndex] == '\"' && translated[quoteIndex - 1] != '\\'))
                        {
                            quoteIndex++;
                            if (quoteIndex >= translated.Length)
                            {
                                //We reached the end
                                output   += translated.Substring(quoteStart);
                                breakLoop = true;
                                break;
                            }
                        }
                        if (breakLoop)
                        {
                            break;
                        }
                        //quoteIndex is pointing at the closing quote
                        output += translated.Substring(quoteStart, quoteIndex - quoteStart + 1);
                    }
                    else
                    {
                        output += '\"';
                    }
                }
                else
                {
                    string s = "";
                    s      += translated[quoteIndex];
                    output += Translitor.Translit(s);
                }
                ++quoteIndex;
            }
            translated = output;
        }
Exemplo n.º 8
0
        public IActionResult EditPost(ArticleViewModel articleViewModel, IFormFile SomeFileUpdate)
        {
            if (!ModelState.IsValid)
            {
                return(View(articleViewModel));
            }
            using (Db db = new Db())
            {
                int      id       = articleViewModel.Id;
                Articles articles = db.article.Find(id);
                articles.Title = articleViewModel.Title;
                string postlink;
                if (string.IsNullOrWhiteSpace(articleViewModel.Slug))
                {
                    postlink = articleViewModel.Title.ToLower();
                    Translitor translitor = new Translitor();
                    foreach (KeyValuePair <string, string> pair in translitor.words)
                    {
                        postlink = postlink.Replace(pair.Key, pair.Value);
                    }
                }
                else
                {
                    postlink = articleViewModel.Slug.ToLower();
                    Translitor translitor = new Translitor();
                    foreach (KeyValuePair <string, string> pair in translitor.words)
                    {
                        postlink = postlink.Replace(pair.Key, pair.Value);
                    }
                }
                if (db.article.Where(x => x.Id != id).Any(x => x.Title == articleViewModel.Title) ||
                    (db.article.Where(x => x.Id != id).Any(x => x.Slug == articleViewModel.Slug)))
                {
                    //return Content("Страница с такой ссылкой или названием уже существует");
                    //ModelState.AddModelError(" ", "Страница с такой ссылкой или названием уже существует");
                    TempData["Error"] = "Error";
                    return(View(articleViewModel));
                    //return RedirectToAction("Edit");
                }
                var picblog = SomeFileUpdate;
                //if (articles.Urlimg != null)
                //{
                if (picblog != null)
                {
                    //articleViewModel.Urlimg = null;

                    var    fileName    = Path.Combine(he.WebRootPath + "\\images\\", Path.GetFileName(picblog.FileName));
                    string picturelink = "/images/" + SomeFileUpdate.FileName;
                    picblog.CopyTo(new FileStream(fileName, FileMode.Create));
                    ViewData["filelocation"] = fileName;
                    articles.Urlimg          = picturelink;
                }
                else
                {
                    articles.Urlimg = articleViewModel.Urlimg;
                }
                //}
                //else
                //{
                //    articles.Urlimg =
                //}
                articles.Slug     = postlink;
                articles.Content  = articleViewModel.Content;
                articles.Timepost = articleViewModel.Timepost;
                db.SaveChanges();
            }
            TempData["SM"] = "Изменения сохранены";

            return(View("EditPost"));
        }
        public ActionResult Edit(PageViewModel pageViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(pageViewModel));
            }

            using (Db db = new Db())
            {
                int       id   = pageViewModel.Id;
                string    link = "home";
                ShopPages shop = db.shoppages.Find(id);

                shop.Title = pageViewModel.Title;
                if (pageViewModel.Link != "home")
                {
                    if (string.IsNullOrWhiteSpace(pageViewModel.Link))
                    {
                        link = pageViewModel.Title.ToLower();
                        Translitor translitor = new Translitor();
                        foreach (KeyValuePair <string, string> pair in translitor._words)
                        {
                            link = link.Replace(pair.Key, pair.Value);
                        }
                    }
                    else
                    {
                        link = pageViewModel.Link.ToLower();
                        Translitor translitor = new Translitor();
                        foreach (KeyValuePair <string, string> pair in translitor._words)
                        {
                            link = link.Replace(pair.Key, pair.Value);
                        }
                    }
                    //Проверим на повторяющие Title или Link из всего спсика
                    if (db.shoppages.Where(x => x.Id != id).Any(x => x.Title == pageViewModel.Title) ||
                        (db.shoppages.Where(x => x.Id != id).Any(x => x.Link == pageViewModel.Link)))
                    {
                        //return Content("Страница с такой ссылкой или названием уже существует");
                        //ModelState.AddModelError(" ", "Страница с такой ссылкой или названием уже существует");
                        TempData["Error"] = "Error";
                        return(View(pageViewModel));
                        //return RedirectToAction("Edit");
                    }

                    shop.Link = link;
                }
                shop.Content = pageViewModel.Content;
                shop.Title   = pageViewModel.Title;

                shop.HasSidebar = pageViewModel.HasSidebar;

                //db.shoppages.Add(shop);
                db.SaveChanges();
            }



            TempData["SM"] = "Изменения сохранены";
            return(RedirectToAction("Edit"));
        }