public ActionResult Send(List <int> people, NotificationTracking notificationTracking, EmailQueue emailQueue, int?mailingListId, int[] attachmentIds)
        {
            if ((people == null || people.Count <= 0) && !mailingListId.HasValue)
            {
                ModelState.AddModelError("People", "No person has been selected to receive the notification.");
            }

            var tracking = new List <NotificationTracking>();

            ModelState.Clear();
            notificationTracking.TransferValidationMessagesTo(ModelState);
            if (string.IsNullOrWhiteSpace(emailQueue.Subject))
            {
                ModelState.AddModelError("EmailQueue.Subject", "You must enter a subject.");
            }

            var mailingList = mailingListId.HasValue ? Repository.OfType <MailingList>().GetNullableById(mailingListId.Value) : null;

            // save the objects if we are good
            if (ModelState.IsValid)
            {
                tracking = ProcessTracking(ModelState, people, notificationTracking, attachmentIds, emailQueue, mailingList);

                foreach (var a in tracking)
                {
                    _notificationTrackingRepository.EnsurePersistent(a);
                }

                Message = string.Format(Messages.Saved, "Notification tracking");

                if (tracking.Count == 1 && !mailingListId.HasValue)
                {
                    var person = tracking[0].Person;

                    var url = Url.Action("AdminEdit", "Person", new { id = person.User.Id, seminarId = SiteService.GetLatestSeminar(Site).Id });
                    return(Redirect(string.Format("{0}#notifications", url)));
                }

                // redirect back to the seminar controller details
                return(this.RedirectToAction <SeminarController>(a => a.Details(null)));
            }

            // not good, hand the page back
            var ntViewModel = NotificationTrackingViewModel.Create(Repository, Site, notificationTracking, mailingList: mailingList);

            ntViewModel.People = tracking.Select(a => a.Person).ToList();

            var viewModel = SendNotificationViewModel.Create(Repository, ntViewModel, emailQueue);

            return(View(viewModel));
        }
        public static NotificationTrackingViewModel Create(IRepository repository, string siteId, NotificationTracking notificationTracking = null, Person person = null, MailingList mailingList = null)
        {
            Check.Require(repository != null, "Repository is required.");

            var seminar = SiteService.GetLatestSeminar(siteId);

            var viewModel = new NotificationTrackingViewModel(){
                                    NotificationTracking = notificationTracking ?? new NotificationTracking(),
                                    NotificationMethods = repository.OfType<NotificationMethod>().GetAll(),
                                    NotificationTypes = repository.OfType<NotificationType>().GetAll(),
                                    People = new List<Person>(),
                                    AllPeople = SiteService.GetLatestSeminar(siteId).SeminarPeople.Select(a => a.Person).ToList(),//seminarService.GetCurrent().SeminarPeople.Select(a=>a.Person).ToList(),
                                    SitePeople = SiteService.LoadSite(siteId).People,
                                    Seminar = seminar,
                                    MailingLists = seminar.MailingLists,
                                    MailingList = mailingList
                                };

            if (person != null) viewModel.People.Add(person);

            return viewModel;
        }
        public ActionResult Create(List <int> people, NotificationTracking notificationTracking)
        {
            if (people == null || people.Count <= 0)
            {
                ModelState.AddModelError("People", "No person has been selected to receive the notification.");
            }

            var tracking = new List <NotificationTracking>();

            if (ModelState.IsValid)
            {
                tracking = ProcessTracking(ModelState, people, notificationTracking, new int[0]);

                foreach (var a in tracking)
                {
                    _notificationTrackingRepository.EnsurePersistent(a);
                }

                Message = string.Format(Messages.Saved, "Notification tracking");

                if (tracking.Count == 1)
                {
                    var person = tracking[0].Person;

                    var url = Url.Action("AdminEdit", "Person", new { id = person.User.Id, seminarId = SiteService.GetLatestSeminar(Site).Id });
                    return(Redirect(string.Format("{0}#notifications", url)));
                }

                // redirect back to the seminar controller details
                return(this.RedirectToAction <SeminarController>(a => a.Details(null)));
            }

            var viewModel = NotificationTrackingViewModel.Create(Repository, Site, notificationTracking);

            viewModel.People = tracking.Select(a => a.Person).ToList();

            return(View(viewModel));
        }
Пример #4
0
        public static NotificationTrackingViewModel Create(IRepository repository, string siteId, NotificationTracking notificationTracking = null, Person person = null, MailingList mailingList = null)
        {
            Check.Require(repository != null, "Repository is required.");

            var seminar = SiteService.GetLatestSeminar(siteId);

            var viewModel = new NotificationTrackingViewModel()
            {
                NotificationTracking = notificationTracking ?? new NotificationTracking(),
                NotificationMethods  = repository.OfType <NotificationMethod>().GetAll(),
                NotificationTypes    = repository.OfType <NotificationType>().GetAll(),
                People       = new List <Person>(),
                AllPeople    = SiteService.GetLatestSeminar(siteId).SeminarPeople.Select(a => a.Person).ToList(),                 //seminarService.GetCurrent().SeminarPeople.Select(a=>a.Person).ToList(),
                SitePeople   = SiteService.LoadSite(siteId).People,
                Seminar      = seminar,
                MailingLists = seminar.MailingLists,
                MailingList  = mailingList
            };

            if (person != null)
            {
                viewModel.People.Add(person);
            }

            return(viewModel);
        }
        /// <summary>
        /// Create all the notification tracking objects and email queue if provided
        /// </summary>
        /// <param name="modelState"></param>
        /// <param name="people"></param>
        /// <param name="notificationTracking"></param>
        /// <param name="emailQueue">(Optional)</param>
        /// <returns></returns>
        protected List <NotificationTracking> ProcessTracking(ModelStateDictionary modelState, List <int> people, NotificationTracking notificationTracking, int[] attachmentIds, EmailQueue emailQueue = null, MailingList mailingList = null)
        {
            Check.Require(people != null || mailingList != null, "people is required.");
            Check.Require(notificationTracking != null, "notificationTracking is required.");

            mailingList = mailingList ?? new MailingList();
            people      = people ?? new List <int>();

            // build the list of people to send to
            var peeps = _personRepository.Queryable.Where(a => people.Contains(a.Id)).ToList();

            peeps.AddRange(mailingList.People);

            var tracking    = new List <NotificationTracking>();
            var passwords   = new List <KeyValuePair <Person, string> >();
            var invitations = new List <Invitation>();

            if (mailingList.Name == MailingLists.Invitation)
            {
                passwords = _personService.ResetPasswords(peeps);
            }

            foreach (var person in peeps.Distinct())
            {
                try
                {
                    var nt = new NotificationTracking();
                    // copy the fields
                    Mapper.Map(notificationTracking, nt);
                    nt.Seminar = notificationTracking.Seminar;
                    // assign the person
                    nt.Person = person;

                    if (emailQueue != null)
                    {
                        var eq = new EmailQueue();

                        Mapper.Map(emailQueue, eq);

                        Invitation invitation = null;
                        string     password   = null;
                        if (mailingList.Name == MailingLists.Invitation)
                        {
                            // get the invitation object
                            invitation = Repository.OfType <Invitation>().Queryable.FirstOrDefault(a => a.Person == person && a.Seminar == notificationTracking.Seminar);

                            invitations.Add(invitation);

                            // get the person object
                            password = passwords.Where(a => a.Key == person).Select(a => a.Value).FirstOrDefault();
                        }

                        eq.Body = _notificationService.GenerateNotification(eq.Body, person, Site, notificationTracking.Seminar.Id, invitation, password);

                        if (attachmentIds != null)
                        {
                            // add attachments
                            var attachments = _attachmentRepository.Queryable.Where(a => attachmentIds.Contains(a.Id)).ToList();
                            foreach (var a in attachments)
                            {
                                eq.Attachments.Add(a);
                            }
                        }

                        eq.Person     = person;
                        nt.EmailQueue = eq;
                    }

                    // add it to the list
                    tracking.Add(nt);
                }
                catch
                {
                }
            }

            // add errors for those not in the list
            foreach (var id in people.Where(x => !peeps.Select(a => a.Id).Contains(x)))
            {
                ModelState.AddModelError("Person", string.Format("Person with id {0} could not be found.", id));
            }

            if (mailingList.Name == MailingLists.Invitation)
            {
                GeneratePasswordReport(passwords, invitations);
            }

            return(tracking);
        }