Created
February 7, 2018 14:46
-
-
Save rbaty-barr/2579b017933a3d0968fa2e6fc9e0416c to your computer and use it in GitHub Desktop.
CSAC app_code helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Net.Mail; | |
using Examine; | |
using Examine.Providers; | |
using Skybrud.Umbraco.GridData; | |
using Skybrud.Umbraco.GridData.Values; | |
using Skybrud.Umbraco.GridData.Interfaces; | |
using Skybrud.Umbraco.GridData.LeBlender.Values; | |
using Lecoati.LeBlender.Extension.Models; | |
using Umbraco.Core; | |
using Umbraco.Core.Logging; | |
using Archetype.Models; | |
using Newtonsoft.Json; | |
using Umbraco.Core; | |
using Umbraco.Core.Logging; | |
using Umbraco.Web; | |
using Umbraco.Web.Mvc; | |
using Umbraco.Core.Models; | |
using Umbraco.Core.Services; | |
using Umbraco.Web; | |
using Ical.Net; | |
using Our.Umbraco.Cloud.Toolkit; | |
namespace csacBAportal.App_Code | |
{ | |
public class Startup : ApplicationEventHandler { | |
private static ExamineIndexer _examineIndexer; | |
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { | |
// Register events for Examine | |
_examineIndexer = new ExamineIndexer(); | |
} | |
} | |
public class ExamineIndexer { | |
public ExamineIndexer() { | |
BaseIndexProvider externalIndexer = ExamineManager.Instance.IndexProviderCollection["baIndexer"]; | |
externalIndexer.GatheringNodeData += OnExamineGatheringNodeData; | |
} | |
private void OnExamineGatheringNodeData(object sender, IndexingNodeDataEventArgs e) { | |
try { | |
string nodeTypeAlias = e.Fields["nodeTypeAlias"]; | |
LogHelper.Info<ExamineIndexer>("Gathering node data for node #" + e.NodeId + " (type: " + nodeTypeAlias + ")"); | |
// if (nodeTypeAlias == "Home" || nodeTypeAlias == "LandingPage" || nodeTypeAlias == "TextPage" || nodeTypeAlias == "BlogPost") { | |
string value; | |
if (e.Fields.TryGetValue("contentGrid", out value)) { | |
LogHelper.Info<ExamineIndexer>("BINGO!! Node has \"content\" value\""); | |
GridDataModel grid = GridDataModel.Deserialize(e.Fields["contentGrid"]); | |
StringBuilder combined = new StringBuilder(); | |
foreach (GridControl ctrl in grid.GetAllControls()) { | |
switch (ctrl.Editor.Alias) { | |
case "rte": { | |
// Get the HTML value | |
string html = ctrl.GetValue<GridControlRichTextValue>().Value; | |
// Strip any HTML tags so we only have text | |
string text = Regex.Replace(html, "<.*?>", ""); | |
// Extra decoding may be necessary | |
text = HttpUtility.HtmlDecode(text); | |
// Now append the text | |
combined.AppendLine(text); | |
break; | |
} | |
case "hideShowItem" : { | |
LogHelper.Info<ExamineIndexer>("found a hide show item"); | |
break; | |
} | |
case "LeBlender.hideShowItem" : { | |
LogHelper.Info<ExamineIndexer>("leBlender HS Items Found!" + ctrl.GetValue<GridControlLeBlenderValue>().Items.Count()); | |
//LogHelper.Info<ExamineIndexer>("Value: " + ctrl.GetValue<GridControlLeBlenderValue>("LeBlender.hideShowTitle")); | |
//GridDataModel subgrid = GridDataModel.Deserialize(ctrl.Fields["LeBlender.hideShowItem"]); | |
int i = 0; | |
foreach(LeBlenderValue thing in ctrl.GetValue<GridControlLeBlenderValue>().Items){ | |
LogHelper.Info<ExamineIndexer>("Value: " + thing.GetValue("LeBlender.hideShowTitle") + "(" + i + ")"); | |
i = i + 1; | |
} | |
//string html = ctrl.GetValue<GridControlLeBlenderValue>().Value; | |
//foreach(LeBlenderValue value in ctrl.Values){ | |
// LogHelper.Info<ExamineIndexer>("Value is: " + value); | |
//} | |
// Strip any HTML tags so we only have text | |
//string text = Regex.Replace(html, "<.*?>", ""); | |
// Extra decoding may be necessary | |
//text = HttpUtility.HtmlDecode(text); | |
// Now append the text | |
//combined.AppendLine(text); | |
break; | |
} | |
case "media": { | |
GridControlMediaValue media = ctrl.GetValue<GridControlMediaValue>(); | |
combined.AppendLine(media.Caption); | |
break; | |
} | |
case "headline": | |
case "quote": { | |
combined.AppendLine(ctrl.GetValue<GridControlTextValue>().Value); | |
break; | |
} | |
} | |
} | |
e.Fields["content"] = combined.ToString(); | |
} else { | |
// LogHelper.Info<ExamineIndexer>("Node has no \"content\" value\""); | |
} | |
// } | |
} catch (Exception ex) { | |
LogHelper.Error<ExamineIndexer>("MAYDAY! MAYDAY! MAYDAY!", ex); | |
} | |
try{ | |
if (e.Fields.TryGetValue("bodyContentBlocks", out value)) | |
{ | |
var archetypeValueAsString = e.Fields["bodyContentBlocks"]; | |
if (!string.IsNullOrEmpty(archetypeValueAsString)) | |
{ | |
var archetype = JsonConvert.DeserializeObject<ArchetypeModel>(archetypeValueAsString); | |
var index = 0; | |
/* | |
so basically, what i have to do here is to grab each individual archetype TYPE that i have | |
above, i deserialized the entire archetype property whihch was bodyContentBlocks | |
you will see farther down, that i am testing for various alias names, then doing some looping if those were | |
indeed nested types of archetypes. | |
by splitting them out, you can in theory use boosts, etc. -- that will be the next evolution. | |
NOTE: you will need to chante this entire archetype section to fit your aliases. | |
*/ | |
foreach (var fieldset in archetype) | |
{ | |
string value = null; | |
string fieldName = null; | |
index++; | |
if (fieldset.HasValue("sectionTitle") && fieldset.GetValue<string>("showTitle") =="1") { | |
value = fieldset.GetValue<string>("sectionTitle"); | |
fieldName = "section-title"; | |
e.Fields.Add(string.Format("archetype-{0}-{1}", fieldName, index), value); | |
} | |
if (fieldset.HasValue("bodyText")) | |
{ | |
value= fieldset.GetValue<string>("bodyText"); | |
fieldName = "body-content"; | |
e.Fields.Add(string.Format("archetype-{0}-{1}", fieldName, index), value); | |
} | |
if (fieldset.HasValue("hideshowitem")) | |
{ | |
var hsItems = JsonConvert.DeserializeObject<ArchetypeModel>(fieldset.GetValue("hideshowitem")); | |
foreach ( var item in hsItems) { | |
if (item.HasValue("hideshowtitle")) | |
{ | |
value = item.GetValue<string>("hideshowtitle"); | |
fieldName = "hide-show-title"; | |
e.Fields.Add(string.Format("archetype-{0}-{1}", fieldName, index), value); | |
} | |
if (item.HasValue("hideshowbody")) | |
{ | |
value = item.GetValue<string>("hideshowbody"); | |
fieldName = "hide-show-body"; | |
e.Fields.Add(string.Format("archetype-{0}-{1}", fieldName, index), value); | |
} | |
} | |
} | |
if (fieldset.HasValue("cboxitem")) | |
{ | |
var cboxItems = JsonConvert.DeserializeObject<ArchetypeModel>(fieldset.GetValue("cboxitem")); | |
foreach ( var cbox in cboxItems) | |
{ | |
if (cbox.HasValue("cboxtitle")) | |
{ | |
value = cbox.GetValue<string>("cboxtitle"); | |
fieldName = "callout-box"; | |
e.Fields.Add(string.Format("archetype-{0}-{1}", fieldName, index), value); | |
} | |
if (cbox.HasValue("cboxcontent")) | |
{ | |
value = cbox.GetValue<string>("cboxcontent"); | |
fieldName = "callout-box-content"; | |
e.Fields.Add(string.Format("archetype-{0}-{1}", fieldName, index), value); | |
} | |
} | |
} | |
} | |
} | |
} | |
// Stuff all the fields into a single field for easier searching | |
var combinedFields = new StringBuilder(); | |
foreach (var keyValuePair in e.Fields) | |
{ | |
combinedFields.AppendLine(keyValuePair.Value); | |
} | |
e.Fields.Add("contents", combinedFields.ToString()); | |
} catch (Exception ex) { | |
LogHelper.Error<ExamineIndexer>("MAYDAY! MAYDAY! MAYDAY!", ex); | |
} | |
} | |
} | |
public class resetEmailSurfaceController : SurfaceController | |
{ | |
[HttpPost] | |
public ActionResult sendReset() | |
{ | |
string host = System.Web.HttpContext.Current.Request.Url.Host; | |
string email = Request["email"]; | |
var theMember = Members.GetByEmail(email); | |
var resultText = "sending..."; | |
Guid theKey = Guid.NewGuid(); | |
if(theMember != null){ | |
var memberService = ApplicationContext.Current.Services.MemberService; | |
var member = memberService.GetByEmail(email); | |
member.SetValue("resetPasswordKey", theKey.ToString()); | |
memberService.Save(member); | |
//var member = Services.MemberService.GetByusername(theMember.LoginName); | |
//member.SetValue("resetPasswordKey", theKey); | |
MailMessage message = new MailMessage(); | |
string fromEmail = "password-reset@carpenterssw.org"; | |
message.From = new MailAddress(fromEmail); | |
message.To.Add(new MailAddress(@email)); | |
message.Bcc.Add(new MailAddress("rbaty-barr@segalco.com")); | |
message.Subject = "Password reset request form the Carpenters BA Portal"; | |
message.Body = "<table border='0' cellpadding='20' cellspacing='0' width='600' align='center' style='font-family:Helvetica,Arial,sans-serif;'><tr><td>"; | |
message.Body = message.Body + "<h1>Password Reset Details</h1>"; | |
message.Body = message.Body + "<p>You have requested a password reset for the Southwest Carpenters BA Portal. To reset your password, please use the following link.</p>"; | |
message.Body = message.Body + "<p><a href='http://" + host + "/reset?id="+ @theKey +"'>Password reset link.</a></p>"; | |
message.Body = message.Body + "</td></tr></table>"; | |
message.IsBodyHtml = true; | |
SmtpClient client = new SmtpClient(); | |
NetworkCredential basicCredential = new NetworkCredential("apikey", "youThinkIwouldPostMySecretKey?"); | |
client.Host = "smtp.sendgrid.net"; | |
client.UseDefaultCredentials = false; | |
client.Credentials = basicCredential; | |
//client.EnableSsl = true; | |
client.Port=587; | |
client.Send(message); | |
resultText = "success"; | |
} else { | |
resultText = "No Member Exists"; | |
} | |
return Content(resultText); | |
} | |
[HttpPost] | |
public ActionResult resetPass() | |
{ | |
string email = Request["email"]; | |
string guid = Request["guid"]; | |
string newPass = Request["newPass"]; | |
var resultText = "resetting..."; | |
var theMember = Members.GetByEmail(email); | |
if(theMember != null && email != null && guid != null && newPass != null){ | |
var memberService = ApplicationContext.Current.Services.MemberService; | |
var member = memberService.GetByEmail(email); | |
if(member.GetValue<string>("resetPasswordKey") == guid){ | |
member.SetValue("resetPasswordKey", ""); | |
memberService.SavePassword(member, newPass); | |
memberService.Save(member); | |
resultText = "0"; | |
} else { | |
resultText = "1"; | |
} | |
} else { | |
resultText ="2"; | |
} | |
return Content(resultText); | |
} | |
} | |
class CalDate | |
{ | |
public string name; | |
public string date; | |
} | |
class DayEvent | |
{ | |
public string category; | |
public string description; | |
public string summary; | |
public string start; | |
public string duration; | |
} | |
public class Helpers | |
{ | |
public static CalendarCollection LoadFromUriAsync(Uri uri) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
using (var response = client.GetAsync(uri).Result) | |
{ | |
response.EnsureSuccessStatusCode(); | |
var result = response.Content.ReadAsStringAsync().Result; | |
var calendarCollection = Ical.Net.Calendar.LoadFromStream(new StringReader(result)) as CalendarCollection; | |
return calendarCollection; | |
} | |
} | |
} | |
} | |
public class RenderCalendarController : SurfaceController | |
{ | |
public ActionResult GetDates() | |
{ | |
CalendarCollection calendarCollection = Helpers.LoadFromUriAsync(new Uri("https://calendar.google.com/calendar/ical/bps101.net_6s20210upb019c4kd3t9an1t08%40group.calendar.google.com/public/basic.ics")); | |
var firstCalendar = calendarCollection.First(); | |
//CalDate theDates = new CalDate(); | |
List<CalDate> theDates = new List<CalDate>(); | |
foreach (var item in firstCalendar.Events) | |
{ | |
DateTime startDate = item.Start.AsSystemLocal; | |
var duration = item.Duration; | |
var theCategory = item.IsAllDay ? "allday" : "regular"; | |
theDates.Add(new CalDate() { name = theCategory, date = startDate.ToString("yyyy-MM-dd") }); | |
} | |
return Json(theDates, JsonRequestBehavior.AllowGet); | |
} | |
public ActionResult ListEvents(string matchThis) | |
{ | |
//CalendarCollection calendarCollection = Helpers.LoadFromUriAsync(new Uri("https://calendar.google.com/calendar/ical/bps101.net_6s20210upb019c4kd3t9an1t08%40group.calendar.google.com/public/basic.ics")); | |
var envirLive = Umbraco.IsLive(); | |
var envirDev = Umbraco.IsDevelopment(); | |
var calNode = 1562; | |
if(envirDev){ | |
calNode = 2495; | |
} | |
if(envirLive){ | |
calNode = 2662; | |
} | |
var startPage = Umbraco.TypedContent(calNode); | |
var firstCalendar = startPage.Descendants("bACalendarItem").Where(x =>x.IsVisible()); | |
List <DayEvent> theEvents = new List<DayEvent>(); | |
foreach( var item in firstCalendar ) | |
{ | |
DateTime startDate = item.GetPropertyValue<DateTime>("calStartDate"); | |
//var startDate = "2017-12-13"; | |
var description = item.GetPropertyValue("eventName"); | |
var theCategory = item.GetPropertyValue<bool>("IsMailing").ToString() == "True" ? "mailing" : "event"; | |
var summary = item.GetPropertyValue("eventDetails"); | |
var duration = "01:00"; | |
theEvents.Add(new DayEvent { description = description.ToString(), start = startDate.ToString("yyyy-MM-dd"), summary = summary.ToString(), duration = duration, category= theCategory.ToString() }); //startDate.ToString("MMMM dd, yyyy") | |
//.ToString("yyyy-MM-dd") | |
} | |
//return null; | |
return Json(theEvents, JsonRequestBehavior.AllowGet); //, JsonRequestBehavior.AllowGet | |
//return Json(DateTime.ParseExact(matchThis, "ddd MMM dd yyyy HH:mm:ss 'GMT'K", null).ToString("MMMM dd, yyyy")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment