mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 03:25:23 +08:00
- Blogs: refining Tags in blog post view/edit pages to use comma separated lists instead of checkboxes, and other minor changes.
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4042986
This commit is contained in:
@@ -48,26 +48,15 @@ namespace Orchard.Tags.Controllers {
|
|||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult Edit(FormCollection input, int taggedContentId, string returnUrl, string newTagName) {
|
public ActionResult Edit(FormCollection input, int taggedContentId, string returnUrl, string newTagName) {
|
||||||
try {
|
try {
|
||||||
if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Save"])) {
|
if (!_authorizer.Authorize(Permissions.CreateTag, T("Couldn't create tag")))
|
||||||
if (!_authorizer.Authorize(Permissions.ApplyTag, T("Couldn't apply tag")))
|
return new HttpUnauthorizedResult();
|
||||||
return new HttpUnauthorizedResult();
|
if (!String.IsNullOrEmpty(newTagName)) {
|
||||||
List<int> tagsForContentItem = new List<int>();
|
foreach (var tagName in ParseCommaSeparatedTagNames(newTagName)) {
|
||||||
foreach (string key in input.Keys) {
|
if (_tagService.GetTagByName(tagName) == null) {
|
||||||
if (key.StartsWith("Checkbox.") && input[key] == "true") {
|
_tagService.CreateTag(tagName);
|
||||||
int tagId = Convert.ToInt32(key.Substring("Checkbox.".Length));
|
|
||||||
tagsForContentItem.Add(tagId);
|
|
||||||
}
|
}
|
||||||
|
_tagService.TagContentItem(taggedContentId, tagName);
|
||||||
}
|
}
|
||||||
_tagService.UpdateTagsForContentItem(taggedContentId, tagsForContentItem);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (!_authorizer.Authorize(Permissions.CreateTag, T("Couldn't create tag")))
|
|
||||||
return new HttpUnauthorizedResult();
|
|
||||||
if (!String.IsNullOrEmpty(newTagName)) {
|
|
||||||
_tagService.CreateTag(newTagName);
|
|
||||||
_tagService.TagContentItem(taggedContentId, newTagName);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
if (!String.IsNullOrEmpty(returnUrl)) {
|
if (!String.IsNullOrEmpty(returnUrl)) {
|
||||||
return Redirect(returnUrl);
|
return Redirect(returnUrl);
|
||||||
@@ -83,6 +72,38 @@ namespace Orchard.Tags.Controllers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public ActionResult Update(string tags, int taggedContentId, string returnUrl) {
|
||||||
|
try {
|
||||||
|
if (!_authorizer.Authorize(Permissions.CreateTag, T("Couldn't create tag")))
|
||||||
|
return new HttpUnauthorizedResult();
|
||||||
|
List<string> tagNames = ParseCommaSeparatedTagNames(tags);
|
||||||
|
_tagService.UpdateTagsForContentItem(taggedContentId, tagNames);
|
||||||
|
if (!String.IsNullOrEmpty(returnUrl)) {
|
||||||
|
return Redirect(returnUrl);
|
||||||
|
}
|
||||||
|
return RedirectToAction("Index");
|
||||||
|
}
|
||||||
|
catch (Exception exception) {
|
||||||
|
_notifier.Error(T("Updating tags failed: " + exception.Message));
|
||||||
|
if (!String.IsNullOrEmpty(returnUrl)) {
|
||||||
|
return Redirect(returnUrl);
|
||||||
|
}
|
||||||
|
return RedirectToAction("Index");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<string> ParseCommaSeparatedTagNames(string tags) {
|
||||||
|
IEnumerable<string> tagNames = tags.Split(',');
|
||||||
|
List<string> sanitizedTagNames = new List<string>();
|
||||||
|
foreach (var tagName in tagNames) {
|
||||||
|
if (!String.IsNullOrEmpty(tagName)) {
|
||||||
|
sanitizedTagNames.Add(tagName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sanitizedTagNames;
|
||||||
|
}
|
||||||
|
|
||||||
public ActionResult Search(string tagName) {
|
public ActionResult Search(string tagName) {
|
||||||
try {
|
try {
|
||||||
Tag tag = _tagService.GetTagByName(tagName);
|
Tag tag = _tagService.GetTagByName(tagName);
|
||||||
|
@@ -19,7 +19,7 @@ namespace Orchard.Tags.Services {
|
|||||||
void UpdateTag(int id, string tagName);
|
void UpdateTag(int id, string tagName);
|
||||||
IEnumerable<IContent> GetTaggedContentItems(int id);
|
IEnumerable<IContent> GetTaggedContentItems(int id);
|
||||||
void TagContentItem(int contentItemId, string tagName);
|
void TagContentItem(int contentItemId, string tagName);
|
||||||
void UpdateTagsForContentItem(int contentItemId, IEnumerable<int> tagsForContentItem);
|
void UpdateTagsForContentItem(int contentItemId, IEnumerable<string> tagNamesForContentItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class TagService : ITagService {
|
public class TagService : ITagService {
|
||||||
@@ -100,7 +100,22 @@ namespace Orchard.Tags.Services {
|
|||||||
_tagsContentItemsRepository.Create(tagsContentItems);
|
_tagsContentItemsRepository.Create(tagsContentItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateTagsForContentItem(int contentItemId, IEnumerable<int> tagsForContentItem) {
|
public void UpdateTagsForContentItem(int contentItemId, IEnumerable<string> tagNamesForContentItem) {
|
||||||
|
List<int> tags = new List<int>();
|
||||||
|
foreach (var tagName in tagNamesForContentItem) {
|
||||||
|
Tag tag = GetTagByName(tagName);
|
||||||
|
if (tag == null) {
|
||||||
|
CreateTag(tagName);
|
||||||
|
tag = GetTagByName(tagName);
|
||||||
|
}
|
||||||
|
tags.Add(tag.Id);
|
||||||
|
}
|
||||||
|
ModifyTagsForContentItem(contentItemId, tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
private void ModifyTagsForContentItem(int contentItemId, IEnumerable<int> tagsForContentItem) {
|
||||||
List<int> newTagsForContentItem = new List<int>(tagsForContentItem);
|
List<int> newTagsForContentItem = new List<int>(tagsForContentItem);
|
||||||
IEnumerable<TagsContentItems> currentTagsForContentItem = _tagsContentItemsRepository.Fetch(x => x.ContentItemId == contentItemId);
|
IEnumerable<TagsContentItems> currentTagsForContentItem = _tagsContentItemsRepository.Fetch(x => x.ContentItemId == contentItemId);
|
||||||
foreach (var tagContentItem in currentTagsForContentItem) {
|
foreach (var tagContentItem in currentTagsForContentItem) {
|
||||||
@@ -115,7 +130,5 @@ namespace Orchard.Tags.Services {
|
|||||||
_tagsContentItemsRepository.Create(new TagsContentItems { ContentItemId = contentItemId, TagId = newTagForContentItem });
|
_tagsContentItemsRepository.Create(new TagsContentItems { ContentItemId = contentItemId, TagId = newTagForContentItem });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,32 +2,21 @@
|
|||||||
<%@ Import Namespace="Orchard.Mvc.Html"%>
|
<%@ Import Namespace="Orchard.Mvc.Html"%>
|
||||||
<%@ Import Namespace="Orchard.Tags.Models"%>
|
<%@ Import Namespace="Orchard.Tags.Models"%>
|
||||||
<h3>Tags</h3>
|
<h3>Tags</h3>
|
||||||
<% Html.BeginForm("Edit", "Home", new { area = "Orchard.Tags" }); %>
|
<% Html.BeginForm("Update", "Home", new { area = "Orchard.Tags" }); %>
|
||||||
<%= Html.ValidationSummary() %>
|
<%= Html.ValidationSummary() %>
|
||||||
<div class="yui-g">
|
<div class="yui-g">
|
||||||
<h2 class="separator">Edit Tags</h2>
|
<h2 class="separator">Edit Tags</h2>
|
||||||
<%= Html.Hidden("TaggedContentId", Model.ContentItem.Id) %>
|
<%= Html.Hidden("TaggedContentId", Model.ContentItem.Id) %>
|
||||||
<%= Html.Hidden("ReturnUrl", Context.Request.Url) %>
|
<%= Html.Hidden("ReturnUrl", Context.Request.Url) %>
|
||||||
<h3>Choose from existing tags</h3>
|
<%
|
||||||
<ol>
|
string tags = "";
|
||||||
<% foreach (var tag in Model.AllTags) { %>
|
foreach (var tag in Model.CurrentTags) {
|
||||||
<li>
|
tags += tag.TagName;
|
||||||
<label for"<%= tag.TagName %>"><%= tag.TagName %>:</label>
|
tags += ",";
|
||||||
<% if (Model.CurrentTags.Contains(tag)) {%>
|
} %>
|
||||||
<input type="checkbox" value="true" checked="checked" name="Checkbox.<%=tag.Id%>"/>
|
|
||||||
<% } else {%>
|
|
||||||
<input type="checkbox" value="true" name="Checkbox.<%=tag.Id%>"/>
|
|
||||||
<% } %>
|
|
||||||
</li>
|
|
||||||
<% } %>
|
|
||||||
<li>
|
|
||||||
<input type="submit" class="button" name="submit.Save" value="Save" />
|
|
||||||
</li>
|
|
||||||
</ol>
|
|
||||||
<h3>Or add a new tag</h3>
|
|
||||||
<ol>
|
<ol>
|
||||||
<li>
|
<li>
|
||||||
<input id="NewTagName" class="inputText inputTextLarge" name="NewTagName" type="text" value="" />
|
<input id="tags" class="inputText inputTextLarge" name="tags" type="text" value="<%=tags %>" />
|
||||||
<input type="submit" class="button" name="submit.Add" value="Add" />
|
<input type="submit" class="button" name="submit.Add" value="Add" />
|
||||||
</li>
|
</li>
|
||||||
</ol>
|
</ol>
|
||||||
|
Reference in New Issue
Block a user