2012-05-05 13:20:20 +00:00
|
|
|
class Admin::DesignsController < OrbitBackendController
|
2011-08-11 10:20:16 +00:00
|
|
|
require "net/http"
|
|
|
|
require "uri"
|
|
|
|
require 'zip/zip'
|
|
|
|
|
2011-07-22 07:54:38 +00:00
|
|
|
before_filter :authenticate_user!
|
2012-04-29 19:14:15 +00:00
|
|
|
before_filter :is_admin?
|
|
|
|
before_filter :for_admin_only
|
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")
|
|
|
|
original_file = params[:design][:package_file]
|
2011-08-12 09:47:03 +00:00
|
|
|
#if original_file.content_type == 'application/zip'
|
2011-08-25 00:13:38 +00:00
|
|
|
temp_file.write(original_file.read.force_encoding('UTF-8'))
|
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
|
2011-07-19 10:31:53 +00:00
|
|
|
|
2011-07-12 08:02:41 +00:00
|
|
|
def index
|
2012-05-05 13:20:20 +00:00
|
|
|
@designs = params[:sort] ? get_sorted_and_filtered("designs") : Design.all
|
2011-07-12 08:02:41 +00:00
|
|
|
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
|
|
|
|
@design = Design.find(params[:id])
|
2012-10-24 10:40:38 +00:00
|
|
|
@design.update_attributes(params[:design])
|
|
|
|
# if @design.update_attributes(params[:design])
|
|
|
|
# flash[:notice] = "Successfully updated design and tasks."
|
|
|
|
# redirect_to admin_designs_url(@design)
|
|
|
|
# else
|
|
|
|
# render :action => 'edit'
|
|
|
|
# end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js {nil}
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-24 10:40:38 +00:00
|
|
|
def update_file
|
|
|
|
@design = Design.find(params[:id])
|
|
|
|
case params[:type]
|
|
|
|
when 'layout', 'css_default', 'css_reset'
|
|
|
|
@object = @design.send(params[:type])
|
2012-11-20 02:04:50 +00:00
|
|
|
when 'javascripts', 'themes'#, 'images'
|
2012-10-24 10:40:38 +00:00
|
|
|
@object = @design.send(params[:type]).find(params[:object_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
Dir.mktmpdir('f_path') { |dir|
|
|
|
|
temp_file = File.new(dir + '/' + File.basename(@object.file.url), 'w+')
|
|
|
|
temp_file.write(params[:file][:content].force_encoding('UTF-8'))
|
|
|
|
@object.remove_file!
|
|
|
|
@object.file = temp_file
|
|
|
|
@object.save
|
|
|
|
}
|
|
|
|
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]
|
2011-09-09 07:40:13 +00:00
|
|
|
files = @design.themes + @design.javascripts + @design.images
|
2011-07-28 10:08:16 +00:00
|
|
|
file_to_removed = files.find{ |obj|
|
2012-01-24 03:36:24 +00:00
|
|
|
obj.file_identifier == filename
|
2011-07-28 10:08:16 +00:00
|
|
|
}
|
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
|
2011-09-09 07:40:13 +00:00
|
|
|
new_file.save
|
2011-07-28 10:08:16 +00:00
|
|
|
temp_file.close
|
2011-09-09 07:40:13 +00:00
|
|
|
}
|
|
|
|
respond_to do |format|
|
|
|
|
format.js {}
|
|
|
|
end
|
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])
|
2012-12-27 02:20:37 +00:00
|
|
|
if @site.design == @design
|
|
|
|
flash[:error] = "Failed! You can not delete a design that used by default "
|
|
|
|
else
|
|
|
|
@design.destroy
|
|
|
|
flash[:notice] = "Successfully deleted design."
|
|
|
|
end
|
2011-07-22 07:54:38 +00:00
|
|
|
redirect_to admin_designs_url
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@design = Design.new(params[:design])
|
|
|
|
if @design.save
|
2011-08-20 14:54:07 +00:00
|
|
|
redirect_to admin_designs_url
|
2011-07-12 08:02:41 +00:00
|
|
|
else
|
|
|
|
render :action => 'new'
|
|
|
|
end
|
|
|
|
end
|
2012-05-05 13:20:20 +00:00
|
|
|
|
|
|
|
def delete
|
2012-08-13 16:59:01 +00:00
|
|
|
if params[:ids]
|
2013-02-05 10:26:45 +00:00
|
|
|
designs = Design.any_in(:_id => params[:ids]).destroy_all
|
2012-05-05 13:20:20 +00:00
|
|
|
end
|
|
|
|
redirect_to admin_designs_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
|
|
|
|
end
|
2012-10-24 10:40:38 +00:00
|
|
|
|
|
|
|
def upload_image
|
|
|
|
@design = Design.find(params[:id])
|
|
|
|
@image = @design.images.build(params[:image])
|
|
|
|
@image.save
|
|
|
|
render :layout => false
|
|
|
|
end
|
2011-09-01 11:06:49 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def unzip_design(file, zip_name)
|
|
|
|
Zip::ZipFile.open(file) { |zip_file|
|
|
|
|
design = Design.new.from_json(zip_file.read("#{zip_name}/info.json"))
|
|
|
|
Dir.mktmpdir('f_path') { |dir|
|
|
|
|
themes_entries = []
|
|
|
|
javascripts_entries = []
|
|
|
|
images_entries = []
|
2011-09-05 07:18:36 +00:00
|
|
|
|
2011-09-01 11:06:49 +00:00
|
|
|
zip_file.entries.each do |entry|
|
|
|
|
case (path = entry.to_s)
|
2011-09-05 07:18:36 +00:00
|
|
|
when /\A(#{zip_name})\/(default\.css)\z/ #for default css
|
2012-10-24 10:40:38 +00:00
|
|
|
design.build_css_default(:file => get_temp_file(zip_file, dir, entry))
|
2011-09-05 08:06:17 +00:00
|
|
|
when /\A(#{zip_name})\/(reset\.css)\z/ #for reset css
|
2012-10-24 10:40:38 +00:00
|
|
|
design.build_css_reset(:file => get_temp_file(zip_file, dir, entry))
|
2011-09-05 07:18:36 +00:00
|
|
|
when /\A(#{zip_name})\/(layout\.html)\z/ #for layout html
|
2012-10-24 10:40:38 +00:00
|
|
|
design.build_layout(:file => get_temp_file(zip_file, dir, entry))
|
2011-09-05 07:18:36 +00:00
|
|
|
when /\A(#{zip_name})\/(themes)\/.*(\.css)\z/ #for themes css
|
2011-09-01 11:06:49 +00:00
|
|
|
themes_entries << entry
|
2011-09-05 07:18:36 +00:00
|
|
|
when /\A(#{zip_name})\/(javascripts)\/.*(\.js)\z/ #for js
|
2011-09-01 11:06:49 +00:00
|
|
|
javascripts_entries << entry
|
2011-09-05 07:18:36 +00:00
|
|
|
when /\A(#{zip_name})\/(images)\/.*((\.jpg)|(\.png)|(\.gif))\z/ #for img
|
2011-09-01 11:06:49 +00:00
|
|
|
images_entries << entry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
['themes', 'javascripts', 'images'].each do |type|
|
|
|
|
eval("#{type}_entries").each do |entry|
|
2012-10-24 10:40:38 +00:00
|
|
|
eval("design.#{type}").build(:file => get_temp_file(zip_file, dir, entry))
|
2011-09-01 11:06:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
design.save
|
|
|
|
}
|
|
|
|
end
|
2011-07-12 08:02:41 +00:00
|
|
|
|
2012-10-24 10:40:38 +00:00
|
|
|
def get_temp_file(zip_file, dir, entry)
|
|
|
|
filename = File.basename(entry.to_s)
|
|
|
|
temp_file = File.new(dir + '/' + filename, 'w+')
|
|
|
|
temp_file.write (zip_file.read entry ).force_encoding('UTF-8')
|
|
|
|
temp_file
|
|
|
|
end
|
|
|
|
|
2011-07-12 08:02:41 +00:00
|
|
|
end
|