Skip to content

Commit

Permalink
Added import support to blog
Browse files Browse the repository at this point in the history
  • Loading branch information
cykod committed Jun 16, 2010
1 parent c670725 commit 883bac5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
20 changes: 18 additions & 2 deletions vendor/modules/blog/app/controllers/blog/manage_controller.rb
Expand Up @@ -4,9 +4,9 @@ class Blog::ManageController < ModuleController

permit 'blog_writer', :except => [ :configure]

permit 'blog_config', :only => [ :configure, :delete ]
permit 'blog_config', :only => [ :configure, :delete, :import ]

before_filter :check_view_permission, :except => [ :configure, :delete, :display_blog_list_table, :list, :generate_mail, :generate_mail_generate ]
before_filter :check_view_permission, :except => [ :configure, :delete, :display_blog_list_table, :list, :generate_mail, :generate_mail_generate, :import ]

component_info 'Blog'

Expand Down Expand Up @@ -241,6 +241,20 @@ def list
cms_page_path ['Content'], 'Site Blogs'
display_blog_list_table(false)
end

def import
@blog = Blog::BlogBlog.find(params[:path][0])
blog_path(@blog,"Import Blog")

if request.post? && params[:import] && @file = DomainFile.find_by_id(params[:import][:import_file_id])
if params[:commit]
@blog.import_file(@file,myself)
end
redirect_to :action => 'index', :path => [ @blog.id ]
end


end

protected

Expand All @@ -266,4 +280,6 @@ def check_view_permission
end
end



end
22 changes: 22 additions & 0 deletions vendor/modules/blog/app/models/blog/blog_blog.rb
Expand Up @@ -108,4 +108,26 @@ def send_pingbacks(post)
return unless self.trackback? && post.published?
post.run_pingbacks(post.active_revision.body_html)
end

@@import_fields = %w(title permalink author published_at preview body embedded_media).map(&:to_sym)

def import_file(domain_file,user)
filename = domain_file.filename
reader = CSV.open(filename,"r",',')
file_fields = reader.shift
reader.each do |row|
args = {}
@@import_fields.each_with_index { |fld,idx| args[fld] = row[idx] }

post = self.blog_posts.find_by_permalink(args[:permalink]) if !args[:permalink].blank?

args[:author] = user.name if args[:author].blank?
post ||= self.blog_posts.build

post.attributes = args
post.publish(args[:published_at]) if !args[:published_at].blank?
post.save
end
end

end
16 changes: 16 additions & 0 deletions vendor/modules/blog/app/views/blog/manage/import.rhtml
@@ -0,0 +1,16 @@
<div class='admin_content'>


<% admin_form_for :import do |f| %>
<%= f.header 'Select a CSV file to import' %>
<%= f.filemanager_file :import_file_id, :description => "File must be a CSV with the following columns:\n Title, permalink, author,published date, preview, body, embed\nOnly title and body are required to have non-empty values but all columns must be present" %>
<%= f.spacer %>
<%= f.cancel_submit_buttons "Cancel","Submit" %>
<% end -%>




</div>
1 change: 1 addition & 0 deletions vendor/modules/blog/app/views/blog/manage/index.rhtml
Expand Up @@ -15,6 +15,7 @@ var PostEditor = {
<%= p.link "Post a new Entry", :action => 'post', :path => @blog.id, :icon => 'add.gif' %>
<%= p.link "Blog Categories", :controller => 'categories', :path => @blog.id, :icon => 'edit.gif' %>
<%= p.link "Configure Blog", :action => 'configure', :path => @blog.id, :icon => 'configure.gif' if myself.has_role?('blog_config') %>
<%= p.link "Import Entries", :action => "import", :path => @blog.id, :icon => "upload.gif" if myself.has_role?('blog_config') %>
<%= p.link "Delete Blog", :action => 'delete', :path => @blog.id, :icon =>'delete.gif',:right => true if myself.has_role?('blog_config') %>
<% end -%>

Expand Down

0 comments on commit 883bac5

Please sign in to comment.