|
| 1 | +using System.Web.Mvc; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using Orchard; |
| 4 | +using Orchard.Data; |
| 5 | +using Orchard.Email.Models; |
| 6 | +using Orchard.Localization; |
| 7 | +using Orchard.Security; |
| 8 | +using Orchard.UI.Admin; |
| 9 | +using Orchard.UI.Notify; |
| 10 | +using Orchard.Workflows.Models; |
| 11 | +using Upgrade.Services; |
| 12 | + |
| 13 | +namespace Upgrade.Controllers |
| 14 | +{ |
| 15 | + [Admin] |
| 16 | + public class MessagingController : Controller |
| 17 | + { |
| 18 | + private readonly IUpgradeService _upgradeService; |
| 19 | + private readonly IOrchardServices _orchardServices; |
| 20 | + private readonly IRepository<ActivityRecord> _repository; |
| 21 | + |
| 22 | + public MessagingController( |
| 23 | + IUpgradeService upgradeService, |
| 24 | + IOrchardServices orchardServices, |
| 25 | + IRepository<ActivityRecord> repository) |
| 26 | + { |
| 27 | + _upgradeService = upgradeService; |
| 28 | + _orchardServices = orchardServices; |
| 29 | + _repository = repository; |
| 30 | + } |
| 31 | + |
| 32 | + public Localizer T { get; set; } |
| 33 | + |
| 34 | + public ActionResult Index() |
| 35 | + { |
| 36 | + var found = false; |
| 37 | + var activityTable = _upgradeService.GetPrefixedTableName("Orchard_Workflows_ActivityRecord"); |
| 38 | + if (_upgradeService.TableExists(activityTable)) |
| 39 | + { |
| 40 | + _upgradeService.ExecuteReader("SELECT * FROM " + activityTable + " WHERE Name = 'SendEmail'", |
| 41 | + (reader, connection) => |
| 42 | + { |
| 43 | + found = true; |
| 44 | + }); |
| 45 | + |
| 46 | + if (!found) |
| 47 | + { |
| 48 | + _orchardServices.Notifier.Warning(T("This step is unnecessary as no Send Email activities were found.")); |
| 49 | + } |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + _orchardServices.Notifier.Warning(T("This step appears unnecessary since it appears Orchard Workflows is not enabled.")); |
| 54 | + } |
| 55 | + |
| 56 | + return View(); |
| 57 | + } |
| 58 | + |
| 59 | + [HttpPost, ActionName("Index")] |
| 60 | + public ActionResult IndexPOST() |
| 61 | + { |
| 62 | + if (!_orchardServices.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not allowed to upgrade."))) |
| 63 | + return new HttpUnauthorizedResult(); |
| 64 | + |
| 65 | + var activityTable = _upgradeService.GetPrefixedTableName("Orchard_Workflows_ActivityRecord"); |
| 66 | + if (_upgradeService.TableExists(activityTable)) |
| 67 | + { |
| 68 | + _upgradeService.ExecuteReader("SELECT * FROM " + activityTable + " WHERE Name = 'SendEmail'", |
| 69 | + (reader, connection) => |
| 70 | + { |
| 71 | + var record = _repository.Get((int)reader["Id"]); |
| 72 | + |
| 73 | + if (record == null) |
| 74 | + { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + var state = JsonConvert.DeserializeAnonymousType(record.State, new |
| 79 | + { |
| 80 | + Body = "", |
| 81 | + Subject = "", |
| 82 | + Recipient = "", |
| 83 | + RecipientOther = "", |
| 84 | + }); |
| 85 | + |
| 86 | + var newState = new EmailMessage |
| 87 | + { |
| 88 | + Body = state.Body, |
| 89 | + Subject = state.Subject |
| 90 | + }; |
| 91 | + |
| 92 | + if (!newState.Body.StartsWith("<p ")) |
| 93 | + { |
| 94 | + newState.Body = |
| 95 | + newState.Body |
| 96 | + + System.Environment.NewLine; |
| 97 | + } |
| 98 | + |
| 99 | + if (state.Recipient == "owner") |
| 100 | + { |
| 101 | + newState.Recipients = "{Content.Author.Email}"; |
| 102 | + } |
| 103 | + else if (state.Recipient == "author") |
| 104 | + { |
| 105 | + newState.Recipients = "{User.Current.Email}"; |
| 106 | + } |
| 107 | + else if (state.Recipient == "admin") |
| 108 | + { |
| 109 | + newState.Recipients = "{Site.SuperUser.Email}"; |
| 110 | + } |
| 111 | + else if (state.Recipient == "other") |
| 112 | + { |
| 113 | + newState.Recipients = state.RecipientOther; |
| 114 | + } |
| 115 | + |
| 116 | + record.State = JsonConvert.SerializeObject(newState); |
| 117 | + }); |
| 118 | + |
| 119 | + _orchardServices.Notifier.Success(T("Email activities updated successfully")); |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + _orchardServices.Notifier.Warning(T("No email activities were updated.")); |
| 124 | + } |
| 125 | + |
| 126 | + return RedirectToAction("Index"); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments