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.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.Packaging.Services;
@@ -8,6 +9,7 @@ using Orchard.Packaging.ViewModels;
using Orchard.Themes;
using Orchard.UI.Admin;
using Orchard.UI.Notify;
using System.Xml.XPath;
namespace Orchard.Packaging.Controllers {
[OrchardFeature("Gallery")]
@@ -58,23 +60,37 @@ namespace Orchard.Packaging.Controllers {
}
[HttpPost]
public ActionResult AddSource(string title, string url) {
public ActionResult AddSource(string url) {
try {
if ( !String.IsNullOrEmpty(url) ) {
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)) {
ModelState.AddModelError("Url", T("Url is required").Text);
}
if ( String.IsNullOrWhiteSpace(title) ) {
ModelState.AddModelError("Url", T("Title is required").Text);
string title = null;
// 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 )
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 });
_notifier.Information(T("The feed has been added successfully."));
@@ -83,7 +99,7 @@ namespace Orchard.Packaging.Controllers {
}
catch ( Exception exception ) {
_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]
public string Url { get; set; }
[Required]
public string Title { get; set; }
}
}

View File

@@ -5,8 +5,6 @@
<%using ( Html.BeginFormAntiForgeryPost(Url.Action("AddSource")) ) { %>
<%: Html.ValidationSummary() %>
<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>
<input id="Url" class="textMedium" name="Url" type="text" value="<%: Model.Url %>"/>
</fieldset>

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.ServiceModel.Syndication;
@@ -14,7 +15,8 @@ namespace PackageIndexReferenceImplementation.Services {
var formatter = new Atom10FeedFormatter<SyndicationFeed>();
var feedPath = HostingEnvironment.MapPath("~/App_Data/Feed.xml");
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)) {
formatter.ReadFrom(reader);

View File

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