mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 19:04:51 +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]
|
||||
public ActionResult Edit(FormCollection input, int taggedContentId, string returnUrl, string newTagName) {
|
||||
try {
|
||||
if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Save"])) {
|
||||
if (!_authorizer.Authorize(Permissions.ApplyTag, T("Couldn't apply tag")))
|
||||
return new HttpUnauthorizedResult();
|
||||
List<int> tagsForContentItem = new List<int>();
|
||||
foreach (string key in input.Keys) {
|
||||
if (key.StartsWith("Checkbox.") && input[key] == "true") {
|
||||
int tagId = Convert.ToInt32(key.Substring("Checkbox.".Length));
|
||||
tagsForContentItem.Add(tagId);
|
||||
if (!_authorizer.Authorize(Permissions.CreateTag, T("Couldn't create tag")))
|
||||
return new HttpUnauthorizedResult();
|
||||
if (!String.IsNullOrEmpty(newTagName)) {
|
||||
foreach (var tagName in ParseCommaSeparatedTagNames(newTagName)) {
|
||||
if (_tagService.GetTagByName(tagName) == null) {
|
||||
_tagService.CreateTag(tagName);
|
||||
}
|
||||
_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)) {
|
||||
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) {
|
||||
try {
|
||||
Tag tag = _tagService.GetTagByName(tagName);
|
||||
|
@@ -19,7 +19,7 @@ namespace Orchard.Tags.Services {
|
||||
void UpdateTag(int id, string tagName);
|
||||
IEnumerable<IContent> GetTaggedContentItems(int id);
|
||||
void TagContentItem(int contentItemId, string tagName);
|
||||
void UpdateTagsForContentItem(int contentItemId, IEnumerable<int> tagsForContentItem);
|
||||
void UpdateTagsForContentItem(int contentItemId, IEnumerable<string> tagNamesForContentItem);
|
||||
}
|
||||
|
||||
public class TagService : ITagService {
|
||||
@@ -100,7 +100,22 @@ namespace Orchard.Tags.Services {
|
||||
_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);
|
||||
IEnumerable<TagsContentItems> currentTagsForContentItem = _tagsContentItemsRepository.Fetch(x => x.ContentItemId == contentItemId);
|
||||
foreach (var tagContentItem in currentTagsForContentItem) {
|
||||
@@ -115,7 +130,5 @@ namespace Orchard.Tags.Services {
|
||||
_tagsContentItemsRepository.Create(new TagsContentItems { ContentItemId = contentItemId, TagId = newTagForContentItem });
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@@ -2,32 +2,21 @@
|
||||
<%@ Import Namespace="Orchard.Mvc.Html"%>
|
||||
<%@ Import Namespace="Orchard.Tags.Models"%>
|
||||
<h3>Tags</h3>
|
||||
<% Html.BeginForm("Edit", "Home", new { area = "Orchard.Tags" }); %>
|
||||
<% Html.BeginForm("Update", "Home", new { area = "Orchard.Tags" }); %>
|
||||
<%= Html.ValidationSummary() %>
|
||||
<div class="yui-g">
|
||||
<h2 class="separator">Edit Tags</h2>
|
||||
<%= Html.Hidden("TaggedContentId", Model.ContentItem.Id) %>
|
||||
<%= Html.Hidden("ReturnUrl", Context.Request.Url) %>
|
||||
<h3>Choose from existing tags</h3>
|
||||
<ol>
|
||||
<% foreach (var tag in Model.AllTags) { %>
|
||||
<li>
|
||||
<label for"<%= tag.TagName %>"><%= tag.TagName %>:</label>
|
||||
<% 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>
|
||||
<%= Html.Hidden("ReturnUrl", Context.Request.Url) %>
|
||||
<%
|
||||
string tags = "";
|
||||
foreach (var tag in Model.CurrentTags) {
|
||||
tags += tag.TagName;
|
||||
tags += ",";
|
||||
} %>
|
||||
<ol>
|
||||
<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" />
|
||||
</li>
|
||||
</ol>
|
||||
|
Reference in New Issue
Block a user