using NEducation.Models; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.ServiceModel; using System.Web; using System.Web.Mvc; namespace NEducation.Controllers { public class AdminController : Controller { // GET: Admin //public ActionResult Index(string searchString) //{ // if (Session["user"] == null) // { // ViewBag.username = "Welcome!"; // return View("Login"); // } // return View(); //} public ActionResult Login() { ViewBag.username = "Welcome!"; return View(); } public ActionResult Logout() { Session.Clear(); ViewBag.username = "Welcome!"; return View("Login"); } public ActionResult LoginAction(string account, string password) { String wsUser = ConfigurationManager.AppSettings["wsUser"]; String wsPassword = ConfigurationManager.AppSettings["wsPassword"]; NEduService.WsNEduClient wsClient = new NEduService.WsNEduClient(); SetWsClient(ref wsClient, Session.SessionID); NEduService.response res = wsClient.wsLoginAdmin(wsUser, wsPassword, password, account); if (res.errorCode != "0") { ViewBag.errorMessage = "Wrong account or password"; ViewBag.errorMessageCss = "inherit"; ViewBag.username = "Welcome!"; return View("Login"); } else { Session["user"] = account; ViewBag.errorMessageCss = "none"; ViewBag.username = "Welcome, " + account; return View("Index"); } } //public ActionResult CourseListAdmin(string searchString, string alert, int? page) //{ // if (Session["user"] == null) // { // ViewBag.username = "Welcome!"; // return View("Login"); // } // ViewBag.username = "Welcome, " + (Session["user"] as String); // ViewBag.searchString = searchString; // if (alert == null || alert.Trim().Length == 0) // { // ViewBag.DisplayAlert = "none"; // } // else // { // ViewBag.DisplayAlert = "inherit"; // ViewBag.Alert = alert; // } // String wsUser = ConfigurationManager.AppSettings["wsUser"]; // String wsPassword = ConfigurationManager.AppSettings["wsPassword"]; // NEduService.WsNEduClient wsClient = new NEduService.WsNEduClient(); // SetWsClient(ref wsClient, Session.SessionID); // NEduService.response res = wsClient.wsGetListEventAdmin(wsUser, wsPassword, "-1", null); // ListEvents model = new ListEvents(); // List listTemp = new List(); // model.listEvent = new List(); // if (res.listEvent != null && res.listEvent.Length > 0) // { // foreach (NEduService.eventObj eventObj in res.listEvent) // { // if (searchString != null) // { // if (eventObj.name.ToUpper().Contains(searchString.ToUpper().Trim()) // || eventObj.eventContent.ToUpper().Contains(searchString.ToUpper().Trim()) // || eventObj.eventContentFr.ToUpper().Contains(searchString.ToUpper().Trim()) // || (eventObj.htmlContent == null || eventObj.htmlContent.ToUpper().Contains(searchString.ToUpper().Trim())) // || (eventObj.htmlContentFr == null || eventObj.htmlContentFr.ToUpper().Contains(searchString.ToUpper().Trim())) // ) // { // listTemp.Add(Utils.buildEventDetailFromWs(eventObj)); // } // } // else // { // listTemp.Add(Utils.buildEventDetailFromWs(eventObj)); // } // } // } // if (listTemp.Count > 0) // { // int pageSize = 8; // int pageNumber = (page ?? 1); // listTemp = listTemp.OrderByDescending(x => x.import_time).ToList(); // model.listEventPaging = listTemp.ToPagedList(pageNumber, pageSize); // // get list event in page only // for (int i = 0; i < listTemp.Count; i++) // { // if (i >= pageSize * (pageNumber - 1) && i < pageSize * pageNumber) // { // model.listEvent.Add(listTemp[i]); // } // } // listTemp.Clear(); // ViewBag.Result = Lang.FoundEvent.Replace("%count%", model.listEvent.Count + ""); // } // else // { // ViewBag.Result = Lang.NoEvent; // } // return View("CourseListAdmin", model); //} public ActionResult CreateCourse(long? course_id) { if (Session["user"] == null) { ViewBag.username = "Welcome!"; return View("Login"); } ViewBag.username = "Welcome, " + (Session["user"] as String); String wsUser = ConfigurationManager.AppSettings["wsUser"]; String wsPassword = ConfigurationManager.AppSettings["wsPassword"]; NEduService.WsNEduClient wsClient = new NEduService.WsNEduClient(); SetWsClient(ref wsClient, Session.SessionID); SelectListItem[] courseTypeItems = new SelectListItem[] { new SelectListItem { Text = "Vocabulary", Value = "1" }, new SelectListItem { Text = "Grammar", Value = "2" }, new SelectListItem { Text = "Listening", Value = "3" } }; if (course_id == null) { Session.Remove("course_id"); // create mode CourseModel model = new CourseModel(); //model.listCourseType = courseTypeItems.ToList(); ViewBag.action = "Create"; ViewBag.Title = "Create new course"; return View(model); } else { Session["course_id"] = course_id; // edit mode NEduService.getCourseRes res = wsClient.wsGetCoursesById(wsUser, wsPassword, course_id + ""); CourseModel model = new CourseModel(); //model.selectedCourse = res.listCourse[0]; //model.listCourseType = courseTypeItems.ToList(); ViewBag.action = "Edit"; ViewBag.Title = "Edit course"; return View(model); } } //public ActionResult DeleteCourse() //{ // long? course_id = Session["course_id"] as long?; // if (course_id != null) // { // String wsUser = ConfigurationManager.AppSettings["wsUser"]; // String wsPassword = ConfigurationManager.AppSettings["wsPassword"]; // NEduService.WsNEduClient wsClient = new NEduService.WsNEduClient(); // SetWsClient(ref wsClient, Session.SessionID); // wsClient.wsDeleteEvent(wsUser, wsPassword, course_id.ToString()); // string alert = "Delete course success!"; // ViewBag.AlertCss = "alert-success"; // return RedirectToAction("CourseListAdmin", new { alert = alert });// (null, alert, null); // } // else // { // string alert = "Not found course!"; // ViewBag.AlertCss = "alert-danger"; // return RedirectToAction("CourseListAdmin", new { alert = alert }); // } //} public ActionResult GeneralReport(string fromDate, string toDate) { if (Session["user"] == null) { ViewBag.username = "Welcome!"; return View("Login"); } String wsUser = ConfigurationManager.AppSettings["wsUser"]; String wsPassword = ConfigurationManager.AppSettings["wsPassword"]; NEduService.WsNEduClient wsClient = new NEduService.WsNEduClient(); SetWsClient(ref wsClient, Session.SessionID); if (fromDate == null) { fromDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("dd/MM/yyyy"); } if (toDate == null) { toDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).ToString("dd/MM/yyyy"); } NEduService.reportRes res = wsClient.wsGetReportGeneral(wsUser, wsPassword, fromDate, toDate); //List listTemp = new List(); return View("GeneralReport", res.listReport); } public ActionResult GetGeneralReport(string fromDate, string toDate) { if (Session["user"] == null) { ViewBag.username = "Welcome!"; return View("Login"); } String wsUser = ConfigurationManager.AppSettings["wsUser"]; String wsPassword = ConfigurationManager.AppSettings["wsPassword"]; NEduService.WsNEduClient wsClient = new NEduService.WsNEduClient(); SetWsClient(ref wsClient, Session.SessionID); NEduService.reportRes res = wsClient.wsGetReportGeneral(wsUser, wsPassword, fromDate, toDate); //List listTemp = new List(); return View("GeneralReport", res.listReport); } //[HttpPost] //public ActionResult CreateCourseAction(CourseModel model, HttpPostedFileBase imageAvatar, HttpPostedFileBase imageBanner) //{ // if (Session["user"] == null) // { // ViewBag.username = "Welcome!"; // return View("Login"); // } // ViewBag.username = "Welcome, " + (Session["user"] as String); // String wsUser = ConfigurationManager.AppSettings["wsUser"]; // String wsPassword = ConfigurationManager.AppSettings["wsPassword"]; // NEduService.WsNEduClient wsClient = new NEduService.WsNEduClient(); // SetWsClient(ref wsClient, Session.SessionID); // NEduService.responseLoad res = wsClient.wsLoadCategory(wsUser, wsPassword); // List provinceItems = new List(); // List eventTypeItems = new List(); // foreach (NEduService.provinces pro in res.listProvince) // { // provinceItems.Add(new SelectListItem { Text = pro.name, Value = pro.id.ToString() }); // } // foreach (NEduService.eventTypes eventType in res.listEventType) // { // eventTypeItems.Add(new SelectListItem { Text = eventType.name, Value = eventType.id.ToString() }); // } // if (model == null || model.eventDetail == null) // { // string[] langs = Request.UserLanguages; // model = new CourseModel(); // model.listEventType = eventTypeItems; // model.listProvince = provinceItems; // return RedirectToAction("CourseListAdmin");// (null, null, null); // } // else // { // String pathSvImg = "~/Content/img/event/"; // String pathImgRel = "../../Content/img/event/"; // String imgAvatar = ""; // String imgFull = ""; // String now = DateTime.Now.ToString("yyyyMMddHHmmss"); // model.listEventType = eventTypeItems; // model.listProvince = provinceItems; // // check image upload // if (fileAvatar != null) // { // string pic = now + "_" + Path.GetFileName(fileAvatar.FileName); // string path = Path.Combine(Server.MapPath(pathSvImg), pic); // // file is uploaded // fileAvatar.SaveAs(path); // // save the image path path to the database or you can send image // // directly to database // // in-case if you want to store byte[] ie. for DB // using (MemoryStream ms = new MemoryStream()) // { // fileAvatar.InputStream.CopyTo(ms); // byte[] array = ms.GetBuffer(); // } // imgAvatar = Path.Combine(pathImgRel, pic); // model.eventDetail.image_avatar = imgAvatar; // } // if (fileFullImg != null) // { // string pic = now + "_" + Path.GetFileName(fileFullImg.FileName); // string path = Path.Combine(Server.MapPath(pathSvImg), pic); // // file is uploaded // fileFullImg.SaveAs(path); // // save the image path path to the database or you can send image // // directly to database // // in-case if you want to store byte[] ie. for DB // using (MemoryStream ms = new MemoryStream()) // { // fileFullImg.InputStream.CopyTo(ms); // byte[] array = ms.GetBuffer(); // } // imgFull = Path.Combine(pathImgRel, pic); // model.eventDetail.image_full = imgFull; // } // DateTime startTime = DateTime.ParseExact(model.eventDetail.start_time, "dd/MM/yyyy HH:mm", null); // DateTime endTime = DateTime.ParseExact(model.eventDetail.end_time, "dd/MM/yyyy HH:mm", null); // DateTime scheduleTime = DateTime.ParseExact(model.eventDetail.schedule_time, "dd/MM/yyyy HH:mm", null); // if (savemode == "-1") // { // model.eventDetail.status = -1; // } // else // { // model.eventDetail.status = 1; // } // long? course_id = Session["course_id"] as long?; // if (course_id == null) // { // // create event // NEduService.response res2 = wsClient.wsCreateEvent( // wsUser, // wsPassword, // model.eventDetail.name, // model.eventDetail.name_fr, // model.eventDetail.url, // model.eventDetail.event_type_id.ToString(), // model.eventDetail.province_id.ToString(), // model.eventDetail.place, // model.eventDetail.place_fr, // startTime.ToString("yyyMMddHHmmss"), // endTime.ToString("yyyMMddHHmmss"), // scheduleTime.ToString("yyyMMddHHmmss"), // model.eventDetail.image_avatar, // model.eventDetail.image_full, // model.eventDetail.event_content, // model.eventDetail.event_content_fr, // model.eventDetail.htmlContent, // model.eventDetail.htmlContentFr, // model.eventDetail.fee.ToString(), // model.eventDetail.status + "" // ); // if (res2.errorCode == "0") // { // string alert = "Success create new event!"; // ViewBag.AlertCss = "alert-success"; // return RedirectToAction("CourseListAdmin", new { alert = alert }); // } // else // { // string alert = "Error create event: " + res2.content; // ViewBag.AlertCss = "alert-danger"; // return RedirectToAction("CourseListAdmin", new { alert = alert }); // } // } // else // { // // update event // NEduService.response res2 = wsClient.wsUpdateEvent( // wsUser, // wsPassword, // course_id.Value.ToString(), // model.eventDetail.name, // model.eventDetail.name_fr, // model.eventDetail.url, // model.eventDetail.event_type_id.ToString(), // model.eventDetail.province_id.ToString(), // model.eventDetail.place, // model.eventDetail.place_fr, // startTime.ToString("yyyMMddHHmmss"), // endTime.ToString("yyyMMddHHmmss"), // scheduleTime.ToString("yyyMMddHHmmss"), // model.eventDetail.image_avatar, // model.eventDetail.image_full, // model.eventDetail.event_content, // model.eventDetail.event_content_fr, // model.eventDetail.htmlContent, // model.eventDetail.htmlContentFr, // model.eventDetail.fee.ToString(), // model.eventDetail.status + "" // ); // if (res2.errorCode == "0") // { // string alert = "Success edit 1 event!"; // ViewBag.AlertCss = "alert-success"; // return RedirectToAction("CourseListAdmin", new { alert = alert }); // } // else // { // string alert = "Error edit event: " + res2.content; // ViewBag.AlertCss = "alert-danger"; // return RedirectToAction("CourseListAdmin", new { alert = alert }); // } // } // } //} // GET: Admin/Details/5 public ActionResult Details(int id) { return View(); } // GET: Admin/Create public ActionResult Create() { return View(); } // POST: Admin/Create [HttpPost] public ActionResult Create(FormCollection collection) { try { // TODO: Add insert logic here return RedirectToAction("Index"); } catch { return View(); } } // GET: Admin/Edit/5 public ActionResult Edit(int id) { return View(); } // POST: Admin/Edit/5 [HttpPost] public ActionResult Edit(int id, FormCollection collection) { try { // TODO: Add update logic here return RedirectToAction("Index"); } catch { return View(); } } // GET: Admin/Delete/5 public ActionResult Delete(int id) { return View(); } // POST: Admin/Delete/5 [HttpPost] public ActionResult Delete(int id, FormCollection collection) { try { // TODO: Add delete logic here return RedirectToAction("Index"); } catch { return View(); } } private void SetWsClient(ref NEduService.WsNEduClient wsClient, String sessionId) { int lastNum = sessionId[sessionId.Length - 1]; if (lastNum % 2 == 0) { wsClient.Endpoint.Address = new EndpointAddress(ConfigurationManager.AppSettings["wsUrl1"]); } else { wsClient.Endpoint.Address = new EndpointAddress(ConfigurationManager.AppSettings["wsUrl2"]); } } } }