Created
October 17, 2012 12:23
-
-
Save davesherratt/3905261 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PromotionsController < ApplicationController | |
require 'barby/barcode/ean_8' | |
require 'barby/barcode/ean_13' | |
require 'csv' | |
require 'nokogiri' | |
require 'cgi' | |
require 'json' | |
require 'builder/xchar' | |
include Rack::Utils | |
before_filter :authenticate | |
protect_from_forgery :only => [:update, :delete, :create] | |
def index | |
@title = 'management' | |
end | |
def new | |
@title = "Create new promotion" | |
@promotion = get_current_retailer.promotions.new | |
@retailer_name = get_current_retailer_name | |
end | |
def upload | |
ajax_upload = params[:qqfile].is_a?(String) # either a file name (set) or a file (NOT SET, but qqFileUploader allows for it) | |
filename = ajax_upload ? params[:qqfile] : params[:qqfile].original_filename | |
if ajax_upload | |
csv_source = request.body.read | |
else | |
csv_source = params[:qqfile].read | |
end | |
tmp_file = "#{Rails.root}/data/tmp-#{cookies[:remember_token]}.csv" | |
csv_valid = false | |
parsed_csv = CSV.parse(csv_source) | |
if parsed_csv | |
csv_valid = true | |
end | |
File.open(tmp_file, 'wb') do |f| | |
f.write csv_source | |
end | |
count = 0 | |
@csv_error = nil | |
CSV.parse(csv_source) do |row| | |
if row.length > 1 | |
@csv_error = "Only one single value per line is accepted." | |
break | |
end | |
count += 1 | |
end | |
end | |
@messages = {} | |
if @csv_error == nil | |
@messages[:success] = true | |
@messages[:description] = true | |
else | |
@messages[:success] = false | |
@messages[:description] = false | |
end | |
respond_to do |format| | |
format.json { render :json => @messages } | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="promoform"> | |
<% @promotion_id = (@new == true ? "new" : @promotion.id) -%> | |
<h2>Promotions management for <%= get_current_retailer_name %></h2> | |
<h2><%= @title %></h2> | |
<%= template_form_for(@promotion, {:multipart => true }) do |f| %> | |
<% unless flash[:error].nil? -%> | |
<div class="widget popup red"><%= flash[:error] %></div> | |
<% end -%> | |
<% unless flash[:success].nil? -%> | |
<div class="widget popup green"><%= flash[:success] %></div> | |
<% end -%> | |
<% if @promotion.errors.any? -%> | |
<div id="error_explanation" class="errorExplanation"> | |
<h2><%= pluralize(@promotion.errors.count, "error") %> prohibited this post from being saved:</h2> | |
<ul> | |
<% @promotion.errors.full_messages.each do |msg| -%> | |
<li><%= msg %></li> | |
<% end -%> | |
</ul> | |
</div> | |
<% end -%> | |
<div class="errorExplanation"><%= flash[:notice] %></div> | |
<div class="promowidgets"> | |
<% @promotion.each do |k,v| -%> | |
<div class="promowidget" id="<%= k.downcase.strip.gsub(' ', '_').gsub(/[^\w-]/, '') %>"> | |
<h2><%= k %></h2> | |
<table class="promotion_details"> | |
<% v.each do |item| -%> | |
<%= f.text_field item[:label], item %> | |
<% end -%> | |
</table> | |
</div> | |
<% end -%> | |
<% if is_current_retail_user_admin? -%> | |
<div class="submit"> | |
<table> | |
<tr> | |
<td> | |
<% if @new -%> | |
<%= f.submit "Save promotion", :class => "widget button mid blue submitter", :id => 'submitbtn' %> | |
<% else -%> | |
<%= f.submit "Update promotion", :class => "widget button mid blue submitter", :id => 'updatebtn' %> | |
<% end -%> | |
</td> | |
<td> | |
<%= link_to 'Cancel', {:action => "index"}, {:class => "widget button mid grey", :id => 'cancelbtn'} %> | |
</td> | |
</tr> | |
</table> | |
</div> | |
<% end -%> | |
</div> | |
<% end %> | |
</div> | |
<script> | |
var promotion_id = "<%= @promotion_id %>"; | |
function fileUpload(name, id, nid, object_type, inclusion) { | |
var uploader = new qq.FileUploader({ | |
element: document.getElementById(id), | |
action: '/promotions/upload', | |
allowedExtensions: ['csv'], | |
uploadButtonText: 'upload', | |
params: { csv_name: name, csv_type: object_type, list_type: inclusion, promotion_id: '<%= @promotion_id %>' }, | |
onComplete: function(id, fileName, responseJSON){ | |
$('input#'+nid).val(responseJSON.description); | |
$('#'+func+'_'+type+'_clear_button').show(); | |
} | |
}); | |
} | |
function makefileUpload(name, id, object_type, inclusion) { | |
fileUpload(name, id, id, object_type, inclusion); | |
} | |
function files() { | |
var file_inputs = $(".upload"); | |
console.log(file_inputs); | |
for(var i = 0; i < file_inputs.length;i++) { | |
var file = file_inputs[i] | |
var name = $(file).data('name'); | |
var id = $(file).attr('id'); | |
var object_type = $(file).data('objectType'); | |
var inclusion = $(file).data('inclusion'); | |
makefileUpload(name, id, object_type, inclusion); | |
} | |
} | |
$(document).ready(function() { | |
files(); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment