From 883bac5fafd9d6b2ec15cb415576980f753212fe Mon Sep 17 00:00:00 2001 From: Pascal Rettig Date: Wed, 16 Jun 2010 09:39:38 -0400 Subject: [PATCH] Added import support to blog --- .../app/controllers/blog/manage_controller.rb | 20 +++++++++++++++-- .../modules/blog/app/models/blog/blog_blog.rb | 22 +++++++++++++++++++ .../blog/app/views/blog/manage/import.rhtml | 16 ++++++++++++++ .../blog/app/views/blog/manage/index.rhtml | 1 + 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 vendor/modules/blog/app/views/blog/manage/import.rhtml diff --git a/vendor/modules/blog/app/controllers/blog/manage_controller.rb b/vendor/modules/blog/app/controllers/blog/manage_controller.rb index d8c5eef3..72bee239 100644 --- a/vendor/modules/blog/app/controllers/blog/manage_controller.rb +++ b/vendor/modules/blog/app/controllers/blog/manage_controller.rb @@ -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' @@ -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 @@ -266,4 +280,6 @@ def check_view_permission end end + + end diff --git a/vendor/modules/blog/app/models/blog/blog_blog.rb b/vendor/modules/blog/app/models/blog/blog_blog.rb index ddb678ed..57f9db89 100644 --- a/vendor/modules/blog/app/models/blog/blog_blog.rb +++ b/vendor/modules/blog/app/models/blog/blog_blog.rb @@ -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 diff --git a/vendor/modules/blog/app/views/blog/manage/import.rhtml b/vendor/modules/blog/app/views/blog/manage/import.rhtml new file mode 100644 index 00000000..1eae2557 --- /dev/null +++ b/vendor/modules/blog/app/views/blog/manage/import.rhtml @@ -0,0 +1,16 @@ +
+ + + <% 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 -%> + + + + +
diff --git a/vendor/modules/blog/app/views/blog/manage/index.rhtml b/vendor/modules/blog/app/views/blog/manage/index.rhtml index f38e50da..36ef92aa 100644 --- a/vendor/modules/blog/app/views/blog/manage/index.rhtml +++ b/vendor/modules/blog/app/views/blog/manage/index.rhtml @@ -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 -%>