The Gallery Feeds get their title from the feed

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-07-23 18:11:54 -07:00
parent 89495ef6cf
commit ec4a444537
5 changed files with 28 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Web.Mvc; using System.Web.Mvc;
using System.Xml.Linq;
using Orchard.Environment.Extensions; using Orchard.Environment.Extensions;
using Orchard.Localization; using Orchard.Localization;
using Orchard.Packaging.Services; using Orchard.Packaging.Services;
@@ -8,6 +9,7 @@ using Orchard.Packaging.ViewModels;
using Orchard.Themes; using Orchard.Themes;
using Orchard.UI.Admin; using Orchard.UI.Admin;
using Orchard.UI.Notify; using Orchard.UI.Notify;
using System.Xml.XPath;
namespace Orchard.Packaging.Controllers { namespace Orchard.Packaging.Controllers {
[OrchardFeature("Gallery")] [OrchardFeature("Gallery")]
@@ -58,23 +60,37 @@ namespace Orchard.Packaging.Controllers {
} }
[HttpPost] [HttpPost]
public ActionResult AddSource(string title, string url) { public ActionResult AddSource(string url) {
try { try {
if ( !String.IsNullOrEmpty(url) ) { if ( !String.IsNullOrEmpty(url) ) {
if (!url.StartsWith("http")) { if (!url.StartsWith("http")) {
ModelState.AddModelError("Url", T("The Url is a valid").Text); ModelState.AddModelError("Url", T("The Url is not valid").Text);
} }
} }
else if ( String.IsNullOrWhiteSpace(url)) { else if ( String.IsNullOrWhiteSpace(url)) {
ModelState.AddModelError("Url", T("Url is required").Text); ModelState.AddModelError("Url", T("Url is required").Text);
} }
if ( String.IsNullOrWhiteSpace(title) ) { string title = null;
ModelState.AddModelError("Url", T("Title is required").Text); // try to load the feed
try {
XNamespace atomns = "http://www.w3.org/2005/Atom" ;
var feed = XDocument.Load(url, LoadOptions.PreserveWhitespace);
var titleNode = feed.Descendants(atomns + "title").FirstOrDefault();
if ( titleNode != null )
title = titleNode.Value;
if(String.IsNullOrWhiteSpace(title)) {
ModelState.AddModelError("Url", T("The feed has no title.").Text);
}
}
catch {
ModelState.AddModelError("Url", T("The url of the feed or its content is not valid.").Text);
} }
if ( !ModelState.IsValid ) if ( !ModelState.IsValid )
return AddSourceViewResult(new PackagingAddSourceViewModel(){Title = title, Url = url}); return AddSourceViewResult(new PackagingAddSourceViewModel(){ Url = url });
_packagingSourceManager.AddSource(new PackagingSource { Id = Guid.NewGuid(), FeedUrl = url, FeedTitle = title }); _packagingSourceManager.AddSource(new PackagingSource { Id = Guid.NewGuid(), FeedUrl = url, FeedTitle = title });
_notifier.Information(T("The feed has been added successfully.")); _notifier.Information(T("The feed has been added successfully."));
@@ -83,7 +99,7 @@ namespace Orchard.Packaging.Controllers {
} }
catch ( Exception exception ) { catch ( Exception exception ) {
_notifier.Error(T("Adding feed failed: {0}", exception.Message)); _notifier.Error(T("Adding feed failed: {0}", exception.Message));
return AddSourceViewResult(new PackagingAddSourceViewModel() { Title = title, Url = url }); return AddSourceViewResult(new PackagingAddSourceViewModel() { Url = url });
} }
} }

View File

@@ -5,8 +5,5 @@ namespace Orchard.Packaging.ViewModels {
[Required] [Required]
public string Url { get; set; } public string Url { get; set; }
[Required]
public string Title { get; set; }
} }
} }

View File

@@ -5,8 +5,6 @@
<%using ( Html.BeginFormAntiForgeryPost(Url.Action("AddSource")) ) { %> <%using ( Html.BeginFormAntiForgeryPost(Url.Action("AddSource")) ) { %>
<%: Html.ValidationSummary() %> <%: Html.ValidationSummary() %>
<fieldset> <fieldset>
<label for="Title"><%: T("Feed Title") %></label>
<input id="Title" class="textMedium" name="Title" type="text" value="<%: Model.Title %>" />
<label for="Url"><%: T("Feed Url") %></label> <label for="Url"><%: T("Feed Url") %></label>
<input id="Url" class="textMedium" name="Url" type="text" value="<%: Model.Url %>"/> <input id="Url" class="textMedium" name="Url" type="text" value="<%: Model.Url %>"/>
</fieldset> </fieldset>

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.ServiceModel.Syndication; using System.ServiceModel.Syndication;
@@ -14,7 +15,8 @@ namespace PackageIndexReferenceImplementation.Services {
var formatter = new Atom10FeedFormatter<SyndicationFeed>(); var formatter = new Atom10FeedFormatter<SyndicationFeed>();
var feedPath = HostingEnvironment.MapPath("~/App_Data/Feed.xml"); var feedPath = HostingEnvironment.MapPath("~/App_Data/Feed.xml");
if (!File.Exists(feedPath)) { if (!File.Exists(feedPath)) {
return new SyndicationFeed(); string title = ConfigurationManager.AppSettings["Title"];
return new SyndicationFeed() { Title = new TextSyndicationContent(title)};
} }
using (var reader = XmlReader.Create(feedPath)) { using (var reader = XmlReader.Create(feedPath)) {
formatter.ReadFrom(reader); formatter.ReadFrom(reader);

View File

@@ -12,9 +12,11 @@
providerName="System.Data.SqlClient" /> providerName="System.Data.SqlClient" />
</connectionStrings> </connectionStrings>
<appSettings>
<add key="Title" value="Orchard Modules Gallery"/>
</appSettings>
<system.web> <system.web>
<compilation debug="true" targetFramework="4.0"> <compilation debug="true" targetFramework="4.0">
<assemblies> <assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />