Ejemplo n.º 1
0
        public ActionResult Preview(ContentEditViewModel editModel)
        {
            Content content = null;
            if (editModel.ContentId != null)
            {
                content = _entities.Contents
                    .Include(c => c.Urls)
                    .FirstOrDefault(c => c.Id == editModel.ContentId);
            }

            if (content == null)
            {
                content = new Content();
            }

            editModel.UpdateContent(content);
            content.CompileLess();
            content.Parse();

            var model = new ContentPreviewViewModel
            {
                Title = content.Title,
                ContentId = editModel.ContentId ?? 0,
                Contents = content.ParsedContent,
                Stylesheet = content.StylesheetCss,
                Teaser = content.Teaser
            };


            return View("~/Areas/mitarbeit/Views/Content/Preview.cshtml", model);
        }
Ejemplo n.º 2
0
        public ActionResult Edit(ContentEditViewModel model)
        {
            Content content = null;
            if (ModelState.IsValid)
            {
                ContentVersion latestVersion = null;

                if (model.ContentId == null)
                {
                    // Create a new Content Object if there is none
                    var c = new Content
                    {
                        Version = 1,
                        EditPermissionId = null,
                        Published = DateTime.MinValue,
                        PublishedByUserId = null,
                        Created = DateTime.Now,
                        AuthorUserId = User.Identity.GetUserId<int>(),
                        UnparsedContent = "",
                        StylesheetCss = ""
                    };

                    content = _entities.Contents.Add(c);

                    var v = new ContentVersion
                    {
                        ContentId = content.Id,
                        UserId = content.AuthorUserId,
                        UnparsedContent = "",
                        StylesheetCode = "",
                        Title = "",
                        Description = "",
                        Teaser = "",
                        Notes = ""
                    };

                    latestVersion = _entities.ContentVersions.Add(v);
                    _entities.SaveChanges();
                    model.ContentId = content.Id;

                    var url = new ContentUrl
                    {
                        Url = String.Format("inhalt/{0}", content.Id),
                        ContentId = content.Id,
                        Type = UrlType.System,
                        Enabled = true
                    };

                    _entities.ContentUrls.Add(url);
                    content.DefaultUrl = url;
                }
                else
                {
                    // Try to find the existing content object
                    var result = _entities.Contents
                        .Include("EditPermission")
                        .Include("Boilerplates")
                        .Include("BoilerplatesUsed")
                        .Where(c => c.Id == model.ContentId);

                    if (result.Any())
                    {
                        content = result.First();

                        if (content.EditPermission != null && !User.IsInRole(content.EditPermission.Name))
                        {
                            return RedirectToRoute("ContentIndex");
                        }
                    }
                    else
                    {
                        return RedirectToRoute("ContentIndex");
                    }
                }

                if (User.IsInRole("artikel-administrator"))
                {
                    if (model.Status == ContentStatus.Published && content.Status != ContentStatus.Published)
                    {
                        // Publish the content
                        content.Published = DateTime.Now;
                        content.PublishedByUserId = User.Identity.GetUserId<int>();
                    }
                    else if (model.Status != ContentStatus.Published && content.Status == ContentStatus.Published)
                    {
                        // Unpublish the content
                        content.Published = DateTime.MinValue;
                        content.PublishedByUserId = null;
                    }

                    var permission = _entities.Roles.FirstOrDefault(r => r.Id == model.PermissionId);
                    if (permission != null && permission.Metapermission.Name == "mitarbeiter")
                    {
                        content.EditPermissionId = model.PermissionId;
                    }

                    if (string.IsNullOrEmpty(model.Url) && model.Status == ContentStatus.Published)
                    {
                        if (model.Type == ContentType.News)
                        {
                            // Default url is /news/-/1234
                            model.Url = "-";
                        }
                    }

                    if (!string.IsNullOrEmpty(model.Url))
                    {
                        model.Url = model.Url.Trim('/');
                        if (model.Type == ContentType.News && !Regex.IsMatch(model.Url, @"news/[a-zA-Z0-9_\-/]+/\d+"))
                        {
                            model.Url = String.Format("news/{0}/{1}", model.Url, content.Id);
                        }

                        if (content.DefaultUrl == null || content.DefaultUrl.Url != model.Url)
                        {
                            var duplicates = _entities.ContentUrls
                                .Where(u => u.Url == model.Url);

                            if (!duplicates.Any() || duplicates.First().Enabled == false)
                            {
                                _entities.ContentUrls.RemoveRange(duplicates);

                                var url = new ContentUrl
                                {
                                    ContentId = content.Id,
                                    Enabled = true,
                                    Type = UrlType.Default,
                                    Url = model.Url
                                };

                                if (content.DefaultUrl != null && content.DefaultUrl.Type == UrlType.Default)
                                {
                                    content.DefaultUrl.Type = UrlType.Alternative;
                                }

                                content.DefaultUrl = _entities.ContentUrls.Add(url);
                            }
                            else
                            {
                                ModelState.AddModelError("Url", "Die gewählte URL existiert bereits.");
                            }
                        }
                    }
                }
                else
                {
                    // Normal editors cannot change these options
                    model.HomePageOptions = content.HomePageOptions;
                    model.PermissionId = content.EditPermissionId;
                    model.Url = content.DefaultUrl.Url;

                    // Normal editors cannot un-publish content
                    if (model.Status != ContentStatus.Published && content.Status == ContentStatus.Published)
                    {
                        model.Status = content.Status;
                    }
                }

                // Update Database Fields
                model.UpdateContent(content);
                content.Updated = DateTime.Now;

                int userId;
                if (content.Version > 0)
                {
                    latestVersion = latestVersion ?? _entities.ContentVersions.Where(v => v.ContentId == content.Id).OrderByDescending(v => v.Updated).First();
                    userId = latestVersion.UserId;
                }
                else
                {
                    userId = content.AuthorUserId;
                }

                try
                {
                    content.CompileLess();
                }
                catch (ArgumentException)
                {
                    ViewBag.Error =
                        "Dein artikelspezifischer CSS-Code enthält Fehler und wurde daher nicht in den Artikel übernommen (aber in der Datenbank gespeichert).";
                }

                content.Parse();

                // Calculate Changes
                int updateCharsDeleted = 0;
                int updateCharsInserted = 0;
                double updateMagnificance = 0;

                if (latestVersion != null)
                {
                    var dmp = new diff_match_patch();
                    var diffs = dmp.diff_main(latestVersion.UnparsedContent ?? "", model.UnparsedContent ?? "");

                    foreach (var diffItem in diffs.Where(diffItem => diffItem.text.Trim().Length != 0))
                    {
                        switch (diffItem.operation)
                        {
                            case Operation.DELETE:
                                {
                                    updateCharsDeleted += diffItem.text.Length;
                                    break;
                                }
                            case Operation.INSERT:
                                {
                                    updateCharsInserted += diffItem.text.Length;
                                    break;
                                }
                        }
                    }

                    updateMagnificance =
                        Math.Min(updateCharsInserted * 0.9 / Math.Max(1, latestVersion.UnparsedContent.Length) +
                                 updateCharsDeleted * 0.1 / Math.Max(1, latestVersion.UnparsedContent.Length), 1);
                    updateMagnificance = Math.Min(1, Math.Tan(updateMagnificance));
                }

                // If the user was not the one who updated last we have to create a new version
                if (userId != User.Identity.GetUserId<int>())
                {
                    content.Version++;
                    latestVersion = new ContentVersion
                    {
                        ContentId = content.Id,
                        Title = content.Title,
                        Version = content.Version,
                        UnparsedContent = content.UnparsedContent,
                        ParsedContent = content.ParsedContent,
                        Description = content.Description,
                        StylesheetCss = content.StylesheetCss,
                        StylesheetCode = content.StylesheetCode,
                        Teaser = content.Teaser,
                        UserId = User.Identity.GetUserId<int>(),
                        Note = content.Notes,
                        Updated = DateTime.Now,
                        UpdateMagnificance = updateMagnificance,
                        UpdateCharsChanged = updateCharsInserted,
                        UpdateCharsDeleted = updateCharsDeleted
                    };

                    _entities.ContentVersions.Add(latestVersion);
                }
                else
                {
                    if (latestVersion != null)
                    {
                        model.UpdateContentVersion(latestVersion);
                        latestVersion.Version = content.Version;
                        latestVersion.ParsedContent = content.ParsedContent;
                        latestVersion.StylesheetCss = content.StylesheetCss;
                        latestVersion.Updated = DateTime.Now;
                        latestVersion.UpdateMagnificance = updateMagnificance;
                        latestVersion.UpdateCharsChanged = updateCharsInserted;
                        latestVersion.UpdateCharsDeleted = updateCharsDeleted;
                    }
                }

                // Save all Changes
                _entities.SaveChanges();
            }

            model.Saved = ModelState.IsValid;
            model.Categories =
                model.Categories ?? _entities.ContentCategories.OrderBy(c => c.OrderingPosition);
            model.IsContentAdministrator = User.IsInRole("artikel-administrator");

            // Remove status options the user cannot use
            var statusOptions = Enum.GetValues(typeof(ContentStatus))
                .OfType<ContentStatus>();

            if (content != null)
            {
                if (!model.IsContentAdministrator)
                {
                    statusOptions = content.Status == ContentStatus.Published
                        ? statusOptions.Where(e => e == ContentStatus.Published)
                        : statusOptions.Where(e => e != ContentStatus.Published);
                }
            }
            else
            {
                statusOptions = statusOptions.Where(e => e != ContentStatus.Published);
            }

            model.ContentStatusList = statusOptions.Select(e => new SelectListItem
            {
                Text = e.GetDisplayName(),
                Value = ((int)e).ToString()
            });
            model.ContentPermissionList = _entities.Roles
                .Where(r => r.Metapermission.Name == "mitarbeiter")
                .Select(r => new SelectListItem
                {
                    Text = r.FriendlyName,
                    Value = r.Id.ToString()
                });

            return View("~/Areas/mitarbeit/Views/Content/Edit.cshtml", model);
        }