2011-07-12 08:02:41 +00:00
|
|
|
class Admin::DesignsController < ApplicationController
|
2011-08-11 10:20:16 +00:00
|
|
|
require "net/http"
|
|
|
|
require "uri"
|
|
|
|
require 'zip/zip'
|
|
|
|
|
2011-07-12 08:02:41 +00:00
|
|
|
layout "admin"
|
2011-07-22 07:54:38 +00:00
|
|
|
before_filter :authenticate_user!
|
|
|
|
before_filter :is_admin?
|
2011-07-12 08:02:41 +00:00
|
|
|
|
2011-08-11 10:20:16 +00:00
|
|
|
def upload_package
|
|
|
|
if !params[:design].nil?
|
|
|
|
temp_file = Tempfile.new("temp_file")
|
|
|
|
temp_file.binmode
|
|
|
|
original_file = params[:design][:package_file]
|
2011-08-12 09:47:03 +00:00
|
|
|
#if original_file.content_type == 'application/zip'
|
2011-08-11 10:20:16 +00:00
|
|
|
temp_file.write(original_file.read)
|
2011-08-12 09:47:03 +00:00
|
|
|
temp_file.rewind
|
|
|
|
filename = File.basename(original_file.original_filename,".zip")
|
2011-08-11 10:20:16 +00:00
|
|
|
unzip_design(temp_file, filename)
|
2011-08-12 09:47:03 +00:00
|
|
|
#else
|
|
|
|
# flash[:error] = "Upload file should be in zip format"
|
|
|
|
#end
|
2011-08-11 10:20:16 +00:00
|
|
|
temp_file.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def unzip_design(file, zip_name)
|
|
|
|
Zip::ZipFile.open(file) { |zip_file|
|
2011-08-12 09:47:03 +00:00
|
|
|
design = Design.new.from_json(zip_file.read("#{zip_name}/info.json"))
|
2011-08-11 10:20:16 +00:00
|
|
|
Dir.mktmpdir('f_path') { |dir|
|
2011-08-12 09:47:03 +00:00
|
|
|
design.build_layout
|
2011-08-15 10:47:02 +00:00
|
|
|
themes_entries = []
|
|
|
|
javascripts_entries = []
|
|
|
|
images_entries = []
|
|
|
|
#dir/zip_name
|
|
|
|
#dir/zip_name
|
|
|
|
zip_file.entries.each do |entry|
|
|
|
|
case (path = entry.to_s)
|
|
|
|
when /\A(#{zip_name})\/[^\/]*(\.css)\z/ #for structure css
|
2011-08-16 09:10:16 +00:00
|
|
|
filename = File.basename(entry.to_s,".css")
|
|
|
|
temp_file4structure = File.new(dir + '/' + filename, 'w+')
|
|
|
|
temp_file4structure.binmode
|
2011-08-15 10:47:02 +00:00
|
|
|
temp_file4structure.write (zip_file.read entry )
|
|
|
|
design.structure_css = temp_file4structure
|
|
|
|
when /\A(#{zip_name})\/[^\/]*(\.html)\z/ #for layout html
|
2011-08-16 09:10:16 +00:00
|
|
|
filename = File.basename(entry.to_s,".css")
|
|
|
|
temp_file4layout = File.new(dir + '/' + filename, 'w+')
|
|
|
|
temp_file4layout.binmode
|
2011-08-15 10:47:02 +00:00
|
|
|
temp_file4layout.write (zip_file.read entry )
|
2011-08-16 09:10:16 +00:00
|
|
|
design.layout.file = temp_file4layout
|
2011-08-15 10:47:02 +00:00
|
|
|
design.layout.to_save=true
|
2011-08-16 09:10:16 +00:00
|
|
|
when /\A(#{zip_name})\/(themes)\/.*(\.css)\z/
|
|
|
|
#for themes css
|
2011-08-15 10:47:02 +00:00
|
|
|
themes_entries << entry
|
2011-08-16 09:10:16 +00:00
|
|
|
when /\A(#{zip_name})\/(javascripts)\/.*(\.js)\z/
|
|
|
|
#for js
|
2011-08-15 10:47:02 +00:00
|
|
|
javascripts_entries << entry
|
2011-08-16 09:10:16 +00:00
|
|
|
when /\A(#{zip_name})\/(images)\/.*((\.jpg)|(\.png)|(\.gif))\z/ #for img
|
2011-08-15 10:47:02 +00:00
|
|
|
images_entries << entry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-12 09:47:03 +00:00
|
|
|
['themes', 'javascripts', 'images'].each do |type|
|
2011-08-16 04:14:01 +00:00
|
|
|
eval("#{type}_entries").each do |entry|
|
2011-08-15 10:47:02 +00:00
|
|
|
#incompleted
|
2011-08-16 04:14:01 +00:00
|
|
|
filename = File.basename entry.to_s
|
|
|
|
temp_file = File.new(dir + '/' + filename, 'w+')
|
|
|
|
temp_file.binmode
|
2011-08-15 10:47:02 +00:00
|
|
|
temp_file.write zip_file.read entry
|
2011-08-16 04:14:01 +00:00
|
|
|
build_and_store = eval("design.#{type}").build
|
|
|
|
build_and_store.file = temp_file
|
|
|
|
build_and_store.file_filename = filename
|
2011-08-16 09:10:16 +00:00
|
|
|
# build_and_store._type = (File.extname entry.to_s).delete '.'
|
2011-08-16 04:14:01 +00:00
|
|
|
build_and_store.to_save = true
|
2011-08-11 10:20:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
design.save
|
|
|
|
}
|
|
|
|
end
|
2011-07-19 10:31:53 +00:00
|
|
|
|
2011-07-12 08:02:41 +00:00
|
|
|
def index
|
|
|
|
@designs = Design.all.entries
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2011-07-14 00:48:42 +00:00
|
|
|
@design = Design.new
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
params[:design][:existing_task_attributes] ||= {}
|
|
|
|
@design = Design.find(params[:id])
|
|
|
|
if @design.update_attributes(params[:design])
|
|
|
|
flash[:notice] = "Successfully updated design and tasks."
|
|
|
|
redirect_to admin_designs_path(@design)
|
|
|
|
else
|
|
|
|
render :action => 'edit'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-27 09:23:01 +00:00
|
|
|
def edit_file
|
2011-07-26 10:22:17 +00:00
|
|
|
@design = Design.find(params[:id])
|
2011-07-28 10:08:16 +00:00
|
|
|
filename = params[:filename]
|
|
|
|
files = @design.stylesheets + @design.javascripts + @design.images
|
|
|
|
file_to_removed = files.find{ |obj|
|
|
|
|
obj.file_filename == filename
|
|
|
|
}
|
2011-07-28 10:54:31 +00:00
|
|
|
type = file_to_removed._type
|
|
|
|
new_file = ""
|
2011-07-28 10:08:16 +00:00
|
|
|
Dir.mktmpdir('design_temp'){ |dir|
|
|
|
|
temp_file = File.new(dir+'/'+filename,'w+')
|
|
|
|
temp_file.write params[:context]
|
|
|
|
replace_target = eval("@design.#{type.downcase.pluralize}")
|
2011-07-28 10:54:31 +00:00
|
|
|
new_file = replace_target.build(:file => temp_file,:to_save=>true)
|
2011-07-28 10:08:16 +00:00
|
|
|
file_to_removed.destroy
|
|
|
|
@design.save
|
|
|
|
temp_file.close
|
2011-07-28 10:54:31 +00:00
|
|
|
}
|
|
|
|
render :json => new_file.file.url
|
2011-07-28 10:08:16 +00:00
|
|
|
|
2011-07-26 10:22:17 +00:00
|
|
|
end
|
|
|
|
|
2011-07-12 08:02:41 +00:00
|
|
|
def edit
|
|
|
|
@design = Design.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2011-07-22 07:54:38 +00:00
|
|
|
@design = Design.find(params[:id])
|
|
|
|
@design.destroy
|
|
|
|
redirect_to admin_designs_url
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@design = Design.new(params[:design])
|
|
|
|
if @design.save
|
|
|
|
flash[:notice] = "Successfully created design and tasks."
|
|
|
|
redirect_to admin_designs_path
|
|
|
|
else
|
|
|
|
render :action => 'new'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|