//
 // GET: /Permissions/Create
 public ActionResult Create(int id)
 {
     RepositoryViews repository = new RepositoryViews();
     Permission model = new Permission();
     model.ViewId = id;
     model.View = repository.Get(id);
     ViewBag.PermissionTypes = new SelectList(Repository.GetPermissionTypes(), "PermissionTypeId", "Name");
     return View(model);
 }
        public DataCreateModel CreateModel(NameValueCollection formValues, object files, Operation operation)
        {
            CultureInfo culture = CultureInfo.CreateSpecificCulture("da-DK");

            char[] trim = new char[] { '\"' };
            DataViewModel model = new DataViewModel();
            foreach (string key in formValues.Keys)
            {
                if (formValues[key].ToLower() != "null")
                {
                    switch (key)
                    {
                        case "FormId":
                            int formid = 0;
                            if (int.TryParse(formValues[key], out formid))
                            {
                                DatabaseFormsEntities db = new DatabaseFormsEntities();
                                model.Form = db.Forms.SingleOrDefault(m => m.FormId == formid);
                            }
                            break;
                        case "ViewId":
                            int viewid = 0;
                            if (int.TryParse(formValues[key], out viewid))
                            {
                                RepositoryViews repository = new RepositoryViews();
                                model.View = repository.Get(viewid);
                            }
                            break;
                        case "UniqueId":
                            model.UniqueId = formValues[key];
                            break;
                        case "UserId":
                            model.UserId = formValues[key];
                            break;
                        case "ItemId":
                            int itemid = 0;
                            if (int.TryParse(formValues[key], out itemid))
                            {
                                model.ItemId = itemid;
                            }
                            break;
                        case "Date":
                            DateTime date = DateTime.Now;
                            if (DateTime.TryParse(formValues[key], culture, DateTimeStyles.AssumeLocal, out date))
                            {
                                model.Date = date;
                            }
                            break;

                    }
                }
            }
            if (operation == Operation.Create)
            {
                model.Date = DateTime.Now;
                model.UniqueId = Guid.NewGuid().ToString();
                /*if (User.Identity.IsAuthenticated)
                {
                    model.UserName = User.Identity.Name;
                    model.UserId = Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString();
                }*/
            }
            if (!String.IsNullOrEmpty(model.UserName))
            {
                UserProfile profile = UserProfile.GetUserProfile(model.UserName);
                RepositoryCompanies rc = new RepositoryCompanies();
                Company company = rc.Get(profile.CompanyId);
                if (company != null)
                    model.Company = company.Name;
            }

            List<DataEmailModel> emails = new List<DataEmailModel>();
            if (!String.IsNullOrEmpty(model.Form.Email))
            {
                if (operation == Operation.Create && !String.IsNullOrEmpty(model.Form.ViewEmailToReceiverOnCreate))
                {
                    emails.Add(new DataEmailModel { Email = model.Form.Email, View = model.Form.ViewEmailToReceiverOnCreate });
                }
                else if (operation == Operation.Edit && !String.IsNullOrEmpty(model.Form.ViewEmailToReceiverOnEdit))
                {
                    emails.Add(new DataEmailModel { Email = model.Form.Email, View = model.Form.ViewEmailToReceiverOnEdit });
                }
            }
            foreach (Field field in model.Form.Fields)
            {
                if (!string.IsNullOrEmpty(field.FieldColumn))
                {
                    #region 8 Upload
                    if (field.FieldTypeId == 8) //Upload
                    {
                        bool fundet = false;
                        if (files!=null && files is System.Collections.ObjectModel.Collection<MultipartFileData>)
                        {
                            foreach (MultipartFileData file in (System.Collections.ObjectModel.Collection<MultipartFileData>)files)
                            {

                                string id = file.Headers.ContentDisposition.Name.Trim(trim);

                                int id2 = 0;
                                if (int.TryParse(id, out id2))
                                {
                                    if (field.FieldId == id2)
                                    {

                                        string filename = file.Headers.ContentDisposition.FileName.Trim(trim);
                                        if (!string.IsNullOrEmpty(filename))
                                        {
                                            fundet = true;
                                            string name = model.UniqueId + "-" + field.FieldId.ToString() + Path.GetExtension(filename).ToLower();
                                            string filePath = Path.Combine(model.Form.UploadPhysicalPath, name);
                                            field.Data = name;

                                            int width = Properties.Settings.Default.MaxWidth;
                                            int height = Properties.Settings.Default.MaxHeight;
                                            System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(file.LocalFileName);
                                            FullsizeImage.Save(filePath);
                                            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                                            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

                                            if (FullsizeImage.Width <= width)
                                            {
                                                width = FullsizeImage.Width;
                                            }

                                            int NewHeight = FullsizeImage.Height * width / FullsizeImage.Width;
                                            if (NewHeight > height)
                                            {
                                                // Resize with height instead
                                                width = FullsizeImage.Width * height / FullsizeImage.Height;
                                                NewHeight = height;
                                            }

                                            System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(width, NewHeight, null, IntPtr.Zero);

                                            // Clear handle to original file so that we can overwrite it if necessary
                                            FullsizeImage.Dispose();
                                            string newName = "Thumb_" + Path.GetFileName(name);
                                            string newFile = Path.Combine(model.Form.UploadPhysicalPath, newName);
                                            // Save resized picture
                                            NewImage.Save(newFile);
                                            NewImage.Dispose();
                                            File.Delete(file.LocalFileName);
                                        }
                                    }
                                }
                                else {
                                    File.Delete(file.LocalFileName);
                                }
                            }

                        }
                        else if (files !=null && files is System.Web.HttpFileCollectionWrapper)
                        {
                            HttpFileCollectionWrapper files2 = ((System.Web.HttpFileCollectionWrapper)files);

                            if (files2.AllKeys.Contains(field.FieldId.ToString()))
                            {
                                HttpPostedFileBase file = files2[field.FieldId.ToString()];
                                string filename = file.FileName;
                                if (!string.IsNullOrEmpty(filename))
                                {
                                    fundet = true;
                                    string name = model.UniqueId + "-" + field.FieldId.ToString() + Path.GetExtension(filename).ToLower();
                                    string filePath = Path.Combine(model.Form.UploadPhysicalPath, name);
                                    field.Data = name;

                                    int width = Properties.Settings.Default.MaxWidth;
                                    int height = Properties.Settings.Default.MaxHeight;

                                    file.SaveAs(filePath);
                                    System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(filePath);
                                    FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                                    FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

                                    if (FullsizeImage.Width <= width)
                                    {
                                        width = FullsizeImage.Width;
                                    }

                                    int NewHeight = FullsizeImage.Height * width / FullsizeImage.Width;
                                    if (NewHeight > height)
                                    {
                                        // Resize with height instead
                                        width = FullsizeImage.Width * height / FullsizeImage.Height;
                                        NewHeight = height;
                                    }

                                    System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(width, NewHeight, null, IntPtr.Zero);

                                    // Clear handle to original file so that we can overwrite it if necessary
                                    FullsizeImage.Dispose();
                                    string newName = "Thumb_" + Path.GetFileName(name);
                                    string newFile = Path.Combine(model.Form.UploadPhysicalPath, newName);
                                    // Save resized picture
                                    NewImage.Save(newFile);
                                    NewImage.Dispose();
                                }
                            }
                        }
                        if (!fundet && !string.IsNullOrEmpty(formValues[field.FieldId.ToString()]))
                        {
                            string[] info = formValues[field.FieldId.ToString()].Split(new char[] { ';' });
                            int index = info[1].IndexOf(",");
                            string name = model.UniqueId + "-" + field.FieldId.ToString() + info[0].Replace("data:image/", ".");
                            field.Data = name;
                            if (info[1].Length > (index + 1))
                            {
                                string image = info[1].Substring(index + 1);
                                string filePath = Path.Combine(model.Form.UploadPhysicalPath, name);
                                using (var fs = new FileStream(filePath, FileMode.Create))
                                {
                                    byte[] byteFromString;
                                    byteFromString = Convert.FromBase64String(image);
                                    fs.Write(byteFromString, 0, byteFromString.Length);

                                }
                                try
                                {
                                    int width = Properties.Settings.Default.MaxWidth;
                                    int height = Properties.Settings.Default.MaxHeight;
                                    System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(filePath);

                                    // Prevent using images internal thumbnail
                                    FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                                    FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

                                    if (FullsizeImage.Width <= width)
                                    {
                                        width = FullsizeImage.Width;
                                    }

                                    int NewHeight = FullsizeImage.Height * width / FullsizeImage.Width;
                                    if (NewHeight > height)
                                    {
                                        // Resize with height instead
                                        width = FullsizeImage.Width * height / FullsizeImage.Height;
                                        NewHeight = height;
                                    }

                                    System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(width, NewHeight, null, IntPtr.Zero);

                                    // Clear handle to original file so that we can overwrite it if necessary
                                    FullsizeImage.Dispose();
                                    string newName = "Thumb_" + Path.GetFileName(name);
                                    string newFile = Path.Combine(model.Form.UploadPhysicalPath, newName);
                                    // Save resized picture
                                    NewImage.Save(newFile);
                                    NewImage.Dispose();
                                }
                                catch
                                { }
                            }
                        }
                    }
                    #endregion
                    else if (field.FieldTypeId == 16) //Upload
                    {
                        field.Data = formValues["Geometri"];
                    }
                    else if (field.FieldTypeId == 17) //Upload
                    {
                        field.Data = formValues["Position"];
                    }
                    else if (field.FieldTypeId == 18) //Upload
                    {
                        field.Data = formValues["Accuracy"];
                    }
                    else
                    {
                        string value = formValues[field.FieldId.ToString()];
                        field.Data = value;
                        switch (field.FieldTypeId)
                        {
                            case 7:

                                if (email.IsMatch(field.Data))
                                {
                                    string emailView = "";
                                    Field operationField = null;
                                    if (operation == Operation.Create)
                                    {
                                        emailView = field.Form.ViewEmailToSenderOnCreate;
                                        operationField = field.Form.Fields.FirstOrDefault(m => m.FieldTypeId == 12);
                                    }
                                    else if (operation == Operation.Edit)
                                    {
                                        emailView = field.Form.ViewEmailToSenderOnEdit;
                                        operationField = field.Form.Fields.FirstOrDefault(m => m.FieldTypeId == 13);
                                    }
                                    if (!String.IsNullOrEmpty(emailView) && operationField != null)
                                    {
                                        string send = formValues[operationField.FieldId.ToString()];
                                        if (!String.IsNullOrEmpty(send))
                                        {
                                            if (send != "false" && send != "off")
                                                emails.Add(new DataEmailModel { Email = field.Data, View = emailView });
                                        }

                                    }
                                }
                                break;
                            /*case 10:

                                DateTime dt;
                                if (!DateTime.TryParse(field.Data, culture, DateTimeStyles.AssumeLocal, out dt))
                                {
                                    ModelState.AddModelError(field.FieldId.ToString(), field.Data + " er ikke en gyldig dato");
                                }
                                break;*/
                        }
                        Selection selection = field.Selections.SingleOrDefault(m => m.Name == field.Data);
                        if (selection != null)
                        {

                            if (!String.IsNullOrEmpty(selection.Email))
                            {
                                if (operation == Operation.Create && !String.IsNullOrEmpty(model.Form.ViewEmailToReceiverOnCreate))
                                {
                                    emails.Add(new DataEmailModel { Email = selection.Email, View = model.Form.ViewEmailToReceiverOnCreate });
                                }
                                else if (operation == Operation.Edit && !String.IsNullOrEmpty(model.Form.ViewEmailToReceiverOnEdit))
                                {
                                    emails.Add(new DataEmailModel { Email = selection.Email, View = model.Form.ViewEmailToReceiverOnEdit });
                                }

                            }
                        }

                    }
                }
            }
            return new DataCreateModel()
            {
                Emails = emails,
                Model = model,
            };
        }
 public ActionResult About(int id)
 {
     RepositoryViews repository = new RepositoryViews();
     return View("System_About",repository.Get(id));
 }
        private DataCreateModel CreateModel(DataViewModel postModel, FormCollection formValues, Operation operation)
        {
            CultureInfo culture = CultureInfo.CreateSpecificCulture("da-DK");
            RepositoryViews repository = new RepositoryViews();
            Models.View view = repository.Get(postModel.View.ViewId);

            DataViewModel model = new DataViewModel
            {
                View = view,
                Form = view.Group.Forms.Single(m => m.FormId == postModel.Form.FormId),
                Date = postModel.Date,
                UniqueId = postModel.UniqueId,
                ItemId = postModel.ItemId,
                UserName = postModel.UserName,
                UserId = postModel.UserId,
            };
            if (operation == Operation.Create)
            {
                model.Date = DateTime.Now;
                model.UniqueId = Guid.NewGuid().ToString();
                if (User.Identity.IsAuthenticated)
                {
                    model.UserName = User.Identity.Name;
                    model.UserId = Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString();
                }
            }
            if (!String.IsNullOrEmpty(model.UserName))
            {
                UserProfile profile = UserProfile.GetUserProfile(model.UserName);
                RepositoryCompanies rc = new RepositoryCompanies();
                Company company = rc.Get(profile.CompanyId);
                if (company != null)
                    model.Company = company.Name;
            }

            List<DataEmailModel> emails = new List<DataEmailModel>();
            if (!String.IsNullOrEmpty(model.Form.Email))
            {
                if (operation == Operation.Create && !String.IsNullOrEmpty(model.Form.ViewEmailToReceiverOnCreate))
                {
                    emails.Add(new DataEmailModel { Email = model.Form.Email, View = model.Form.ViewEmailToReceiverOnCreate });
                }
                else if (operation == Operation.Edit && !String.IsNullOrEmpty(model.Form.ViewEmailToReceiverOnEdit))
                {
                    emails.Add(new DataEmailModel { Email = model.Form.Email, View = model.Form.ViewEmailToReceiverOnEdit });
                }
            }
            foreach (Field field in model.Form.Fields)
            {
                #region 8 Upload
                if (field.FieldTypeId == 8) //Upload
                {
                    string value = formValues[field.FieldId.ToString()];
                    if (!String.IsNullOrEmpty(value))
                    {
                        field.Data = value;
                    }
                    if (Request.Files.AllKeys.Contains(field.FieldId.ToString()))
                    {
                        HttpPostedFileBase file = Request.Files[field.FieldId.ToString()];
                        if (file.ContentLength > 0)
                        {
                            string name = model.UniqueId + "-" + field.FieldId.ToString() + Path.GetExtension(file.FileName);
                            string filePath = Path.Combine(model.Form.UploadPhysicalPath, name);
                            file.SaveAs(filePath);
                            field.Data = name;
                            try
                            {
                                int width = Properties.Settings.Default.MaxWidth;
                                int height = Properties.Settings.Default.MaxHeight;
                                System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(filePath);

                                // Prevent using images internal thumbnail
                                FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                                FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

                                if (FullsizeImage.Width <= width)
                                {
                                    width = FullsizeImage.Width;
                                }

                                int NewHeight = FullsizeImage.Height * width / FullsizeImage.Width;
                                if (NewHeight > height)
                                {
                                    // Resize with height instead
                                    width = FullsizeImage.Width * height / FullsizeImage.Height;
                                    NewHeight = height;
                                }

                                System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(width, NewHeight, null, IntPtr.Zero);

                                // Clear handle to original file so that we can overwrite it if necessary
                                FullsizeImage.Dispose();
                                string newName = "Thumb_" + Path.GetFileName(name);
                                string newFile = Path.Combine(model.Form.UploadPhysicalPath, newName);
                                // Save resized picture
                                NewImage.Save(newFile);
                                NewImage.Dispose();
                            }
                            catch
                            { }
                        }
                        else if (field.Required)
                        {
                            ModelState.AddModelError(field.FieldId.ToString(), field.Name + " er påkrævet");
                        }
                    }
                }
                #endregion
                else if (field.FieldTypeId == 16) //Geometry
                {
                    string value = formValues["Geometri"];
                    if (String.IsNullOrEmpty(value))
                    {
                        if (field.Required)
                        {
                            string error = field.Name + " er påkrævet";
                            ModelState.AddModelError("Geometri", error);
                        }

                    }
                    else
                    {
                        field.Data = value;
                    }
                }
                else if (field.FieldTypeId == 17) //Position
                {
                    string value = formValues["Position"];
                    if (String.IsNullOrEmpty(value))
                    {
                        if (field.Required)
                        {
                            string error = field.Name + " er påkrævet";
                            ModelState.AddModelError("Position", error);
                        }

                    }
                    else
                    {
                        field.Data = value;
                    }
                }
                else if (field.FieldTypeId == 18) //Accuracy
                {
                    string value = formValues["Accuracy"];
                    if (String.IsNullOrEmpty(value))
                    {
                        if (field.Required)
                        {
                            string error = field.Name + " er påkrævet";
                            ModelState.AddModelError("Accuracy", error);
                        }

                    }
                    else
                    {
                        field.Data = value;
                    }
                }
                else
                {
                    string value = formValues[field.FieldId.ToString()];
                    if (String.IsNullOrEmpty(value))
                    {
                        if (field.Required)
                        {
                            string error = field.Name + " er påkrævet";
                            ModelState.AddModelError(field.FieldId.ToString(), error);
                        }

                    }
                    else
                    {
                        field.Data = value;
                        switch (field.FieldTypeId)
                        {
                            case 7:

                                if (email.IsMatch(field.Data))
                                {
                                    string emailView = "";
                                    Field operationField = null;
                                    if (operation == Operation.Create)
                                    {
                                        emailView = field.Form.ViewEmailToSenderOnCreate;
                                        operationField = field.Form.Fields.FirstOrDefault(m => m.FieldTypeId == 12);
                                    }
                                    else if (operation == Operation.Edit)
                                    {
                                        emailView = field.Form.ViewEmailToSenderOnEdit;
                                        operationField = field.Form.Fields.FirstOrDefault(m => m.FieldTypeId == 13);
                                    }
                                    if (!String.IsNullOrEmpty(emailView) && operationField != null)
                                    {
                                        string send = formValues[operationField.FieldId.ToString()];
                                        if (!String.IsNullOrEmpty(send))
                                        {
                                            if (send != "false")
                                                emails.Add(new DataEmailModel { Email = field.Data, View = emailView });
                                        }

                                    }
                                }
                                else
                                {
                                    ModelState.AddModelError(field.FieldId.ToString(), field.Data + " er ikke en gyldig email");
                                }
                                break;
                            case 10:

                                DateTime dt;
                                if (!DateTime.TryParse(field.Data, culture, DateTimeStyles.AssumeLocal, out dt))
                                {
                                    ModelState.AddModelError(field.FieldId.ToString(), field.Data + " er ikke en gyldig dato");
                                }
                                break;
                        }
                        Selection selection = field.Selections.SingleOrDefault(m => m.Name == field.Data);
                        if (selection != null)
                        {

                            if (!String.IsNullOrEmpty(selection.Email))
                            {
                                if (operation == Operation.Create && !String.IsNullOrEmpty(model.Form.ViewEmailToReceiverOnCreate))
                                {
                                    emails.Add(new DataEmailModel { Email = selection.Email, View = model.Form.ViewEmailToReceiverOnCreate });
                                }
                                else if (operation == Operation.Edit && !String.IsNullOrEmpty(model.Form.ViewEmailToReceiverOnEdit))
                                {
                                    emails.Add(new DataEmailModel { Email = selection.Email, View = model.Form.ViewEmailToReceiverOnEdit });
                                }

                            }
                        }
                    }
                }
            }
            return new DataCreateModel()
            {
                Emails = emails,
                Model = model,
            };
        }
        public ActionResult Items(int id, int formId)
        {
            RepositoryViews repository = new RepositoryViews();
            Models.View view = repository.Get(id);
            view.Group.DefaultFormId = formId;
            RepositoryMapguide mapguide = new RepositoryMapguide();
            DataListModel model = null;

            if (view.ViewTypeId == 5 || !User.Identity.IsAuthenticated)
            {
                model = mapguide.GetList(view, "");
            }
            else
            {
                model = mapguide.GetList(view, Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString());
            }
            return PartialView("System_Items", model);
        }
 public ActionResult Items(int id, int formId, List<DataSelectionModel> dataSelections)
 {
     RepositoryViews repository = new RepositoryViews();
     Models.View view = repository.Get(id);
     view.Group.DefaultFormId = formId;
     RepositoryOpenLayers openLayers = new RepositoryOpenLayers();
     DataListModel model = openLayers.GetList(view, dataSelections);
     return PartialView("System_Items", model);
 }
 public ActionResult Index(int id, int? itemId, int? formId, string column, bool? ascending)
 {
     RepositoryViews repositoryViews = new RepositoryViews();
     Models.View view = repositoryViews.Get(id);
     if (formId.HasValue)
         view.Group.DefaultFormId = formId.Value;
     if (itemId.HasValue)
     {
         //2 = Edit
         //3 = Delete
         //4 = Details
         if (view.ViewTypeId == 2 || view.ViewTypeId == 3 || view.ViewTypeId == 4)
         {
             RepositoryMapguide mapguide = new RepositoryMapguide();
             DataViewModel model = new DataViewModel()
             {
                 ItemId = itemId.Value,
                 View = view,
                 Form = view.Group.Forms.Single(m => m.FormId == view.Group.DefaultFormId)
             };
             model = mapguide.Get(model);
             return View(view.Name, model);
         }
     }
     else
     {
         //1 = Create
         if (view.ViewTypeId == 1)
         {
             DataViewModel model = new DataViewModel
             {
                 View = view,
                 Form = view.Group.Forms.SingleOrDefault(m => m.FormId == view.Group.DefaultFormId),
                 Date = DateTime.Now,
                 UniqueId = Guid.NewGuid().ToString()
             };
             if (User.Identity.IsAuthenticated)
             {
                 model.UserName = User.Identity.Name;
                 model.UserId = Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString();
                 UserProfile profile = UserProfile.GetUserProfile(User.Identity.Name);
                 RepositoryCompanies rc = new RepositoryCompanies();
                 Company company = rc.Get(profile.CompanyId);
                 if (company != null)
                     model.Company = company.Name;
             }
             return View(view.Name, model);
         }
         //5 = List All
         else if (view.ViewTypeId == 5)
         {
             RepositoryMapguide mapguide = new RepositoryMapguide();
             DataListModel model = mapguide.GetList(view, "");
             return View(view.Name, model);
         }
         //6 = List My
         else if (view.ViewTypeId == 6)
         {
             RepositoryMapguide mapguide = new RepositoryMapguide();
             DataListModel model = null;
             if (User.Identity.IsAuthenticated)
             {
                 model = mapguide.GetList(view, Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString());
             }
             else
             {
                 model = mapguide.GetList(view, "");
             }
             return View(view.Name, model);
         }
     }
     return View("Error");
 }
 public ActionResult Fields(int id, int formId)
 {
     RepositoryViews repository = new RepositoryViews();
     Models.View view = repository.Get(id);
     view.Group.DefaultFormId = formId;
     DataViewModel model = new DataViewModel
     {
         View = view,
         Form = view.Group.Forms.Single(m => m.FormId == formId)
     };
     return PartialView("System_Fields", model);
 }
        public ActionResult Delete(DataViewModel postModel)
        {
            RepositoryMapguide mapguide = new RepositoryMapguide();
            RepositoryViews repository = new RepositoryViews();
            Models.View view = repository.Get(postModel.View.ViewId);
            DataViewModel model = new DataViewModel
            {
                View = view,
                Form = view.Group.Forms.Single(m => m.FormId == postModel.Form.FormId),
                ItemId = postModel.ItemId,
                UniqueId = postModel.UniqueId
            };
            model = mapguide.Get(model);
            mapguide.Delete(model);
            List<DataEmailModel> emails = new List<DataEmailModel>();
            if (!String.IsNullOrEmpty(model.Form.Email)&&!String.IsNullOrEmpty(model.Form.ViewEmailToReceiverOnDelete))
            {
                emails.Add(new DataEmailModel { Email = model.Form.Email, View = model.Form.ViewEmailToReceiverOnDelete });
            }
            foreach (Field field in model.Form.Fields)
            {
                if (field.FieldTypeId == 7)
                {
                    if (!string.IsNullOrEmpty(field.Data))
                    {
                        if (email.IsMatch(field.Data))
                        {
                            string emailView = field.Form.ViewEmailToSenderOnDelete;
                            Field operationField = field.Form.Fields.FirstOrDefault(m => m.FieldTypeId == 13);
                            if (!String.IsNullOrEmpty(emailView) && operationField != null)
                            {
                                if (!String.IsNullOrEmpty(operationField.Data))
                                {
                                    if (operationField.Data == "1" || operationField.Data == "True")
                                    {
                                        emails.Add(new DataEmailModel { Email = field.Data, View = emailView });
                                    }
                                }

                            }
                        }
                    }
                }
                Selection selection = field.Selections.SingleOrDefault(m => m.Name == field.Data);
                if (selection != null && !String.IsNullOrEmpty(model.Form.ViewEmailToReceiverOnDelete))
                {

                    if (!String.IsNullOrEmpty(selection.Email))
                    {
                            emails.Add(new DataEmailModel { Email = selection.Email, View = model.Form.ViewEmailToReceiverOnDelete });
                    }
                }
            }
            DataCreateModel dcm = new DataCreateModel()
            {
                Model = model,
                Emails = emails
            };
            mapguide.SendEmails(dcm);
            Models.View endView = model.View.Group.Views.SingleOrDefault(m => m.ViewTypeId == 7);
            if (endView == null)
            {
                Models.View listUser = model.View.Group.Views.SingleOrDefault(m => m.ViewTypeId == 6);
                if (listUser == null)
                {
                    Models.View listAll = model.View.Group.Views.SingleOrDefault(m => m.ViewTypeId == 5);
                    if (listAll == null)
                    {
                        return View(model.View.Name, model);
                    }
                    else
                    {
                        return RedirectToAction("Index", new { id = listAll.ViewId, formId = model.Form.FormId });
                    }
                }
                else
                {
                    return RedirectToAction("Index", new { id = listUser.ViewId, formId = model.Form.FormId });
                }

            }
            else
            {
                return View(endView.Name, model);
            }
        }
 //
 // GET: /Permissions/
 public ActionResult Index(int id)
 {
     RepositoryViews repository = new RepositoryViews();
     ViewBag.View = repository.Get(id);
     return View(Repository.GetPermissions(id));
 }