Esempio n. 1
0
        public ActionResult NewMessage(MessageListModel MessageContext)
        {
            List <NR.Models.NRMembership> Active = new List <NR.Models.NRMembership>();

            if (MessageContext.NewMessageTo == null || !MessageContext.NewMessageTo.Any())
            {
                ModelState.AddModelError("NewMessageTo", General.ErrorNoRecipients);
            }

            if (MessageContext.Short)
            {
                if (String.IsNullOrWhiteSpace(MessageContext.BodyShort))
                {
                    ModelState.AddModelError("BodyShort", General.ErrorNoBody);
                }
            }
            else
            {
                if (String.IsNullOrWhiteSpace(MessageContext.Head) & String.IsNullOrWhiteSpace(MessageContext.Body))
                {
                    ModelState.AddModelError("", General.ErrorNoHeadOrBody);
                }
            }

            if (ModelState.IsValid)
            {
                //An entity object cannot be referenced by multiple instances of IEntityChangeTracker

                Message Msg = new Message
                {
                    Recivers = new List <MessageReciver>(),
                    SenderID = CurrentProfile.PersonID
                };

                List <Guid> Recivers = new List <Guid>();

                Active = reposetory.GetAssociationActivePersons(CurrentProfile.AssociationID);



                foreach (Guid PersonID in MessageContext.NewMessageTo)
                {
                    if (PersonID == MistConversations.Int2Guid(0))
                    {
                        Recivers = Active.Select(m => m.Person.PersonID).ToList();
                    }
                    else if (PersonID == MistConversations.Int2Guid(1))
                    {
                        Recivers.AddRange(reposetory.GetAssociationTeamLeadersPersons(CurrentProfile.AssociationID).Select(m => m.PersonID).ToList());
                    }
                    else if (PersonID == MistConversations.Int2Guid(2))
                    {
                        Recivers.AddRange(reposetory.GetAssociationBoardPersons(CurrentProfile.AssociationID).Select(m => m.PersonID).ToList());
                    }
                    else if (PersonID == MistConversations.Int2Guid(3))
                    {
                        Recivers.AddRange(reposetory.GetAssociationActivePersons(CurrentProfile.AssociationID, Gender.Woman).Select(m => m.PersonID).ToList());
                    }
                    else if (PersonID == MistConversations.Int2Guid(4))
                    {
                        Recivers.AddRange(reposetory.GetAssociationActivePersons(CurrentProfile.AssociationID, Gender.Man).Select(m => m.PersonID).ToList());
                    }
                    else
                    {
                        Recivers.Add(PersonID);
                    }
                }

                //Recivers = Recivers.Distinct().ToList();

                Msg.Recivers = Recivers.Distinct().Select(Id => new MessageReciver {
                    ReciverID = Id
                }).ToList();
                //.Add(
                //   new MessageReciver
                //   {
                //       ReciverID = Active.Where(p => p.PersonID == PersonID).FirstOrDefault().PersonID
                //   }
                //   );
                if (MessageContext.Short)
                {
                    Msg.Body = MessageContext.BodyShort;
                    Msg.Type = MessageType.shortMessage;
                }
                else
                {
                    Msg.Head = MessageContext.Head;
                    Msg.Body = MessageContext.Body;
                    Msg.Type = MessageType.LongMessage;
                }
                reposetory.MessageSend(Msg);
                ViewBag.FormSucces = true;
            }


            List <SelectListItem> AssItems = reposetory.GetAssociationActivePersons(CurrentProfile.AssociationID).Select(d => new SelectListItem {
                Value = d.Person.PersonID.ToString(), Text = d.Person.FullName
            }).ToList();

            AssItems.Insert(0, new SelectListItem {
                Text = General.GenderMale, Value = MistConversations.Int2Guid(4).ToString()
            });
            AssItems.Insert(0, new SelectListItem {
                Text = General.GenderFemale, Value = MistConversations.Int2Guid(3).ToString()
            });
            AssItems.Insert(0, new SelectListItem {
                Text = General.Board, Value = MistConversations.Int2Guid(2).ToString()
            });
            AssItems.Insert(0, new SelectListItem {
                Text = General.Teamleaders, Value = MistConversations.Int2Guid(1).ToString()
            });
            AssItems.Insert(0, new SelectListItem {
                Text = General.All, Value = MistConversations.Int2Guid(0).ToString()
            });

            MessageContext.List = AssItems;

            return(View(MessageContext));
        }