Skip to content

Commit

Permalink
Created a WebivaExporter, now exports can follow you around the site.
Browse files Browse the repository at this point in the history
Changed Webform to use WebivaExporter.
  • Loading branch information
Doug Youch committed Oct 13, 2010
1 parent f5212ca commit dc279dc
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 66 deletions.
30 changes: 29 additions & 1 deletion app/controllers/file_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class FileController < CmsController # :nodoc: all

permit "editor_files"
permit "editor_files", :except => [:export_status, :export_file]

layout "manage"

Expand Down Expand Up @@ -423,4 +423,32 @@ def edit_file

def update_storage
end

def export_status
return render(:nothing => true) unless session[:download_worker_key]

results = Workling.return.get session[:download_worker_key]

@completed = false
@failed = false
if results
@completed = results[:processed]
@failed = false
end

render :json => {:completed => @completed, :failed => @failed}
end

def export_file
return render(:nothing => true) unless session[:download_worker_key]

results = Workling.return.get session[:download_worker_key]
session[:download_worker_key] = nil

if results
send_domain_file results[:domain_file_id], :type => results[:type]
else
render :nothing => true
end
end
end
1 change: 1 addition & 0 deletions app/views/application/_header.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<% end -%>
<%= javascript_include_tag 'active_table' %>
<%= javascript_include_tag 'shortcut' %>
<%= javascript_include_tag 'exporter' %>
<%= @header if @header %>
<%= @extra_header if @extra_header %>
Expand Down
8 changes: 8 additions & 0 deletions app/views/file/_still_downloading.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% if session[:download_worker_key] && Workling.return.get(session[:download_worker_key]) -%>
<div id="download_notice" class='flash_notice'>
<span id="download_status"><%= 'Still Exporting...' %></span>
</div>
<script>
new WebivaExporter({status: "#download_status", onComplete: function() {$j('#download_notice').hide();}});
</script>
<% end -%>
2 changes: 2 additions & 0 deletions app/views/layouts/themes/standard/_manage.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
</div>
<% end -%>
<%= render :partial => '/file/still_downloading' %>

<h1><%= render_webiva_title(@cms_page_info[:title].is_a?(Array) ? @cms_page_info[:title][-1] : @cms_page_info[:title]) %></h1>

<%= render :partial => '/layouts/wizard_top' if @wizard %>
Expand Down
61 changes: 61 additions & 0 deletions public/javascripts/exporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

WebivaExporter = function(opts) {
var $ = jQuery;
var buttonId = opts.button;
var statusId = opts.status ? opts.status : buttonId + '_status';
var generateUrl = opts.url;
var statusUrl = opts.statusUrl ? opts.statusUrl : '/website/file/export_status';
var downloadUrl = opts.downloadUrl ? opts.downloadUrl : '/website/file/export_file';
var onComplete = opts.onComplete;

function showButton() {
if(buttonId) {
$(buttonId).show();
}

$(statusId).hide();

if(onComplete) {
onComplete();
}
}

function download() {
$(statusId).html("Starting Download");
document.location = downloadUrl;
setTimeout(function(){showButton();}, 3000);
}

function failure() {
$(statusId).html("Download Failed");
setTimeout(function(){showButton();}, 3000);
}

function status() {
$.get(statusUrl, function(data) {
if(data.completed) {
download();
} else if(data.failed) {
failure();
} else {
$(statusId).html('Still Exporting...');
setTimeout(function() {status();}, 1000);
}
});
}

if(buttonId && generateUrl) {
$(buttonId).click(function() {
$(buttonId).hide();
$(statusId).show();
$(statusId).html('Exporting File');

$.get(generateUrl, function(data) {
$(statusId).html('Generating File');
status();
});
});
} else if(statusId) {
setTimeout(function() {status();}, 500);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,9 @@ class Webform::ExportController < ModuleController # :nodoc: all

component_info 'Webform'

cms_admin_paths 'content',
'Content' => { :controller => '/content' },
'Webforms' => { :action => 'index' }

def generate_file
form = WebformForm.find params[:path][0]
session[:webform_download_worker_key] = form.run_worker(:run_export_csv)
session[:download_worker_key] = form.run_worker(:run_export_csv)
render :nothing => true
end

def status
if(session[:webform_download_worker_key])
results = Workling.return.get(session[:webform_download_worker_key])
logger.error results.inspect
@completed = results ? (results[:completed] || false) : false
end
end

def download_file
if(session[:webform_download_worker_key])
results = Workling.return.get(session[:webform_download_worker_key])
send_domain_file(results[:domain_file_id], :type => "text/" + results[:type])
session[:webform_download_worker_key] = nil
else
render :nothing => true
end
end
end
14 changes: 6 additions & 8 deletions vendor/modules/webform/app/models/webform_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ def result_content_model_fields
end

def run_export_csv(args)
results = {:completed => 0}

tmp_path = "#{RAILS_ROOT}/tmp/export/"
FileUtils.mkpath(tmp_path)

Expand All @@ -149,12 +147,12 @@ def run_export_csv(args)

domain_file = DomainFile.save_temporary_file filename, :name => sprintf("%s_%d.%s",'Webform_Results'.t,Time.now.strftime("%Y_%m_%d"),'csv')

results[:domain_file_id] = domain_file.id
results[:entries] = self.webform_form_results.count
results[:type] = 'csv'
results[:completed] = 1

results
{ :filename => filename,
:domain_file_id => domain_file.id,
:entries => self.webform_form_results.count,
:type => 'text/csv',
:completed => 1
}
end

def export_csv(filename)
Expand Down
45 changes: 11 additions & 34 deletions vendor/modules/webform/app/views/webform/manage/results.html.erb
Original file line number Diff line number Diff line change
@@ -1,44 +1,21 @@
<script>
<%= active_table_javascript %>
<% action_panel do |p| -%>
<% p.custom do %>
<a href="javascript:void(0);" id="export_button"><%= theme_image_tag('icons/title_actions/configure.gif') %> <%= 'Export Results to CSV'.t %></a>
<span id="export_button_status"></span>
<% end %>
<% end -%>

Exporter = {
generate: function() {
$('export_download').innerHTML = '<%= jh "Exporting File".t %>';
Element.show('export_download');
$('export_submit_button').hide();
new Ajax.Request("<%= url_for :controller => '/webform/export', :action => 'generate_file', :path => [@webform.id] %>",
{ onComplete: function(req) {
setTimeout(Exporter.status,1000);
}
}
);
},

status: function() {
$('export_download').innerHTML = '<%= jh "Still Exporting...".t %>';
new Ajax.Request("<%= url_for :controller => '/webform/export', :action => 'status', :path => [@webform.id] %>")
},

download: function() {
$('export_download').innerHTML = '<%= jh "Starting Download".t %>';
document.location = "<%= url_for :controller => '/webform/export', :action => 'download_file', :path => [@webform.id] %>";
setTimeout(Exporter.backToContent,3000);

},

backToContent: function() {
document.location = "<%= url_for %>";
}
}
<script>
new WebivaExporter({button: "#export_button",
url: "<%= url_for :controller => '/webform/export', :action => 'generate_file', :path => [@webform.id] %>"
});
</script>

<div class="admin_content"><ul class="action_panel"> <li><a onclick="Exporter.generate();" href="javascript:void(0);" id="export_submit_button"><img alt="Add" src="/themes/standard/images/icons/actions/add.gif?1259075050" align="absmiddle">Export Results to CSV</a> <span id="export_download"></span></li>
</ul></div>

<hr/>

<div class='admin_content'>
<div id='webform_result_table'>
<%= render :partial => 'webform_result_table' %>
</div>
</div>

0 comments on commit dc279dc

Please sign in to comment.