module BcmsFckeditor # FckEditor File Browser Integration class FileBrowserController < Cms::BaseController protect_from_forgery :except => :index # All requests generated by the file browser are handled by this controller # action. Currently, there are 3 commands implemented: # a) Uploading a new image, which creates a new ImageBlock # b) Creating a new CMS Section # c) Displaying file browser contents def index @section = Cms::Section.find_by_name_path(params[:CurrentFolder]) if request.post? && params[:NewFile] handle_file_browser_upload elsif params["Command"] == "CreateFolder" create_section else render_file_browser end end private def handle_file_browser_upload begin case params[:Type].downcase when "file" create_new(Cms::FileBlock) when "image" create_new(Cms::ImageBlock) end result = "0" rescue Exception => e logger.error(e) result = "1,'#{escape_javascript(e.message)}'" end render_response("0") end def create_new(klass) uploaded_file = params[:NewFile] f = klass.new(:name => uploaded_file.original_filename, :publish_on_save=>true) a = f.attachments.build(:parent => @section, :data_file_path => uploaded_file.original_filename, :attachment_name =>'file', :data => uploaded_file) f.save! end def render_file_browser @files = case params[:Type].downcase when "file" Cms::FileBlock.by_section(@section) when "image" Cms::ImageBlock.by_section(@section) else @section.pages end render 'index', :layout => false, :format => :xml end # Sections created through FCKeditor's file browser, are assigned to groups # "guest", "cms-admin" and "content-editor" and are hidden from menus by # default. def create_section section_path = build_path(params[:NewFolderName].to_slug) begin Cms::Section.create!(:name => params[:NewFolderName], :path => section_path, :parent => @section, :allow_groups => :all, :hidden => true) result = "0" rescue Exception => e logger.error(e) result = build_error_string(e) end render_response(result) end def build_path(resource_name) "%s%s%s" % [@section.path, @section.path == "/" ? "" : "/", resource_name] end def build_error_string(e) "1, '#{escape_javascript(e.message)}'" end def render_response(result) result_string = %Q{} render :js => result_string, :layout => false end end end