Advertisement
Guest User

cms

a guest
Nov 13th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using CrisisManagementSystem.Models;
  7. using CrisisManagementSystem.DataAccess;
  8. using GoogleMaps.LocationServices;
  9. using CrisisManagementSystem.Controllers.Alert;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Drawing.Imaging;
  13. using System.Drawing.Drawing2D;
  14. using System.Drawing.Text;
  15. using CrisisManagementSystem.Controllers;
  16.  
  17. namespace CrisisManagementSystem.Controllers
  18. {
  19.     public class ReportController : Controller
  20.     {
  21.         // GET: Report
  22.         private CMSEntities cmsDB = new CMSEntities();
  23.         private DataAccessController daControl = new DataAccessController();
  24.         // GET: Report
  25.         public ActionResult Index()
  26.         {
  27.             return View();
  28.         }
  29.  
  30.         public ActionResult ViewReport(string reportId)
  31.         {
  32.             int rId = Convert.ToInt32(reportId);
  33.             IncidentReport ir = new IncidentReport();
  34.             ir = daControl.GetReportById(rId);
  35.             //return View(cmsDB.Report.Where( a=> a.Id.Equals(rId)).ToList());
  36.             return View(ir);
  37.         }
  38.         public ActionResult Close(string reportId)
  39.         {
  40.             // var del = new Report { Id = Int32.Parse(reportId) };
  41.             //cmsDB.Report.Attach(del);
  42.             //cmsDB.Report.Remove(del);
  43.             //cmsDB.SaveChanges();
  44.             int rId = Convert.ToInt32(reportId);
  45.             IncidentReport ir = new IncidentReport();
  46.             ir = daControl.GetReportById(rId);
  47.             IncidentStatus resolved = IncidentStatus.Resolved;
  48.  
  49.             if (ir.AlertSent == "true" || ir.Priority == 5)
  50.             {
  51.                 SendPublicAlert(rId, ir.IncidentType.ToString() + " at " + ir.Location.FullAddress.ToString() + " has been resolved.", true, true, true);
  52.             }
  53.  
  54.             daControl.UpdateReport(ir.Id, ir.ReporterName, ir.ReporterNRIC, ir.ReportedDateTime, ir.OperatorUsername, ir.IncidentDetails, ir.IncidentType, ir.Location.FullAddress, ir.Location.Latitude, ir.Location.Longitude, ir.Priority, resolved, ir.ContactNumber, ir.AssistanceRequested, "true");
  55.             return RedirectToAction("Index", "Dashboard");
  56.         }
  57.         public ActionResult SearchReport()
  58.         {
  59.             return View(daControl.GetAllReports());
  60.         }
  61.  
  62.         [HttpPost]
  63.         public ActionResult SearchReport(string searchValue, int searchChoice)
  64.         {
  65.             IncidentReport ir = new IncidentReport();
  66.             List<IncidentReport> listIR = new List<IncidentReport>();
  67.             if (searchChoice == 1)
  68.             {
  69.                 int rId = Convert.ToInt32(searchValue);
  70.                 ir = daControl.GetReportById(rId);
  71.                 if (ir != null)
  72.                     listIR.Add(ir);
  73.                 return View(listIR);
  74.             }
  75.             else if (searchChoice == 2)
  76.             {
  77.                 listIR = daControl.SearchReportByReporterName(searchValue);
  78.                 return View(listIR);
  79.             }
  80.             else if (searchChoice == 3)
  81.             {
  82.                 listIR = daControl.SearchReportByReporterNRIC(searchValue);
  83.                 return View(listIR);
  84.             }
  85.             else
  86.             {
  87.                 listIR = daControl.SearchReportByIncidentDetails(searchValue);
  88.                 return View(listIR);
  89.             }
  90.  
  91.             return RedirectToAction("Index", "Dashboard");
  92.         }
  93.  
  94.         private DataAccessController daController = new DataAccessController();
  95.         // GET: Report
  96.         [HttpGet]
  97.         public ActionResult Create()
  98.         {
  99.  
  100.             return View(new CreateReportModel() { HasMessage = false });
  101.         }
  102.  
  103.         [HttpPost]
  104.         public ActionResult Create(string reporterName, string ReporterNRIC, string reporterContact,
  105.             string IncidentDetails, IncidentType IncidentType, string LocationFullAddress,
  106.             int Priority, CreateReportModel model)
  107.         {
  108.             AlertController alertController = new AlertController();
  109.             var createReportModel = new CreateReportModel()
  110.             {
  111.                 HasMessage = false,
  112.                 InvalidAddress = false
  113.             };
  114.  
  115.             var locationService = new GoogleLocationService("AIzaSyAoYnvnulCCUYNLiV2zwi3XZslf3DFTHC8");
  116.             var point = locationService.GetLatLongFromAddress(LocationFullAddress);
  117.             if (point == null)
  118.             {
  119.                 createReportModel.InvalidAddress = true;
  120.                 return View(createReportModel);
  121.             }
  122.             var latitude = point.Latitude;
  123.             var longitude = point.Longitude;
  124.  
  125.             bool checkDuplicate = searchReportByLocationAndTime(LocationFullAddress, DateTime.Now.ToString());
  126.             List<AssistanceType> asType = new List<AssistanceType>();
  127.             bool[] trytry = new bool[4];
  128.             int i = 0;
  129.             foreach (var item in model.AssistanceTypeList)
  130.             {
  131.                 if (item.IsSelected)
  132.                 {
  133.                     trytry[i] = true;
  134.                     asType.Add(item.AssistanceType);
  135.                 }
  136.                 i++;  
  137.             }
  138.  
  139.             string alertSent = "false";
  140.  
  141.             if (!checkDuplicate)
  142.             {
  143.                 if (Priority == 5)
  144.                 {
  145.                     alertSent = "true";
  146.                     SendPublicAlert(0, "Alert: " + IncidentType + " at " + LocationFullAddress, true, true, true);
  147.                 }
  148.  
  149.                 daController.CreateReport(reporterName, ReporterNRIC, DateTime.Now, (User.Identity != null && User.Identity.IsAuthenticated ? User.Identity.Name : "UnknownUser"),
  150.                 IncidentDetails, IncidentType, LocationFullAddress, latitude.ToString(), longitude.ToString(), Priority, reporterContact, asType, alertSent);
  151.                
  152.                 alertController.SendGovAlert("Alert sent", trytry[0], trytry[1], trytry[2], trytry[3]);
  153.                 return RedirectToAction("Index", "Dashboard");
  154.             }
  155.             else//existing report
  156.             {
  157.                 if (model.AcceptedWarning)
  158.                 {
  159.                     if (Priority == 5)
  160.                     {
  161.                         alertSent = "true";
  162.                         SendPublicAlert(0, "Alert: " + IncidentType + " at " + LocationFullAddress, true, true, true);
  163.                     }
  164.  
  165.                     daController.CreateReport(reporterName, ReporterNRIC, DateTime.Now, (User.Identity != null && User.Identity.IsAuthenticated ? User.Identity.Name : "UnknownUser"),
  166.                     IncidentDetails, IncidentType, LocationFullAddress, latitude.ToString(), longitude.ToString(), Priority, reporterContact, asType, alertSent);
  167.                     alertController.SendGovAlert("Alert sent", trytry[0], trytry[1], trytry[2], trytry[3]);
  168.                     return RedirectToAction("Index", "Dashboard");
  169.                 }
  170.                 else
  171.                 {
  172.                     createReportModel.HasMessage = true;
  173.                     return View(createReportModel);
  174.                 }
  175.             }
  176.  
  177.            
  178.  
  179.         }
  180.  
  181.         private bool searchReportByLocationAndTime(string Location, string dateTimeReported)
  182.         {
  183.             var reports = daController.GetAllReports();
  184.  
  185.             DateTime datetime = Convert.ToDateTime(dateTimeReported);
  186.  
  187.             //get all the reports with the same location
  188.             var report_2 = reports.Where(r => r.Location.FullAddress.Contains(Location)).ToList();
  189.             foreach (var rep in report_2)
  190.             {
  191.                 if (checkDateTime(rep.ReportedDateTime))//existing report->not ok to make
  192.  
  193.                     return true; //not ok to make
  194.             }
  195.             /*
  196.             if (report_2 != null)
  197.                 return true; //existing report
  198.  
  199.             else
  200.                 return false; //no existing report*/
  201.             return false; //no existing report->ok to make report
  202.         }
  203.  
  204.  
  205.         private bool checkDateTime(DateTime dateTimeReported)
  206.         {
  207.             DateTime givenDateTime = dateTimeReported;
  208.             DateTime currentTime = DateTime.Now;
  209.             DateTime twoHoursGap = givenDateTime.AddHours(2);
  210.  
  211.             if (twoHoursGap < currentTime)
  212.                 return false; //2 hours elapsed since the report was made ->ok to make report
  213.             else
  214.                 return true; //hasnt 2 hours yet-existing report->not ok to make report
  215.         }
  216.  
  217.         public ActionResult Cancel()
  218.         {
  219.  
  220.             return RedirectToAction("Index", "Dashboard");
  221.         }
  222.  
  223.         public ActionResult Update(string reportId)
  224.         {
  225.             int rId = Convert.ToInt32(reportId);
  226.             IncidentReport ir = new IncidentReport();
  227.             ir = daControl.GetReportById(rId);
  228.             ir.checkAddress = false; //by default, address entered is correct
  229.             return View(ir);
  230.         }
  231.  
  232.         [HttpPost]
  233.         public ActionResult Update(int reportId, string opUser, string reporterNRIC, string reporterName, string reporterContactNum, string reportDateTime,
  234.              IncidentType incidentType, string incidentDetails, string locationFullAddress, string locationLatitude, string locationLongitude,
  235.              IncidentStatus status, int priorityLevel, int[] selecteditems, IncidentReport ir)
  236.         {
  237.             //a list to temporary hold the old values for Assistance Type
  238.             List<AssistanceType> tempTest = new List<AssistanceType>();
  239.             for(int i =0; i<selecteditems.Length;i++)
  240.             {
  241.                 tempTest.Add((AssistanceType)Enum.Parse(typeof(AssistanceType), Enum.GetName(typeof(AssistanceType), selecteditems[i])));
  242.             }
  243.  
  244.             //temporary model to hold the values from the update form page
  245.             IncidentReport tempModel = new IncidentReport()
  246.             {
  247.                 Id = reportId,
  248.                 OperatorUsername = opUser,
  249.                 ReporterNRIC = reporterNRIC,
  250.                 ReporterName = reporterName,
  251.                 ContactNumber = reporterContactNum,
  252.                 ReportedDateTime = Convert.ToDateTime(reportDateTime),
  253.                 IncidentType = incidentType,
  254.                 IncidentDetails = incidentDetails,
  255.                 Location = new Location()
  256.                 {
  257.                     FullAddress = locationFullAddress,
  258.                     Latitude = locationLatitude,
  259.                     Longitude = locationLongitude,
  260.                 },
  261.                 FullAddress = locationFullAddress,
  262.                 Latitude = locationFullAddress,
  263.                 Longitude = locationLongitude,
  264.                 Status = status,
  265.                 Priority = priorityLevel,
  266.                 AssistanceRequested = tempTest,
  267.                 checkAddress = false,
  268.             };
  269.  
  270.  
  271.             DateTime DateTimeReported = DateTime.Parse(reportDateTime); //convert string reportDateTime to Date Time reportDateTime to be stored in the database
  272.          
  273.             //based on address supplied, get the longtitude and latitude
  274.             var locationService = new GoogleLocationService("AIzaSyAoYnvnulCCUYNLiV2zwi3XZslf3DFTHC8");
  275.             var point = locationService.GetLatLongFromAddress(locationFullAddress);
  276.             if (point == null)
  277.             {
  278.                 tempModel.checkAddress = true;
  279.                 return View(tempModel);
  280.             }
  281.             var latitude = point.Latitude;
  282.             var longitude = point.Longitude;
  283.  
  284.             //a list to temporary hold the new values for Assistance Type that the user has updated
  285.             List<AssistanceType> assistanceRequested = new List<AssistanceType>();
  286.             if (selecteditems != null)
  287.                 for (int i = 0; i < selecteditems.Length; i++)
  288.                 {
  289.                     assistanceRequested.Add((AssistanceType)selecteditems[i]);
  290.                 }
  291.  
  292.  
  293.             //the sequence in the daControl.UpdateReport must be the same as the one in DataAccessController.cs UpdateReport
  294.             //this step is the passing of data from the update form page to the database
  295.             daControl.UpdateReport(reportId, reporterName, reporterNRIC, DateTimeReported, opUser, incidentDetails, incidentType, locationFullAddress,
  296.                     latitude.ToString(), longitude.ToString(), priorityLevel, status, reporterContactNum, assistanceRequested, null);
  297.            
  298.             TempData["Message"] = "Operation successful!";
  299.             return RedirectToAction("ViewReport", new { reportId = reportId });
  300.         }
  301.  
  302.         public ActionResult SendPublicReportAlert(string reportId)
  303.         {
  304.             int rId = Convert.ToInt32(reportId);
  305.             IncidentReport ir = new IncidentReport();
  306.             ir = daControl.GetReportById(rId);
  307.             //return View(cmsDB.Report.Where( a=> a.Id.Equals(rId)).ToList());
  308.             return View(ir);
  309.         }
  310.  
  311.         [HttpPost]
  312.         public ActionResult SendPublicAlert(int reportId, string message, bool checkBoxFacebook, bool checkBoxTwitter, bool checkBoxSMS)
  313.         {
  314.             if (checkBoxFacebook != true && checkBoxTwitter != true && checkBoxSMS != true)
  315.             {
  316.                 TempData["Message"] = "Please select a social media platform!";
  317.                 return RedirectToAction("SendPublicReportAlert", new { reportId = reportId });
  318.             }
  319.             else
  320.             {
  321.                 if (checkBoxTwitter == true && message.Length > 280)
  322.                 {
  323.                     Bitmap bitmap = new Bitmap(1, 1);
  324.                     Font font = new Font("Helvetica Neue", 18, FontStyle.Regular, GraphicsUnit.Pixel);
  325.                     Graphics graphics = Graphics.FromImage(bitmap);
  326.                     int width = (int)graphics.MeasureString(message, font).Width;
  327.                     int height = (int)graphics.MeasureString(message, font).Height;
  328.                     bitmap = new Bitmap(bitmap, new Size(width, height));
  329.                     graphics = Graphics.FromImage(bitmap);
  330.                     graphics.Clear(Color.White);
  331.                     graphics.SmoothingMode = SmoothingMode.AntiAlias;
  332.                     graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
  333.                     graphics.DrawString(message, font, new SolidBrush(Color.FromArgb(0, 0, 0)), 0, 0);
  334.                     graphics.Flush();
  335.                     graphics.Dispose();
  336.                     string fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".jpg";
  337.                     bitmap.Save(Server.MapPath("~/TwitterImage/") + fileName, ImageFormat.Jpeg);
  338.  
  339.                     // Send to Twitter as image
  340.                     string ConsumerKey = "sY6UcLziYU9PjSyVlcH2YPiVM",
  341.                     ConsumerKeySecret = "CqeK2ltBLhLgcpI4PKq3VJYDIRztxK9CjJizviUgeFW66zyL5E",
  342.                     AccessToken = "1051168092997410816-KhUyjE3BJPhRvA8gX6AGTZynZ34r68",
  343.                     AccessTokenSecret = "zzngFRf5L59NjFbWq3paaLTuTTjh30CQjZK3KPkBT7zml";
  344.  
  345.                     var twitter = new Twitter(
  346.                         ConsumerKey,
  347.                         ConsumerKeySecret,
  348.                         AccessToken,
  349.                         AccessTokenSecret
  350.                         );
  351.  
  352.                     string response = twitter.PublishImageToTwitter("", Server.MapPath("~/TwitterImage/") + fileName);
  353.                 }
  354.  
  355.                 if (checkBoxTwitter == true && message.Length <= 280)
  356.                 {
  357.                     // Send to Twitter
  358.                     string ConsumerKey = "sY6UcLziYU9PjSyVlcH2YPiVM",
  359.                     ConsumerKeySecret = "CqeK2ltBLhLgcpI4PKq3VJYDIRztxK9CjJizviUgeFW66zyL5E",
  360.                     AccessToken = "1051168092997410816-KhUyjE3BJPhRvA8gX6AGTZynZ34r68",
  361.                     AccessTokenSecret = "zzngFRf5L59NjFbWq3paaLTuTTjh30CQjZK3KPkBT7zml";
  362.  
  363.                     var twitter = new Twitter(
  364.                         ConsumerKey,
  365.                         ConsumerKeySecret,
  366.                         AccessToken,
  367.                         AccessTokenSecret
  368.                         );
  369.  
  370.                     string response = twitter.PublishToTwitter(message);
  371.                 }
  372.                 // Send to FaceBook
  373.                 if (checkBoxFacebook == true)
  374.                 {
  375.                     var accessToken = "EAAe3ZBqsZCN44BAHCOrZB6DPy1IHShRlZCxwnLsyUirbRFS5BUqWs8c7NlZAkFQ58xjZCwXvsaZAQ9BbGl0nxFcoAqwqUsoZCdNPiQYhg7FchGXodfVc88GVU0UmVamtZCltgChvFAe57iehAba66DUoU3mngbnHhN58DljSeRVUjtkZBejuBezeMDNTYUwLKSJm8ZD";
  376.                     var facebookClient = new FacebookClient();
  377.                     var facebookService = new FacebookService(facebookClient);
  378.  
  379.                     var postOnWallTask = facebookService.PostOnWallAsync(accessToken, message);
  380.                     //Task.WaitAll(postOnWallTask);
  381.                 }
  382.  
  383.                 // Send to SMS
  384.                 if (checkBoxSMS == true)
  385.                 {
  386.                     var allMobiles = cmsDB.MobileNo.Select(a => a.MobileNo1);
  387.                     var allNames = cmsDB.MobileNo.Select(a => a.Name);
  388.                     string[] mobileNo = allMobiles.ToArray();
  389.                     string[] names = allNames.ToArray();
  390.  
  391.                     using (System.Net.WebClient Client = new System.Net.WebClient())
  392.                     {
  393.                         for (int i = 0; i < allMobiles.Count(); i++)
  394.                         {
  395.                             if (names[i] != "Emergency Ambulance")
  396.                                 if (names[i] != "Rescue and Evacuation")
  397.                                     if (names[i] != "Fire-Fighting")
  398.                                         if (names[i] != "Gas Leak Control")
  399.                                         {
  400.                                             string url = "http://smsc.vianett.no/v3/send.ashx?" +
  401.                                                          "src=6597654678&" +
  402.                                                          "dst=65" + mobileNo[i] + "&" +
  403.                                                          "msg=" + message + "&" +
  404.                                                          "username=cmsrandom3003@gmail.com&" +
  405.                                                          "password=1wecx";
  406.                                             Client.DownloadString(url);
  407.                                         }
  408.                         }
  409.                     }
  410.                 }
  411.  
  412.                 if (reportId != 0)
  413.                 {
  414.                     int rId = Convert.ToInt32(reportId);
  415.                     IncidentReport ir = new IncidentReport();
  416.                     ir = daControl.GetReportById(rId);
  417.                     daControl.UpdateReport(ir.Id, ir.ReporterName, ir.ReporterNRIC, ir.ReportedDateTime, ir.OperatorUsername, ir.IncidentDetails, ir.IncidentType, ir.Location.FullAddress, ir.Location.Latitude, ir.Location.Longitude, ir.Priority, ir.Status, ir.ContactNumber, ir.AssistanceRequested, "true");
  418.                     TempData["Message"] = "Operation successful!";
  419.                    
  420.                 }
  421.                 return RedirectToAction("SendPublicReportAlert", new { reportId = reportId });
  422.                 //daControl.UpdateReportAfterAlertSent(reportId);
  423.                
  424.             }
  425.         }
  426.  
  427.     }
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement