using System; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using System.Data.Entity; using atakanozbancom.Models.classes; namespace atakanozbancom.Controllers { public class adminController : Controller { private readonly Context db = new Context(); [Authorize] public ActionResult Index() // admin dashboard landing page { ViewBag.MyProjectsCount = db.MyProjects.Count(); ViewBag.AffiliateLinksCount = db.AffiliateLinks.Count(); ViewBag.SocialIconsCount = db.SocialIcons.Count(); return View(); } [Authorize] public ActionResult MyProjects() // shows what we have { var value = db.MyProjects.ToList(); return View(value); } [Authorize] [HttpGet] public ActionResult myprojectsget(int id) { var p = db.MyProjects .Include(x => x.Translations) .FirstOrDefault(x => x.id == id); if (p == null) return HttpNotFound(); var tr = p.Translations? .FirstOrDefault(t => t.culture == "tr" || t.culture == "tr-TR"); var vm = new AdminMyProjectsEditVM { id = p.id, TitleEN = p.title, DescEN = p.description, TitleTR = tr != null ? tr.title : "", DescTR = tr != null ? tr.description : "" }; // NEW: vm.medias = db.projectmedia .Where(m => m.project_id == p.id) .OrderBy(m => m.sort_order) .ThenBy(m => m.id) .ToList(); return View("myprojectsget", vm); } [Authorize] [HttpPost] [ValidateAntiForgeryToken] public ActionResult myprojectsget(AdminMyProjectsEditVM vm) { if (!ModelState.IsValid) { vm.medias = db.projectmedia .Where(m => m.project_id == vm.id) .OrderBy(m => m.sort_order) .ThenBy(m => m.id) .ToList(); return View("myprojectsget", vm); } var p = db.MyProjects.FirstOrDefault(x => x.id == vm.id); if (p == null) return HttpNotFound(); // EN -> base p.title = vm.TitleEN; p.description = vm.DescEN; // TR -> translations var tr = db.myprojectstranslations .FirstOrDefault(x => x.project_id == p.id && (x.culture == "tr" || x.culture == "tr-TR")); if (tr == null) { tr = new myprojectstranslation { project_id = p.id, culture = "tr" }; db.myprojectstranslations.Add(tr); } tr.title = vm.TitleTR; tr.description = vm.DescTR; db.SaveChanges(); TempData["ok"] = "Project updated successfully!"; return RedirectToAction("myprojectsget", new { id = p.id }); } [Authorize] [HttpGet] public ActionResult newmppost() // newmppost = New My Projects Post { return View(new AdminMyProjectsEditVM()); } [Authorize] [HttpPost] [ValidateAntiForgeryToken] public ActionResult newmppost(AdminMyProjectsEditVM vm) { if (!ModelState.IsValid) return View(vm); // EN -> base table var newProject = new myprojects { title = vm.TitleEN, description = vm.DescEN // image/iframe artık yok }; db.MyProjects.Add(newProject); db.SaveChanges(); // TR -> translations var tr = new myprojectstranslation { project_id = newProject.id, culture = "tr", title = vm.TitleTR ?? "", description = vm.DescTR ?? "" }; db.myprojectstranslations.Add(tr); db.SaveChanges(); TempData["ok"] = "Project added successfully!"; return RedirectToAction("myprojectsget", new { id = newProject.id }); // direkt edit sayfasına at, media eklesin } [Authorize] [HttpPost] [ValidateAntiForgeryToken] public ActionResult myprojectsdelete(int id) { var mpd = db.MyProjects.Find(id); // mpd = my projects delete if (mpd != null) { db.MyProjects.Remove(mpd); db.SaveChanges(); } return RedirectToAction("MyProjects"); } [HttpPost] [Authorize] [ValidateAntiForgeryToken] public ActionResult addmedia( int projectId, int mediaType, int sortOrder = 0, string iframeUrl = null, HttpPostedFileBase mediaFile = null) { var type = (MediaType)mediaType; // 1) IFRAME -> URL if (type == MediaType.Iframe) { if (string.IsNullOrWhiteSpace(iframeUrl)) { TempData["ok"] = "Iframe URL boş olamaz."; return RedirectToAction("myprojectsget", new { id = projectId }); } var trimmed = iframeUrl.Trim(); if (!Uri.TryCreate(trimmed, UriKind.Absolute, out var embedUri) || (embedUri.Scheme != Uri.UriSchemeHttps && embedUri.Scheme != Uri.UriSchemeHttp)) { TempData["ok"] = "Iframe URL must be a valid http(s) address."; return RedirectToAction("myprojectsget", new { id = projectId }); } var row = new projectmedia { project_id = projectId, media_type = MediaType.Iframe, url = trimmed, sort_order = sortOrder }; db.projectmedia.Add(row); db.SaveChanges(); TempData["ok"] = "Media eklendi!"; return RedirectToAction("myprojectsget", new { id = projectId }); } // 2) IMAGE/VIDEO/DOCUMENT -> FILE if (mediaFile == null || mediaFile.ContentLength <= 0) { TempData["ok"] = "Dosya seçmelisin."; return RedirectToAction("myprojectsget", new { id = projectId }); } var ext = Path.GetExtension(mediaFile.FileName)?.ToLowerInvariant() ?? ""; // Tip bazlı uzantı whitelist string[] allowed; switch (type) { case MediaType.Image: allowed = new[] { ".jpg", ".jpeg", ".png", ".webp", ".gif" }; break; //case MediaType.Video: // allowed = new[] { ".mp4", ".webm", ".mov" }; // break; //case MediaType.Document: // allowed = new[] { ".pdf" }; // break; default: TempData["ok"] = "Geçersiz media type."; return RedirectToAction("myprojectsget", new { id = projectId }); } if (!allowed.Contains(ext)) { TempData["ok"] = "Bu dosya tipi kabul edilmiyor: " + ext; return RedirectToAction("myprojectsget", new { id = projectId }); } // Upload path var uploadsRoot = Server.MapPath("~/Content/uploads/projects-media"); if (!Directory.Exists(uploadsRoot)) Directory.CreateDirectory(uploadsRoot); var fileName = Guid.NewGuid().ToString("N") + ext; var filePath = Path.Combine(uploadsRoot, fileName); mediaFile.SaveAs(filePath); var publicUrl = "/Content/uploads/projects-media/" + fileName; var m = new projectmedia { project_id = projectId, media_type = type, url = publicUrl, sort_order = sortOrder }; db.projectmedia.Add(m); db.SaveChanges(); TempData["ok"] = "Media eklendi!"; return RedirectToAction("myprojectsget", new { id = projectId }); } [Authorize] [HttpPost] [ValidateAntiForgeryToken] public ActionResult deletemedia(int id, int projectId) { var m = db.projectmedia.FirstOrDefault(x => x.id == id && x.project_id == projectId); if (m == null) return RedirectToAction("myprojectsget", new { id = projectId }); // Dosya silme: sadece Iframe dışındakiler + URL bizim uploads klasörümüzse if (m.media_type != MediaType.Iframe && !string.IsNullOrWhiteSpace(m.url)) { try { // Güvenlik: sadece bizim klasördeki dosyaları sil var prefix = "/Content/uploads/projects-media/"; if (m.url.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) { var relative = "~" + m.url; // ~/Content/uploads/... var physicalPath = Server.MapPath(relative); if (System.IO.File.Exists(physicalPath)) System.IO.File.Delete(physicalPath); } } catch { // istersen log yazdırırız, şimdilik sessiz geçiyoruz } } db.projectmedia.Remove(m); db.SaveChanges(); TempData["ok"] = "Media silindi."; return RedirectToAction("myprojectsget", new { id = projectId }); } // ================= AFFILIATE LINKS ================= // .svg intentionally excluded: SVGs can embed