using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using SuperAdmin.Models.Http; using SuperAdmin.Models.Object; using SuperAdmin.Models.View; using SuperCms.Extensions; using static SuperAdmin.Models.View.CommonModel; namespace SuperAdmin.Controllers { public class TreeController : BaseController { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); public TreeController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment) : base(_configuration, hostEnvironment) { // init } public IActionResult Index(String id) { try { if (!CheckAuthToken()) { return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login"); } string serviceName = GetParameter(UtilsController.Constant.SERVICE_NAME); HttpContext.Session.SetComplexData("serviceName", serviceName); if (!CheckAuthToken()) { return RedirectToAction("Index", "Home"); } RowStructures rows = HttpContext.Session.GetComplexData("rows"); TreeViewModel model = new TreeViewModel(); model.rows = new RowStructures(); model.rows.data = new List(); if (rows == null || 1 == 1) { GetRequest request = new GetRequest(); request.ID = id != null ? id : BaseController.PARENT_ID; String rs = SendPost(request, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_PARENT_ID)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { RowStructures rowsGet = new RowStructures(configuration, rs); rowsGet.data.ForEach(x => model.rows.data.Add(x)); } HttpContext.Session.SetComplexData("rows", model.rows); } else { rows.data.ForEach(x => model.rows.data.Add(x)); model.rows.data = model.rows.data.FindAll(x => x.parent_id == BaseController.PARENT_ID); } return View("Index", model); } catch (Exception ex) { log.Error("Exception " + ex); return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error"); } } public IActionResult GetTreeData(String id) { try { if (!CheckAuthToken()) { return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login"); } TreeViewModel model = new TreeViewModel(); model.rows = new RowStructures(); model.rows.data = new List(); RowStructures rows = HttpContext.Session.GetComplexData("rows"); List rowGetSession = rows != null ? rows.data.FindAll(x => x.parent_id == id) : null; if (rowGetSession == null || rowGetSession.Count == 0) { GetRequest request = new GetRequest(); request.ID = id; String rs = SendPost(request, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_PARENT_ID)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { RowStructures rowsGet = new RowStructures(configuration, rs); rowsGet.data.ForEach(x => rows.data.Add(x)); model.rows = rowsGet; } HttpContext.Session.SetComplexData("rows", rows); } else { model.rows.data = rowGetSession; } return PartialView("TreePartial", model); } catch (Exception ex) { log.Error("Exception " + ex); return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error"); } } public IActionResult GetTableData(String id) { try { if (!CheckAuthToken()) { return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login"); } TreeViewModel model = new TreeViewModel(); model.rows = new RowStructures(); model.rows.data = new List(); RowStructures rows = HttpContext.Session.GetComplexData("rows"); if (rows != null) { RowStructure parent = rows.data.Find(x => x.id == id); model.parent = parent; List rowGetSession = rows != null ? rows.data.FindAll(x => x.parent_id == id) : null; if (rowGetSession == null || rowGetSession.Count == 0) { GetRequest request = new GetRequest(); request.ID = id; String rs = SendPost(request, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_PARENT_ID)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { RowStructures rowsGet = new RowStructures(configuration, rs); rowsGet.data.ForEach(x => rows.data.Add(x)); model.rows = rowsGet; } HttpContext.Session.SetComplexData("rows", rows); } else { model.rows.data = rowGetSession; } } return PartialView("TablePartial", model); } catch (Exception ex) { log.Error("Exception " + ex); return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error"); } } // data private string UploadedFile(String folder, IFormFile image) { try { //string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "img"); string uploadsFolder = GetParameter(UtilsController.Constant.PATH_OUTSIDE); uploadsFolder = Path.Combine(uploadsFolder, folder); if (!Directory.Exists(uploadsFolder)) { Directory.CreateDirectory(uploadsFolder); } string uniqueFileName = Guid.NewGuid().ToString() + "_" + image.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { image.CopyTo(fileStream); } return Path.Combine(folder, uniqueFileName); } catch (Exception ex) { log.Error("Exception: " + ex); return ""; } } public IActionResult Adding(String id) { try { if (!CheckAuthToken()) { return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login"); } EventViewModel model = new EventViewModel(); if (id != BaseController.PARENT_ID) { RowStructures rows = HttpContext.Session.GetComplexData("rows"); model.parent = rows.data.Find(x => x.id == id); } Provinces provinces = HttpContext.Session.GetComplexData("provinces"); Topics topics = HttpContext.Session.GetComplexData("topics"); if (provinces == null) { provinces = new Provinces(); provinces.data = new List(); AppRequest userRequest = new AppRequest(); userRequest.language = UtilsController.Constant.LANGUAGE_GLOBAL; String rsProvince = SendPost(userRequest, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_PROVINCES)); AppResponse resProvince = new AppResponse(rsProvince); if (resProvince.status == UtilsController.Constant.SUCCESS) { provinces = new Provinces(rsProvince); HttpContext.Session.SetComplexData("provinces", provinces); } } if (topics == null) { topics = new Topics(); topics.data = new List(); AppRequest topicRequest = new AppRequest(); topicRequest.language = UtilsController.Constant.LANGUAGE_GLOBAL; String rsTopic = SendPost(topicRequest, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_TOPICS)); AppResponse resTopic = new AppResponse(rsTopic); if (resTopic.status == UtilsController.Constant.SUCCESS) { topics = new Topics(rsTopic); HttpContext.Session.SetComplexData("topics", topics); } } model.provinces = provinces; model.topics = topics; return PartialView("~/Views/Tree/Event/Adding.cshtml", model); } catch (Exception ex) { log.Error("Exception " + ex); return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error"); } } [ValidateAntiForgeryToken] [HttpPost] public JsonResult AddingAction(RowStructure data, IFormFile icon, IFormFile logo) { try { if (!CheckAuthToken()) { return Json(new { code = UtilsController.Constant.AUTHEN_FAIL, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login" }); } String page = HttpContext.Session.GetComplexData("page"); // check image upload if (icon != null && (icon.FileName.EndsWith(".png") || icon.FileName.EndsWith(".jpg") || icon.FileName.EndsWith(".jpeg")) && logo != null) { //List newFiles = new List(); //newFiles.Add(icon); //newFiles.Add(logo); //Task resUpload = OnPostUploadAsync(newFiles); //data.realIcon = icon.FileName; //data.realLogo = logo.FileName; String resIcon = UploadedFile(data.parent_id, icon); String resLogo = UploadedFile(data.parent_id, logo); if (resIcon != "" && resLogo != "") { data.realIcon = resIcon; data.realLogo = resLogo; } else { return Json(new { error_code = UtilsController.Constant.FAILURE, error_content = "An error happens with your logo or your icon.", }); } } else if (icon != null || logo != null) { return Json(new { error_code = UtilsController.Constant.FAILURE, error_content = "An error happens with your logo", }); } data.sv_id = GetParameter(UtilsController.Constant.SERVICE_ID); data.from_date = convertToDateTimeServer(data.from_date); data.to_date = convertToDateTimeServer(data.to_date); // push event to the server ActionRequest userRequest = new ActionRequest(); userRequest.NOTE = ""; userRequest.PARENT_ID = data.parent_id; userRequest.CODE = data.code; userRequest.NAME_GLOBAL = data.name_global; userRequest.NAME_LOCAL = data.name_local; userRequest.DESCRIPTION_GLOBAL = data.description_global; userRequest.DESCRIPTION_LOCAL = data.description_local; userRequest.INTRODUCTION_GLOBAL = data.introduction_global; userRequest.INTRODUCTION_LOCAL = data.introduction_local; userRequest.ICON = data.realIcon; userRequest.LOGO = data.realLogo; userRequest.CONTENT = data.content; userRequest.CONTENT_TYPE = UtilsController.Constant.TEXT_TYPE; userRequest.IS_SHOW = data.is_show; userRequest.PROVINCE_ID = UtilsController.Constant.ALL_CATEGORIES; userRequest.TOPIC_ID = UtilsController.Constant.ALL_CATEGORIES; userRequest.FROM_DATE = data.from_date; userRequest.TO_DATE = data.to_date; userRequest.TYPE = UtilsController.Constant.ADMIN_TYPE; String rs = SendPost(userRequest, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.INSERT_DATA)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { RowStructures rows = HttpContext.Session.GetComplexData("rows"); data.id = res.newsId; data.name = data.name_global; rows.data.Add(data); HttpContext.Session.SetComplexData("rows", rows); List ids = new List(); ids.Add(data.parent_id); return Json(new { code = UtilsController.Constant.SUCCESS, message = "Adding successful", ids = ids, }); } else { return Json(new { code = UtilsController.Constant.FAILURE, message = "We have a problem!", }); } } catch (Exception ex) { log.Error("Exception " + ex); return Json(new { code = UtilsController.Constant.EXCEPTION, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error" }); } } public IActionResult Editing(String id, String action) { try { if (!CheckAuthToken()) { return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login"); } if (id == null) { //return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error"); id = BaseController.PARENT_ID; } EventViewModel model = new EventViewModel(); model.action = action; if (action == "add") { if (id != BaseController.PARENT_ID) { RowStructures rows = HttpContext.Session.GetComplexData("rows"); model.parent = rows.data.Find(x => x.id == id); } } else { // edit RowStructures rows = HttpContext.Session.GetComplexData("rows"); model.now = rows.data.Find(x => x.id == id); model.parent = rows.data.Find(x => x.id == model.now.parent_id); if (model.now != null) { // get details GetRequest request1 = new GetRequest(); request1.ID = id; request1.isGetContet = UtilsController.Constant.GET_CONTENT; String rs1 = SendPost(request1, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_ID)); AppResponse res1 = new AppResponse(rs1); if (res1.status == UtilsController.Constant.SUCCESS) { RowStructures rowsGet = new RowStructures(configuration, rs1); model.now = rowsGet.data[0]; } } HttpContext.Session.SetComplexData("editing", model.now); } //Provinces provinces = HttpContext.Session.GetComplexData("provinces"); //Topics topics = HttpContext.Session.GetComplexData("topics"); //if (provinces == null) //{ // provinces = new Provinces(); // provinces.data = new List(); // AppRequest userRequest = new AppRequest(); // userRequest.language = UtilsController.Constant.LANGUAGE_GLOBAL; // String rsProvince = SendPost(userRequest, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_PROVINCES)); // AppResponse resProvince = new AppResponse(rsProvince); // if (resProvince.status == UtilsController.Constant.SUCCESS) // { // provinces = new Provinces(rsProvince); // HttpContext.Session.SetComplexData("provinces", provinces); // } //} //if (topics == null) //{ // topics = new Topics(); // topics.data = new List(); // AppRequest topicRequest = new AppRequest(); // topicRequest.language = UtilsController.Constant.LANGUAGE_GLOBAL; // String rsTopic = SendPost(topicRequest, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_TOPICS)); // AppResponse resTopic = new AppResponse(rsTopic); // if (resTopic.status == UtilsController.Constant.SUCCESS) // { // topics = new Topics(rsTopic); // HttpContext.Session.SetComplexData("topics", topics); // } //} //model.provinces = provinces; //model.topics = topics; // get all parents GetRequest request = new GetRequest(); request.ID = id; request.isGetChildById = "2"; // get all parents String rs = SendPost(request, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_PARENT_ID)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { model.categories = new RowStructures(configuration, rs); } return PartialView("~/Views/Tree/Event/Editing.cshtml", model); } catch (Exception ex) { log.Error("Exception " + ex); return Redirect(HttpContext.Session.GetComplexData(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error"); } } [ValidateAntiForgeryToken] [HttpPost] public JsonResult EditingAction(RowStructure data, IFormFile icon, IFormFile logo, string action) { try { if (!CheckAuthToken()) { return Json(new { code = UtilsController.Constant.AUTHEN_FAIL, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login" }); } String page = HttpContext.Session.GetComplexData("page"); // check image upload if (icon != null && (icon.FileName.EndsWith(".png") || icon.FileName.EndsWith(".jpg") || icon.FileName.EndsWith(".jpeg")) && logo != null) { String resIcon = UploadedFile(data.parent_id, icon); //String resLogo = UploadedFile(logo); if (resIcon != "") { data.realIcon = resIcon; //data.realLogo = resLogo; } else { return Json(new { error_code = UtilsController.Constant.FAILURE, error_content = "An error happens with your logo or your icon.", }); } } if (logo != null) { //String resIcon = UploadedFile(icon); String resLogo = UploadedFile(data.parent_id, logo); if (resLogo != "") { //data.realIcon = resIcon; data.realLogo = resLogo; } else { return Json(new { error_code = UtilsController.Constant.FAILURE, error_content = "An error happens with your logo or your icon.", }); } } //else if (icon != null || logo != null) //{ // return Json(new // { // error_code = UtilsController.Constant.FAILURE, // error_content = "An error happens with your logo", // }); //} RowStructure edit = HttpContext.Session.GetComplexData("editing"); data.sv_id = GetParameter(UtilsController.Constant.SERVICE_ID); data.from_date = convertToDateTimeServer(data.from_date); data.to_date = convertToDateTimeServer(data.to_date); // push event to the server ActionRequest userRequest = new ActionRequest(); userRequest.NOTE = ""; userRequest.PARENT_ID = data.parent_id; userRequest.NAME_GLOBAL = data.name_global; userRequest.NAME_GLOBAL1 = data.name_global1; userRequest.NAME_GLOBAL2 = data.name_global2; userRequest.NAME_LOCAL = data.name_local; userRequest.DESCRIPTION_GLOBAL = data.description_global; userRequest.DESCRIPTION_GLOBAL1 = data.description_global1; userRequest.DESCRIPTION_GLOBAL2 = data.description_global2; userRequest.DESCRIPTION_LOCAL = data.description_local; userRequest.INTRODUCTION_GLOBAL = data.introduction_global; userRequest.INTRODUCTION_GLOBAL1 = data.introduction_global1; userRequest.INTRODUCTION_GLOBAL2 = data.introduction_global2; userRequest.INTRODUCTION_LOCAL = data.introduction_local; userRequest.ICON = data.realIcon; userRequest.LOGO = data.realLogo; userRequest.CONTENT = data.content; userRequest.CONTENT_GLOBAL = data.content_global; userRequest.CONTENT_GLOBAL1 = data.content_global1; userRequest.CONTENT_GLOBAL2 = data.content_global2; userRequest.CONTENT_TYPE = UtilsController.Constant.TEXT_TYPE; userRequest.IS_SHOW = UtilsController.Constant.WAIT_APPROVAL;//data.is_show; userRequest.PROVINCE_ID = UtilsController.Constant.ALL_CATEGORIES; userRequest.TOPIC_ID = UtilsController.Constant.ALL_CATEGORIES; userRequest.FROM_DATE = data.from_date; userRequest.TO_DATE = data.to_date; userRequest.TYPE = UtilsController.Constant.EDITING; if (action == "add") { userRequest.CODE = data.code; } else { data.id = edit.id; data.parent_id = edit.parent_id; userRequest.CODE = data.code; } if (action == "add") { // add new String rs = SendPost(userRequest, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.INSERT_DATA)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { RowStructures rows = HttpContext.Session.GetComplexData("rows"); data.id = res.newsId; data.name = data.name_global; rows.data.Add(data); HttpContext.Session.SetComplexData("rows", rows); List ids = new List(); ids.Add(data.parent_id); return Json(new { code = UtilsController.Constant.SUCCESS, message = "Adding successful", ids = ids, }); } else { return Json(new { code = UtilsController.Constant.FAILURE, message = "We have a problem!", }); } } else { // edit userRequest.ID = data.id; String rs = SendPost(userRequest, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.UPDATE_DATA)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { RowStructures rows = HttpContext.Session.GetComplexData("rows"); rows.data.Where(S => S.id == data.id).Select(S => { S.code = data.code; S.name = data.name; S.name_global = data.name_global; S.name_global1 = data.name_global1; S.name_global2 = data.name_global2; S.name_local = data.name_local; S.description_global = data.description_global; S.description_global1 = data.description_global1; S.description_global2 = data.description_global2; S.description_local = data.description_local; S.introduction_global = data.introduction_global; S.introduction_global1 = data.introduction_global1; S.introduction_global2 = data.introduction_global2; S.introduction_local = data.introduction_local; S.icon = data.realIcon; S.logo = data.realLogo; S.content = data.content; S.content_global = data.content_global; S.content_global1 = data.content_global1; S.content_global2 = data.content_global2; S.content_type = UtilsController.Constant.TEXT_TYPE; S.is_show = data.is_show; S.province_id = data.province_id; S.topic_id = data.topic_id; S.from_date = data.from_date; S.to_date = data.to_date; S.permission = UtilsController.Constant.ADMIN_TYPE; return S; }).ToList(); HttpContext.Session.SetComplexData("rows", rows); List ids = new List(); ids.Add(data.parent_id); return Json(new { ids = ids, code = UtilsController.Constant.SUCCESS, message = "Editing successful", }); } else { return Json(new { code = UtilsController.Constant.FAILURE, message = "We have a problem!", }); } } } catch (Exception ex) { log.Error("Exception " + ex); return Json(new { code = UtilsController.Constant.EXCEPTION, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error" }); } } [ValidateAntiForgeryToken] public JsonResult DeletingAction(String id) { try { if (!CheckAuthToken()) { return Json(new { code = UtilsController.Constant.AUTHEN_FAIL, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login" }); } RowStructures rows = HttpContext.Session.GetComplexData("rows"); RowStructure data = rows.data.Find(x => x.id == id); if (data == null) { return Json(new { code = UtilsController.Constant.FAILURE, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home" }); } ActionRequest request = new ActionRequest(); request.ID = data.id; request.TYPE = UtilsController.Constant.DELETE; String rs = SendPost(request, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.UPDATE_DATA)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { //return Redirect("/EventAdmin/EventShowing"); rows.data.RemoveAll(x => x.id == id); HttpContext.Session.SetComplexData("rows", rows); List ids = new List(); ids.Add(data.parent_id); List funcs = new List(); funcs.Add(UtilsController.Constant.GetTableAction); List paras = new List(); paras.Add(data.parent_id); return Json(new { ids = ids, code = UtilsController.Constant.SUCCESS, message = "Deleting successful", functions = funcs, parameters = paras, }); } else { return Json(new { code = UtilsController.Constant.FAILURE, message = "We have a problem!", }); } } catch (Exception ex) { log.Error("Exception " + ex); return Json(new { code = UtilsController.Constant.EXCEPTION, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home" }); } } [ValidateAntiForgeryToken] public JsonResult MovingAction(String sourceID, String destinationID) { try { if (!CheckAuthToken()) { return Json(new { code = UtilsController.Constant.AUTHEN_FAIL, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login" }); } if (sourceID == null || destinationID == null) { return Json(new { code = UtilsController.Constant.FAILURE, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error" }); } RowStructures rows = HttpContext.Session.GetComplexData("rows"); RowStructure source = rows.data.Find(x => x.id == sourceID); RowStructure destination = rows.data.Find(x => x.id == destinationID); if (source == null || destination == null) { return Json(new { code = UtilsController.Constant.FAILURE, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error" }); } String oldParent = source.parent_id; // update source parent id to destination source.parent_id = destination.id; // push event to the server ActionRequest userRequest = new ActionRequest(); userRequest.ID = source.id; userRequest.NOTE = ""; userRequest.PARENT_ID = source.parent_id; userRequest.CODE = source.code; userRequest.NAME_GLOBAL = source.name_global; userRequest.NAME_LOCAL = source.name_local; userRequest.DESCRIPTION_GLOBAL = source.description_global; userRequest.DESCRIPTION_LOCAL = source.description_local; userRequest.INTRODUCTION_GLOBAL = source.introduction_global; userRequest.INTRODUCTION_LOCAL = source.introduction_local; userRequest.ICON = source.realIcon; userRequest.LOGO = source.realLogo; userRequest.CONTENT = source.content; userRequest.CONTENT_TYPE = UtilsController.Constant.TEXT_TYPE; userRequest.IS_SHOW = source.is_show; userRequest.PROVINCE_ID = source.province_id; userRequest.TOPIC_ID = source.topic_id; userRequest.FROM_DATE = source.from_date; userRequest.TO_DATE = source.to_date; userRequest.TYPE = UtilsController.Constant.ADMIN_TYPE; String rs = SendPost(userRequest, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.UPDATE_DATA)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { rows.data.Where(S => S.id == source.id).Select(S => { S.parent_id = source.parent_id; return S; }).ToList(); HttpContext.Session.SetComplexData("rows", rows); List ids = new List(); ids.Add(destination.id); ids.Add(oldParent); return Json(new { ids = ids, code = UtilsController.Constant.SUCCESS, message = "Moving successful", }); } else { return Json(new { code = UtilsController.Constant.FAILURE, message = "We have a problem!", }); } } catch (Exception ex) { log.Error("Exception " + ex); return Json(new { code = UtilsController.Constant.EXCEPTION, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home" }); } } [ValidateAntiForgeryToken] public JsonResult CopyingAction(String sourceID, String destinationID) { try { if (!CheckAuthToken()) { return Json(new { code = UtilsController.Constant.AUTHEN_FAIL, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login" }); } if (sourceID == null || destinationID == null) { return Json(new { code = UtilsController.Constant.FAILURE, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error" }); } RowStructures rows = HttpContext.Session.GetComplexData("rows"); RowStructure source = rows.data.Find(x => x.id == sourceID); RowStructure destination = rows.data.Find(x => x.id == destinationID); if (source == null || destination == null) { return Json(new { code = UtilsController.Constant.FAILURE, message = "We have a problem. Parent id was not found!", href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error" }); } MapData map = new MapData(); map.oldData = source; DistributedByLevel one = new DistributedByLevel(); one.mapData = new List(); one.mapData.Add(map); one.level = 1; List listOne = new List(); listOne.Add(one); List dataCopy = GetAllDataSource(1, listOne); Boolean res = CopyAllDataSource(destinationID, dataCopy, 1, new List()); if (res) { List ids = new List(); ids.Add(source.parent_id); ids.Add(destination.id); return Json(new { ids = ids, code = UtilsController.Constant.SUCCESS, message = "Copying successful", }); } else { return Json(new { code = UtilsController.Constant.FAILURE, message = "We have a problem!", }); } } catch (Exception ex) { log.Error("Exception " + ex); return Json(new { code = UtilsController.Constant.EXCEPTION, message = "We have a problem. Exception!", href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home" }); } } private List GetAllDataSource(int level, List result) { DistributedByLevel now = result.Find(x => x.level == level); DistributedByLevel newDis = new DistributedByLevel(); newDis.level = level + 1; newDis.mapData = new List(); int check = 1; for (int i = 0; i < now.mapData.Count; i++) { GetRequest request = new GetRequest(); request.ID = now.mapData[i].oldData.id; request.isGetContet = UtilsController.Constant.GET_CONTENT; String rs = SendPost(request, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_PARENT_ID)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { RowStructures rowsGet = new RowStructures(configuration, rs); for (int a = 0; a < rowsGet.data.Count; a++) { MapData map = new MapData(); map.oldData = rowsGet.data[a]; newDis.mapData.Add(map); } } else { check *= 0; } } if (check == 1) { result.Add(newDis); return GetAllDataSource(level + 1, result); } else { return result; } } private Boolean CopyAllDataSource(String destinationID, List source, int count, List result) { if (source.Count == count - 1) { return true; } else { DistributedByLevel child = source.Find(x => x.level == count); DistributedByLevel parent = result.Find(x => x.level == count - 1); for (int i = 0; i < child.mapData.Count; i++) { MapData childMap = child.mapData[i]; MapData parentMap = count > 1 ? parent.mapData.Find(x => x.oldData.id == childMap.oldData.parent_id) : null; childMap.newData = (RowStructure)childMap.oldData.Clone(); // push event to the server ActionRequest userRequest = new ActionRequest(); userRequest.PARENT_ID = count > 1 ? parentMap.newData.id : destinationID; userRequest.CODE = childMap.newData.code; userRequest.NAME_GLOBAL = childMap.newData.name_global; userRequest.NAME_LOCAL = childMap.newData.name_local; userRequest.DESCRIPTION_GLOBAL = childMap.newData.description_global; userRequest.DESCRIPTION_LOCAL = childMap.newData.description_local; userRequest.INTRODUCTION_GLOBAL = childMap.newData.introduction_global; userRequest.INTRODUCTION_LOCAL = childMap.newData.introduction_local; userRequest.ICON = childMap.newData.realIcon; userRequest.LOGO = childMap.newData.realLogo; userRequest.CONTENT = childMap.newData.content; userRequest.CONTENT_TYPE = UtilsController.Constant.TEXT_TYPE; userRequest.IS_SHOW = UtilsController.Constant.ALL_TYPE; userRequest.PROVINCE_ID = UtilsController.Constant.ALL_CATEGORIES; userRequest.TOPIC_ID = UtilsController.Constant.ALL_CATEGORIES; userRequest.FROM_DATE = childMap.newData.from_date; userRequest.TO_DATE = childMap.newData.to_date; userRequest.TYPE = UtilsController.Constant.ADMIN_TYPE; userRequest.NOTE = ""; String rs = SendPost(userRequest, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.INSERT_DATA)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { childMap.newData.id = res.newsId; childMap.newData.parent_id = count > 1 ? parentMap.newData.id : destinationID; child.mapData.Where(S => S.oldData.id == childMap.oldData.id).Select(S => { S.newData = childMap.newData; return S; }).ToList(); RowStructures rows = HttpContext.Session.GetComplexData("rows"); rows.data.Add(childMap.newData); HttpContext.Session.SetComplexData("rows", rows); } else { return false; } } child.level = count; result.Add(child); return CopyAllDataSource(destinationID, source, count + 1, result); } } // PROCEDURES public IActionResult Approves(String page, String status) { try { if (!CheckAuthToken()) { return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login"); } string serviceName = GetParameter(UtilsController.Constant.SERVICE_NAME); HttpContext.Session.SetComplexData("serviceName", serviceName); EventViewModel model = new EventViewModel(); //model.categories = new RowStructures(); //model.categories.data = new List(); //model.columns = new RowStructures(); //model.columns.data = new List(); model.page = page == null ? "1" : page; model.status = status != null ? status : UtilsController.Constant.WAIT_APPROVAL; model.events = new RowStructures(); model.events.data = new List(); //RowStructures categories = HttpContext.Session.GetComplexData("categories"); //RowStructures columns = HttpContext.Session.GetComplexData("columns"); //if (categories == null) //{ // // get all categories // GetRequest request = new GetRequest(); // request.ID = BaseController.PARENT_ID; // String rs = SendPost(request, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_PARENT_ID)); // AppResponse res = new AppResponse(rs); // if (res.status == UtilsController.Constant.SUCCESS) // { // categories = new RowStructures(configuration, rs); // } // HttpContext.Session.SetComplexData("categories", categories); //} //model.categories = categories; //if (columns == null) //{ // columns = new RowStructures(); // columns.data = new List(); // for (int i = 0; i < model.categories.data.Count; i++) // { // // get all columns // GetRequest columnsReq = new GetRequest(); // columnsReq.ID = model.categories.data[i].id; // String rsColumn = SendPost(columnsReq, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_PARENT_ID)); // AppResponse resColumn = new AppResponse(rsColumn); // if (resColumn.status == UtilsController.Constant.SUCCESS) // { // RowStructures dataGet = new RowStructures(configuration, rsColumn); // dataGet.data.ForEach(x => columns.data.Add(x)); // } // } // HttpContext.Session.SetComplexData("columns", columns); //} //model.columns = columns; //for (int i = 0; i < model.columns.data.Count; i++) //{ // // get all approved events // GetRequest requestEvent = new GetRequest(); // //request.users = account.username; // //request.msisdn = account.username; // requestEvent.ID = model.columns.data[i].id; // requestEvent.type = status == UtilsController.Constant.USER_TYPE ? UtilsController.Constant.ADMIN_TYPE : UtilsController.Constant.ALL_TYPE; // requestEvent.rowsOnPage = UtilsController.Constant.ROWS_ON_PAGE; // requestEvent.seqPage = page == null ? "1" : page; // requestEvent.isGetContet = UtilsController.Constant.NOT_GET_CONTENT; // requestEvent.isShow = status != null ? status : UtilsController.Constant.WAIT_APPROVAL; // String rsEvent = SendPost(requestEvent, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_PARENT_ID)); // AppResponse resEvent = new AppResponse(rsEvent); // if (resEvent.status == UtilsController.Constant.SUCCESS) // { // model.totalPage = resEvent.totalPage; // RowStructures events = new RowStructures(configuration, rsEvent); // events.data.ForEach(x => model.events.data.Add(x)); // } //} //model.events.data = model.events.data.FindAll(x => x.is_show != UtilsController.Constant.ALL_TYPE); // get all approved events GetRequest requestEvent = new GetRequest(); //request.users = account.username; //request.msisdn = account.username; requestEvent.ID = BaseController.PARENT_ID; requestEvent.type = status == UtilsController.Constant.USER_TYPE ? UtilsController.Constant.ADMIN_TYPE : UtilsController.Constant.ALL_TYPE; requestEvent.rowsOnPage = UtilsController.Constant.ROWS_ON_PAGE; requestEvent.seqPage = page == null ? "1" : page; requestEvent.isGetContet = UtilsController.Constant.NOT_GET_CONTENT; requestEvent.isShow = status != null ? status : UtilsController.Constant.WAIT_APPROVAL; requestEvent.isGetChildById = "1"; String rsEvent = SendPost(requestEvent, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_PARENT_ID)); AppResponse resEvent = new AppResponse(rsEvent); if (resEvent.status == UtilsController.Constant.SUCCESS) { model.totalPage = resEvent.totalPage; RowStructures events = new RowStructures(configuration, rsEvent); events.data.ForEach(x => model.events.data.Add(x)); } HttpContext.Session.SetComplexData("events", model.events); return View("~/Views/Tree/Event/Approves.cshtml", model); //return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Event/Approves?page=" + page + "&status=" + status); } catch (Exception ex) { log.Error("Exception " + ex); return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home"); } } public IActionResult Aprroving(String id) { try { if (!CheckAuthToken()) { return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login"); } if (id == null) { return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error"); } string serviceName = GetParameter(UtilsController.Constant.SERVICE_NAME); HttpContext.Session.SetComplexData("serviceName", serviceName); EventViewModel model = new EventViewModel(); model.categories = new RowStructures(); model.categories.data = new List(); model.columns = new RowStructures(); model.columns.data = new List(); //RowStructures rows = HttpContext.Session.GetComplexData("events"); //model.now = rows.data.Find(x => x.id == id); //model.parent = rows.data.Find(x => x.id == model.now.parent_id); //RowStructures categories = HttpContext.Session.GetComplexData("categories"); //RowStructures columns = HttpContext.Session.GetComplexData("columns"); //if (categories == null) //{ // // get all categories // GetRequest request = new GetRequest(); // request.ID = BaseController.PARENT_ID; // String rs = SendPost(request, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_PARENT_ID)); // AppResponse res = new AppResponse(rs); // if (res.status == UtilsController.Constant.SUCCESS) // { // categories = new RowStructures(configuration, rs); // } // HttpContext.Session.SetComplexData("categories", categories); //} //model.categories = categories; //if (columns == null) //{ // columns = new RowStructures(); // columns.data = new List(); // for (int i = 0; i < model.categories.data.Count; i++) // { // // get all columns // GetRequest columnsReq = new GetRequest(); // columnsReq.ID = BaseController.PARENT_ID; // String rsColumn = SendPost(columnsReq, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_PARENT_ID)); // AppResponse resColumn = new AppResponse(rsColumn); // if (resColumn.status == UtilsController.Constant.SUCCESS) // { // RowStructures dataGet = new RowStructures(configuration, rsColumn); // dataGet.data.ForEach(x => columns.data.Add(x)); // } // } // HttpContext.Session.SetComplexData("columns", columns); //} //model.columns = columns; //if (model.now != null) //{ // get details GetRequest request1 = new GetRequest(); request1.ID = id; request1.isGetContet = UtilsController.Constant.GET_CONTENT; String rs1 = SendPost(request1, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_ID)); AppResponse res1 = new AppResponse(rs1); if (res1.status == UtilsController.Constant.SUCCESS) { RowStructures rowsGet = new RowStructures(configuration, rs1); model.now = rowsGet.data[0]; } // get all parents GetRequest request = new GetRequest(); request.ID = model.now.id; request.isGetChildById = "2"; // get all parents String rs = SendPost(request, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.GET_BY_PARENT_ID)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { model.categories = new RowStructures(configuration, rs); } //} HttpContext.Session.SetComplexData("approving", model.now); return PartialView("~/Views/Tree/Event/Approving.cshtml", model); } catch (Exception ex) { log.Error("Exception " + ex); return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error"); } } [ValidateAntiForgeryToken] public JsonResult ApprovingAction(String action, String note) { try { if (!CheckAuthToken()) { return Json(new { code = UtilsController.Constant.AUTHEN_FAIL, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login" }); } String page = HttpContext.Session.GetComplexData("page"); RowStructure data = HttpContext.Session.GetComplexData("approving"); data.note = note; // push event to the server ActionRequest userRequest = new ActionRequest(); userRequest.ID = data.id; userRequest.NOTE = ""; userRequest.PARENT_ID = data.parent_id; userRequest.CODE = data.code; userRequest.NAME_GLOBAL = data.name_global; userRequest.NAME_LOCAL = data.name_local; userRequest.DESCRIPTION_GLOBAL = data.description_global; userRequest.DESCRIPTION_LOCAL = data.description_local; userRequest.INTRODUCTION_GLOBAL = data.introduction_global; userRequest.INTRODUCTION_LOCAL = data.introduction_local; userRequest.ICON = data.realIcon; userRequest.LOGO = data.realLogo; userRequest.CONTENT = data.content; userRequest.CONTENT_TYPE = UtilsController.Constant.TEXT_TYPE; //userRequest.IS_SHOW = data.is_show; userRequest.PROVINCE_ID = data.province_id; userRequest.TOPIC_ID = data.topic_id; userRequest.FROM_DATE = data.from_date; userRequest.TO_DATE = data.to_date; userRequest.TYPE = UtilsController.Constant.EDITING; userRequest.IS_SHOW = action == UtilsController.Constant.SAVE ? data.is_show : action == UtilsController.Constant.PUBLISH ? UtilsController.Constant.APPROVED : UtilsController.Constant.REJECTED; String rs = SendPost(userRequest, GetParameter(UtilsController.Constant.SERVICE_ID), GetParameter(UtilsController.Constant.UPDATE_DATA)); AppResponse res = new AppResponse(rs); if (res.status == UtilsController.Constant.SUCCESS) { RowStructures rows = HttpContext.Session.GetComplexData("events"); rows.data.Where(S => S.id == data.id).Select(S => { S.note = data.note; return S; }).ToList(); HttpContext.Session.SetComplexData("events", rows); return Json(new { code = UtilsController.Constant.SUCCESS, message = "Approve successful", }); } else { return Json(new { code = UtilsController.Constant.FAILURE, message = "We have a problem!", }); } } catch (Exception ex) { log.Error("Exception " + ex); return Json(new { code = UtilsController.Constant.EXCEPTION, href = GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Shared/Error" }); } } } }