Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using CrisisManagementSystem.Models;
- using CrisisManagementSystem.DataAccess;
- using GoogleMaps.LocationServices;
- using CrisisManagementSystem.Controllers.Alert;
- using System.Drawing;
- using System.IO;
- using System.Drawing.Imaging;
- using System.Drawing.Drawing2D;
- using System.Drawing.Text;
- using CrisisManagementSystem.Controllers;
- namespace CrisisManagementSystem.Controllers
- {
- public class ReportController : Controller
- {
- // GET: Report
- private CMSEntities cmsDB = new CMSEntities();
- private DataAccessController daControl = new DataAccessController();
- // GET: Report
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult ViewReport(string reportId)
- {
- int rId = Convert.ToInt32(reportId);
- IncidentReport ir = new IncidentReport();
- ir = daControl.GetReportById(rId);
- //return View(cmsDB.Report.Where( a=> a.Id.Equals(rId)).ToList());
- return View(ir);
- }
- public ActionResult Close(string reportId)
- {
- // var del = new Report { Id = Int32.Parse(reportId) };
- //cmsDB.Report.Attach(del);
- //cmsDB.Report.Remove(del);
- //cmsDB.SaveChanges();
- int rId = Convert.ToInt32(reportId);
- IncidentReport ir = new IncidentReport();
- ir = daControl.GetReportById(rId);
- IncidentStatus resolved = IncidentStatus.Resolved;
- if (ir.AlertSent == "true" || ir.Priority == 5)
- {
- SendPublicAlert(rId, ir.IncidentType.ToString() + " at " + ir.Location.FullAddress.ToString() + " has been resolved.", true, true, true);
- }
- 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");
- return RedirectToAction("Index", "Dashboard");
- }
- public ActionResult SearchReport()
- {
- return View(daControl.GetAllReports());
- }
- [HttpPost]
- public ActionResult SearchReport(string searchValue, int searchChoice)
- {
- IncidentReport ir = new IncidentReport();
- List<IncidentReport> listIR = new List<IncidentReport>();
- if (searchChoice == 1)
- {
- int rId = Convert.ToInt32(searchValue);
- ir = daControl.GetReportById(rId);
- if (ir != null)
- listIR.Add(ir);
- return View(listIR);
- }
- else if (searchChoice == 2)
- {
- listIR = daControl.SearchReportByReporterName(searchValue);
- return View(listIR);
- }
- else if (searchChoice == 3)
- {
- listIR = daControl.SearchReportByReporterNRIC(searchValue);
- return View(listIR);
- }
- else
- {
- listIR = daControl.SearchReportByIncidentDetails(searchValue);
- return View(listIR);
- }
- return RedirectToAction("Index", "Dashboard");
- }
- private DataAccessController daController = new DataAccessController();
- // GET: Report
- [HttpGet]
- public ActionResult Create()
- {
- return View(new CreateReportModel() { HasMessage = false });
- }
- [HttpPost]
- public ActionResult Create(string reporterName, string ReporterNRIC, string reporterContact,
- string IncidentDetails, IncidentType IncidentType, string LocationFullAddress,
- int Priority, CreateReportModel model)
- {
- AlertController alertController = new AlertController();
- var createReportModel = new CreateReportModel()
- {
- HasMessage = false,
- InvalidAddress = false
- };
- var locationService = new GoogleLocationService("AIzaSyAoYnvnulCCUYNLiV2zwi3XZslf3DFTHC8");
- var point = locationService.GetLatLongFromAddress(LocationFullAddress);
- if (point == null)
- {
- createReportModel.InvalidAddress = true;
- return View(createReportModel);
- }
- var latitude = point.Latitude;
- var longitude = point.Longitude;
- bool checkDuplicate = searchReportByLocationAndTime(LocationFullAddress, DateTime.Now.ToString());
- List<AssistanceType> asType = new List<AssistanceType>();
- bool[] trytry = new bool[4];
- int i = 0;
- foreach (var item in model.AssistanceTypeList)
- {
- if (item.IsSelected)
- {
- trytry[i] = true;
- asType.Add(item.AssistanceType);
- }
- i++;
- }
- string alertSent = "false";
- if (!checkDuplicate)
- {
- if (Priority == 5)
- {
- alertSent = "true";
- SendPublicAlert(0, "Alert: " + IncidentType + " at " + LocationFullAddress, true, true, true);
- }
- daController.CreateReport(reporterName, ReporterNRIC, DateTime.Now, (User.Identity != null && User.Identity.IsAuthenticated ? User.Identity.Name : "UnknownUser"),
- IncidentDetails, IncidentType, LocationFullAddress, latitude.ToString(), longitude.ToString(), Priority, reporterContact, asType, alertSent);
- alertController.SendGovAlert("Alert sent", trytry[0], trytry[1], trytry[2], trytry[3]);
- return RedirectToAction("Index", "Dashboard");
- }
- else//existing report
- {
- if (model.AcceptedWarning)
- {
- if (Priority == 5)
- {
- alertSent = "true";
- SendPublicAlert(0, "Alert: " + IncidentType + " at " + LocationFullAddress, true, true, true);
- }
- daController.CreateReport(reporterName, ReporterNRIC, DateTime.Now, (User.Identity != null && User.Identity.IsAuthenticated ? User.Identity.Name : "UnknownUser"),
- IncidentDetails, IncidentType, LocationFullAddress, latitude.ToString(), longitude.ToString(), Priority, reporterContact, asType, alertSent);
- alertController.SendGovAlert("Alert sent", trytry[0], trytry[1], trytry[2], trytry[3]);
- return RedirectToAction("Index", "Dashboard");
- }
- else
- {
- createReportModel.HasMessage = true;
- return View(createReportModel);
- }
- }
- }
- private bool searchReportByLocationAndTime(string Location, string dateTimeReported)
- {
- var reports = daController.GetAllReports();
- DateTime datetime = Convert.ToDateTime(dateTimeReported);
- //get all the reports with the same location
- var report_2 = reports.Where(r => r.Location.FullAddress.Contains(Location)).ToList();
- foreach (var rep in report_2)
- {
- if (checkDateTime(rep.ReportedDateTime))//existing report->not ok to make
- return true; //not ok to make
- }
- /*
- if (report_2 != null)
- return true; //existing report
- else
- return false; //no existing report*/
- return false; //no existing report->ok to make report
- }
- private bool checkDateTime(DateTime dateTimeReported)
- {
- DateTime givenDateTime = dateTimeReported;
- DateTime currentTime = DateTime.Now;
- DateTime twoHoursGap = givenDateTime.AddHours(2);
- if (twoHoursGap < currentTime)
- return false; //2 hours elapsed since the report was made ->ok to make report
- else
- return true; //hasnt 2 hours yet-existing report->not ok to make report
- }
- public ActionResult Cancel()
- {
- return RedirectToAction("Index", "Dashboard");
- }
- public ActionResult Update(string reportId)
- {
- int rId = Convert.ToInt32(reportId);
- IncidentReport ir = new IncidentReport();
- ir = daControl.GetReportById(rId);
- ir.checkAddress = false; //by default, address entered is correct
- return View(ir);
- }
- [HttpPost]
- public ActionResult Update(int reportId, string opUser, string reporterNRIC, string reporterName, string reporterContactNum, string reportDateTime,
- IncidentType incidentType, string incidentDetails, string locationFullAddress, string locationLatitude, string locationLongitude,
- IncidentStatus status, int priorityLevel, int[] selecteditems, IncidentReport ir)
- {
- //a list to temporary hold the old values for Assistance Type
- List<AssistanceType> tempTest = new List<AssistanceType>();
- for(int i =0; i<selecteditems.Length;i++)
- {
- tempTest.Add((AssistanceType)Enum.Parse(typeof(AssistanceType), Enum.GetName(typeof(AssistanceType), selecteditems[i])));
- }
- //temporary model to hold the values from the update form page
- IncidentReport tempModel = new IncidentReport()
- {
- Id = reportId,
- OperatorUsername = opUser,
- ReporterNRIC = reporterNRIC,
- ReporterName = reporterName,
- ContactNumber = reporterContactNum,
- ReportedDateTime = Convert.ToDateTime(reportDateTime),
- IncidentType = incidentType,
- IncidentDetails = incidentDetails,
- Location = new Location()
- {
- FullAddress = locationFullAddress,
- Latitude = locationLatitude,
- Longitude = locationLongitude,
- },
- FullAddress = locationFullAddress,
- Latitude = locationFullAddress,
- Longitude = locationLongitude,
- Status = status,
- Priority = priorityLevel,
- AssistanceRequested = tempTest,
- checkAddress = false,
- };
- DateTime DateTimeReported = DateTime.Parse(reportDateTime); //convert string reportDateTime to Date Time reportDateTime to be stored in the database
- //based on address supplied, get the longtitude and latitude
- var locationService = new GoogleLocationService("AIzaSyAoYnvnulCCUYNLiV2zwi3XZslf3DFTHC8");
- var point = locationService.GetLatLongFromAddress(locationFullAddress);
- if (point == null)
- {
- tempModel.checkAddress = true;
- return View(tempModel);
- }
- var latitude = point.Latitude;
- var longitude = point.Longitude;
- //a list to temporary hold the new values for Assistance Type that the user has updated
- List<AssistanceType> assistanceRequested = new List<AssistanceType>();
- if (selecteditems != null)
- for (int i = 0; i < selecteditems.Length; i++)
- {
- assistanceRequested.Add((AssistanceType)selecteditems[i]);
- }
- //the sequence in the daControl.UpdateReport must be the same as the one in DataAccessController.cs UpdateReport
- //this step is the passing of data from the update form page to the database
- daControl.UpdateReport(reportId, reporterName, reporterNRIC, DateTimeReported, opUser, incidentDetails, incidentType, locationFullAddress,
- latitude.ToString(), longitude.ToString(), priorityLevel, status, reporterContactNum, assistanceRequested, null);
- TempData["Message"] = "Operation successful!";
- return RedirectToAction("ViewReport", new { reportId = reportId });
- }
- public ActionResult SendPublicReportAlert(string reportId)
- {
- int rId = Convert.ToInt32(reportId);
- IncidentReport ir = new IncidentReport();
- ir = daControl.GetReportById(rId);
- //return View(cmsDB.Report.Where( a=> a.Id.Equals(rId)).ToList());
- return View(ir);
- }
- [HttpPost]
- public ActionResult SendPublicAlert(int reportId, string message, bool checkBoxFacebook, bool checkBoxTwitter, bool checkBoxSMS)
- {
- if (checkBoxFacebook != true && checkBoxTwitter != true && checkBoxSMS != true)
- {
- TempData["Message"] = "Please select a social media platform!";
- return RedirectToAction("SendPublicReportAlert", new { reportId = reportId });
- }
- else
- {
- if (checkBoxTwitter == true && message.Length > 280)
- {
- Bitmap bitmap = new Bitmap(1, 1);
- Font font = new Font("Helvetica Neue", 18, FontStyle.Regular, GraphicsUnit.Pixel);
- Graphics graphics = Graphics.FromImage(bitmap);
- int width = (int)graphics.MeasureString(message, font).Width;
- int height = (int)graphics.MeasureString(message, font).Height;
- bitmap = new Bitmap(bitmap, new Size(width, height));
- graphics = Graphics.FromImage(bitmap);
- graphics.Clear(Color.White);
- graphics.SmoothingMode = SmoothingMode.AntiAlias;
- graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
- graphics.DrawString(message, font, new SolidBrush(Color.FromArgb(0, 0, 0)), 0, 0);
- graphics.Flush();
- graphics.Dispose();
- string fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".jpg";
- bitmap.Save(Server.MapPath("~/TwitterImage/") + fileName, ImageFormat.Jpeg);
- // Send to Twitter as image
- string ConsumerKey = "sY6UcLziYU9PjSyVlcH2YPiVM",
- ConsumerKeySecret = "CqeK2ltBLhLgcpI4PKq3VJYDIRztxK9CjJizviUgeFW66zyL5E",
- AccessToken = "1051168092997410816-KhUyjE3BJPhRvA8gX6AGTZynZ34r68",
- AccessTokenSecret = "zzngFRf5L59NjFbWq3paaLTuTTjh30CQjZK3KPkBT7zml";
- var twitter = new Twitter(
- ConsumerKey,
- ConsumerKeySecret,
- AccessToken,
- AccessTokenSecret
- );
- string response = twitter.PublishImageToTwitter("", Server.MapPath("~/TwitterImage/") + fileName);
- }
- if (checkBoxTwitter == true && message.Length <= 280)
- {
- // Send to Twitter
- string ConsumerKey = "sY6UcLziYU9PjSyVlcH2YPiVM",
- ConsumerKeySecret = "CqeK2ltBLhLgcpI4PKq3VJYDIRztxK9CjJizviUgeFW66zyL5E",
- AccessToken = "1051168092997410816-KhUyjE3BJPhRvA8gX6AGTZynZ34r68",
- AccessTokenSecret = "zzngFRf5L59NjFbWq3paaLTuTTjh30CQjZK3KPkBT7zml";
- var twitter = new Twitter(
- ConsumerKey,
- ConsumerKeySecret,
- AccessToken,
- AccessTokenSecret
- );
- string response = twitter.PublishToTwitter(message);
- }
- // Send to FaceBook
- if (checkBoxFacebook == true)
- {
- var accessToken = "EAAe3ZBqsZCN44BAHCOrZB6DPy1IHShRlZCxwnLsyUirbRFS5BUqWs8c7NlZAkFQ58xjZCwXvsaZAQ9BbGl0nxFcoAqwqUsoZCdNPiQYhg7FchGXodfVc88GVU0UmVamtZCltgChvFAe57iehAba66DUoU3mngbnHhN58DljSeRVUjtkZBejuBezeMDNTYUwLKSJm8ZD";
- var facebookClient = new FacebookClient();
- var facebookService = new FacebookService(facebookClient);
- var postOnWallTask = facebookService.PostOnWallAsync(accessToken, message);
- //Task.WaitAll(postOnWallTask);
- }
- // Send to SMS
- if (checkBoxSMS == true)
- {
- var allMobiles = cmsDB.MobileNo.Select(a => a.MobileNo1);
- var allNames = cmsDB.MobileNo.Select(a => a.Name);
- string[] mobileNo = allMobiles.ToArray();
- string[] names = allNames.ToArray();
- using (System.Net.WebClient Client = new System.Net.WebClient())
- {
- for (int i = 0; i < allMobiles.Count(); i++)
- {
- if (names[i] != "Emergency Ambulance")
- if (names[i] != "Rescue and Evacuation")
- if (names[i] != "Fire-Fighting")
- if (names[i] != "Gas Leak Control")
- {
- string url = "http://smsc.vianett.no/v3/send.ashx?" +
- "src=6597654678&" +
- "dst=65" + mobileNo[i] + "&" +
- "msg=" + message + "&" +
- "username=cmsrandom3003@gmail.com&" +
- "password=1wecx";
- Client.DownloadString(url);
- }
- }
- }
- }
- if (reportId != 0)
- {
- int rId = Convert.ToInt32(reportId);
- IncidentReport ir = new IncidentReport();
- ir = daControl.GetReportById(rId);
- 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");
- TempData["Message"] = "Operation successful!";
- }
- return RedirectToAction("SendPublicReportAlert", new { reportId = reportId });
- //daControl.UpdateReportAfterAlertSent(reportId);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement