2010-06-03 17:41:04 -07:00
using System ;
using System.Linq ;
using System.Text.RegularExpressions ;
using System.Xml.Linq ;
2011-02-21 14:14:36 -08:00
using Orchard.Blogs.Models ;
2010-06-03 17:41:04 -07:00
using Orchard.Commands ;
using Orchard.ContentManagement ;
using Orchard.ContentManagement.Aspects ;
using Orchard.Core.Common.Models ;
using Orchard.Core.Navigation.Models ;
2010-07-13 02:52:02 -07:00
using Orchard.Core.Routable.Models ;
2010-11-13 01:52:53 -08:00
using Orchard.Core.Routable.Services ;
2010-06-03 17:41:04 -07:00
using Orchard.Security ;
using Orchard.Blogs.Services ;
using Orchard.Core.Navigation.Services ;
2011-02-21 13:37:18 -08:00
using Orchard.Settings ;
2011-12-07 21:28:15 +00:00
using Orchard.Core.Title.Models ;
2010-06-03 17:41:04 -07:00
namespace Orchard.Blogs.Commands {
public class BlogCommands : DefaultOrchardCommandHandler {
private readonly IContentManager _contentManager ;
private readonly IMembershipService _membershipService ;
private readonly IBlogService _blogService ;
private readonly IMenuService _menuService ;
2011-02-21 13:37:18 -08:00
private readonly ISiteService _siteService ;
2010-06-03 17:41:04 -07:00
public BlogCommands (
IContentManager contentManager ,
IMembershipService membershipService ,
IBlogService blogService ,
2011-02-21 13:37:18 -08:00
IMenuService menuService ,
ISiteService siteService ) {
2010-06-03 17:41:04 -07:00
_contentManager = contentManager ;
_membershipService = membershipService ;
_blogService = blogService ;
_menuService = menuService ;
2011-02-21 13:37:18 -08:00
_siteService = siteService ;
2010-06-03 17:41:04 -07:00
}
[OrchardSwitch]
public string FeedUrl { get ; set ; }
[OrchardSwitch]
2011-12-07 23:33:43 +00:00
public int Id { get ; set ; }
2010-06-03 17:41:04 -07:00
2010-10-14 18:19:52 -07:00
[OrchardSwitch]
public string Owner { get ; set ; }
2010-06-03 17:41:04 -07:00
[OrchardSwitch]
public string Title { get ; set ; }
2011-02-21 14:14:36 -08:00
[OrchardSwitch]
public string Description { get ; set ; }
2010-06-03 17:41:04 -07:00
[OrchardSwitch]
public string MenuText { get ; set ; }
2011-02-21 13:37:18 -08:00
[OrchardSwitch]
public bool Homepage { get ; set ; }
2010-06-03 17:41:04 -07:00
[CommandName("blog create")]
2011-12-07 21:28:15 +00:00
[CommandHelp("blog create /Title:<title> [/Owner:<username>] [ / Description : < description > ] [ / MenuText : < menu text > ] [ / Homepage : true | false ] \ r \ n \ t " + " Creates a new Blog ")]
[OrchardSwitches("Title,Owner,Description,MenuText,Homepage")]
2011-06-10 13:34:29 -07:00
public void Create ( ) {
2011-02-21 13:37:18 -08:00
if ( String . IsNullOrEmpty ( Owner ) ) {
Owner = _siteService . GetSiteSettings ( ) . SuperUser ;
}
2010-10-14 18:19:52 -07:00
var owner = _membershipService . GetUser ( Owner ) ;
2011-06-10 13:34:29 -07:00
if ( owner = = null ) {
Context . Output . WriteLine ( T ( "Invalid username: {0}" , Owner ) ) ;
return ;
2010-10-14 18:19:52 -07:00
}
2010-06-03 17:41:04 -07:00
2010-06-24 16:34:10 -07:00
var blog = _contentManager . New ( "Blog" ) ;
2010-10-14 18:19:52 -07:00
blog . As < ICommonPart > ( ) . Owner = owner ;
2011-12-07 21:28:15 +00:00
blog . As < TitlePart > ( ) . Title = Title ;
2011-02-21 14:14:36 -08:00
if ( ! String . IsNullOrEmpty ( Description ) ) {
blog . As < BlogPart > ( ) . Description = Description ;
}
2010-06-03 17:41:04 -07:00
if ( ! String . IsNullOrWhiteSpace ( MenuText ) ) {
blog . As < MenuPart > ( ) . OnMainMenu = true ;
blog . As < MenuPart > ( ) . MenuPosition = _menuService . Get ( ) . Select ( menuPart = > menuPart . MenuPosition ) . Max ( ) + 1 + ".0" ;
blog . As < MenuPart > ( ) . MenuText = MenuText ;
}
_contentManager . Create ( blog ) ;
2011-06-10 13:34:29 -07:00
Context . Output . WriteLine ( T ( "Blog created successfully" ) ) ;
2010-06-03 17:41:04 -07:00
}
[CommandName("blog import")]
2011-12-07 23:33:43 +00:00
[CommandHelp("blog import /BlogId:<id> /FeedUrl:<feed url> /Owner:<username>\r\n\t" + "Import all items from <feed url> into the blog specified by <id>")]
[OrchardSwitches("FeedUrl,Id,Owner")]
2011-06-10 13:34:29 -07:00
public void Import ( ) {
2010-10-14 18:19:52 -07:00
var owner = _membershipService . GetUser ( Owner ) ;
if ( owner = = null ) {
2011-06-10 13:34:29 -07:00
Context . Output . WriteLine ( T ( "Invalid username: {0}" , Owner ) ) ;
return ;
2010-10-14 18:19:52 -07:00
}
2010-06-03 17:41:04 -07:00
XDocument doc ;
try {
2011-06-10 13:34:29 -07:00
Context . Output . WriteLine ( T ( "Loading feed..." ) ) ;
2010-06-03 17:41:04 -07:00
doc = XDocument . Load ( FeedUrl ) ;
2011-06-10 13:34:29 -07:00
Context . Output . WriteLine ( T ( "Found {0} items" , doc . Descendants ( "item" ) . Count ( ) ) ) ;
2010-06-03 17:41:04 -07:00
}
2011-06-10 13:34:29 -07:00
catch ( Exception ex ) {
throw new OrchardException ( T ( "An error occured while loading the feed at {0}." , FeedUrl ) , ex ) ;
2010-06-03 17:41:04 -07:00
}
2011-12-07 23:33:43 +00:00
var blog = _blogService . Get ( Id , VersionOptions . Latest ) ;
2010-06-03 17:41:04 -07:00
if ( blog = = null ) {
2011-12-07 23:33:43 +00:00
Context . Output . WriteLine ( T ( "Blog not found with specified Id: {0}" , Id ) ) ;
2011-06-10 13:34:29 -07:00
return ;
2010-06-03 17:41:04 -07:00
}
foreach ( var item in doc . Descendants ( "item" ) ) {
2010-12-07 16:47:30 -08:00
if ( item ! = null ) {
2010-12-07 17:42:52 -08:00
var postName = item . Element ( "title" ) . Value ;
2010-12-07 16:47:30 -08:00
2011-06-10 13:34:29 -07:00
Context . Output . WriteLine ( T ( "Adding post: {0}..." , postName . Substring ( 0 , Math . Min ( postName . Length , 40 ) ) ) ) ;
2010-12-07 16:47:30 -08:00
var post = _contentManager . New ( "BlogPost" ) ;
post . As < ICommonPart > ( ) . Owner = owner ;
post . As < ICommonPart > ( ) . Container = blog ;
2011-12-07 23:33:43 +00:00
post . As < TitlePart > ( ) . Title = postName ;
2010-12-07 16:47:30 -08:00
post . As < BodyPart > ( ) . Text = item . Element ( "description" ) . Value ;
_contentManager . Create ( post ) ;
}
2010-06-03 17:41:04 -07:00
}
2011-06-10 13:34:29 -07:00
Context . Output . WriteLine ( T ( "Import feed completed." ) ) ;
2010-06-03 17:41:04 -07:00
}
}
}