public JsonResult ImportContacts(Attachment attachment) { var text = Encoding.UTF8.GetString(attachment.Content); var contacts = text.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); if (contacts.Count < 2) { return Json(false); } var indexes = Indexes.IndexString(contacts[0]); contacts.RemoveAt(0); foreach (var contactText in contacts) { var parts = contactText.Split(new [] { "," }, StringSplitOptions.None); var contact = new Contact { Email = parts[indexes.Email], FirstName = parts[indexes.FirstName], LastName = parts[indexes.LastName], MiddleName = parts[indexes.MiddleName], Organization = parts[indexes.Organization] }; if (User.Contacts.Any(x => x.Email == contact.Email)) { continue; } var groups = parts[indexes.Grouops].Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries); foreach (var groupName in groups) { var group = User.ContactsGroups.FirstOrDefault(x => x.Name.ToLower() == groupName.ToLower()); if (group == null) { group = new ContactsGroup { Name = groupName }; User.AddContactsGroup(group); } contact.AddGroup(group); } User.AddContact(contact); } return Json(true); }
public virtual void AddAttachment(Attachment attachment) { attachment.ParentTemplate = this; Attachments.Add(attachment); }