- Tags: Admin

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4042089
This commit is contained in:
suhacan
2009-11-24 20:34:25 +00:00
parent 15efc294e6
commit f989285dd2
9 changed files with 244 additions and 4 deletions

View File

@@ -0,0 +1,14 @@
using Orchard.UI.Navigation;
namespace Orchard.Tags {
public class AdminMenu : INavigationProvider {
public string MenuName { get { return "admin"; } }
public void GetNavigation(NavigationBuilder builder) {
builder.Add("Tags", "3",
menu => menu
.Add("Manage Tags", "1.0", item => item.Action("Index", "Admin", new { area = "Orchard.Tags" }))
);
}
}
}

View File

@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Models;
using Orchard.Settings;
using Orchard.Tags.Models;
using Orchard.Tags.ViewModels;
using Orchard.UI.Notify;
using Orchard.Security;
using Orchard.Tags.Services;
namespace Orchard.Tags.Controllers {
[ValidateInput(false)]
public class AdminController : Controller {
private readonly ITagService _tagService;
private readonly IAuthorizer _authorizer;
private readonly INotifier _notifier;
public AdminController(ITagService tagService, INotifier notifier, IAuthorizer authorizer) {
_tagService = tagService;
_authorizer = authorizer;
_notifier = notifier;
Logger = NullLogger.Instance;
T = NullLocalizer.Instance;
}
public IUser CurrentUser { get; set; }
public ISite CurrentSite { get; set; }
public ILogger Logger { get; set; }
public Localizer T { get; set; }
public ActionResult Index() {
try {
IEnumerable<Tag> tags = _tagService.GetTags();
var entries = tags.Select(tag => CreateTagEntry(tag)).ToList();
var model = new TagsAdminIndexViewModel { Tags = entries };
return View(model);
}
catch (Exception exception) {
_notifier.Error(T("Listing tags failed: " + exception.Message));
return Index();
}
}
[HttpPost]
public ActionResult Index(FormCollection input) {
var viewModel = new TagsAdminIndexViewModel { Tags = new List<TagEntry>(), BulkAction = new TagAdminIndexBulkAction() };
UpdateModel(viewModel, input.ToValueProvider());
try {
IEnumerable<TagEntry> checkedEntries = viewModel.Tags.Where(t => t.IsChecked);
switch (viewModel.BulkAction) {
case TagAdminIndexBulkAction.None:
break;
case TagAdminIndexBulkAction.Delete:
if (!_authorizer.Authorize(Permissions.DeleteTag, T("Couldn't delete tag")))
return new HttpUnauthorizedResult();
foreach (TagEntry entry in checkedEntries) {
_tagService.DeleteTag(entry.Tag.Id);
}
break;
default:
throw new ArgumentOutOfRangeException();
}
}
catch (Exception exception) {
_notifier.Error(T("Editing tags failed: " + exception.Message));
return Index();
}
return RedirectToAction("Index");
}
public ActionResult Create() {
return View(new TagsAdminCreateViewModel());
}
[HttpPost]
public ActionResult Create(FormCollection input) {
var viewModel = new TagsAdminCreateViewModel();
try {
UpdateModel(viewModel, input.ToValueProvider());
if (!_authorizer.Authorize(Permissions.CreateTag, T("Couldn't create tag")))
return new HttpUnauthorizedResult();
_tagService.CreateTag(viewModel.TagName);
return RedirectToAction("Index");
}
catch (Exception exception) {
_notifier.Error(T("Creating Tag failed: " + exception.Message));
return View(viewModel);
}
}
private static TagEntry CreateTagEntry(Tag tag) {
return new TagEntry {
Tag = tag,
IsChecked = false,
};
}
}
}

View File

@@ -61,6 +61,8 @@
<Reference Include="System.Web.Mobile" />
</ItemGroup>
<ItemGroup>
<Compile Include="AdminMenu.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Controllers\TagsController.cs" />
<Compile Include="Models\Tag.cs" />
<Compile Include="Models\TagSettings.cs" />
@@ -68,11 +70,15 @@
<Compile Include="Permissions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\TagService.cs" />
<Compile Include="ViewModels\TagsAdminCreateViewModel.cs" />
<Compile Include="ViewModels\TagsAdminIndexViewModel.cs" />
<Compile Include="ViewModels\TagsCreateViewModel.cs" />
<Compile Include="ViewModels\TagsIndexViewModel.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Package.txt" />
<Content Include="Views\Admin\Create.aspx" />
<Content Include="Views\Admin\Index.aspx" />
<Content Include="Views\Models\DisplayTemplates\HasTags.ascx" />
<Content Include="Views\Models\EditorTemplates\TagSettingsRecord.ascx" />
<Content Include="Views\Tags\Create.aspx" />

View File

@@ -5,6 +5,7 @@ namespace Orchard.Tags {
public class Permissions : IPermissionProvider {
public static readonly Permission CreateTag = new Permission { Description = "Creating a Tag", Name = "CreateTag" };
public static readonly Permission ApplyTag = new Permission { Description = "Applying a Tag", Name = "ApplyTag" };
public static readonly Permission DeleteTag = new Permission { Description = "Deleting a Tag", Name = "DeleteTag" };
public string PackageName {
get {
@@ -16,6 +17,7 @@ namespace Orchard.Tags {
return new List<Permission> {
CreateTag,
ApplyTag,
DeleteTag,
};
}
}

View File

@@ -3,7 +3,6 @@ using System.Linq;
using Orchard.Data;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Security;
using Orchard.Settings;
using Orchard.Tags.Models;
using Orchard.UI.Notify;
@@ -11,8 +10,10 @@ using Orchard.UI.Notify;
namespace Orchard.Tags.Services {
public interface ITagService : IDependency {
IEnumerable<Tag> GetTags();
Tag GetTag(int id);
Tag GetTagByName(string tagName);
void CreateTag(string tagName);
void DeleteTag(int id);
void TagContentItem(int contentItemId, string tagName);
void UpdateTagsForContentItem(int contentItemId, IEnumerable<int> tagsForContentItem);
}
@@ -20,15 +21,13 @@ namespace Orchard.Tags.Services {
public class TagService : ITagService {
private readonly IRepository<Tag> _tagRepository;
private readonly IRepository<TagsContentItems> _tagsContentItemsRepository;
private readonly IAuthorizer _authorizer;
private readonly INotifier _notifier;
public TagService(IRepository<Tag> tagRepository,
IRepository<TagsContentItems> tagsContentItemsRepository,
IAuthorizer authorizer, INotifier notifier) {
INotifier notifier) {
_tagRepository = tagRepository;
_tagsContentItemsRepository = tagsContentItemsRepository;
_authorizer = authorizer;
_notifier = notifier;
Logger = NullLogger.Instance;
T = NullLocalizer.Instance;
@@ -44,6 +43,10 @@ namespace Orchard.Tags.Services {
return from tags in _tagRepository.Table.ToList() select tags;
}
public Tag GetTag(int id) {
return _tagRepository.Get(x => x.Id == id);
}
public Tag GetTagByName(string tagName) {
return _tagRepository.Get(x => x.TagName == tagName);
}
@@ -58,6 +61,14 @@ namespace Orchard.Tags.Services {
}
}
public void DeleteTag(int id) {
_tagRepository.Delete(GetTag(id));
IEnumerable<TagsContentItems> tagsContentItems = _tagsContentItemsRepository.Fetch(x => x.TagId == id);
foreach (var tagContentItem in tagsContentItems) {
_tagsContentItemsRepository.Delete(tagContentItem);
}
}
public void TagContentItem(int contentItemId, string tagName) {
Tag tag = GetTagByName(tagName);
TagsContentItems tagsContentItems = new TagsContentItems { ContentItemId = contentItemId, TagId = tag.Id };

View File

@@ -0,0 +1,7 @@
using Orchard.Mvc.ViewModels;
namespace Orchard.Tags.ViewModels {
public class TagsAdminCreateViewModel : AdminViewModel {
public string TagName { get; set; }
}
}

View File

@@ -0,0 +1,20 @@
using System.Collections.Generic;
using Orchard.Mvc.ViewModels;
using Orchard.Tags.Models;
namespace Orchard.Tags.ViewModels {
public class TagsAdminIndexViewModel : AdminViewModel {
public IList<TagEntry> Tags { get; set; }
public TagAdminIndexBulkAction BulkAction { get; set; }
}
public class TagEntry {
public Tag Tag { get; set; }
public bool IsChecked { get; set; }
}
public enum TagAdminIndexBulkAction {
None,
Delete,
}
}

View File

@@ -0,0 +1,14 @@
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<TagsAdminCreateViewModel>" %>
<%@ Import Namespace="Orchard.Tags.ViewModels"%>
<%@ Import Namespace="Orchard.Mvc.Html" %>
<% Html.Include("Header"); %>
<% Html.BeginForm(); %>
<%= Html.ValidationSummary() %>
<div class="yui-g">
<h2 class="separator">Add a Tag</h2>
<label for="TagName">Name:</label>
<input id="TagName" class="inputText inputTextLarge" name="TagName" type="text" value="<%= Model.TagName %>" />
<input type="submit" class="button" value="Save" />
</div>
<% Html.EndForm(); %>
<% Html.Include("Footer"); %>

View File

@@ -0,0 +1,57 @@
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<TagsAdminIndexViewModel>" %>
<%@ Import Namespace="Orchard.Tags.ViewModels"%>
<%@ Import Namespace="Orchard.Mvc.Html"%>
<% Html.Include("Header"); %>
<% Html.BeginForm(); %>
<div class="yui-g">
<h2 class="separator">Manage Comments</h2>
<%=Html.ValidationSummary() %>
<ol class="horizontal actions floatLeft">
<li>
<label class="floatLeft" for="publishActions"> Actions:</label>
<select id="publishActions" name="<%=Html.NameOf(m => m.BulkAction)%>">
<%=Html.SelectOption(Model.BulkAction, TagAdminIndexBulkAction.None, "Choose action...")%>
<%=Html.SelectOption(Model.BulkAction, TagAdminIndexBulkAction.Delete, "Delete")%>
</select>
</li>
<li>
<input class="button roundCorners" type="submit" name="submit" value="Apply" />
</li>
</ol>
<span class="filterActions">
<%=Html.ActionLink("Add a new tag", "Create") %>
</span>
<table id="Table1" cellspacing="0" class="roundCorners clearLayout" summary="This is a table of the tags in your application">
<colgroup>
<col id="Col1" />
<col id="Col2" />
<col id="Col3" />
</colgroup>
<thead>
<tr>
<th scope="col"><%--<input type="checkbox" value="" />--%></th>
<th scope="col">Name</th>
<th scope="col"></th>
</tr>
</thead>
<%
int tagIndex = 0;
foreach (var tagEntry in Model.Tags) {
%>
<tr>
<td>
<input type="hidden" value="<%=Model.Tags[tagIndex].Tag.Id%>" name="<%=Html.NameOf(m => m.Tags[tagIndex].Tag.Id)%>"/>
<input type="checkbox" value="true" name="<%=Html.NameOf(m => m.Tags[tagIndex].IsChecked)%>"/>
</td>
<td>
<%= tagEntry.Tag.TagName %>
</td>
<td>
<%=Html.ActionLink("Edit", "Edit", new {id = tagEntry.Tag.Id}, new {@class="floatRight topSpacer"}) %>
</td>
</tr>
<% tagIndex++; } %>
</table>
</div>
<% Html.EndForm(); %>
<% Html.Include("Footer"); %>